/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
- * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
+ * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* POSSIBILITY OF SUCH DAMAGE.
*/
-#include "ucx/allocator.h"
+#include "cx/allocator.h"
-#include <stdlib.h>
+__attribute__((__malloc__, __alloc_size__(2)))
+static void *cx_malloc_stdlib(
+ __attribute__((__unused__)) void *d,
+ size_t n
+) {
+ return malloc(n);
+}
+
+__attribute__((__warn_unused_result__, __alloc_size__(3)))
+static void *cx_realloc_stdlib(
+ __attribute__((__unused__)) void *d,
+ void *mem,
+ size_t n
+) {
+ return realloc(mem, n);
+}
+
+__attribute__((__malloc__, __alloc_size__(2, 3)))
+static void *cx_calloc_stdlib(
+ __attribute__((__unused__)) void *d,
+ size_t nelem,
+ size_t n
+) {
+ return calloc(nelem, n);
+}
-static UcxAllocator default_allocator = {
- NULL,
- ucx_default_malloc,
- ucx_default_calloc,
- ucx_default_realloc,
- ucx_default_free
+__attribute__((__nonnull__))
+static void cx_free_stdlib(
+ __attribute__((__unused__)) void *d,
+ void *mem
+) {
+ free(mem);
+}
+
+static cx_allocator_class cx_default_allocator_class = {
+ cx_malloc_stdlib,
+ cx_realloc_stdlib,
+ cx_calloc_stdlib,
+ cx_free_stdlib
+};
+
+struct cx_allocator_s cx_default_allocator = {
+ &cx_default_allocator_class,
+ NULL
};
+CxAllocator *cxDefaultAllocator = &cx_default_allocator;
+
+// IMPLEMENTATION OF HIGH LEVEL API
-UcxAllocator *ucx_default_allocator() {
- UcxAllocator *allocator = &default_allocator;
- return allocator;
+void *cxMalloc(
+ CxAllocator const *allocator,
+ size_t n
+) {
+ return allocator->cl->malloc(allocator->data, n);
}
-void *ucx_default_malloc(void *ignore, size_t n) {
- return malloc(n);
+void *cxRealloc(
+ CxAllocator const *allocator,
+ void *mem,
+ size_t n
+) {
+ return allocator->cl->realloc(allocator->data, mem, n);
}
-void *ucx_default_calloc(void *ignore, size_t n, size_t size) {
- return calloc(n, size);
+int cxReallocate(
+ CxAllocator const *allocator,
+ void **mem,
+ size_t n
+) {
+ void *nmem = allocator->cl->realloc(allocator->data, *mem, n);
+ if (nmem == NULL) {
+ return 1;
+ } else {
+ *mem = nmem;
+ return 0;
+ }
}
-void *ucx_default_realloc(void *ignore, void *data, size_t n) {
- return realloc(data, n);
+void *cxCalloc(
+ CxAllocator const *allocator,
+ size_t nelem,
+ size_t n
+) {
+ return allocator->cl->calloc(allocator->data, nelem, n);
}
-void ucx_default_free(void *ignore, void *data) {
- free(data);
+void cxFree(
+ CxAllocator const *allocator,
+ void *mem
+) {
+ allocator->cl->free(allocator->data, mem);
}