Sun, 02 Jul 2023 13:23:51 +0200
update ucx
dav/main.c | file | annotate | diff | comparison | revisions | |
dav/sync.c | file | annotate | diff | comparison | revisions | |
libidav/davqlexec.c | file | annotate | diff | comparison | revisions | |
libidav/utils.c | file | annotate | diff | comparison | revisions | |
libidav/utils.h | file | annotate | diff | comparison | revisions | |
ucx/Makefile | file | annotate | diff | comparison | revisions | |
ucx/allocator.c | file | annotate | diff | comparison | revisions | |
ucx/basic_mempool.c | file | annotate | diff | comparison | revisions | |
ucx/cx/allocator.h | file | annotate | diff | comparison | revisions | |
ucx/cx/collection.h | file | annotate | diff | comparison | revisions | |
ucx/cx/map.h | file | annotate | diff | comparison | revisions | |
ucx/cx/mempool.h | file | annotate | diff | comparison | revisions | |
ucx/cx/utils.h | file | annotate | diff | comparison | revisions | |
ucx/mempool.c | file | annotate | diff | comparison | revisions | |
ucx/tree.c | file | annotate | diff | comparison | revisions |
--- a/dav/main.c Sun Jul 02 12:06:45 2023 +0200 +++ b/dav/main.c Sun Jul 02 13:23:51 2023 +0200 @@ -1929,7 +1929,7 @@ char *path = NULL; Repository *repo = url2repo(url, &path); DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a); - util_regdestr(sn->mp, path, free); + cxMempoolRegister(sn->mp, path, free); if(set_session_config(sn, a)) { return -1; @@ -2002,7 +2002,7 @@ char *path = NULL; Repository *repo = url2repo(url, &path); DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a); - util_regdestr(sn->mp, path, free); + cxMempoolRegister(sn->mp, path, free); if(set_session_config(sn, a)) { return -1; }
--- a/dav/sync.c Sun Jul 02 12:06:45 2023 +0200 +++ b/dav/sync.c Sun Jul 02 13:23:51 2023 +0200 @@ -635,7 +635,7 @@ } DavSession *sn = create_session(a, ctx, repo, dir->collection); - util_regdestr(sn->mp, db, (cx_destructor_func)destroy_db); + cxMempoolRegister(sn->mp, db, (cx_destructor_func)destroy_db); if (cmd_getoption(a, "verbose")) { curl_easy_setopt(sn->handle, CURLOPT_VERBOSE, 1L); curl_easy_setopt(sn->handle, CURLOPT_STDERR, stderr); @@ -1966,7 +1966,7 @@ remove_deleted_conflicts(dir, db); DavSession *sn = create_session(a, ctx, repo, dir->collection); - util_regdestr(sn->mp, db, (cx_destructor_func)destroy_db); + cxMempoolRegister(sn->mp, db, (cx_destructor_func)destroy_db); if (cmd_getoption(a, "verbose")) { curl_easy_setopt(sn->handle, CURLOPT_VERBOSE, 1L); curl_easy_setopt(sn->handle, CURLOPT_STDERR, stderr); @@ -2634,7 +2634,7 @@ return -1; } DavSession *sn = create_session(a, ctx, repo, dir->collection); - util_regdestr(sn->mp, db, (cx_destructor_func)destroy_db); + cxMempoolRegister(sn->mp, db, (cx_destructor_func)destroy_db); if (cmd_getoption(a, "verbose")) { curl_easy_setopt(sn->handle, CURLOPT_VERBOSE, 1L); curl_easy_setopt(sn->handle, CURLOPT_STDERR, stderr); @@ -5016,7 +5016,7 @@ remove_deleted_conflicts(dir, db); DavSession *sn = create_session(a, ctx, repo, dir->collection); - util_regdestr(sn->mp, db, (cx_destructor_func)destroy_db); + cxMempoolRegister(sn->mp, db, (cx_destructor_func)destroy_db); if (cmd_getoption(a, "verbose")) { curl_easy_setopt(sn->handle, CURLOPT_VERBOSE, 1L); curl_easy_setopt(sn->handle, CURLOPT_STDERR, stderr);
--- a/libidav/davqlexec.c Sun Jul 02 12:06:45 2023 +0200 +++ b/libidav/davqlexec.c Sun Jul 02 13:23:51 2023 +0200 @@ -473,7 +473,7 @@ if(!args) { return result; } - util_regdestr(mp, args, (cx_destructor_func)dav_ql_free_arglist); + cxMempoolRegister(mp, args, (cx_destructor_func)dav_ql_free_arglist); int isallprop; CxBuffer *rqbuf = fieldlist2propfindrequest(sn, mp->allocator, st->fields, &isallprop); @@ -481,7 +481,7 @@ cxMempoolDestroy(mp); return result; } - util_regdestr(mp, rqbuf, (cx_destructor_func)cxBufferFree); + cxMempoolRegister(mp, rqbuf, (cx_destructor_func)cxBufferFree); // compile field list CxList *cfieldlist = cxLinkedListCreate(mp->allocator, NULL, CX_STORE_POINTERS);
--- a/libidav/utils.c Sun Jul 02 12:06:45 2023 +0200 +++ b/libidav/utils.c Sun Jul 02 13:23:51 2023 +0200 @@ -1209,24 +1209,3 @@ return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH); } -struct regdestructor { - cx_destructor_func destructor; - void *data; -}; - -static void call_destructor(struct regdestructor *d) { - d->destructor(d->data); -} - -int util_regdestr(CxMempool *mp, void *data, cx_destructor_func destructor) { - // the ucx maintainer doesn't like me anymore and forces me to - // implement basic stuff by myself - struct regdestructor *reg = cxMalloc(mp->allocator, sizeof(struct regdestructor)); - if(!reg) { - return 1; - } - reg->destructor = destructor; - reg->data = data; - cxMempoolSetDestructor(mp, reg, (cx_destructor_func)call_destructor); - return 0; -}
--- a/libidav/utils.h Sun Jul 02 12:06:45 2023 +0200 +++ b/libidav/utils.h Sun Jul 02 13:23:51 2023 +0200 @@ -125,7 +125,6 @@ char* util_file_hash(const char *path); -int util_regdestr(CxMempool *mp, void *data, cx_destructor_func destructor); #ifdef __cplusplus }
--- a/ucx/Makefile Sun Jul 02 12:06:45 2023 +0200 +++ b/ucx/Makefile Sun Jul 02 13:23:51 2023 +0200 @@ -32,7 +32,7 @@ # list of source files SRC = allocator.c SRC += array_list.c -SRC += basic_mempool.c +SRC += mempool.c SRC += buffer.c SRC += compare.c SRC += hash_key.c
--- a/ucx/allocator.c Sun Jul 02 12:06:45 2023 +0200 +++ b/ucx/allocator.c Sun Jul 02 13:23:51 2023 +0200 @@ -75,6 +75,20 @@ }; CxAllocator *cxDefaultAllocator = &cx_default_allocator; + +int cx_reallocate( + void **mem, + size_t n +) { + void *nmem = realloc(*mem, n); + if (nmem == NULL) { + return 1; + } else { + *mem = nmem; + return 0; + } +} + // IMPLEMENTATION OF HIGH LEVEL API void *cxMalloc(
--- a/ucx/basic_mempool.c Sun Jul 02 12:06:45 2023 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,235 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * 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: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "cx/basic_mempool.h" -#include "cx/utils.h" -#include <string.h> - -#define of_chk_(n) if (SIZE_MAX - sizeof(cx_destructor_func) < (n)) return NULL - -/** Internal structure for denoting pooled memory. */ -typedef struct { - /** The destructor. */ - cx_destructor_func destructor; - /** - * Access to the first byte of the polled memory. - */ - char c; -} cx_basic_mempool_memory; - -static int cx_basic_mempool_chcap( - struct cx_basic_mempool_s *pool, - size_t newcap -) { - if (newcap < pool->ndata) { - return 1; - } - - size_t newcapsz; - if (cx_szmul(newcap, sizeof(void *), &newcapsz)) { - return 1; - } - - void **data = realloc(pool->data, newcapsz); - if (data) { - pool->data = data; - pool->size = newcap; - return 0; - } else { - return 1; - } -} - -void *cx_malloc_basic_mempool( - void *data, - size_t n -) { - of_chk_(n); - struct cx_basic_mempool_s *pool = data; - - if (pool->ndata >= pool->size) { - size_t newcap = pool->size * 2; - if (newcap < pool->size || cx_basic_mempool_chcap(pool, newcap)) { - return NULL; - } - } - - cx_basic_mempool_memory *mem = malloc(sizeof(cx_destructor_func) + n); - if (mem == NULL) { - return NULL; - } - - mem->destructor = NULL; - pool->data[pool->ndata] = mem; - pool->ndata++; - - return &(mem->c); -} - -void *cx_calloc_basic_mempool( - void *data, - size_t nelem, - size_t elsize -) { - size_t msz; - if (cx_szmul(nelem, elsize, &msz)) { - return NULL; - } - void *ptr = cx_malloc_basic_mempool(data, msz); - if (ptr == NULL) { - return NULL; - } - memset(ptr, 0, nelem * elsize); - return ptr; -} - -void *cx_realloc_basic_mempool( - void *data, - void *ptr, - size_t n -) { - of_chk_(n); - struct cx_basic_mempool_s *pool = data; - - char *mem = ((char *) ptr) - sizeof(cx_destructor_func); - char *newm = (char *) realloc(mem, n + sizeof(cx_destructor_func)); - if (newm == NULL) { - return NULL; - } - if (mem != newm) { - cx_for_n(i, pool->ndata) { - if (pool->data[i] == mem) { - pool->data[i] = newm; - return newm + sizeof(cx_destructor_func); - } - } - abort(); - } else { - return newm + sizeof(cx_destructor_func); - } -} - -void cx_free_basic_mempool( - void *data, - void *ptr -) { - struct cx_basic_mempool_s *pool = data; - - cx_basic_mempool_memory *mem = (cx_basic_mempool_memory *) - ((char *) ptr - sizeof(cx_destructor_func)); - cx_for_n(i, pool->ndata) { - if (mem == pool->data[i]) { - if (mem->destructor != NULL) { - mem->destructor(&(mem->c)); - } - free(mem); - size_t last_index = pool->ndata - 1; - if (i != last_index) { - pool->data[i] = pool->data[last_index]; - pool->data[last_index] = NULL; - } - pool->ndata--; - return; - } - } - abort(); -} - -void cx_basic_mempool_destroy(CxMempool *p) { - struct cx_basic_mempool_s *pool = (struct cx_basic_mempool_s *) p; - cx_basic_mempool_memory *mem; - cx_for_n(i, pool->ndata) { - mem = (cx_basic_mempool_memory *) pool->data[i]; - if (mem) { - if (mem->destructor) { - mem->destructor(&(mem->c)); - } - free(mem); - } - } - free(pool->data); - free((void *) p->allocator); - free(pool); -} - -void cx_basic_mempool_set_destr( - __attribute__((__unused__)) CxMempool *pool, - void *ptr, - cx_destructor_func func -) { - *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func; -} - -static cx_allocator_class cx_basic_mempool_allocator_class = { - cx_malloc_basic_mempool, - cx_realloc_basic_mempool, - cx_calloc_basic_mempool, - cx_free_basic_mempool -}; - -static cx_mempool_class cx_basic_mempool_class = { - cx_basic_mempool_destroy, - cx_basic_mempool_set_destr, -}; - -CxMempool *cxBasicMempoolCreate(size_t capacity) { - size_t poolsize; - if (cx_szmul(capacity, sizeof(void *), &poolsize)) { - return NULL; - } - - struct cx_basic_mempool_s *pool = - malloc(sizeof(struct cx_basic_mempool_s)); - if (pool == NULL) { - return NULL; - } - - - CxAllocator *provided_allocator = malloc(sizeof(CxAllocator)); - if (!provided_allocator) { - free(pool); - return NULL; - } - provided_allocator->cl = &cx_basic_mempool_allocator_class; - provided_allocator->data = pool; - - pool->base.cl = &cx_basic_mempool_class; - pool->base.allocator = provided_allocator; - - pool->data = malloc(poolsize); - if (pool->data == NULL) { - free(provided_allocator); - free(pool); - return NULL; - } - - pool->ndata = 0; - pool->size = capacity; - - return (CxMempool *) pool; -}
--- a/ucx/cx/allocator.h Sun Jul 02 12:06:45 2023 +0200 +++ b/ucx/cx/allocator.h Sun Jul 02 13:23:51 2023 +0200 @@ -133,6 +133,22 @@ ) __attribute__((__nonnull__(2))); /** + * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. + * + * \par Error handling + * \c errno will be set by realloc() on failure. + * + * @param mem pointer to the pointer to allocated block + * @param n the new size in bytes + * @return zero on success, non-zero on failure + */ +int cx_reallocate( + void **mem, + size_t n +) +__attribute__((__nonnull__)); + +/** * Allocate \p n bytes of memory. * * @param allocator the allocator @@ -169,7 +185,6 @@ /** * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. * This function acts like cxRealloc() using the pointer pointed to by \p mem. - * On success, the pointer is changed to the new location (in case the * * \note Re-allocating a block allocated by a different allocator is undefined. *
--- a/ucx/cx/collection.h Sun Jul 02 12:06:45 2023 +0200 +++ b/ucx/cx/collection.h Sun Jul 02 13:23:51 2023 +0200 @@ -127,6 +127,15 @@ (c)->store_pointer ? (*((void **) (e))) : (e)) +/** + * Invokes all available destructor functions for a specific element. + * + * Usually only used by collection implementations. There should be no need + * to invoke this macro manually. + * + * @param c the collection + * @param e the element + */ #define cx_invoke_destructor(c, e) \ if ((c)->simple_destructor) cx_invoke_simple_destructor(c,e); \ if ((c)->advanced_destructor) cx_invoke_advanced_destructor(c,e)
--- a/ucx/cx/map.h Sun Jul 02 12:06:45 2023 +0200 +++ b/ucx/cx/map.h Sun Jul 02 13:23:51 2023 +0200 @@ -146,6 +146,11 @@ void *value; }; +/** + * A shared instance of an empty map. + * + * Writing to that map is undefined. + */ extern CxMap *const cxEmptyMap; /**
--- a/ucx/cx/mempool.h Sun Jul 02 12:06:45 2023 +0200 +++ b/ucx/cx/mempool.h Sun Jul 02 13:23:51 2023 +0200 @@ -44,24 +44,31 @@ extern "C" { #endif -/** - * Memory pool class type. - */ -typedef struct cx_mempool_class_s cx_mempool_class; +/** Internal structure for pooled memory. */ +struct cx_mempool_memory_s; /** * The basic structure of a memory pool. * Should be the first member of an actual memory pool implementation. */ struct cx_mempool_s { + /** The provided allocator. */ + CxAllocator const *allocator; + /** - * The pool class definition. + * A destructor that shall be automatically registered for newly allocated memory. + * This destructor MUST NOT free the memory. */ - cx_mempool_class *cl; - /** - * The provided allocator. - */ - CxAllocator const *allocator; + cx_destructor_func auto_destr; + + /** Array of pooled memory. */ + struct cx_mempool_memory_s **data; + + /** Number of pooled memory items. */ + size_t size; + + /** Memory pool capacity. */ + size_t capacity; }; /** @@ -70,22 +77,27 @@ typedef struct cx_mempool_s CxMempool; /** - * The class definition for a memory pool. + * Creates an array-based memory pool with a shared destructor function. + * + * This destructor MUST NOT free the memory. + * + * @param capacity the initial capacity of the pool + * @param destr the destructor function to use for allocated memory + * @return the created memory pool or \c NULL if allocation failed */ -struct cx_mempool_class_s { - /** Member function for destroying the pool. */ - __attribute__((__nonnull__)) - void (*destroy)(CxMempool *pool); +__attribute__((__warn_unused_result__)) +CxMempool *cxMempoolCreate(size_t capacity, cx_destructor_func destr); - /** Member function for setting a destructor. */ - __attribute__((__nonnull__)) - void (*set_destructor)( - CxMempool *pool, - void *memory, - cx_destructor_func fnc - ); -}; - +/** + * Creates a basic array-based memory pool. + * + * @param capacity the initial capacity of the pool + * @return the created memory pool or \c NULL if allocation failed + */ +__attribute__((__warn_unused_result__)) +static inline CxMempool *cxBasicMempoolCreate(size_t capacity) { + return cxMempoolCreate(capacity, NULL); +} /** * Destroys a memory pool including their contents. @@ -93,27 +105,42 @@ * @param pool the memory pool to destroy */ __attribute__((__nonnull__)) -static inline void cxMempoolDestroy(CxMempool *pool) { - pool->cl->destroy(pool); -} +void cxMempoolDestroy(CxMempool *pool); /** - * Sets a destructor function for an allocated memory object. + * Sets the destructor function for a specific allocated memory object. * - * If the memory is not managed by the pool, the behavior is undefined. + * If the memory is not managed by a UCX memory pool, the behavior is undefined. + * The destructor MUST NOT free the memory. * - * @param pool the pool - * @param memory the objected allocated in the pool + * @param memory the object allocated in the pool * @param fnc the destructor function */ __attribute__((__nonnull__)) -static inline void cxMempoolSetDestructor( - CxMempool *pool, +void cxMempoolSetDestructor( void *memory, cx_destructor_func fnc -) { - pool->cl->set_destructor(pool, memory, fnc); -} +); + +/** + * Registers foreign memory with this pool. + * + * The destructor, in contrast to memory allocated by the pool, MUST free the memory. + * + * A small portion of memory will be allocated to register the information in the pool. + * If that allocation fails, this function will return non-zero. + * + * @param pool the pool + * @param memory the object allocated in the pool + * @param destr the destructor function + * @return zero on success, non-zero on failure + */ +__attribute__((__nonnull__)) +int cxMempoolRegister( + CxMempool *pool, + void *memory, + cx_destructor_func destr +); #ifdef __cplusplus } // extern "C"
--- a/ucx/cx/utils.h Sun Jul 02 12:06:45 2023 +0200 +++ b/ucx/cx/utils.h Sun Jul 02 13:23:51 2023 +0200 @@ -183,7 +183,6 @@ * @param dest the destination stream * @param rfnc the read function * @param wfnc the write function - * @param n the maximum number of bytes that shall be copied. * @return total number of bytes copied */ #define cx_stream_copy(src, dest, rfnc, wfnc) \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ucx/mempool.c Sun Jul 02 13:23:51 2023 +0200 @@ -0,0 +1,232 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * 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: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "cx/mempool.h" +#include "cx/utils.h" +#include <string.h> + +struct cx_mempool_memory_s { + /** The destructor. */ + cx_destructor_func destructor; + /** The actual memory. */ + char c[]; +}; + +static void *cx_mempool_malloc( + void *p, + size_t n +) { + struct cx_mempool_s *pool = p; + + if (pool->size >= pool->capacity) { + size_t newcap = pool->capacity - (pool->capacity % 16) + 16; + struct cx_mempool_memory_s **newdata = realloc(pool->data, newcap*sizeof(struct cx_mempool_memory_s*)); + if (newdata == NULL) { + return NULL; + } + pool->data = newdata; + pool->capacity = newcap; + } + + struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n); + if (mem == NULL) { + return NULL; + } + + mem->destructor = pool->auto_destr; + pool->data[pool->size] = mem; + pool->size++; + + return mem->c; +} + +static void *cx_mempool_calloc( + void *p, + size_t nelem, + size_t elsize +) { + size_t msz; + if (cx_szmul(nelem, elsize, &msz)) { + return NULL; + } + void *ptr = cx_mempool_malloc(p, msz); + if (ptr == NULL) { + return NULL; + } + memset(ptr, 0, nelem * elsize); + return ptr; +} + +static void *cx_mempool_realloc( + void *p, + void *ptr, + size_t n +) { + struct cx_mempool_s *pool = p; + + struct cx_mempool_memory_s *mem, *newm; + mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func)); + newm = realloc(mem, n + sizeof(cx_destructor_func)); + + if (newm == NULL) { + return NULL; + } + if (mem != newm) { + cx_for_n(i, pool->size) { + if (pool->data[i] == mem) { + pool->data[i] = newm; + return ((char*)newm) + sizeof(cx_destructor_func); + } + } + abort(); + } else { + return ptr; + } +} + +static void cx_mempool_free( + void *p, + void *ptr +) { + struct cx_mempool_s *pool = p; + + struct cx_mempool_memory_s *mem = (struct cx_mempool_memory_s *) + ((char *) ptr - sizeof(cx_destructor_func)); + + cx_for_n(i, pool->size) { + if (mem == pool->data[i]) { + if (mem->destructor) { + mem->destructor(mem->c); + } + free(mem); + size_t last_index = pool->size - 1; + if (i != last_index) { + pool->data[i] = pool->data[last_index]; + pool->data[last_index] = NULL; + } + pool->size--; + return; + } + } + abort(); +} + +void cxMempoolDestroy(CxMempool *pool) { + struct cx_mempool_memory_s *mem; + cx_for_n(i, pool->size) { + mem = pool->data[i]; + if (mem->destructor) { + mem->destructor(mem->c); + } + free(mem); + } + free(pool->data); + free((void*) pool->allocator); + free(pool); +} + +void cxMempoolSetDestructor( + void *ptr, + cx_destructor_func func +) { + *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func; +} + +struct cx_mempool_foreign_mem_s { + cx_destructor_func destr; + void* mem; +}; + +static void cx_mempool_destr_foreign_mem(void* ptr) { + struct cx_mempool_foreign_mem_s *fm = ptr; + fm->destr(fm->mem); +} + +int cxMempoolRegister( + CxMempool *pool, + void *memory, + cx_destructor_func destr +) { + struct cx_mempool_foreign_mem_s *fm = cx_mempool_malloc( + pool, + sizeof(struct cx_mempool_foreign_mem_s) + ); + if (fm == NULL) return 1; + + fm->mem = memory; + fm->destr = destr; + *(cx_destructor_func *) ((char *) fm - sizeof(cx_destructor_func)) = cx_mempool_destr_foreign_mem; + + return 0; +} + +static cx_allocator_class cx_mempool_allocator_class = { + cx_mempool_malloc, + cx_mempool_realloc, + cx_mempool_calloc, + cx_mempool_free +}; + +CxMempool *cxMempoolCreate( + size_t capacity, + cx_destructor_func destr +) { + size_t poolsize; + if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) { + return NULL; + } + + struct cx_mempool_s *pool = + malloc(sizeof(struct cx_mempool_s)); + if (pool == NULL) { + return NULL; + } + + CxAllocator *provided_allocator = malloc(sizeof(CxAllocator)); + if (provided_allocator == NULL) { + free(pool); + return NULL; + } + provided_allocator->cl = &cx_mempool_allocator_class; + provided_allocator->data = pool; + + pool->allocator = provided_allocator; + + pool->data = malloc(poolsize); + if (pool->data == NULL) { + free(provided_allocator); + free(pool); + return NULL; + } + + pool->size = 0; + pool->capacity = capacity; + pool->auto_destr = destr; + + return (CxMempool *) pool; +}
--- a/ucx/tree.c Sun Jul 02 12:06:45 2023 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * 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: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "cx/tree.h" -#include "cx/linked_list.h" - -#define CX_TR_PTR(cur, off) *((void**)(((char*)(cur))+(off))) - -void cx_tree_add_sibling(void *node, ptrdiff_t loc_prev, ptrdiff_t loc_next, ptrdiff_t loc_parent, void *new_node) { - cx_linked_list_add(&node, NULL, loc_prev, loc_next, new_node); - - // optional parent link - if (loc_parent >= 0) { - CX_TR_PTR(new_node, loc_parent) = CX_TR_PTR(node, loc_parent); - } -} - -void cx_tree_add_child(void **children_begin, void **children_end, - ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node, - ptrdiff_t loc_parent, void *parent) { - cx_linked_list_add(children_begin, children_end, loc_prev, loc_next, new_node); - - // optional parent link - if (loc_parent >= 0) { - CX_TR_PTR(new_node, loc_parent) = parent; - } -}