diff -r a569148841ff -r efbd59642577 ucx/allocator.c --- a/ucx/allocator.c Sun Apr 16 14:12:24 2023 +0200 +++ b/ucx/allocator.c Fri Apr 21 21:25:32 2023 +0200 @@ -1,7 +1,7 @@ /* * 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: @@ -26,35 +26,97 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "ucx/allocator.h" - -#include +#include "cx/allocator.h" -static UcxAllocator default_allocator = { - NULL, - ucx_default_malloc, - ucx_default_calloc, - ucx_default_realloc, - ucx_default_free -}; - -UcxAllocator *ucx_default_allocator() { - UcxAllocator *allocator = &default_allocator; - return allocator; -} - -void *ucx_default_malloc(void *ignore, size_t n) { +__attribute__((__malloc__, __alloc_size__(2))) +static void *cx_malloc_stdlib( + __attribute__((__unused__)) void *d, + size_t n +) { return malloc(n); } -void *ucx_default_calloc(void *ignore, size_t n, size_t size) { - return calloc(n, size); +__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); +} + +__attribute__((__nonnull__)) +static void cx_free_stdlib( + __attribute__((__unused__)) void *d, + void *mem +) { + free(mem); } -void *ucx_default_realloc(void *ignore, void *data, size_t n) { - return realloc(data, n); +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 + +void *cxMalloc( + CxAllocator const *allocator, + size_t n +) { + return allocator->cl->malloc(allocator->data, n); } -void ucx_default_free(void *ignore, void *data) { - free(data); +void *cxRealloc( + CxAllocator const *allocator, + void *mem, + size_t n +) { + return allocator->cl->realloc(allocator->data, mem, n); } + +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 *cxCalloc( + CxAllocator const *allocator, + size_t nelem, + size_t n +) { + return allocator->cl->calloc(allocator->data, nelem, n); +} + +void cxFree( + CxAllocator const *allocator, + void *mem +) { + allocator->cl->free(allocator->data, mem); +}