Fri, 19 Dec 2025 17:53:18 +0100
update ucx
--- a/dav/config.c Sun Dec 07 20:16:59 2025 +0100 +++ b/dav/config.c Fri Dec 19 17:53:18 2025 +0100 @@ -112,7 +112,7 @@ } CxBuffer buf; - cxBufferInit(&buf, NULL, 1024, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&buf, cxDefaultAllocator, NULL, 1024, CX_BUFFER_AUTO_EXTEND); cx_stream_copy(file, &buf, (cx_read_func)fread, (cx_write_func)cxBufferWrite); fclose(file); @@ -304,8 +304,9 @@ * The list secrets->location contains urls or repo names as * location strings. We need a list, that contains only urls */ - CxList *locations = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)cmp_url_cred_entry, CX_STORE_POINTERS); - cxDefineDestructor(locations, free_cred_location); + CxList *locations = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + cxSetCompareFunc(locations, (cx_compare_func)cmp_url_cred_entry); + cxSetDestructor(locations, free_cred_location); CxIterator i = cxListIterator(secrets->locations); cx_foreach(PwdIndexEntry*, e, i) { CxIterator entry_iter = cxListIterator(e->locations); @@ -325,9 +326,9 @@ // create full request url string and remove protocol prefix cxmutstr req_url_proto = util_concat_path_s(cx_strcast(repo->url.value), cx_str(path)); cxstring req_url = cx_strcast(req_url_proto); - if(cx_strprefix(req_url, CX_STR("http://"))) { + if(cx_strprefix(req_url, cx_str("http://"))) { req_url = cx_strsubs(req_url, 7); - } else if(cx_strprefix(req_url, CX_STR("https://"))) { + } else if(cx_strprefix(req_url, cx_str("https://"))) { req_url = cx_strsubs(req_url, 8); } @@ -340,9 +341,9 @@ cxstring cred_url = cx_str(cred->location); // remove protocol prefix - if(cx_strprefix(cred_url, CX_STR("http://"))) { + if(cx_strprefix(cred_url, cx_str("http://"))) { cred_url = cx_strsubs(cred_url, 7); - } else if(cx_strprefix(cred_url, CX_STR("https://"))) { + } else if(cx_strprefix(cred_url, cx_str("https://"))) { cred_url = cx_strsubs(cred_url, 8); }
--- a/dav/db.c Sun Dec 07 20:16:59 2025 +0100 +++ b/dav/db.c Fri Dec 19 17:53:18 2025 +0100 @@ -58,8 +58,8 @@ db->resources = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 2048); db->conflict = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); - cxDefineDestructor(db->resources, local_resource_free); - cxDefineDestructor(db->conflict, local_resource_free); + cxSetDestructor(db->resources, local_resource_free); + cxSetDestructor(db->conflict, local_resource_free); xmlTextReaderPtr reader = xmlReaderForFile(db_file, NULL, 0); if(!reader) { @@ -112,9 +112,7 @@ } void process_parts(xmlTextReaderPtr reader, LocalResource *res) { - size_t parts_alloc = 32; - size_t parts_size = 0; - FilePart *parts = calloc(parts_alloc, sizeof(FilePart)); + CX_ARRAY(FilePart, parts); FilePart *current_part = NULL; @@ -128,10 +126,10 @@ if(type == XML_READER_TYPE_ELEMENT) { if(depth == 3 && xstreq(name, "part")) { FilePart newpart = { 0 }; - cx_array_add(&parts, &parts_size, &parts_alloc, sizeof(FilePart), &newpart, cx_array_default_reallocator); + cx_array_add(parts, newpart); - current_part = &parts[parts_size-1]; - current_part->block = parts_size; + current_part = &parts.data[parts.size-1]; + current_part->block = parts.size; } else if(depth == 4) { if(xstreq(name, "hash")) { field = 0; @@ -164,14 +162,14 @@ } if(!err) { - res->parts = parts; - res->numparts = parts_size; + res->parts = parts.data; + res->numparts = parts.size; } else { - for(int i=0;i<parts_size;i++) { - free(parts[i].etag); - free(parts[i].hash); + for(int i=0;i<parts.size;i++) { + free(parts.data[i].etag); + free(parts.data[i].hash); } - free(parts); + free(parts.data); } }
--- a/dav/finfo.c Sun Dec 07 20:16:59 2025 +0100 +++ b/dav/finfo.c Fri Dec 19 17:53:18 2025 +0100 @@ -50,22 +50,22 @@ uint32_t parse_finfo_settings(const char *str, char **error) { cxstring s = cx_str(str); - if(!cx_strcmp(s, CX_STR("*")) || !cx_strcmp(s, CX_STR("a")) || !cx_strcmp(s, CX_STR("all"))) { + if(!cx_strcmp(s, cx_str("*")) || !cx_strcmp(s, cx_str("a")) || !cx_strcmp(s, cx_str("all"))) { return FINFO_MTIME|FINFO_OWNER|FINFO_MODE|FINFO_XATTR; } - CxStrtokCtx fs = cx_strtok(s, CX_STR(","), INT_MAX); + CxStrtokCtx fs = cx_strtok(s, cx_str(","), INT_MAX); cxstring f; uint32_t finfo = 0; char *err = NULL; while(cx_strtok_next(&fs, &f)) { - if(!cx_strcasecmp(f, CX_STR("mtime"))) { + if(!cx_strcasecmp(f, cx_str("mtime"))) { finfo |= FINFO_MTIME; - } else if(!cx_strcasecmp(f, CX_STR("owner"))) { + } else if(!cx_strcasecmp(f, cx_str("owner"))) { finfo |= FINFO_OWNER; - } else if(!cx_strcasecmp(f, CX_STR("mode"))) { + } else if(!cx_strcasecmp(f, cx_str("mode"))) { finfo |= FINFO_MODE; - } else if(!cx_strcasecmp(f, CX_STR("xattr"))) { + } else if(!cx_strcasecmp(f, cx_str("xattr"))) { finfo |= FINFO_XATTR; } else if(error && !err) { err = cx_strdup(f).ptr; @@ -130,13 +130,6 @@ } -static void* array_realloc(void *array, - size_t capacity, - size_t elem_size, - struct cx_array_reallocator_s *alloc) -{ - return realloc(array, capacity * elem_size); -} XAttributes* xml_get_attributes(DavXmlNode *xml) { XAttributes *attributes = calloc(1, sizeof(XAttributes)); @@ -161,16 +154,11 @@ char *xattr_name = dav_xml_get_attr(node, "name"); if(xattr_name) { char *xname = strdup(xattr_name); - cx_array_copy( - (void**)&attributes->names, - &x_names_size, - &x_names_alloc, - sizeof(size_t), - count, - &xname, - sizeof(void*), - 1, - cx_array_default_reallocator); + if(x_names_size >= x_names_alloc) { + x_names_alloc += 16; + attributes->names = realloc(attributes->names, sizeof(char*) * x_names_alloc); + } + attributes->names[x_names_size++] = xname; char *text = dav_xml_getstring(node->children); if(!text) { @@ -184,16 +172,11 @@ value.ptr = val; value.length = len; - cx_array_copy( - (void**)&attributes->values, - &x_values_size, - &x_values_alloc, - sizeof(size_t), - count, - &value, - sizeof(cxmutstr), - 1, - cx_array_default_reallocator); + if(x_values_size >= x_values_alloc) { + x_values_alloc += 16; + attributes->values = realloc(attributes->values, sizeof(cxmutstr) * x_values_alloc); + } + attributes->values[x_values_size++] = value; count++; }
--- a/dav/main.c Sun Dec 07 20:16:59 2025 +0100 +++ b/dav/main.c Fri Dec 19 17:53:18 2025 +0100 @@ -706,8 +706,8 @@ } // get list of resources - CxList *reslist = cxLinkedListCreateSimple(CX_STORE_POINTERS); - cxDefineDestructor(reslist, free_getres); + CxList *reslist = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + cxSetDestructor(reslist, free_getres); uint64_t totalsize = 0; uint64_t rescount = 0; @@ -718,7 +718,7 @@ char *structure = cmd_getoption(a, "structure"); // iterate over resource tree - CxList *stack = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *stack = cxLinkedListCreate(NULL, CX_STORE_POINTERS); cxListInsert(stack, 0, getres); while(cxListSize(stack) > 0) { GetResource *g = cxListAt(stack, 0); @@ -1721,7 +1721,7 @@ //printf("header: %.*s\n", s*n, header); cxstring h = cx_strn(header, s*n); - if(cx_strprefix(h, CX_STR("Date:"))) { + if(cx_strprefix(h, cx_str("Date:"))) { cxstring v = cx_strsubs(h, 5); *date_str = cx_strdup(cx_strtrim(v)).ptr; } @@ -1961,7 +1961,7 @@ time_t timeout = 0; char *timeoutstr = cmd_getoption(a, "timeout"); if(timeoutstr) { - if(!cx_strcasecmp(cx_str(timeoutstr), CX_STR("infinite"))) { + if(!cx_strcasecmp(cx_str(timeoutstr), cx_str("infinite"))) { timeout = -1; } else { uint64_t i; @@ -1997,7 +1997,7 @@ static char* read_line() { CxBuffer buf; - cxBufferInit(&buf, NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&buf, cxDefaultAllocator, NULL, 128, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); int c; while((c = getchar()) != EOF) { if(c == '\n') { @@ -2345,7 +2345,7 @@ char* stdin2str() { CxBuffer buf; - cxBufferInit(&buf, NULL, 1024, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&buf, cxDefaultAllocator, NULL, 1024, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); size_t size = cx_stream_copy(stdin, &buf, (cx_read_func)fread, (cx_write_func)cxBufferWrite); if(size == 0) { cxBufferDestroy(&buf); @@ -2393,7 +2393,7 @@ char* xml2str(DavXmlNode *node) { CxBuffer buf; - cxBufferInit(&buf, NULL, 256, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&buf, cxDefaultAllocator, NULL, 256, CX_BUFFER_AUTO_EXTEND); xml2str_i(node, &buf, 0); cxBufferPut(&buf, 0); return buf.space; @@ -2401,7 +2401,7 @@ void printxmldoc(FILE *out, char *root, char *rootns, DavXmlNode *content) { CxMap *nsmap = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); - cxDefineDestructor(nsmap, free); + cxSetDestructor(nsmap, free); cxMapPut(nsmap, cx_hash_key_str(rootns), strdup("x0")); fprintf(out, "%s", "<?xml version=\"1.0\"?>\n"); @@ -2533,10 +2533,10 @@ cxstring url = cx_strcast(repo->url.value); if(repo->user.value.ptr && !cmd_getoption(args, "plain")) { int hostindex = 0; - if(cx_strprefix(url, CX_STR("https://"))) { + if(cx_strprefix(url, cx_str("https://"))) { printf("https://"); hostindex = 8; - } else if(cx_strprefix(url, CX_STR("http://"))) { + } else if(cx_strprefix(url, cx_str("http://"))) { printf("http://"); hostindex = 7; } @@ -2661,8 +2661,8 @@ // optionally, get one or more locations char *location = NULL; - CxList *locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); - cxDefineDestructor(locations, free); + CxList *locations = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + cxSetDestructor(locations, free); while((location = assistant_getoptcfg("Location"))) { cxListAdd(locations, location); } @@ -2968,13 +2968,13 @@ static char** read_args_from_stdin(int *argc) { // read stdin into buffer - CxBuffer *in = cxBufferCreate(NULL, 1024, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *in = cxBufferCreate(cxDefaultAllocator, NULL, 1024, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); cx_stream_copy(stdin, in, (cx_read_func)fread, (cx_write_func)cxBufferWrite); // split input into lines size_t count = 0; cxmutstr *lines; - count = cx_strsplit_ma(cxDefaultAllocator, cx_mutstrn(in->space, in->pos), CX_STR("\n"), INT_MAX, &lines); + count = cx_strsplit_ma(cxDefaultAllocator, cx_mutstrn(in->space, in->pos), cx_str("\n"), INT_MAX, &lines); char **args = NULL; if(count > 0) { @@ -3172,7 +3172,7 @@ char *lspath = NULL; if(path[plen-1] == '/') { lspath = strdup(path); - filter = CX_STR(""); + filter = cx_str(""); } else { lspath = util_parent_path(path); filter = cx_str(util_resource_name(path)); @@ -3191,7 +3191,7 @@ } } - CxBuffer *out = cxBufferCreate(NULL, 512, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *out = cxBufferCreate(cxDefaultAllocator, NULL, 512, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); cxBufferWrite(repo->name.value.ptr, repo->name.value.length, 1, out); if(space) { size_t l = strlen(elm->path);
--- a/dav/scfg.c Sun Dec 07 20:16:59 2025 +0100 +++ b/dav/scfg.c Fri Dec 19 17:53:18 2025 +0100 @@ -164,12 +164,12 @@ } Filter* parse_filter(xmlNode *node) { - CxList *include = cxLinkedListCreate(cxDefaultAllocator, NULL, sizeof(regex_t)); - CxList *exclude = cxLinkedListCreate(cxDefaultAllocator, NULL, sizeof(regex_t)); - CxList *tags = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS); + CxList *include = cxLinkedListCreate(cxDefaultAllocator, sizeof(regex_t)); + CxList *exclude = cxLinkedListCreate(cxDefaultAllocator, sizeof(regex_t)); + CxList *tags = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); - cxDefineDestructor(include, regfree); - cxDefineDestructor(exclude, regfree); + cxSetDestructor(include, regfree); + cxSetDestructor(exclude, regfree); // TODO: set tags destructor if(scfg_load_filter(node, include, exclude, tags)) { @@ -347,7 +347,7 @@ } static CxList* parse_splitconfig(xmlNode *node, int *error) { - CxList *splitconfig = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *splitconfig = cxLinkedListCreate(NULL, CX_STORE_POINTERS); int err = 0; xmlNode *c = node->children; while(c) { @@ -431,9 +431,9 @@ char *logfile = NULL; TagConfig *tagconfig = NULL; Versioning *versioning = NULL; - CxList *include = cxLinkedListCreateSimple(sizeof(regex_t)); - CxList *exclude = cxLinkedListCreateSimple(sizeof(regex_t)); - CxList *tagfilter = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *include = cxLinkedListCreate(NULL, sizeof(regex_t)); + CxList *exclude = cxLinkedListCreate(NULL, sizeof(regex_t)); + CxList *tagfilter = cxLinkedListCreate(NULL, CX_STORE_POINTERS); CxList *splitconfig = NULL; int max_retry = 0; int allow_cmd = SYNC_CMD_PULL | SYNC_CMD_PUSH
--- a/dav/sync.c Sun Dec 07 20:16:59 2025 +0100 +++ b/dav/sync.c Fri Dec 19 17:53:18 2025 +0100 @@ -684,7 +684,7 @@ } void res2map(DavResource *root, CxMap *map) { - CxList *stack = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *stack = cxLinkedListCreate(NULL, CX_STORE_POINTERS); cxListInsert(stack, 0, root->children); while(cxListSize(stack) > 0) { DavResource *res = cxListAt(stack, 0); @@ -837,20 +837,20 @@ int sync_error = 0; int sync_conflict = 0; - CxList *res_modified = cxLinkedListCreateSimple(CX_STORE_POINTERS); - CxList *res_new = cxLinkedListCreateSimple(CX_STORE_POINTERS); - CxList *res_moved = cxLinkedListCreateSimple(CX_STORE_POINTERS); // type: MovedFile* - CxList *res_link = cxLinkedListCreateSimple(CX_STORE_POINTERS); - CxList *res_conflict = cxLinkedListCreateSimple(CX_STORE_POINTERS); - CxList *res_mkdir = cxLinkedListCreateSimple(CX_STORE_POINTERS); - CxList *res_metadata = cxLinkedListCreateSimple(CX_STORE_POINTERS); - CxList *res_broken = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *res_modified = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + CxList *res_new = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + CxList *res_moved = cxLinkedListCreate(NULL, CX_STORE_POINTERS); // type: MovedFile* + CxList *res_link = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + CxList *res_conflict = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + CxList *res_mkdir = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + CxList *res_metadata = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + CxList *res_broken = cxLinkedListCreate(NULL, CX_STORE_POINTERS); CxMap *lres_removed = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); // type: LocalResource* //UcxMap *svrres = ucx_map_new(db->resources->count); CxMap *dbres = mapClone(cxDefaultAllocator, db->resources, NULL, NULL); - CxList *stack = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *stack = cxLinkedListCreate(NULL, CX_STORE_POINTERS); cxListInsert(stack, 0, ls->children); while(cxListSize(stack) > 0) { DavResource *res = cxListAt(stack, 0); @@ -1100,7 +1100,8 @@ } } - CxList *rmdirs = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)resource_pathlen_cmp, CX_STORE_POINTERS); + CxList *rmdirs = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + cxSetCompareFunc(rmdirs, (cx_compare_func)resource_pathlen_cmp); mi = cxMapIteratorValues(lres_removed); cx_foreach(LocalResource *, removed_res, mi) { if(sync_shutdown) { @@ -1252,7 +1253,7 @@ } } else if(local->etag) { cxstring e = cx_str(etag); - if(cx_strprefix(e, CX_STR("W/"))) { + if(cx_strprefix(e, cx_str("W/"))) { e = cx_strsubs(e, 2); } if(!strcmp(e.ptr, local->etag)) { @@ -1429,8 +1430,8 @@ int64_t *truncate_file, int *err) { - CxList *updates = cxLinkedListCreateSimple(CX_STORE_POINTERS); - cxDefineDestructor(updates, filepart_free); + CxList *updates = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + cxSetDestructor(updates, filepart_free); size_t local_numparts = local ? local->numparts : 0; fseeko(out, 0, SEEK_END); @@ -1438,7 +1439,7 @@ int error = 0; - CxBuffer *buf = cxBufferCreate(NULL, blocksize, cxDefaultAllocator, 0); + CxBuffer *buf = cxBufferCreate(cxDefaultAllocator, NULL, blocksize, 0); int64_t maxsize = -1; @@ -2175,15 +2176,23 @@ int sync_conflict = 0; int sync_error = 0; - CxList *ls_new = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)resource_path_cmp, CX_STORE_POINTERS); - CxList *ls_modified = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)resource_path_cmp, CX_STORE_POINTERS); - CxList *ls_conflict = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)resource_path_cmp, CX_STORE_POINTERS); - CxList *ls_update = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)resource_path_cmp, CX_STORE_POINTERS); - CxList *ls_delete = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)resource_path_cmp, CX_STORE_POINTERS); - CxList *ls_move = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)resource_path_cmp, CX_STORE_POINTERS); - CxList *ls_copy = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)resource_path_cmp, CX_STORE_POINTERS); - CxList *ls_mkcol = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)resource_path_cmp, CX_STORE_POINTERS); - + CxList *ls_new = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + CxList *ls_modified = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + CxList *ls_conflict = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + CxList *ls_update = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + CxList *ls_delete = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + CxList *ls_move = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + CxList *ls_copy = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + CxList *ls_mkcol = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + + cxSetCompareFunc(ls_new, (cx_compare_func)resource_path_cmp); + cxSetCompareFunc(ls_modified, (cx_compare_func)resource_path_cmp); + cxSetCompareFunc(ls_conflict, (cx_compare_func)resource_path_cmp); + cxSetCompareFunc(ls_update, (cx_compare_func)resource_path_cmp); + cxSetCompareFunc(ls_delete, (cx_compare_func)resource_path_cmp); + cxSetCompareFunc(ls_move, (cx_compare_func)resource_path_cmp); + cxSetCompareFunc(ls_copy, (cx_compare_func)resource_path_cmp); + cxSetCompareFunc(ls_mkcol, (cx_compare_func)resource_path_cmp); // upload all changed files //UcxList *resources = cmd_getoption(a, "read") ? @@ -2297,7 +2306,7 @@ // find all deleted files and cleanup the database CxMapIterator mapIter = cxMapIterator(db->resources); - CxList *removed_res = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *removed_res = cxLinkedListCreate(NULL, CX_STORE_POINTERS); cx_foreach(CxMapEntry *, entry, mapIter) { LocalResource *local = entry->value; // all filtered files should be removed from the database @@ -2579,7 +2588,8 @@ // delete all removed files cxListSort(ls_delete); - CxList *cols = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)localres_cmp_path_desc, CX_STORE_POINTERS); + CxList *cols = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + cxSetCompareFunc(cols, (cx_compare_func)localres_cmp_path_desc); CxList *cols_del = cols; // remember pointer for cleanup CxList *col_list = cols; CxList *deletelist = ls_delete; @@ -2734,8 +2744,10 @@ } remove_deleted_conflicts(dir, db); - CxList *modified = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)localres_cmp_path, CX_STORE_POINTERS); - CxList *deleted = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)localres_cmp_path, CX_STORE_POINTERS); + CxList *modified = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + CxList *deleted = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + cxSetCompareFunc(modified, (cx_compare_func)localres_cmp_path); + cxSetCompareFunc(deleted, (cx_compare_func)localres_cmp_path); // iterate over all db resources and check if any resource is // modified or deleted @@ -3039,10 +3051,10 @@ } CxList* local_scan(SyncDirectory *dir, SyncDatabase *db) { - CxList *resources = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *resources = cxLinkedListCreate(NULL, CX_STORE_POINTERS); char *path = strdup("/"); - CxList *stack = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *stack = cxLinkedListCreate(NULL, CX_STORE_POINTERS); cxListInsert(stack, 0, path); while(cxListSize(stack) > 0) { // get a directory path from the stack and read all entries @@ -3429,7 +3441,7 @@ ret = 1; } else if(etag) { cxstring e = cx_str(etag); - if(cx_strprefix(e, CX_STR("W/"))) { + if(cx_strprefix(e, cx_str("W/"))) { e = cx_strsubs(e, 2); } if(strcmp(e.ptr, res->etag)) { @@ -3467,7 +3479,7 @@ } cxstring e = cx_str(etag); - if(cx_strprefix(e, CX_STR("W/"))) { + if(cx_strprefix(e, cx_str("W/"))) { e = cx_strsubs(e, 2); } local->etag = cx_strdup(e).ptr; @@ -3878,7 +3890,7 @@ free(local_path); if(tag_length > 0) { - buf = cxBufferCreate(tag_data, (size_t)tag_length, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS); + buf = cxBufferCreate(cxDefaultAllocator, tag_data, (size_t)tag_length, CX_BUFFER_FREE_CONTENTS); buf->size = (size_t)tag_length; } } @@ -4204,7 +4216,7 @@ } CxMap *updated_parts_map = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, (nblocks/2)+64); - cxDefineDestructor(updated_parts_map, filepart_free); + cxSetDestructor(updated_parts_map, filepart_free); int blockindex = 0; int uploaded_parts = 0; @@ -4287,7 +4299,7 @@ // get etags from uploaded resources // also delete everything, that is not part of the file - CxList *updated_parts = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *updated_parts = cxLinkedListCreate(NULL, CX_STORE_POINTERS); DavResource *parts = dav_query(res->session, "select D:getetag from %s order by name", res->path); if(!parts) { log_resource_error(res->session, parts->path); @@ -5032,9 +5044,8 @@ CxMapIterator mi = cxMapIteratorValues(db->conflict); CxList* conflict_sources = cxLinkedListCreate( cxDefaultAllocator, - (cx_compare_func) strcmp, - CX_STORE_POINTERS - ); + CX_STORE_POINTERS); + cxSetCompareFunc(conflict_sources, (cx_compare_func)strcmp); cx_foreach(LocalResource *, res, mi) { cxListAdd(conflict_sources, res->conflict_source); } @@ -5148,7 +5159,8 @@ } DavResource *child = vcol->children; - CxList *children = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)strcmp, CX_STORE_POINTERS); + CxList *children = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + cxSetCompareFunc(children, (cx_compare_func)strcmp); while(child) { cxListAdd(children, child); child = child->next; @@ -5396,7 +5408,7 @@ newtag->name = tag; newtag->color = tagcolor; if(!tags) { - tags = cxLinkedListCreateSimple(CX_STORE_POINTERS); + tags = cxLinkedListCreate(NULL, CX_STORE_POINTERS); } cxListAdd(tags, newtag); store_tags = TRUE; @@ -5677,7 +5689,7 @@ int cmd_check_repositories(CmdArgs *a) { int ret = EXIT_SUCCESS; - CxList *reponames = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *reponames = cxLinkedListCreate(NULL, CX_STORE_POINTERS); { CxMapIterator iter = scfg_directory_iterator(); cx_foreach(SyncDirectory *, dir, iter) {
--- a/dav/system.c Sun Dec 07 20:16:59 2025 +0100 +++ b/dav/system.c Fri Dec 19 17:53:18 2025 +0100 @@ -339,7 +339,7 @@ int ret = 0; cxstring path_s = cx_str(path); - if(cx_strsuffix(path_s, CX_STR(".lnk"))) { + if(cx_strsuffix(path_s, cx_str(".lnk"))) { // looks like a .lnk file // check content IShellLink *sl;
--- a/dav/tags.c Sun Dec 07 20:16:59 2025 +0100 +++ b/dav/tags.c Fri Dec 19 17:53:18 2025 +0100 @@ -71,8 +71,8 @@ } CxList* parse_text_taglist(const char *buf, size_t length) { - CxList *tags = cxLinkedListCreateSimple(CX_STORE_POINTERS); - cxDefineDestructor(tags, free_dav_tag); + CxList *tags = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + cxSetDestructor(tags, free_dav_tag); int line_start = 0; for(int i=0;i<length;i++) { @@ -126,7 +126,7 @@ return NULL; } - CxBuffer *buf = cxBufferCreate(NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *buf = cxBufferCreate(cxDefaultAllocator, NULL, 128, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); CxIterator i = cxListIterator(tags); cx_foreach(DavTag *, tag, i) { if(tag->color) { @@ -140,11 +140,11 @@ CxList* parse_csv_taglist(const char *buf, size_t length) { - CxList *taglist = cxLinkedListCreateSimple(CX_STORE_POINTERS); - cxDefineDestructor(taglist, free_dav_tag); + CxList *taglist = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + cxSetDestructor(taglist, free_dav_tag); cxstring str = cx_strn(buf, length); - CxStrtokCtx tags = cx_strtok(str, CX_STR(","), INT_MAX); + CxStrtokCtx tags = cx_strtok(str, cx_str(","), INT_MAX); cxstring tagstr; while(cx_strtok_next(&tags, &tagstr)) { cxstring trimmed_tag = cx_strtrim(tagstr); @@ -159,7 +159,7 @@ } CxBuffer* create_csv_taglist(CxList *tags) { - CxBuffer *buf = cxBufferCreate(NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *buf = cxBufferCreate(cxDefaultAllocator, NULL, 128, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); int insertsep = 0; CxIterator i = cxListIterator(tags); cx_foreach(DavTag*, tag, i) { @@ -205,8 +205,8 @@ } CxList* parse_dav_xml_taglist(DavXmlNode *taglistnode) { - CxList *tags = cxLinkedListCreateSimple(CX_STORE_POINTERS); - cxDefineDestructor(tags, free_dav_tag); + CxList *tags = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + cxSetDestructor(tags, free_dav_tag); DavXmlNode *node = taglistnode; while(node) { @@ -287,7 +287,7 @@ } CxList* parse_macos_taglist(const char *buf, size_t length) { - CxList *taglist = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *taglist = cxLinkedListCreate(NULL, CX_STORE_POINTERS); taglist->collection.simple_destructor = (cx_destructor_func)free_dav_tag; CFDataRef data = CFDataCreateWithBytesNoCopy( @@ -330,7 +330,7 @@ cx_foreach(DavTag*, tag, iter) { CFStringRef str = NULL; if(tag->color) { - cxmutstr s = cx_strcat(3, cx_mutstr(tag->name), CX_STR("\n"), cx_str(tag->color)); + cxmutstr s = cx_strcat(3, cx_mutstr(tag->name), cx_str("\n"), cx_str(tag->color)); str = CFStringCreateWithCString(kCFAllocatorDefault, s.ptr, kCFStringEncodingUTF8); free(s.ptr); } else { @@ -417,8 +417,8 @@ // this map is used to check the existence of tags CxMap *tag_map = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 32); // merged taglist - CxList *new_tags = cxLinkedListCreateSimple(CX_STORE_POINTERS); - cxDefineDestructor(new_tags, free_dav_tag); + CxList *new_tags = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + cxSetDestructor(new_tags, free_dav_tag); // add all local tags if(tags1) {
--- a/libidav/config.c Sun Dec 07 20:16:59 2025 +0100 +++ b/libidav/config.c Fri Dec 19 17:53:18 2025 +0100 @@ -285,7 +285,7 @@ return NULL; } - CxBuffer *buf = cxBufferCreate(NULL, textLen, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); + CxBuffer *buf = cxBufferCreate(cxDefaultAllocator, NULL, textLen, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); cxBufferWrite(xmlText, 1, textLen, buf); xmlFree(xmlText); return buf; @@ -940,9 +940,9 @@ path->length = 0; int s; - if(cx_strprefix(url, CX_STR("http://"))) { + if(cx_strprefix(url, cx_str("http://"))) { s = 7; - } else if(cx_strprefix(url, CX_STR("https://"))) { + } else if(cx_strprefix(url, cx_str("https://"))) { s = 8; } else { s = 1; @@ -963,10 +963,10 @@ // TODO: who is responsible for freeing this repository? // how can the callee know, if he has to call free()? repo = dav_repository_new(config); - repo->name.value = cx_strdup_a(config->mp->allocator, CX_STR("")); + repo->name.value = cx_strdup_a(config->mp->allocator, cx_str("")); if(url.ptr[url.length-1] == '/') { repo->url.value = cx_strdup_a(config->mp->allocator, url); - *path = cx_strdup(CX_STR("/")); + *path = cx_strdup(cx_str("/")); } else if (cx_strchr(url, '/').length > 0) { // TODO: fix the following workaround after // fixing the inconsistent behavior of util_url_*() @@ -975,7 +975,7 @@ *path = cx_strdup(util_url_path_s(url)); } else { repo->url.value = cx_strdup(url); - *path = cx_strdup(CX_STR("/")); + *path = cx_strdup(cx_str("/")); } }
--- a/libidav/crypto.c Sun Dec 07 20:16:59 2025 +0100 +++ b/libidav/crypto.c Fri Dec 19 17:53:18 2025 +0100 @@ -1522,7 +1522,7 @@ } CxBuffer* aes_encrypt_buffer(CxBuffer *in, DavKey *key) { - CxBuffer *encbuf = cxBufferCreate(NULL, in->size, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *encbuf = cxBufferCreate(cxDefaultAllocator, NULL, in->size, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); if(!encbuf) { return NULL; } @@ -1549,7 +1549,7 @@ } CxBuffer* aes_decrypt_buffer(CxBuffer *in, DavKey *key) { - CxBuffer *decbuf = cxBufferCreate(NULL, in->size, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *decbuf = cxBufferCreate(cxDefaultAllocator, NULL, in->size, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); if(!decbuf) { return NULL; }
--- a/libidav/davqlexec.c Sun Dec 07 20:16:59 2025 +0100 +++ b/libidav/davqlexec.c Fri Dec 19 17:53:18 2025 +0100 @@ -179,7 +179,7 @@ cxmutstr dav_format_string(const CxAllocator *a, cxstring fstr, DavQLArgList *ap, davqlerror_t *error) { CxBuffer buf; - cxBufferInit(&buf, NULL, 128, a, CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&buf, a, NULL, 128, CX_BUFFER_AUTO_EXTEND); int placeholder = 0; for(int i=0;i<fstr.length;i++) { @@ -285,11 +285,11 @@ CxIterator i = cxListIterator(fields); cx_foreach(DavQLField*, field, i) { - if(!cx_strcmp(field->name, CX_STR("*"))) { + if(!cx_strcmp(field->name, cx_str("*"))) { cxMapFree(properties); *isallprop = 1; return create_allprop_propfind_request(); - } else if(!cx_strcmp(field->name, CX_STR("-"))) { + } else if(!cx_strcmp(field->name, cx_str("-"))) { cxMapFree(properties); return create_propfind_request(sn, NULL, "propfind", 0); } else { @@ -302,7 +302,7 @@ } CxMapIterator mi = cxMapIteratorValues(properties); - CxList *list = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxList *list = cxLinkedListCreate(NULL, CX_STORE_POINTERS); cx_foreach(DavProperty*, value, mi) { cxListAdd(list, value); } @@ -483,11 +483,11 @@ cxMempoolRegister(mp, rqbuf, (cx_destructor_func)cxBufferFree); // compile field list - CxList *cfieldlist = cxLinkedListCreate(mp->allocator, NULL, CX_STORE_POINTERS); + CxList *cfieldlist = cxLinkedListCreate(mp->allocator, CX_STORE_POINTERS); if(st->fields) { CxIterator i = cxListIterator(st->fields); cx_foreach(DavQLField*, field, i) { - if(cx_strcmp(field->name, CX_STR("*")) && cx_strcmp(field->name, CX_STR("-"))) { + if(cx_strcmp(field->name, cx_str("*")) && cx_strcmp(field->name, cx_str("-"))) { // compile field expression CxBuffer *code = dav_compile_expr( sn->context, @@ -543,7 +543,7 @@ // compile order criterion CxList *ordercr = NULL; if(st->orderby) { - ordercr = cxLinkedListCreate(mp->allocator, NULL, sizeof(DavOrderCriterion)); + ordercr = cxLinkedListCreate(mp->allocator, sizeof(DavOrderCriterion)); CxIterator i = cxListIterator(st->orderby); cx_foreach(DavQLOrderCriterion*, oc, i) { DavQLExpression *column = oc->column; @@ -601,7 +601,7 @@ DavResource *selroot = dav_resource_new(sn, path.ptr); - CxList *stack = cxLinkedListCreateSimple(sizeof(DavQLRes)); + CxList *stack = cxLinkedListCreate(NULL, sizeof(DavQLRes)); // initialize the stack with the requested resource DavQLRes res; res.resource = selroot; @@ -609,7 +609,7 @@ cxListInsert(stack, 0, &res); // reuseable response buffer - CxBuffer *rpbuf = cxBufferCreate(NULL, 4096, mp->allocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *rpbuf = cxBufferCreate(mp->allocator, NULL, 4096, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); if(!rpbuf) { // TODO: cleanup cxMempoolFree(mp); @@ -750,21 +750,21 @@ } int dav_identifier2resprop(cxstring src, davqlresprop_t *prop) { - if(!cx_strcmp(src, CX_STR("name"))) { + if(!cx_strcmp(src, cx_str("name"))) { *prop = DAVQL_RES_NAME; - } else if(!cx_strcmp(src, CX_STR("path"))) { + } else if(!cx_strcmp(src, cx_str("path"))) { *prop = DAVQL_RES_PATH; - } else if(!cx_strcmp(src, CX_STR("href"))) { + } else if(!cx_strcmp(src, cx_str("href"))) { *prop = DAVQL_RES_HREF; - } else if(!cx_strcmp(src, CX_STR("contentlength"))) { + } else if(!cx_strcmp(src, cx_str("contentlength"))) { *prop = DAVQL_RES_CONTENTLENGTH; - } else if(!cx_strcmp(src, CX_STR("contenttype"))) { + } else if(!cx_strcmp(src, cx_str("contenttype"))) { *prop = DAVQL_RES_CONTENTTYPE; - } else if(!cx_strcmp(src, CX_STR("creationdate"))) { + } else if(!cx_strcmp(src, cx_str("creationdate"))) { *prop = DAVQL_RES_CREATIONDATE; - } else if(!cx_strcmp(src, CX_STR("lastmodified"))) { + } else if(!cx_strcmp(src, cx_str("lastmodified"))) { *prop = DAVQL_RES_LASTMODIFIED; - } else if(!cx_strcmp(src, CX_STR("iscollection"))) { + } else if(!cx_strcmp(src, cx_str("iscollection"))) { *prop = DAVQL_RES_ISCOLLECTION; } else { return 0; @@ -835,10 +835,10 @@ return -1; } } else if(!dav_identifier2resprop(src, &cmd.data.resprop)) { - if(!cx_strcmp(src, CX_STR("true"))) { + if(!cx_strcmp(src, cx_str("true"))) { cmd.type = DAVQL_CMD_INT; cmd.data.integer = 1; - } else if(!cx_strcmp(src, CX_STR("false"))) { + } else if(!cx_strcmp(src, cx_str("false"))) { cmd.type = DAVQL_CMD_INT; cmd.data.integer = 0; } else { @@ -1032,7 +1032,7 @@ } CxBuffer* dav_compile_expr(DavContext *ctx, const CxAllocator *a, DavQLExpression *lexpr, DavQLArgList *ap) { - CxBuffer *bcode = cxBufferCreate(NULL, 512, a, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *bcode = cxBufferCreate(a, NULL, 512, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); if(!bcode) { return NULL; }
--- a/libidav/davqlparser.c Sun Dec 07 20:16:59 2025 +0100 +++ b/libidav/davqlparser.c Fri Dec 19 17:53:18 2025 +0100 @@ -169,7 +169,7 @@ static void dav_debug_ql_expr_print(DavQLExpression *expr) { if (dav_debug_ql_expr_selected(expr)) { - cxstring empty = CX_STR("(empty)"); + cxstring empty = cx_str("(empty)"); printf( "Text: %.*s\nType: %s\nOperator: %s\n", sfmtarg(expr->srctext), @@ -422,7 +422,7 @@ // we try to achieve two things: get as many information as possible // and recover the concrete source string (and not the token strings) - cxstring emptystring = CX_STR(""); + cxstring emptystring = cx_str(""); cxstring prev = token->prev ? (token->prev->prev ? token_sstr(token->prev->prev) : token_sstr(token->prev)) : emptystring; @@ -457,10 +457,10 @@ static const char *special_token_symbols = ",()+-*/&|^~=!<>"; static _Bool iskeyword(DavQLToken *token) { - cxstring keywords[] ={CX_STR("select"), CX_STR("set"), CX_STR("from"), CX_STR("at"), CX_STR("as"), - CX_STR("where"), CX_STR("anywhere"), CX_STR("like"), CX_STR("unlike"), CX_STR("and"), - CX_STR("or"), CX_STR("not"), CX_STR("xor"), CX_STR("with"), CX_STR("infinity"), - CX_STR("order"), CX_STR("by"), CX_STR("asc"), CX_STR("desc") + cxstring keywords[] ={cx_str("select"), cx_str("set"), cx_str("from"), cx_str("at"), cx_str("as"), + cx_str("where"), cx_str("anywhere"), cx_str("like"), cx_str("unlike"), cx_str("and"), + cx_str("or"), cx_str("not"), cx_str("xor"), cx_str("with"), cx_str("infinity"), + cx_str("order"), cx_str("by"), cx_str("asc"), cx_str("desc") }; for (int i = 0 ; i < sizeof(keywords)/sizeof(cxstring) ; i++) { if (!cx_strcasecmp(token->value, keywords[i])) { @@ -471,8 +471,8 @@ } static _Bool islongoperator(DavQLToken *token) { - cxstring operators[] = {CX_STR("and"), CX_STR("or"), CX_STR("not"), CX_STR("xor"), - CX_STR("like"), CX_STR("unlike") + cxstring operators[] = {cx_str("and"), cx_str("or"), cx_str("not"), cx_str("xor"), + cx_str("like"), cx_str("unlike") }; for (int i = 0 ; i < sizeof(operators)/sizeof(cxstring) ; i++) { if (!cx_strcasecmp(token->value, operators[i])) { @@ -484,7 +484,7 @@ static int dav_stmt_add_field(DavQLStatement *stmt, DavQLField *field) { if(!stmt->fields) { - stmt->fields = cxLinkedListCreateSimple(CX_STORE_POINTERS); + stmt->fields = cxLinkedListCreate(NULL, CX_STORE_POINTERS); if(!stmt->fields) { stmt->errorcode = DAVQL_ERROR_OUT_OF_MEMORY; return 1; @@ -645,7 +645,7 @@ alloc_token(); token->tokenclass = DAVQL_TOKEN_END; - token->value = CX_STR(""); + token->value = cx_str(""); cx_linked_list_add((void**)&tokens_begin, (void**)&tokens_end, offsetof(DavQLToken, prev), offsetof(DavQLToken, next), token); return tokens_begin; @@ -745,7 +745,7 @@ static void fmt_args_add(DavQLStatement *stmt, void *data) { if(!stmt->args) { - stmt->args = cxLinkedListCreateSimple(CX_STORE_POINTERS); + stmt->args = cxLinkedListCreate(NULL, CX_STORE_POINTERS); } cxListAdd(stmt->args, data); } @@ -1508,7 +1508,7 @@ DavQLOrderCriterion crit; if(!stmt->orderby) { - stmt->orderby = cxLinkedListCreateSimple(sizeof(DavQLOrderCriterion)); + stmt->orderby = cxLinkedListCreate(NULL, sizeof(DavQLOrderCriterion)); if(!stmt->orderby) { return 0; } @@ -1837,7 +1837,7 @@ void dav_free_statement(DavQLStatement *stmt) { if(stmt->fields) { - cxDefineDestructor(stmt->fields, dav_free_field); + cxSetDestructor(stmt->fields, dav_free_field); cxListFree(stmt->fields); } @@ -1849,7 +1849,7 @@ } if(stmt->orderby) { - cxDefineDestructor(stmt->orderby, dav_free_order_criterion); + cxSetDestructor(stmt->orderby, dav_free_order_criterion); cxListFree(stmt->orderby); } if(stmt->args) {
--- a/libidav/methods.c Sun Dec 07 20:16:59 2025 +0100 +++ b/libidav/methods.c Fri Dec 19 17:53:18 2025 +0100 @@ -74,7 +74,7 @@ curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, cxBufferWrite); curl_easy_setopt(handle, CURLOPT_WRITEDATA, response); CxMap *respheaders = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 32); - cxDefineDestructor(respheaders, free); + cxSetDestructor(respheaders, free); util_capture_header(handle, respheaders); for(int i=0;i<maxretry;i++) { @@ -122,39 +122,39 @@ } CxBuffer* create_allprop_propfind_request(void) { - CxBuffer *buf = cxBufferCreate(NULL, 512, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *buf = cxBufferCreate(cxDefaultAllocator, NULL, 512, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); cxstring s; - s = CX_STR("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); + s = cx_str("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<D:propfind xmlns:D=\"DAV:\">\n"); + s = cx_str("<D:propfind xmlns:D=\"DAV:\">\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<D:allprop/></D:propfind>\n"); + s = cx_str("<D:allprop/></D:propfind>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); return buf; } CxBuffer* create_cryptoprop_propfind_request(void) { - CxBuffer *buf = cxBufferCreate(NULL, 256, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *buf = cxBufferCreate(cxDefaultAllocator, NULL, 256, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); cxstring s; - s = CX_STR("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); + s = cx_str("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<D:propfind xmlns:D=\"DAV:\" xmlns:idav=\"" DAV_NS "\">\n"); + s = cx_str("<D:propfind xmlns:D=\"DAV:\" xmlns:idav=\"" DAV_NS "\">\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<D:prop><idav:crypto-prop/></D:prop></D:propfind>\n"); + s = cx_str("<D:prop><idav:crypto-prop/></D:prop></D:propfind>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); return buf; } CxBuffer* create_propfind_request(DavSession *sn, CxList *properties, char *rootelm, DavBool nocrypt) { - CxBuffer *buf = cxBufferCreate(NULL, 512, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *buf = cxBufferCreate(cxDefaultAllocator, NULL, 512, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); cxstring s; int add_crypto_name = 1; @@ -193,7 +193,7 @@ cxMapPut(namespaces, cx_hash_key_str("idav"), &idav_ns); } - s = CX_STR("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); + s = cx_str("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); // write root element and namespaces @@ -201,31 +201,31 @@ CxMapIterator mapi = cxMapIteratorValues(namespaces); cx_foreach(DavNamespace*, ns, mapi) { - s = CX_STR(" xmlns:"); + s = cx_str(" xmlns:"); cxBufferWrite(s.ptr, 1, s.length, buf); s = cx_str(ns->prefix); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("=\""); + s = cx_str("=\""); cxBufferWrite(s.ptr, 1, s.length, buf); s = cx_str(ns->name); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("\""); + s = cx_str("\""); cxBufferWrite(s.ptr, 1, s.length, buf); } - s = CX_STR(">\n"); + s = cx_str(">\n"); cxBufferWrite(s.ptr, 1, s.length, buf); // default properties - s = CX_STR("<D:prop>\n"); + s = cx_str("<D:prop>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<D:creationdate />\n<D:getlastmodified />\n"); + s = cx_str("<D:creationdate />\n<D:getlastmodified />\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<D:getcontentlength />\n<D:getcontenttype />\n"); + s = cx_str("<D:getcontentlength />\n<D:getcontenttype />\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<D:resourcetype />\n"); + s = cx_str("<D:resourcetype />\n"); cxBufferWrite(s.ptr, 1, s.length, buf); // crypto properties @@ -233,19 +233,19 @@ if(add_crypto_name) { cxBufferPut(buf, '<'); cxBufferPutString(buf, crypto_ns); - s = CX_STR(":crypto-name />\n"); + s = cx_str(":crypto-name />\n"); cxBufferWrite(s.ptr, 1, s.length, buf); } if(add_crypto_key) { cxBufferPut(buf, '<'); cxBufferPutString(buf, crypto_ns); - s = CX_STR(":crypto-key />\n"); + s = cx_str(":crypto-key />\n"); cxBufferWrite(s.ptr, 1, s.length, buf); } if(add_crypto_hash) { cxBufferPut(buf, '<'); cxBufferPutString(buf, crypto_ns); - s = CX_STR(":crypto-hash />\n"); + s = cx_str(":crypto-hash />\n"); cxBufferWrite(s.ptr, 1, s.length, buf); } } @@ -254,15 +254,15 @@ if(properties) { CxIterator i = cxListIterator(properties); cx_foreach(DavProperty*, prop, i) { - s = CX_STR("<"); + s = cx_str("<"); cxBufferWrite(s.ptr, 1, s.length, buf); s = cx_str(prop->ns->prefix); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR(":"); + s = cx_str(":"); cxBufferWrite(s.ptr, 1, s.length, buf); s = cx_str(prop->name); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR(" />\n"); + s = cx_str(" />\n"); cxBufferWrite(s.ptr, 1, s.length, buf); } } @@ -275,35 +275,35 @@ } CxBuffer* create_basic_propfind_request(void) { - CxBuffer *buf = cxBufferCreate(NULL, 512, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *buf = cxBufferCreate(cxDefaultAllocator, NULL, 512, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); cxstring s; - s = CX_STR("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); + s = cx_str("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<D:propfind xmlns:D=\"DAV:\" xmlns:i=\""); + s = cx_str("<D:propfind xmlns:D=\"DAV:\" xmlns:i=\""); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR(DAV_NS); + s = cx_str(DAV_NS); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("\" >\n"); + s = cx_str("\" >\n"); cxBufferWrite(s.ptr, 1, s.length, buf); // properties - s = CX_STR("<D:prop>\n"); + s = cx_str("<D:prop>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<D:resourcetype />\n"); + s = cx_str("<D:resourcetype />\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<i:crypto-key />\n"); + s = cx_str("<i:crypto-key />\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<i:crypto-name />\n"); + s = cx_str("<i:crypto-name />\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<i:crypto-hash />\n"); + s = cx_str("<i:crypto-hash />\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("</D:prop>\n"); + s = cx_str("</D:prop>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); // end - s = CX_STR("</D:propfind>\n"); + s = cx_str("</D:propfind>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); return buf; @@ -356,7 +356,7 @@ char *crypto_name = NULL; // name set by crypto-name property char *crypto_key = NULL; - result->properties = cxLinkedListCreateSimple(CX_STORE_POINTERS); // xmlNode list + result->properties = cxLinkedListCreate(NULL, CX_STORE_POINTERS); // xmlNode list xmlNode *node = parser->current->children; while(node) { @@ -389,7 +389,7 @@ return -1; } status_str = cx_strsubsl(status_str, 9, 3); - if(!cx_strcmp(status_str, CX_STR("200"))) { + if(!cx_strcmp(status_str, cx_str("200"))) { ok = 1; } } @@ -590,7 +590,7 @@ //DavResource *res = resource; DavResource *res = NULL; const char *href = NULL; - CxList *properties = cxLinkedListCreateSimple(CX_STORE_POINTERS); // xmlNode list + CxList *properties = cxLinkedListCreate(NULL, CX_STORE_POINTERS); // xmlNode list char *crypto_name = NULL; // name set by crypto-name property char *crypto_key = NULL; @@ -639,7 +639,7 @@ return 1; } status_str = cx_strsubsl(status_str, 9, 3); - if(!cx_strcmp(status_str, CX_STR("200"))) { + if(!cx_strcmp(status_str, cx_str("200"))) { ok = 1; } } @@ -822,11 +822,11 @@ } CxBuffer* create_proppatch_request(DavResourceData *data) { - CxBuffer *buf = cxBufferCreate(NULL, 512, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *buf = cxBufferCreate(cxDefaultAllocator, NULL, 512, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); cxstring s; CxMap *namespaces = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 8); - cxDefineDestructor(namespaces, free); + cxSetDestructor(namespaces, free); { char prefix[8]; @@ -851,30 +851,30 @@ } } - s = CX_STR("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); + s = cx_str("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); // write root element and namespaces - s = CX_STR("<D:propertyupdate xmlns:D=\"DAV:\""); + s = cx_str("<D:propertyupdate xmlns:D=\"DAV:\""); cxBufferWrite(s.ptr, 1, s.length, buf); CxMapIterator mapi = cxMapIterator(namespaces); cx_foreach(CxMapEntry*, entry, mapi) { - s = CX_STR(" xmlns:"); + s = cx_str(" xmlns:"); cxBufferWrite(s.ptr, 1, s.length, buf); s = cx_str(entry->value); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("=\""); + s = cx_str("=\""); cxBufferWrite(s.ptr, 1, s.length, buf); s = cx_strn(entry->key->data, entry->key->len); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("\""); + s = cx_str("\""); cxBufferWrite(s.ptr, 1, s.length, buf); } - s = CX_STR(">\n"); + s = cx_str(">\n"); cxBufferWrite(s.ptr, 1, s.length, buf); if(data->set) { - s = CX_STR("<D:set>\n<D:prop>\n"); + s = cx_str("<D:set>\n<D:prop>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); CxIterator i = cxListIterator(data->set); cx_foreach(DavProperty*, property, i) { @@ -884,15 +884,15 @@ } // begin tag - s = CX_STR("<"); + s = cx_str("<"); cxBufferWrite(s.ptr, 1, s.length, buf); s = cx_str(prefix); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR(":"); + s = cx_str(":"); cxBufferWrite(s.ptr, 1, s.length, buf); s = cx_str(property->name); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR(">"); + s = cx_str(">"); cxBufferWrite(s.ptr, 1, s.length, buf); // content @@ -904,43 +904,43 @@ } // end tag - s = CX_STR("</"); + s = cx_str("</"); cxBufferWrite(s.ptr, 1, s.length, buf); s = cx_str(prefix); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR(":"); + s = cx_str(":"); cxBufferWrite(s.ptr, 1, s.length, buf); s = cx_str(property->name); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR(">\n"); + s = cx_str(">\n"); cxBufferWrite(s.ptr, 1, s.length, buf); } - s = CX_STR("</D:prop>\n</D:set>\n"); + s = cx_str("</D:prop>\n</D:set>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); } if(data->remove) { - s = CX_STR("<D:remove>\n<D:prop>\n"); + s = cx_str("<D:remove>\n<D:prop>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); CxIterator i = cxListIterator(data->remove); cx_foreach(DavProperty*, property, i) { char *prefix = cxMapGet(namespaces, cx_hash_key_str(property->ns->name)); - s = CX_STR("<"); + s = cx_str("<"); cxBufferWrite(s.ptr, 1, s.length, buf); s = cx_str(prefix); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR(":"); + s = cx_str(":"); cxBufferWrite(s.ptr, 1, s.length, buf); s = cx_str(property->name); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR(" />\n"); + s = cx_str(" />\n"); cxBufferWrite(s.ptr, 1, s.length, buf); } - s = CX_STR("</D:prop>\n</D:remove>\n"); + s = cx_str("</D:prop>\n</D:remove>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); } - s = CX_STR("</D:propertyupdate>\n"); + s = cx_str("</D:propertyupdate>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); // cleanup namespace map @@ -950,43 +950,43 @@ } CxBuffer* create_crypto_proppatch_request(DavSession *sn, DavKey *key, const char *name, const char *hash) { - CxBuffer *buf = cxBufferCreate(NULL, 512, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *buf = cxBufferCreate(cxDefaultAllocator, NULL, 512, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); cxstring s; - s = CX_STR("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); + s = cx_str("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<D:propertyupdate xmlns:D=\"DAV:\" xmlns:idav=\"" DAV_NS "\">\n"); + s = cx_str("<D:propertyupdate xmlns:D=\"DAV:\" xmlns:idav=\"" DAV_NS "\">\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<D:set>\n<D:prop>\n"); + s = cx_str("<D:set>\n<D:prop>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); if(DAV_ENCRYPT_NAME(sn)) { - s = CX_STR("<idav:crypto-name>"); + s = cx_str("<idav:crypto-name>"); cxBufferWrite(s.ptr, 1, s.length, buf); char *crname = aes_encrypt(name, strlen(name), key); cxBufferPutString(buf, crname); free(crname); - s = CX_STR("</idav:crypto-name>\n"); + s = cx_str("</idav:crypto-name>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); } - s = CX_STR("<idav:crypto-key>"); + s = cx_str("<idav:crypto-key>"); cxBufferWrite(s.ptr, 1, s.length, buf); cxBufferPutString(buf, key->name); - s = CX_STR("</idav:crypto-key>\n"); + s = cx_str("</idav:crypto-key>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); if(hash) { - s = CX_STR("<idav:crypto-hash>"); + s = cx_str("<idav:crypto-hash>"); cxBufferWrite(s.ptr, 1, s.length, buf); cxBufferPutString(buf, hash); - s = CX_STR("</idav:crypto-hash>\n"); + s = cx_str("</idav:crypto-hash>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); } - s = CX_STR("</D:prop>\n</D:set>\n</D:propertyupdate>\n"); + s = cx_str("</D:prop>\n</D:set>\n</D:propertyupdate>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); return buf; @@ -1025,7 +1025,7 @@ CxBuffer *buf = NULL; if(!read_func) { - buf = cxBufferCreate(data, length, cxDefaultAllocator, 0); + buf = cxBufferCreate(cxDefaultAllocator, data, length, 0); buf->size = length; data = buf; read_func = (dav_read_func)cxBufferRead; @@ -1148,7 +1148,7 @@ free(ltheader); } //cxstring deststr = ucx_sprintf("Destination: %s", dest); - cxmutstr deststr = cx_strcat(2, CX_STR("Destination: "), cx_str(dest)); + cxmutstr deststr = cx_strcat(2, cx_str("Destination: "), cx_str(dest)); headers = curl_slist_append(headers, deststr.ptr); if(override) { headers = curl_slist_append(headers, "Overwrite: T"); @@ -1167,19 +1167,19 @@ CxBuffer* create_lock_request(void) { - CxBuffer *buf = cxBufferCreate(NULL, 512, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *buf = cxBufferCreate(cxDefaultAllocator, NULL, 512, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); cxstring s; - s = CX_STR("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); + s = cx_str("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("<D:lockinfo xmlns:D=\"DAV:\">\n" + s = cx_str("<D:lockinfo xmlns:D=\"DAV:\">\n" "<D:lockscope><D:exclusive/></D:lockscope>\n" "<D:locktype><D:write/></D:locktype>\n" "<D:owner><D:href>http://davutils.org/libidav/</D:href></D:owner>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); - s = CX_STR("</D:lockinfo>\n"); + s = cx_str("</D:lockinfo>\n"); cxBufferWrite(s.ptr, 1, s.length, buf); return buf;
--- a/libidav/pwdstore.c Sun Dec 07 20:16:59 2025 +0100 +++ b/libidav/pwdstore.c Fri Dec 19 17:53:18 2025 +0100 @@ -64,7 +64,7 @@ return NULL; } - CxBuffer *buf = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *buf = cxBufferCreate(cxDefaultAllocator, NULL, 2048, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); cx_stream_copy(in, buf, (cx_read_func)fread, (cx_write_func)cxBufferWrite); fclose(in); @@ -75,8 +75,8 @@ PwdStore *p = malloc(sizeof(PwdStore)); p->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); - p->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); - p->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS); + p->locations = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + p->noloc = cxLinkedListCreate(NULL, CX_STORE_POINTERS); p->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); p->content = buf; p->key = NULL; @@ -96,10 +96,10 @@ PwdStore* pwdstore_new(void) { PwdStore *p = calloc(1, sizeof(PwdStore)); p->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); - p->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); - p->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS); + p->locations = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + p->noloc = cxLinkedListCreate(NULL, CX_STORE_POINTERS); p->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); - p->content = cxBufferCreate(NULL, PWDS_HEADER_SIZE, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + p->content = cxBufferCreate(cxDefaultAllocator, NULL, PWDS_HEADER_SIZE, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); PWDS_MAGIC(p) = PWDS_MAGIC_CHAR; PWDS_VERSION(p) = 1; PWDS_ENC(p) = DAV_KEY_AES256; @@ -128,8 +128,8 @@ PwdStore *newp = calloc(1, sizeof(PwdStore)); newp->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); - newp->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); - newp->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS); + newp->locations = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + newp->noloc = cxLinkedListCreate(NULL, CX_STORE_POINTERS); newp->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); newp->content = newbuffer; newp->key = key; @@ -149,7 +149,7 @@ PwdIndexEntry *entry = e->value; CxList *locations = NULL; if(entry->locations) { - locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); + locations = cxLinkedListCreate(NULL, CX_STORE_POINTERS); CxIterator li = cxListIterator(entry->locations); cx_foreach(char *, location, li) { cxListAdd(locations, strdup(location)); @@ -206,8 +206,8 @@ } char *id = NULL; - CxList *locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); - cxDefineDestructor(locations, free); + CxList *locations = cxLinkedListCreate(NULL, CX_STORE_POINTERS); + cxSetDestructor(locations, free); // get id (required) int ret = 0; @@ -330,7 +330,7 @@ s->encoffset = PWDS_HEADER_SIZE + indexlen; // the index starts after the header - CxBuffer *index = cxBufferCreate(s->content->space+PWDS_HEADER_SIZE, indexlen, cxDefaultAllocator, 0); + CxBuffer *index = cxBufferCreate(cxDefaultAllocator, s->content->space+PWDS_HEADER_SIZE, indexlen, 0); index->size = indexlen; // read index @@ -352,7 +352,7 @@ // decrypt contet size_t encsz = p->content->size - p->encoffset; - CxBuffer *enc = cxBufferCreate(p->content->space + p->encoffset, encsz, cxDefaultAllocator, 0); + CxBuffer *enc = cxBufferCreate(cxDefaultAllocator, p->content->space + p->encoffset, encsz, 0); enc->size = encsz; enc->size = p->content->size - p->encoffset; CxBuffer *content = aes_decrypt_buffer(enc, p->key); @@ -398,7 +398,7 @@ } void pwdstore_free(PwdStore* p) { - cxDefineDestructor(p->ids, pwdstore_free_entry); + cxSetDestructor(p->ids, pwdstore_free_entry); cxMapFree(p->ids); cxListFree(p->locations); @@ -477,8 +477,8 @@ return 1; } - CxBuffer *index = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); - CxBuffer *content = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *index = cxBufferCreate(cxDefaultAllocator, NULL, 2048, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *content = cxBufferCreate(cxDefaultAllocator, NULL, 2048, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); // create index CxIterator i = cxListIterator(p->noloc); @@ -547,7 +547,7 @@ char *ps_password = NULL; if(secrets->unlock_cmd && strlen(secrets->unlock_cmd) > 0) { - CxBuffer *cmd_out = cxBufferCreate(NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *cmd_out = cxBufferCreate(cxDefaultAllocator, NULL, 128, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); if(!util_exec_command(secrets->unlock_cmd, cmd_out)) { // command successful, get first line from output without newline // and use that as password for the secretstore
--- a/libidav/resource.c Sun Dec 07 20:16:59 2025 +0100 +++ b/libidav/resource.c Fri Dec 19 17:53:18 2025 +0100 @@ -360,7 +360,7 @@ cxstring ns_str = cx_str(ns); cxstring name_str = cx_str(name); - return cx_strcat_a(a, 4, ns_str, CX_STR("\0"), name_str, CX_STR("\0")); + return cx_strcat_a(a, 4, ns_str, cx_str("\0"), name_str, cx_str("\0")); } @@ -619,7 +619,7 @@ static int add2propertylist(const CxAllocator *a, CxList **list, DavProperty *property) { if(!*list) { - CxList *newlist = cxLinkedListCreate(a, NULL, CX_STORE_POINTERS); + CxList *newlist = cxLinkedListCreate(a, CX_STORE_POINTERS); if(!newlist) { return 1; } @@ -788,7 +788,7 @@ CxMempool *mp = cxMempoolCreateSimple(64); const CxAllocator *a = mp->allocator; - CxList *proplist = cxArrayListCreate(a, NULL, sizeof(DavProperty), numprop); + CxList *proplist = cxArrayListCreate(a, sizeof(DavProperty), numprop); for(size_t i=0;i<numprop;i++) { DavProperty p; p.name = properties[i].name; @@ -870,7 +870,7 @@ data->read, data->seek); } else { - buf = cxBufferCreate(data->content, data->length, cxDefaultAllocator, 0); + buf = cxBufferCreate(cxDefaultAllocator, data->content, data->length, 0); buf->size = data->length; enc = aes_encrypter_new( sn->key, @@ -912,7 +912,7 @@ HashStream hstr; CxBuffer *iobuf = NULL; if(!data->read) { - iobuf = cxBufferCreate(data->content, data->length, cxDefaultAllocator, 0); + iobuf = cxBufferCreate(cxDefaultAllocator, data->content, data->length, 0); iobuf->size = data->length; init_hash_stream( &hstr, @@ -1045,7 +1045,7 @@ sn->error = DAV_OK; if(data->set || data->remove > 0) { CxBuffer *request = create_proppatch_request(data); - CxBuffer *response = cxBufferCreate(NULL, 1024, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *response = cxBufferCreate(cxDefaultAllocator, NULL, 1024, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); //printf("request:\n%.*s\n\n", request->pos, request->space); CURLcode ret = do_proppatch_request(sn, locktoken, request, response); @@ -1196,7 +1196,7 @@ DavLock *lock = dav_get_lock(res->session, res->path); char *locktoken = lock ? lock->token : NULL; - CxBuffer *response = cxBufferCreate(NULL, 4096, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *response = cxBufferCreate(cxDefaultAllocator, NULL, 4096, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); CURLcode ret = do_delete_request(res->session, locktoken, response); long status = 0; curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status); @@ -1399,7 +1399,7 @@ util_set_url(sn, dav_resource_get_href(res)); CxBuffer *request = create_lock_request(); - CxBuffer *response = cxBufferCreate(NULL, 512, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *response = cxBufferCreate(cxDefaultAllocator, NULL, 512, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); CURLcode ret = do_lock_request(sn, request, response, timeout); //printf("\nlock\n"); @@ -1476,7 +1476,7 @@ } CxBuffer *request = create_crypto_proppatch_request(sn, sn->key, name, hash); - CxBuffer *response = cxBufferCreate(NULL, 1024, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *response = cxBufferCreate(cxDefaultAllocator, NULL, 1024, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); util_set_url(sn, href); // TODO: lock @@ -1503,11 +1503,11 @@ return NULL; } - CxBuffer *content = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *content = cxBufferCreate(cxDefaultAllocator, NULL, 2048, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); // create an xml document containing all properties CxMap *nsmap = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 8); - cxDefineDestructor(nsmap, free); + cxSetDestructor(nsmap, free); cxMapPut(nsmap, cx_hash_key_str("DAV:"), strdup("D")); cxBufferPutString(content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
--- a/libidav/session.c Sun Dec 07 20:16:59 2025 +0100 +++ b/libidav/session.c Fri Dec 19 17:53:18 2025 +0100 @@ -65,7 +65,7 @@ sn->locks = NULL; // set proxy - DavProxy *proxy = cx_strprefix(url, CX_STR("https")) ? context->https_proxy + DavProxy *proxy = cx_strprefix(url, cx_str("https")) ? context->https_proxy : context->http_proxy; if (proxy->url) { @@ -395,8 +395,8 @@ cxstring p = cx_str(path); CxBuffer href; CxBuffer pbuf; - cxBufferInit(&href, NULL, 256, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); - cxBufferInit(&pbuf, NULL, 256, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&href, cxDefaultAllocator, NULL, 256, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&pbuf, cxDefaultAllocator, NULL, 256, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); int start = 0; int begin = 0; @@ -435,7 +435,7 @@ CxBuffer *rqbuf = create_basic_propfind_request(); cxstring remaining = cx_strsubs(p, start); - CxStrtokCtx elms = cx_strtok(remaining, CX_STR("/"), INT_MAX); + CxStrtokCtx elms = cx_strtok(remaining, cx_str("/"), INT_MAX); DavResource *res = root; cxBufferPutString(&pbuf, res->path); // iterate over all remaining path elements @@ -568,7 +568,8 @@ // create lock manager DavLockManager *locks = cxMalloc(sn->mp->allocator, sizeof(DavLockManager)); locks->resource_locks = cxHashMapCreate(sn->mp->allocator, CX_STORE_POINTERS, 16); - locks->collection_locks = cxLinkedListCreate(sn->mp->allocator, dav_lock_cmp, CX_STORE_POINTERS); + locks->collection_locks = cxLinkedListCreate(sn->mp->allocator, CX_STORE_POINTERS); + cxSetCompareFunc(locks->collection_locks, dav_lock_cmp); sn->locks = locks; return 0; }
--- a/libidav/utils.c Sun Dec 07 20:16:59 2025 +0100 +++ b/libidav/utils.c Fri Dec 19 17:53:18 2025 +0100 @@ -521,7 +521,7 @@ char* util_path_normalize(const char *path) { size_t len = strlen(path); CxBuffer buf; - cxBufferInit(&buf, NULL, len+1, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&buf, cxDefaultAllocator, NULL, len+1, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); if(path[0] == '/') { cxBufferPut(&buf, '/'); @@ -541,7 +541,7 @@ if(seg_len > 0) { cxstring seg = cx_strn(seg_ptr, seg_len); - if(!cx_strcmp(seg, CX_STR(".."))) { + if(!cx_strcmp(seg, cx_str(".."))) { for(int j=buf.pos;j>=0;j--) { char t = j < buf.pos ? buf.space[j] : 0; if(IS_PATH_SEPARATOR(t) || j == 0) { @@ -552,7 +552,7 @@ break; } } - } else if(!cx_strcmp(seg, CX_STR("."))) { + } else if(!cx_strcmp(seg, cx_str("."))) { // ignore } else { if(add_separator) { @@ -615,13 +615,13 @@ } } - cxBufferInit(&out, NULL, dircount*3+path_len-last_dir, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&out, cxDefaultAllocator, NULL, dircount*3+path_len-last_dir, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); for(size_t i=0;i<dircount;i++) { cxBufferPutString(&out, "../"); } } else { - cxBufferInit(&out, NULL, path_len - last_dir, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&out, cxDefaultAllocator, NULL, path_len - last_dir, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); } cxBufferPutString(&out, abspath + last_dir + 1); @@ -719,7 +719,7 @@ if(p) { path = cx_str((char*)p); } else { - path = CX_STR(""); + path = cx_str(""); } return util_concat_path_s(base, path).ptr; @@ -727,7 +727,7 @@ cxmutstr util_concat_path_s(cxstring base, cxstring path) { if(!path.ptr) { - path = CX_STR(""); + path = cx_str(""); } int add_separator = 0; @@ -743,7 +743,7 @@ cxmutstr url; if(add_separator) { - url = cx_strcat(3, base, CX_STR("/"), path); + url = cx_strcat(3, base, cx_str("/"), path); } else { url = cx_strcat(2, base, path); } @@ -753,7 +753,7 @@ cxmutstr util_concat_path_ext(cxstring base, cxstring path, char separator) { if(!path.ptr) { - path = CX_STR(""); + path = cx_str(""); } int add_separator = 0; @@ -809,7 +809,7 @@ } CxBuffer url; - cxBufferInit(&url, NULL, 256, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&url, cxDefaultAllocator, NULL, 256, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); // add base url cxBufferWrite(sn->base_url, 1, strlen(sn->base_url), &url); @@ -818,7 +818,7 @@ cxstring p = cx_strn(path, pathlen); - CxStrtokCtx tkctx = cx_strtok(p, CX_STR("/"), INT_MAX); + CxStrtokCtx tkctx = cx_strtok(p, cx_str("/"), INT_MAX); cxstring node; while(cx_strtok_next(&tkctx, &node)) { if(node.length > 0) { @@ -1155,7 +1155,7 @@ unsigned char *str = malloc(25); str[24] = '\0'; - cxstring t = CX_STR( + cxstring t = cx_str( "01234567890" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); @@ -1224,7 +1224,7 @@ cxmutstr util_readline(FILE *stream) { CxBuffer buf; - cxBufferInit(&buf, NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&buf, cxDefaultAllocator, NULL, 128, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); int c; while((c = fgetc(stream)) != EOF) { @@ -1260,7 +1260,7 @@ // read password input CxBuffer buf; - cxBufferInit(&buf, NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&buf, cxDefaultAllocator, NULL, 128, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); int c = 0; while((c = getpasswordchar()) != EOF) { if(c == '\n' || c == '\r') { @@ -1335,7 +1335,7 @@ char* util_hexstr(const unsigned char *data, size_t len) { size_t buflen = 2*len + 4; CxBuffer buf; - cxBufferInit(&buf, NULL, buflen + 1, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&buf, cxDefaultAllocator, NULL, buflen + 1, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); for(int i=0;i<len;i++) { cx_bprintf(&buf, "%02x", data[i]); }
--- a/libidav/versioning.c Sun Dec 07 20:16:59 2025 +0100 +++ b/libidav/versioning.c Fri Dec 19 17:53:18 2025 +0100 @@ -101,7 +101,7 @@ // create a version-tree request, which is almost the same as propfind CxBuffer *rqbuf = create_propfind_request(sn, proplist, "version-tree", 1); - CxBuffer *rpbuf = cxBufferCreate(NULL, 4096, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *rpbuf = cxBufferCreate(cxDefaultAllocator, NULL, 4096, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); // do the request CURLcode ret = do_report_request(sn, rqbuf, rpbuf);
--- a/libidav/webdav.c Sun Dec 07 20:16:59 2025 +0100 +++ b/libidav/webdav.c Fri Dec 19 17:53:18 2025 +0100 @@ -49,8 +49,8 @@ if(!context) { return NULL; } - context->sessions = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_ptr, CX_STORE_POINTERS); - cxDefineDestructor(context->sessions, dav_session_destructor); + context->sessions = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS); + cxSetDestructor(context->sessions, dav_session_destructor); context->http_proxy = calloc(1, sizeof(DavProxy)); if(!context->http_proxy) { dav_context_destroy(context); @@ -387,7 +387,7 @@ proplist = parse_properties_string(sn->context, cx_str(properties)); } CxBuffer *rqbuf = create_propfind_request(sn, proplist, "propfind", 0); - CxBuffer *rpbuf = cxBufferCreate(NULL, 4096, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *rpbuf = cxBufferCreate(cxDefaultAllocator, NULL, 4096, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); //fwrite(rqbuf->space, 1, rqbuf->size, stdout); //printf("\n"); @@ -432,7 +432,7 @@ CURL *handle = sn->handle; util_set_url(sn, dav_resource_get_href(root)); - CxBuffer *rpbuf = cxBufferCreate(NULL, 4096, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *rpbuf = cxBufferCreate(cxDefaultAllocator, NULL, 4096, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); DavResource *resource = root; CURLcode ret = do_propfind_request(sn, rqbuf, rpbuf); long status = 0; @@ -454,7 +454,7 @@ } CxList* parse_properties_string(DavContext *context, cxstring str) { - CxList *proplist = cxLinkedListCreateSimple(sizeof(DavProperty)); + CxList *proplist = cxLinkedListCreate(NULL, sizeof(DavProperty)); CxStrtokCtx tok = cx_strtok(str, cx_str(","), INT_MAX); cxstring s;
--- a/libidav/xml.c Sun Dec 07 20:16:59 2025 +0100 +++ b/libidav/xml.c Fri Dec 19 17:53:18 2025 +0100 @@ -63,7 +63,7 @@ ConvXmlElm ce; ce.node = node; ce.parent = NULL; - CxList *stack = cxLinkedListCreate(cxDefaultAllocator, NULL, sizeof(ConvXmlElm)); + CxList *stack = cxLinkedListCreate(cxDefaultAllocator, sizeof(ConvXmlElm)); if(!stack) { return NULL; }
--- a/make/gcc.mk Sun Dec 07 20:16:59 2025 +0100 +++ b/make/gcc.mk Fri Dec 19 17:53:18 2025 +0100 @@ -2,7 +2,7 @@ # gcc toolchain config # -CFLAGS = -std=gnu11 +CFLAGS = DEBUG_CFLAGS = -g RELEASE_CFLAGS = -O3 -DNDEBUG LDFLAGS =
--- a/test/crypto.c Sun Dec 07 20:16:59 2025 +0100 +++ b/test/crypto.c Fri Dec 19 17:53:18 2025 +0100 @@ -253,7 +253,7 @@ DavKey *key = i < 16 ? &keys256[i] : &keys128[i%16]; for(int j=0;j<20;j++) { - CxBuffer *content = cxBufferCreate(NULL, 256, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *content = cxBufferCreate(cxDefaultAllocator, NULL, 256, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); cxBufferPutString(content, strings[j]); content->pos = 0; @@ -285,9 +285,9 @@ } UCX_TEST(test_crypto_stream) { - CxBuffer *data = cxBufferCreate(NULL, 1024, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); - CxBuffer *cbuf = cxBufferCreate(NULL, 1024, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); - CxBuffer *pbuf = cxBufferCreate(NULL, 1024, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *data = cxBufferCreate(cxDefaultAllocator, NULL, 1024, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *cbuf = cxBufferCreate(cxDefaultAllocator, NULL, 1024, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); + CxBuffer *pbuf = cxBufferCreate(cxDefaultAllocator, NULL, 1024, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); UCX_TEST_BEGIN;
--- a/test/main.c Sun Dec 07 20:16:59 2025 +0100 +++ b/test/main.c Fri Dec 19 17:53:18 2025 +0100 @@ -47,7 +47,7 @@ } #endif -int test_main(int argc, char **argv) { +int test_main(int argc, char **argv) { printf("libidav tests\n"); printf("-------------\n\n");
--- a/ucx/allocator.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/allocator.c Fri Dec 19 17:53:18 2025 +0100 @@ -31,6 +31,35 @@ #include <errno.h> #include <string.h> +#ifdef _WIN32 +#include <Windows.h> +#include <sysinfoapi.h> +unsigned long cx_system_page_size(void) { + static unsigned long ps = 0; + if (ps == 0) { + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + ps = (unsigned long) sysinfo.dwPageSize; + } + return ps; +} +#else +#include <unistd.h> +unsigned long cx_system_page_size(void) { + static unsigned long ps = 0; + if (ps == 0) { + long sc = sysconf(_SC_PAGESIZE); + if (sc < 0) { + // fallback for systems which do not report a value here + ps = 4096; // LCOV_EXCL_LINE + } else { + ps = (unsigned long) sc; + } + } + return ps; +} +#endif + static void *cx_malloc_stdlib( cx_attr_unused void *d, size_t n @@ -79,6 +108,11 @@ void **mem, size_t n ) { + if (n == 0) { + free(*mem); + *mem = NULL; + return 0; + } void *nmem = realloc(*mem, n); if (nmem == NULL) { return 1; // LCOV_EXCL_LINE @@ -93,6 +127,11 @@ size_t nmemb, size_t size ) { + if (nmemb == 0 || size == 0) { + free(*mem); + *mem = NULL; + return 0; + } size_t n; if (cx_szmul(nmemb, size, &n)) { errno = EOVERFLOW; @@ -156,6 +195,11 @@ void **mem, size_t n ) { + if (n == 0) { + cxFree(allocator, *mem); + *mem = NULL; + return 0; + } void *nmem = allocator->cl->realloc(allocator->data, *mem, n); if (nmem == NULL) { return 1; // LCOV_EXCL_LINE @@ -171,6 +215,11 @@ size_t nmemb, size_t size ) { + if (nmemb == 0 || size == 0) { + cxFree(allocator, *mem); + *mem = NULL; + return 0; + } void *nmem = cxReallocArray(allocator, *mem, nmemb, size); if (nmem == NULL) { return 1; // LCOV_EXCL_LINE @@ -194,3 +243,7 @@ ) { allocator->cl->free(allocator->data, mem); } + +void cxFreeDefault(void *mem) { + cxDefaultAllocator->cl->free(cxDefaultAllocator->data, mem); +}
--- a/ucx/array_list.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/array_list.c Fri Dec 19 17:53:18 2025 +0100 @@ -26,413 +26,192 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#ifdef WITH_MEMRCHR +#define _GNU_SOURCE +#endif + #include "cx/array_list.h" #include "cx/compare.h" #include <assert.h> #include <string.h> #include <errno.h> -// Default array reallocator - -static void *cx_array_default_realloc( - void *array, - cx_attr_unused size_t old_capacity, - size_t new_capacity, - size_t elem_size, - cx_attr_unused CxArrayReallocator *alloc -) { - size_t n; - if (cx_szmul(new_capacity, elem_size, &n)) { - errno = EOVERFLOW; - return NULL; - } - return cxReallocDefault(array, n); -} - -CxArrayReallocator cx_array_default_reallocator_impl = { - cx_array_default_realloc, NULL, NULL -}; - -CxArrayReallocator *cx_array_default_reallocator = &cx_array_default_reallocator_impl; - -// Stack-aware array reallocator - -static void *cx_array_advanced_realloc( - void *array, - size_t old_capacity, - size_t new_capacity, - size_t elem_size, - cx_attr_unused CxArrayReallocator *alloc -) { - // check for overflow - size_t n; - if (cx_szmul(new_capacity, elem_size, &n)) { - errno = EOVERFLOW; - return NULL; - } - - // retrieve the pointer to the actual allocator - const CxAllocator *al = alloc->allocator; - - // check if the array is still located on the stack - void *newmem; - if (array == alloc->stack_ptr) { - newmem = cxMalloc(al, n); - if (newmem != NULL && array != NULL) { - memcpy(newmem, array, old_capacity*elem_size); - } - } else { - newmem = cxRealloc(al, array, n); - } - return newmem; -} - -struct cx_array_reallocator_s cx_array_reallocator( - const struct cx_allocator_s *allocator, - const void *stack_ptr -) { - if (allocator == NULL) { - allocator = cxDefaultAllocator; - } - return (struct cx_array_reallocator_s) { - cx_array_advanced_realloc, - allocator, stack_ptr, - }; -} - // LOW LEVEL ARRAY LIST FUNCTIONS /** - * Increases the capacity until it is a multiple of a some alignment or reaches the maximum. + * Intelligently calculates a new capacity, reserving some more + * elements than required to prevent too many allocations. * - * @param cap the required capacity (must not be larger than @p max) - * @param alignment the alignment - * @param max the maximum capacity - * @return the aligned capacity + * @param current_capacity the current capacity of the array + * @param needed_capacity the required capacity of the array + * @return the new capacity */ -static size_t cx_array_align_capacity( - size_t cap, - size_t alignment, - size_t max +static size_t cx_array_grow_capacity( + size_t current_capacity, + size_t needed_capacity ) { - if (cap > max - alignment) { - return cap; - } else { - return cap - (cap % alignment) + alignment; + if (current_capacity >= needed_capacity) { + return current_capacity; } + size_t cap = needed_capacity; + size_t alignment; + if (cap < 128) alignment = 16; + else if (cap < 1024) alignment = 64; + else if (cap < 8192) alignment = 512; + else alignment = 1024; + return cap - (cap % alignment) + alignment; +} + +int cx_array_init_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity) { + memset(array, 0, sizeof(CxArray)); + return cx_array_reserve_(allocator, array, elem_size, capacity); +} + +void cx_array_init_fixed_(CxArray *array, const void *data, size_t capacity, size_t size) { + array->data = (void*) data; + array->capacity = capacity; + array->size = size; } -int cx_array_reserve( - void **array, - void *size, - void *capacity, - unsigned width, - size_t elem_size, - size_t elem_count, - CxArrayReallocator *reallocator -) { - // assert pointers - assert(array != NULL); - assert(size != NULL); - assert(capacity != NULL); +int cx_array_reserve_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity) { + if (cxReallocateArray(allocator, &array->data, capacity, elem_size)) { + return -1; // LCOV_EXCL_LINE + } + array->capacity = capacity; + if (array->size > capacity) { + array->size = capacity; + } + return 0; +} - // default reallocator - if (reallocator == NULL) { - reallocator = cx_array_default_reallocator; +int cx_array_copy_to_new_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity) { + CxArray heap_array; + if (cx_array_init_(allocator, &heap_array, elem_size, capacity)) { + return -1; // LCOV_EXCL_LINE + } + heap_array.size = array->size; + memcpy(heap_array.data, array->data, elem_size * array->size); + *array = heap_array; + return 0; +} + +int cx_array_insert_(const CxAllocator *allocator, CxArray *array, + size_t elem_size, size_t index, const void *other, size_t n) { + // out of bounds and special case check + if (index > array->size) return -1; + if (n == 0) return 0; + + // guarantee enough capacity + if (array->capacity < array->size + n) { + const size_t new_capacity = cx_array_grow_capacity(array->capacity,array->size + n); + if (cxReallocateArray(allocator, &array->data, new_capacity, elem_size)) { + return -1; // LCOV_EXCL_LINE + } + array->capacity = new_capacity; } - // determine size and capacity - size_t oldcap; - size_t oldsize; - size_t max_size; - if (width == 0 || width == sizeof(size_t)) { - oldcap = *(size_t*) capacity; - oldsize = *(size_t*) size; - max_size = SIZE_MAX; - } else if (width == sizeof(uint16_t)) { - oldcap = *(uint16_t*) capacity; - oldsize = *(uint16_t*) size; - max_size = UINT16_MAX; - } else if (width == sizeof(uint8_t)) { - oldcap = *(uint8_t*) capacity; - oldsize = *(uint8_t*) size; - max_size = UINT8_MAX; - } -#if CX_WORDSIZE == 64 - else if (width == sizeof(uint32_t)) { - oldcap = *(uint32_t*) capacity; - oldsize = *(uint32_t*) size; - max_size = UINT32_MAX; - } -#endif - else { - errno = EINVAL; - return 1; + // determine insert position + char *dst = array->data; + dst += index * elem_size; + + // do we need to move some elements? + if (index < array->size) { + size_t elems_to_move = array->size - index; + char *target = dst + n * elem_size; + memmove(target, dst, elems_to_move * elem_size); } - // assert that the array is allocated when it has capacity - assert(*array != NULL || oldcap == 0); - - // check for overflow - if (elem_count > max_size - oldsize) { - errno = EOVERFLOW; - return 1; + // place the new elements, if any + // otherwise, this function just reserved the memory (a.k.a emplace) + if (other != NULL) { + memcpy(dst, other, n * elem_size); } - - // determine new capacity - size_t newcap = oldsize + elem_count; - - // reallocate if possible - if (newcap > oldcap) { - // calculate new capacity (next number divisible by 16) - newcap = cx_array_align_capacity(newcap, 16, max_size); - - // perform reallocation - void *newmem = reallocator->realloc( - *array, oldcap, newcap, elem_size, reallocator - ); - if (newmem == NULL) { - return 1; // LCOV_EXCL_LINE - } - - // store new pointer - *array = newmem; - - // store new capacity - if (width == 0 || width == sizeof(size_t)) { - *(size_t*) capacity = newcap; - } else if (width == sizeof(uint16_t)) { - *(uint16_t*) capacity = (uint16_t) newcap; - } else if (width == sizeof(uint8_t)) { - *(uint8_t*) capacity = (uint8_t) newcap; - } -#if CX_WORDSIZE == 64 - else if (width == sizeof(uint32_t)) { - *(uint32_t*) capacity = (uint32_t) newcap; - } -#endif - } + array->size += n; return 0; } -int cx_array_copy( - void **target, - void *size, - void *capacity, - unsigned width, - size_t index, - const void *src, +int cx_array_insert_sorted_c_( + const CxAllocator *allocator, + CxArray *array, size_t elem_size, - size_t elem_count, - CxArrayReallocator *reallocator + const void *sorted_data, + size_t n, + cx_compare_func2 cmp_func, + void *context, + bool allow_duplicates ) { // assert pointers - assert(target != NULL); - assert(size != NULL); - assert(capacity != NULL); - assert(src != NULL); - - // default reallocator - if (reallocator == NULL) { - reallocator = cx_array_default_reallocator; - } + assert(allocator != NULL); + assert(array != NULL); + assert(cmp_func != NULL); + assert(sorted_data != NULL); - // determine size and capacity - size_t oldcap; - size_t oldsize; - size_t max_size; - if (width == 0 || width == sizeof(size_t)) { - oldcap = *(size_t*) capacity; - oldsize = *(size_t*) size; - max_size = SIZE_MAX; - } else if (width == sizeof(uint16_t)) { - oldcap = *(uint16_t*) capacity; - oldsize = *(uint16_t*) size; - max_size = UINT16_MAX; - } else if (width == sizeof(uint8_t)) { - oldcap = *(uint8_t*) capacity; - oldsize = *(uint8_t*) size; - max_size = UINT8_MAX; - } -#if CX_WORDSIZE == 64 - else if (width == sizeof(uint32_t)) { - oldcap = *(uint32_t*) capacity; - oldsize = *(uint32_t*) size; - max_size = UINT32_MAX; - } -#endif - else { - errno = EINVAL; - return 1; - } + // corner case + if (n == 0) return 0; - // assert that the array is allocated when it has capacity - assert(*target != NULL || oldcap == 0); - - // check for overflow - if (index > max_size || elem_count > max_size - index) { + // overflow check + // LCOV_EXCL_START + if (n > SIZE_MAX - array->size) { errno = EOVERFLOW; return 1; } - - // check if resize is required - size_t minsize = index + elem_count; - size_t newsize = oldsize < minsize ? minsize : oldsize; - - // reallocate if possible - size_t newcap = oldcap; - if (newsize > oldcap) { - // check, if we need to repair the src pointer - uintptr_t targetaddr = (uintptr_t) *target; - uintptr_t srcaddr = (uintptr_t) src; - bool repairsrc = targetaddr <= srcaddr - && srcaddr < targetaddr + oldcap * elem_size; - - // calculate new capacity (next number divisible by 16) - newcap = cx_array_align_capacity(newsize, 16, max_size); - assert(newcap > newsize); - - // perform reallocation - void *newmem = reallocator->realloc( - *target, oldcap, newcap, elem_size, reallocator - ); - if (newmem == NULL) { - return 1; // LCOV_EXCL_LINE - } - - // repair src pointer, if necessary - if (repairsrc) { - src = ((char *) newmem) + (srcaddr - targetaddr); - } - - // store new pointer - *target = newmem; - } - - // determine target pointer - char *start = *target; - start += index * elem_size; - - // copy elements and set new size - // note: no overflow check here, b/c we cannot get here w/o allocation - memmove(start, src, elem_count * elem_size); - - // if any of size or capacity changed, store them back - if (newsize != oldsize || newcap != oldcap) { - if (width == 0 || width == sizeof(size_t)) { - *(size_t*) capacity = newcap; - *(size_t*) size = newsize; - } else if (width == sizeof(uint16_t)) { - *(uint16_t*) capacity = (uint16_t) newcap; - *(uint16_t*) size = (uint16_t) newsize; - } else if (width == sizeof(uint8_t)) { - *(uint8_t*) capacity = (uint8_t) newcap; - *(uint8_t*) size = (uint8_t) newsize; - } -#if CX_WORDSIZE == 64 - else if (width == sizeof(uint32_t)) { - *(uint32_t*) capacity = (uint32_t) newcap; - *(uint32_t*) size = (uint32_t) newsize; - } -#endif - } - - // return successfully - return 0; -} - -static int cx_array_insert_sorted_impl( - void **target, - size_t *size, - size_t *capacity, - cx_compare_func cmp_func, - const void *sorted_data, - size_t elem_size, - size_t elem_count, - CxArrayReallocator *reallocator, - bool allow_duplicates -) { - // assert pointers - assert(target != NULL); - assert(size != NULL); - assert(capacity != NULL); - assert(cmp_func != NULL); - assert(sorted_data != NULL); - - // default reallocator - if (reallocator == NULL) { - reallocator = cx_array_default_reallocator; - } - - // corner case - if (elem_count == 0) return 0; - - // overflow check - if (elem_count > SIZE_MAX - *size) { - errno = EOVERFLOW; - return 1; - } + // LCOV_EXCL_STOP // store some counts - size_t old_size = *size; - size_t old_capacity = *capacity; + const size_t old_size = array->size; + const size_t old_capacity = array->capacity; // the necessary capacity is the worst case assumption, including duplicates - size_t needed_capacity = old_size + elem_count; + const size_t needed_capacity = cx_array_grow_capacity(old_capacity, old_size + n); // if we need more than we have, try a reallocation if (needed_capacity > old_capacity) { - size_t new_capacity = cx_array_align_capacity(needed_capacity, 16, SIZE_MAX); - void *new_mem = reallocator->realloc( - *target, old_capacity, new_capacity, elem_size, reallocator - ); - if (new_mem == NULL) { - // give it up right away, there is no contract - // that requires us to insert as much as we can - return 1; // LCOV_EXCL_LINE + if (cxReallocateArray(allocator, &array->data, needed_capacity, elem_size)) { + return -1; // LCOV_EXCL_LINE } - *target = new_mem; - *capacity = new_capacity; + array->capacity = needed_capacity; } // now we have guaranteed that we can insert everything - size_t new_size = old_size + elem_count; - *size = new_size; + size_t new_size = old_size + n; + array->size = new_size; // declare the source and destination indices/pointers size_t si = 0, di = 0; const char *src = sorted_data; - char *dest = *target; + char *dest = array->data; // find the first insertion point - di = cx_array_binary_search_sup(dest, old_size, elem_size, src, cmp_func); + di = cx_array_binary_search_sup_c(dest, old_size, elem_size, src, cmp_func, context); dest += di * elem_size; // move the remaining elements in the array completely to the right // we will call it the "buffer" for parked elements size_t buf_size = old_size - di; size_t bi = new_size - buf_size; - char *bptr = ((char *) *target) + bi * elem_size; + char *bptr = ((char *) array->data) + bi * elem_size; memmove(bptr, dest, buf_size * elem_size); // while there are both source and buffered elements left, // copy them interleaving - while (si < elem_count && bi < new_size) { - // determine how many source elements can be inserted + while (si < n && bi < new_size) { + // determine how many source elements can be inserted. + // the first element that shall not be inserted is the smallest element + // that is strictly larger than the first buffered element + // (located at the index of the infimum plus one). + // the infimum is guaranteed to exist: + // - if all src elements are larger, + // there is no buffer, and this loop is skipped + // - if any src element is smaller or equal, the infimum exists + // - when all src elements that are smaller are copied, the second part + // of this loop body will copy the remaining buffer (emptying it) + // Therefore, the buffer can never contain an element that is smaller + // than any element in the source and the infimum exists. size_t copy_len, bytes_copied; - copy_len = cx_array_binary_search_sup( - src, - elem_count - si, - elem_size, - bptr, - cmp_func + copy_len = cx_array_binary_search_inf_c( + src, n - si, elem_size, bptr, cmp_func, context ); - // binary search gives us the smallest index; - // we also want to include equal elements here - while (si + copy_len < elem_count - && cmp_func(bptr, src+copy_len*elem_size) == 0) { - copy_len++; - } + copy_len++; // copy the source elements if (copy_len > 0) { @@ -449,17 +228,17 @@ // for being a duplicate of the bptr const char *end_of_src = src + (copy_len - 1) * elem_size; size_t skip_len = 0; - while (copy_len > 0 && cmp_func(bptr, end_of_src) == 0) { + while (copy_len > 0 && cmp_func(bptr, end_of_src, context) == 0) { end_of_src -= elem_size; skip_len++; copy_len--; } - char *last = dest == *target ? NULL : dest - elem_size; + char *last = dest == array->data ? NULL : dest - elem_size; // then iterate through the source chunk // and skip all duplicates with the last element in the array size_t more_skipped = 0; for (unsigned j = 0; j < copy_len; j++) { - if (last != NULL && cmp_func(last, src) == 0) { + if (last != NULL && cmp_func(last, src, context) == 0) { // duplicate - skip src += elem_size; si++; @@ -478,20 +257,21 @@ si += skip_len; skip_len += more_skipped; // reduce the actual size by the number of skipped elements - *size -= skip_len; + array->size -= skip_len; } } // when all source elements are in place, we are done - if (si >= elem_count) break; + if (si >= n) break; // determine how many buffered elements need to be restored - copy_len = cx_array_binary_search_sup( + copy_len = cx_array_binary_search_sup_c( bptr, new_size - bi, elem_size, src, - cmp_func + cmp_func, + context ); // restore the buffered elements @@ -504,33 +284,24 @@ } // still source elements left? - if (si < elem_count) { + if (si < n) { if (allow_duplicates) { // duplicates allowed or nothing inserted yet: simply copy everything - memcpy(dest, src, elem_size * (elem_count - si)); + memcpy(dest, src, elem_size * (n - si)); } else { - if (dest != *target) { - // skip all source elements that equal the last element - char *last = dest - elem_size; - while (si < elem_count) { - if (last != NULL && cmp_func(last, src) == 0) { - src += elem_size; - si++; - (*size)--; - } else { - break; - } - } - } - // we must check the elements in the chunk one by one - while (si < elem_count) { + // we must check the remaining source elements one by one + // to skip the duplicates. + // Note that no source element can equal the last element in the + // destination, because that would have created an insertion point + // and a buffer, s.t. the above loop already handled the duplicates + while (si < n) { // find a chain of elements that can be copied size_t copy_len = 1, skip_len = 0; { const char *left_src = src; - while (si + copy_len < elem_count) { + while (si + copy_len + skip_len < n) { const char *right_src = left_src + elem_size; - int d = cmp_func(left_src, right_src); + int d = cmp_func(left_src, right_src, context); if (d < 0) { if (skip_len > 0) { // new larger element found; @@ -553,13 +324,13 @@ src += bytes_copied + skip_len * elem_size; si += copy_len + skip_len; di += copy_len; - *size -= skip_len; + array->size -= skip_len; } } } // buffered elements need to be moved when we skipped duplicates - size_t total_skipped = new_size - *size; + size_t total_skipped = new_size - array->size; if (bi < new_size && total_skipped > 0) { // move the remaining buffer to the end of the array memmove(dest, bptr, elem_size * (new_size - bi)); @@ -568,40 +339,116 @@ return 0; } -int cx_array_insert_sorted( - void **target, - size_t *size, - size_t *capacity, - cx_compare_func cmp_func, +int cx_array_insert_sorted_( + const CxAllocator *allocator, + CxArray *array, + size_t elem_size, const void *sorted_data, - size_t elem_size, - size_t elem_count, - CxArrayReallocator *reallocator + size_t n, + cx_compare_func cmp_func, + bool allow_duplicates ) { - return cx_array_insert_sorted_impl(target, size, capacity, - cmp_func, sorted_data, elem_size, elem_count, reallocator, true); + cx_compare_func_wrapper wrapper = {cmp_func}; + return cx_array_insert_sorted_c_(allocator, array, elem_size, sorted_data, + n, cx_ccmp_wrap, &wrapper, allow_duplicates); +} + +#ifndef WITH_QSORT_R +static thread_local cx_compare_func2 cx_array_fn_for_qsort; +static thread_local void *cx_array_context_for_qsort; +static int cx_array_qsort_wrapper(const void *l, const void *r) { + return cx_array_fn_for_qsort(l, r, cx_array_context_for_qsort); +} +#endif + +void cx_array_qsort_c(void *array, size_t nmemb, size_t size, + cx_compare_func2 fn, void *context) { +#ifdef WITH_QSORT_R + qsort_r(array, nmemb, size, fn, context); +#else + cx_array_fn_for_qsort = fn; + cx_array_context_for_qsort = context; + qsort(array, nmemb, size, cx_array_qsort_wrapper); +#endif +} + +void cx_array_sort_(CxArray *array, size_t elem_size, + cx_compare_func fn) { + qsort(array->data, array->size, elem_size, fn); +} + +void cx_array_sort_c_(CxArray *array, size_t elem_size, + cx_compare_func2 fn, void *context) { + cx_array_qsort_c(array->data, array->size, elem_size, fn, context); +} + +CxIterator cx_array_iterator_(CxArray *array, size_t elem_size) { + return cxIterator(array->data, elem_size, array->size); +} + +CxIterator cx_array_iterator_ptr_(CxArray *array) { + return cxIteratorPtr(array->data, array->size); } -int cx_array_insert_unique( - void **target, - size_t *size, - size_t *capacity, - cx_compare_func cmp_func, - const void *sorted_data, - size_t elem_size, - size_t elem_count, - CxArrayReallocator *reallocator -) { - return cx_array_insert_sorted_impl(target, size, capacity, - cmp_func, sorted_data, elem_size, elem_count, reallocator, false); +void cx_array_remove_(CxArray *array, size_t elem_size, size_t index, size_t n, bool fast) { + if (n == 0) return; + if (index >= array->size) return; + if (index + n >= array->size) { + // only tail elements are removed + array->size = index; + return; + } + array->size -= n; + size_t remaining = array->size - index; + char *dest = ((char*)array->data) + index * elem_size; + if (fast) { + char *src = dest + remaining * elem_size; + if (n == 1 && elem_size <= CX_WORDSIZE/8) { + // try to optimize int-sized values + // (from likely to unlikely) + if (elem_size == sizeof(int32_t)) { + *(int32_t*)dest = *(int32_t*)src; + return; + } +#if CX_WORDSIZE == 64 + if (elem_size == sizeof(int64_t)) { + *(int64_t*)dest = *(int64_t*)src; + return; + } +#endif + if (elem_size == sizeof(int8_t)) { + *(int8_t*)dest = *(int8_t*)src; + return; + } + if (elem_size == sizeof(int16_t)) { + *(int16_t*)dest = *(int16_t*)src; + return; + } + // note we cannot optimize the last branch, because + // the elem_size could be crazily misaligned + } + memcpy(dest, src, n * elem_size); + } else { + char *src = dest + n * elem_size; + memmove(dest, src, remaining * elem_size); + } } -size_t cx_array_binary_search_inf( +void cx_array_free_(const CxAllocator *allocator, CxArray *array) { + cxFree(allocator, array->data); + array->data = NULL; + array->size = array->capacity = 0; +} + + +// implementation that finds ANY index +static size_t cx_array_binary_search_inf_impl( const void *arr, size_t size, size_t elem_size, const void *elem, - cx_compare_func cmp_func + cx_compare_func2 cmp_func, + void *context ) { // special case: empty array if (size == 0) return 0; @@ -613,7 +460,7 @@ const char *array = arr; // check the first array element - result = cmp_func(elem, array); + result = cmp_func(elem, array, context); if (result < 0) { return size; } else if (result == 0) { @@ -624,7 +471,7 @@ if (size == 1) return 0; // check the last array element - result = cmp_func(elem, array + elem_size * (size - 1)); + result = cmp_func(elem, array + elem_size * (size - 1), context); if (result >= 0) { return size - 1; } @@ -633,21 +480,14 @@ // so start the binary search size_t left_index = 1; size_t right_index = size - 1; - size_t pivot_index; + size_t pivot_index = 0; while (left_index <= right_index) { pivot_index = left_index + (right_index - left_index) / 2; const char *arr_elem = array + pivot_index * elem_size; - result = cmp_func(elem, arr_elem); + result = cmp_func(elem, arr_elem, context); if (result == 0) { // found it! - // check previous elements; - // when they are equal, report the smallest index - arr_elem -= elem_size; - while (pivot_index > 0 && cmp_func(elem, arr_elem) == 0) { - pivot_index--; - arr_elem -= elem_size; - } return pivot_index; } else if (result < 0) { // element is smaller than pivot, continue search left @@ -662,6 +502,85 @@ return result < 0 ? (pivot_index - 1) : pivot_index; } +size_t cx_array_binary_search_inf_c( + const void *arr, + size_t size, + size_t elem_size, + const void *elem, + cx_compare_func2 cmp_func, + void *context +) { + size_t index = cx_array_binary_search_inf_impl( + arr, size, elem_size, elem, cmp_func, context); + // in case of equality, report the largest index + const char *e = ((const char *) arr) + (index + 1) * elem_size; + while (index + 1 < size && cmp_func(e, elem, context) == 0) { + e += elem_size; + index++; + } + return index; +} + +size_t cx_array_binary_search_c( + const void *arr, + size_t size, + size_t elem_size, + const void *elem, + cx_compare_func2 cmp_func, + void *context +) { + size_t index = cx_array_binary_search_inf_c( + arr, size, elem_size, elem, cmp_func, context + ); + if (index < size && cmp_func(((const char *) arr) + index * elem_size, + elem, context) == 0) { + return index; + } else { + return size; + } +} + +size_t cx_array_binary_search_sup_c( + const void *arr, + size_t size, + size_t elem_size, + const void *elem, + cx_compare_func2 cmp_func, + void *context +) { + size_t index = cx_array_binary_search_inf_impl( + arr, size, elem_size, elem, cmp_func, context + ); + const char *e = ((const char *) arr) + index * elem_size; + if (index == size) { + // no infimum means the first element is supremum + return 0; + } else if (cmp_func(e, elem, context) == 0) { + // found an equal element, search the smallest index + e -= elem_size; // e now contains the element at index-1 + while (index > 0 && cmp_func(e, elem, context) == 0) { + e -= elem_size; + index--; + } + return index; + } else { + // we already have the largest index of the infimum (by design) + // the next element is the supremum (or there is no supremum) + return index + 1; + } +} + +size_t cx_array_binary_search_inf( + const void *arr, + size_t size, + size_t elem_size, + const void *elem, + cx_compare_func cmp_func +) { + cx_compare_func_wrapper wrapper = {cmp_func}; + return cx_array_binary_search_inf_c(arr, size, elem_size, elem, cx_ccmp_wrap, &wrapper); +} + size_t cx_array_binary_search( const void *arr, size_t size, @@ -669,15 +588,8 @@ const void *elem, cx_compare_func cmp_func ) { - size_t index = cx_array_binary_search_inf( - arr, size, elem_size, elem, cmp_func - ); - if (index < size && - cmp_func(((const char *) arr) + index * elem_size, elem) == 0) { - return index; - } else { - return size; - } + cx_compare_func_wrapper wrapper = {cmp_func}; + return cx_array_binary_search_c(arr, size, elem_size, elem, cx_ccmp_wrap, &wrapper); } size_t cx_array_binary_search_sup( @@ -687,17 +599,8 @@ const void *elem, cx_compare_func cmp_func ) { - size_t inf = cx_array_binary_search_inf( - arr, size, elem_size, elem, cmp_func - ); - if (inf == size) { - // no infimum means, first element is supremum - return 0; - } else if (cmp_func(((const char *) arr) + inf * elem_size, elem) == 0) { - return inf; - } else { - return inf + 1; - } + cx_compare_func_wrapper wrapper = {cmp_func}; + return cx_array_binary_search_sup_c(arr, size, elem_size, elem, cx_ccmp_wrap, &wrapper); } #ifndef CX_ARRAY_SWAP_SBO_SIZE @@ -750,7 +653,6 @@ struct cx_list_s base; void *data; size_t capacity; - CxArrayReallocator reallocator; } cx_array_list; static void cx_arl_destructor(struct cx_list_s *list) { @@ -781,43 +683,47 @@ const void *array, size_t n ) { - // out of bounds and special case check - if (index > list->collection.size || n == 0) return 0; - - // get a correctly typed pointer to the list cx_array_list *arl = (cx_array_list *) list; - - // guarantee enough capacity - if (arl->capacity < list->collection.size + n) { - size_t new_capacity = list->collection.size + n; - new_capacity = cx_array_align_capacity(new_capacity, 16, SIZE_MAX); - if (cxReallocateArray( - list->collection.allocator, - &arl->data, new_capacity, - list->collection.elem_size) - ) { - return 0; - } - arl->capacity = new_capacity; + CxArray wrap = { + arl->data, list->collection.size, arl->capacity + }; + if (cx_array_insert_(list->collection.allocator, &wrap, + list->collection.elem_size, index, array, n)) { + return 0; } + arl->data = wrap.data; + arl->capacity = wrap.capacity; + list->collection.size = wrap.size; + return n; +} - // determine insert position - char *arl_data = arl->data; - char *insert_pos = arl_data + index * list->collection.elem_size; +static size_t cx_arl_insert_sorted_impl( + struct cx_list_s *list, + const void *sorted_data, + size_t n, + bool allow_duplicates +) { + cx_array_list *arl = (cx_array_list *) list; + CxArray wrap = { + arl->data, list->collection.size, arl->capacity + }; - // do we need to move some elements? - if (index < list->collection.size) { - size_t elems_to_move = list->collection.size - index; - char *target = insert_pos + n * list->collection.elem_size; - memmove(target, insert_pos, elems_to_move * list->collection.elem_size); + if (cx_array_insert_sorted_c_( + list->collection.allocator, + &wrap, + list->collection.elem_size, + sorted_data, + n, + cx_list_compare_wrapper, + list, + allow_duplicates + )) { + // array list implementation is "all or nothing" + return 0; // LCOV_EXCL_LINE } - - // place the new elements, if any - if (array != NULL) { - memcpy(insert_pos, array, n * list->collection.elem_size); - } - list->collection.size += n; - + arl->data = wrap.data; + arl->capacity = wrap.capacity; + list->collection.size = wrap.size; return n; } @@ -826,24 +732,7 @@ const void *sorted_data, size_t n ) { - // get a correctly typed pointer to the list - cx_array_list *arl = (cx_array_list *) list; - - if (cx_array_insert_sorted( - &arl->data, - &list->collection.size, - &arl->capacity, - list->collection.cmpfunc, - sorted_data, - list->collection.elem_size, - n, - &arl->reallocator - )) { - // array list implementation is "all or nothing" - return 0; - } else { - return n; - } + return cx_arl_insert_sorted_impl(list, sorted_data, n, true); } static size_t cx_arl_insert_unique( @@ -851,24 +740,7 @@ const void *sorted_data, size_t n ) { - // get a correctly typed pointer to the list - cx_array_list *arl = (cx_array_list *) list; - - if (cx_array_insert_unique( - &arl->data, - &list->collection.size, - &arl->capacity, - list->collection.cmpfunc, - sorted_data, - list->collection.elem_size, - n, - &arl->reallocator - )) { - // array list implementation is "all or nothing" - return 0; - } else { - return n; - } + return cx_arl_insert_sorted_impl(list, sorted_data, n, false); } static void *cx_arl_insert_element( @@ -892,7 +764,7 @@ if (iter->index < list->collection.size) { if (cx_arl_insert_element(list, iter->index + 1 - prepend, elem) == NULL) { - return 1; + return 1; // LCOV_EXCL_LINE } iter->elem_count++; if (prepend != 0) { @@ -902,7 +774,7 @@ return 0; } else { if (cx_arl_insert_element(list, list->collection.size, elem) == NULL) { - return 1; + return 1; // LCOV_EXCL_LINE } iter->elem_count++; iter->index = list->collection.size; @@ -947,24 +819,20 @@ ); } + // calculate how many elements would need to be moved + size_t remaining = list->collection.size - index - remove; + // short-circuit removal of last elements - if (index + remove == list->collection.size) { + if (remaining == 0) { list->collection.size -= remove; return remove; } // just move the elements to the left - cx_array_copy( - &arl->data, - &list->collection.size, - &arl->capacity, - 0, - index, - ((char *) arl->data) + (index + remove) * list->collection.elem_size, - list->collection.elem_size, - list->collection.size - index - remove, - &arl->reallocator - ); + char *dst_move = arl->data; + dst_move += index * list->collection.elem_size; + char *first_remaining = dst_move + remove * list->collection.elem_size; + memmove(dst_move, first_remaining, remaining * list->collection.elem_size); // decrease the size list->collection.size -= remove; @@ -1025,18 +893,18 @@ bool remove ) { assert(list != NULL); - assert(list->collection.cmpfunc != NULL); if (list->collection.size == 0) return 0; char *cur = ((const cx_array_list *) list)->data; // optimize with binary search, when sorted if (list->collection.sorted) { - size_t i = cx_array_binary_search( + size_t i = cx_array_binary_search_c( cur, list->collection.size, list->collection.elem_size, elem, - list->collection.cmpfunc + cx_list_compare_wrapper, + list ); if (remove && i < list->collection.size) { cx_arl_remove(list, i, 1, NULL); @@ -1046,7 +914,7 @@ // fallback: linear search for (size_t i = 0; i < list->collection.size; i++) { - if (0 == list->collection.cmpfunc(elem, cur)) { + if (0 == cx_list_compare_wrapper(elem, cur, list)) { if (remove) { cx_arl_remove(list, i, 1, NULL); } @@ -1058,11 +926,11 @@ } static void cx_arl_sort(struct cx_list_s *list) { - assert(list->collection.cmpfunc != NULL); - qsort(((cx_array_list *) list)->data, + cx_array_qsort_c(((cx_array_list *) list)->data, list->collection.size, list->collection.elem_size, - list->collection.cmpfunc + cx_list_compare_wrapper, + list ); } @@ -1070,12 +938,11 @@ const struct cx_list_s *list, const struct cx_list_s *other ) { - assert(list->collection.cmpfunc != NULL); if (list->collection.size == other->collection.size) { const char *left = ((const cx_array_list *) list)->data; const char *right = ((const cx_array_list *) other)->data; for (size_t i = 0; i < list->collection.size; i++) { - int d = list->collection.cmpfunc(left, right); + int d = cx_list_compare_wrapper(left, right, (void*)list); if (d != 0) { return d; } @@ -1137,6 +1004,14 @@ } } +static int cx_arl_change_capacity( + struct cx_list_s *list, + size_t new_capacity +) { + cx_array_list *arl = (cx_array_list *)list; + return cxReallocateArray(list->collection.allocator, + &arl->data, new_capacity, list->collection.elem_size); +} static struct cx_iterator_s cx_arl_iterator( const struct cx_list_s *list, @@ -1174,12 +1049,12 @@ cx_arl_sort, cx_arl_compare, cx_arl_reverse, + cx_arl_change_capacity, cx_arl_iterator, }; CxList *cxArrayListCreate( const CxAllocator *allocator, - cx_compare_func comparator, size_t elem_size, size_t initial_capacity ) { @@ -1190,7 +1065,7 @@ cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list)); if (list == NULL) return NULL; cx_list_init((CxList*)list, &cx_array_list_class, - allocator, comparator, elem_size); + allocator, elem_size); list->capacity = initial_capacity; // allocate the array after the real elem_size is known @@ -1201,8 +1076,5 @@ return NULL; } // LCOV_EXCL_STOP - // configure the reallocator - list->reallocator = cx_array_reallocator(allocator, NULL); - return (CxList *) list; }
--- a/ucx/buffer.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/buffer.c Fri Dec 19 17:53:18 2025 +0100 @@ -32,28 +32,10 @@ #include <string.h> #include <errno.h> -#ifdef _WIN32 -#include <Windows.h> -#include <sysinfoapi.h> -static unsigned long system_page_size() { - static unsigned long ps = 0; - if (ps == 0) { - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); - ps = sysinfo.dwPageSize; - } - return ps; -} -#define SYSTEM_PAGE_SIZE system_page_size() -#else -#include <unistd.h> -#define SYSTEM_PAGE_SIZE sysconf(_SC_PAGESIZE) -#endif - static int buffer_copy_on_write(CxBuffer* buffer) { if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0; void *newspace = cxMalloc(buffer->allocator, buffer->capacity); - if (NULL == newspace) return -1; + if (NULL == newspace) return -1; // LCOV_EXCL_LINE memcpy(newspace, buffer->space, buffer->size); buffer->space = newspace; buffer->flags &= ~CX_BUFFER_COPY_ON_WRITE; @@ -63,11 +45,11 @@ int cxBufferInit( CxBuffer *buffer, + const CxAllocator *allocator, void *space, size_t capacity, - const CxAllocator *allocator, int flags -) { + ) { if (allocator == NULL) { allocator = cxDefaultAllocator; } @@ -78,52 +60,39 @@ buffer->flags = flags; if (!space) { buffer->bytes = cxMalloc(allocator, capacity); - if (buffer->bytes == NULL) { - return -1; // LCOV_EXCL_LINE - } + if (buffer->bytes == NULL) return -1; // LCOV_EXCL_LINE buffer->flags |= CX_BUFFER_FREE_CONTENTS; } else { buffer->bytes = space; } buffer->capacity = capacity; + buffer->max_capacity = SIZE_MAX; buffer->size = 0; buffer->pos = 0; - buffer->flush = NULL; - - return 0; -} - -int cxBufferEnableFlushing( - CxBuffer *buffer, - CxBufferFlushConfig config -) { - buffer->flush = cxMallocDefault(sizeof(CxBufferFlushConfig)); - if (buffer->flush == NULL) return -1; // LCOV_EXCL_LINE - memcpy(buffer->flush, &config, sizeof(CxBufferFlushConfig)); return 0; } void cxBufferDestroy(CxBuffer *buffer) { - if (buffer->flags & CX_BUFFER_FREE_CONTENTS) { + if ((buffer->flags & (CX_BUFFER_FREE_CONTENTS | CX_BUFFER_DO_NOT_FREE)) + == CX_BUFFER_FREE_CONTENTS) { cxFree(buffer->allocator, buffer->bytes); } - cxFreeDefault(buffer->flush); memset(buffer, 0, sizeof(CxBuffer)); } CxBuffer *cxBufferCreate( + const CxAllocator *allocator, void *space, size_t capacity, - const CxAllocator *allocator, int flags -) { + ) { if (allocator == NULL) { allocator = cxDefaultAllocator; } CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer)); - if (buf == NULL) return NULL; - if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) { + if (buf == NULL) return NULL; // LCOV_EXCL_LINE + if (0 == cxBufferInit(buf, allocator, space, capacity, flags)) { return buf; } else { // LCOV_EXCL_START @@ -183,6 +152,35 @@ } +size_t cxBufferPop(CxBuffer *buffer, size_t size, size_t nitems) { + size_t len; + if (cx_szmul(size, nitems, &len)) { + // LCOV_EXCL_START + errno = EOVERFLOW; + return 0; + // LCOV_EXCL_STOP + } + if (len == 0) return 0; + if (len > buffer->size) { + if (size == 1) { + // simple case: everything can be discarded + len = buffer->size; + } else { + // complicated case: misaligned bytes must stay + size_t misalignment = buffer->size % size; + len = buffer->size - misalignment; + } + } + buffer->size -= len; + + // adjust position, if required + if (buffer->pos > buffer->size) { + buffer->pos = buffer->size; + } + + return len / size; +} + void cxBufferClear(CxBuffer *buffer) { if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) { memset(buffer->bytes, 0, buffer->size); @@ -200,36 +198,13 @@ return buffer->pos >= buffer->size; } -int cxBufferMinimumCapacity( - CxBuffer *buffer, - size_t newcap -) { - if (newcap <= buffer->capacity) { +int cxBufferReserve(CxBuffer *buffer, size_t newcap) { + if (newcap == buffer->capacity) { return 0; } - - unsigned long pagesize = SYSTEM_PAGE_SIZE; - // if page size is larger than 64 KB - for some reason - truncate to 64 KB - if (pagesize > 65536) pagesize = 65536; - if (newcap < pagesize) { - // when smaller as one page, map to the next power of two - newcap--; - newcap |= newcap >> 1; - newcap |= newcap >> 2; - newcap |= newcap >> 4; - // last operation only needed for pages larger 4096 bytes - // but if/else would be more expensive than just doing this - newcap |= newcap >> 8; - newcap++; - } else { - // otherwise, map to a multiple of the page size - newcap -= newcap % pagesize; - newcap += pagesize; - // note: if newcap is already page aligned, - // this gives a full additional page (which is good) + if (newcap > buffer->max_capacity) { + return -1; } - - const int force_copy_flags = CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_COPY_ON_EXTEND; if (buffer->flags & force_copy_flags) { void *newspace = cxMalloc(buffer->allocator, newcap); @@ -242,13 +217,60 @@ return 0; } else if (cxReallocate(buffer->allocator, (void **) &buffer->bytes, newcap) == 0) { + buffer->flags |= CX_BUFFER_FREE_CONTENTS; buffer->capacity = newcap; + if (buffer->size > newcap) { + buffer->size = newcap; + } return 0; } else { return -1; // LCOV_EXCL_LINE } } +int cxBufferMaximumCapacity(CxBuffer *buffer, size_t capacity) { + if (capacity < buffer->capacity) { + return -1; + } + buffer->max_capacity = capacity; + return 0; +} + +int cxBufferMinimumCapacity(CxBuffer *buffer, size_t newcap) { + if (newcap <= buffer->capacity) { + return 0; + } + if (newcap > buffer->max_capacity) { + return -1; + } + if (newcap < buffer->max_capacity) { + unsigned long pagesize = cx_system_page_size(); + // if page size is larger than 64 KB - for some reason - truncate to 64 KB + if (pagesize > 65536) pagesize = 65536; + if (newcap < pagesize) { + // when smaller as one page, map to the next power of two + newcap--; + newcap |= newcap >> 1; + newcap |= newcap >> 2; + newcap |= newcap >> 4; + // last operation only needed for pages larger 4096 bytes + // but if/else would be more expensive than just doing this + newcap |= newcap >> 8; + newcap++; + } else { + // otherwise, map to a multiple of the page size + newcap -= newcap % pagesize; + newcap += pagesize; + // note: if newcap is already page aligned, + // this gives a full additional page (which is good) + } + if (newcap > buffer->max_capacity) { + newcap = buffer->max_capacity; + } + } + return cxBufferReserve(buffer, newcap); +} + void cxBufferShrink( CxBuffer *buffer, size_t reserve @@ -271,60 +293,15 @@ } } -static size_t cx_buffer_flush_helper( - const CxBuffer *buffer, - const unsigned char *src, - size_t size, - size_t nitems -) { - // flush data from an arbitrary source - // does not need to be the buffer's contents - size_t max_items = buffer->flush->blksize / size; - size_t fblocks = 0; - size_t flushed_total = 0; - while (nitems > 0 && fblocks < buffer->flush->blkmax) { - fblocks++; - size_t items = nitems > max_items ? max_items : nitems; - size_t flushed = buffer->flush->wfunc( - src, size, items, buffer->flush->target); - if (flushed > 0) { - flushed_total += flushed; - src += flushed * size; - nitems -= flushed; - } else { - // if no bytes can be flushed out anymore, we give up - break; - } - } - return flushed_total; -} - -static size_t cx_buffer_flush_impl(CxBuffer *buffer, size_t size) { - // flush the current contents of the buffer - unsigned char *space = buffer->bytes; - size_t remaining = buffer->pos / size; - size_t flushed_total = cx_buffer_flush_helper( - buffer, space, size, remaining); - - // shift the buffer left after flushing - // IMPORTANT: up to this point, copy on write must have been - // performed already, because we can't do error handling here - cxBufferShiftLeft(buffer, flushed_total*size); - - return flushed_total; -} - -size_t cxBufferFlush(CxBuffer *buffer) { - if (buffer_copy_on_write(buffer)) return 0; - return cx_buffer_flush_impl(buffer, 1); -} - size_t cxBufferWrite( const void *ptr, size_t size, size_t nitems, CxBuffer *buffer ) { + // trivial case + if (size == 0 || nitems == 0) return 0; + // optimize for easy case if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) { if (buffer_copy_on_write(buffer)) return 0; @@ -336,98 +313,52 @@ return nitems; } - size_t len, total_flushed = 0; -cx_buffer_write_retry: + size_t len; if (cx_szmul(size, nitems, &len)) { errno = EOVERFLOW; - return total_flushed; + return 0; } if (buffer->pos > SIZE_MAX - len) { errno = EOVERFLOW; - return total_flushed; + return 0; } + const size_t required = buffer->pos + len; - size_t required = buffer->pos + len; - bool perform_flush = false; + // check if we need to auto-extend if (required > buffer->capacity) { if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { - if (buffer->flush != NULL && required > buffer->flush->threshold) { - perform_flush = true; - } else { - if (cxBufferMinimumCapacity(buffer, required)) { - return total_flushed; // LCOV_EXCL_LINE - } - } - } else { - if (buffer->flush != NULL) { - perform_flush = true; - } else { - // truncate data, if we can neither extend nor flush - len = buffer->capacity - buffer->pos; - if (size > 1) { - len -= len % size; - } - nitems = len / size; + size_t newcap = required < buffer->max_capacity + ? required : buffer->max_capacity; + if (cxBufferMinimumCapacity(buffer, newcap)) { + return 0; // LCOV_EXCL_LINE } } } + // check again and truncate data if capacity is still not enough + if (required > buffer->capacity) { + len = buffer->capacity - buffer->pos; + if (size > 1) { + len -= len % size; + } + nitems = len / size; + } + // check here and not above because of possible truncation if (len == 0) { - return total_flushed; + return 0; } // check if we need to copy if (buffer_copy_on_write(buffer)) return 0; // perform the operation - if (perform_flush) { - size_t items_flushed; - if (buffer->pos == 0) { - // if we don't have data in the buffer, but are instructed - // to flush, it means that we are supposed to relay the data - items_flushed = cx_buffer_flush_helper(buffer, ptr, size, nitems); - if (items_flushed == 0) { - // we needed to relay data, but could not flush anything - // i.e. we have to give up to avoid endless trying - return 0; - } - nitems -= items_flushed; - total_flushed += items_flushed; - if (nitems > 0) { - ptr = ((unsigned char*)ptr) + items_flushed * size; - goto cx_buffer_write_retry; - } - return total_flushed; - } else { - items_flushed = cx_buffer_flush_impl(buffer, size); - if (items_flushed == 0) { - // flush target is full, let's try to truncate - size_t remaining_space; - if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { - remaining_space = buffer->flush->threshold > buffer->pos - ? buffer->flush->threshold - buffer->pos - : 0; - } else { - remaining_space = buffer->capacity > buffer->pos - ? buffer->capacity - buffer->pos - : 0; - } - nitems = remaining_space / size; - if (nitems == 0) { - return total_flushed; - } - } - goto cx_buffer_write_retry; - } - } else { - memcpy(buffer->bytes + buffer->pos, ptr, len); - buffer->pos += len; - if (buffer->pos > buffer->size) { - buffer->size = buffer->pos; - } - return total_flushed + nitems; + memcpy(buffer->bytes + buffer->pos, ptr, len); + buffer->pos += len; + if (buffer->pos > buffer->size) { + buffer->size = buffer->pos; } + return nitems; } size_t cxBufferAppend( @@ -436,20 +367,13 @@ size_t nitems, CxBuffer *buffer ) { - size_t pos = buffer->pos; - size_t append_pos = buffer->size; - buffer->pos = append_pos; - size_t written = cxBufferWrite(ptr, size, nitems, buffer); - // the buffer might have been flushed - // we must compute a possible delta for the position - // expected: pos = append_pos + written - // -> if this is not the case, there is a delta - size_t delta = append_pos + written*size - buffer->pos; - if (delta > pos) { - buffer->pos = 0; - } else { - buffer->pos = pos - delta; - } + // trivial case + if (size == 0 || nitems == 0) return 0; + + const size_t pos = buffer->pos; + buffer->pos = buffer->size; + const size_t written = cxBufferWrite(ptr, size, nitems, buffer); + buffer->pos = pos; return written; } @@ -467,19 +391,35 @@ } int cxBufferTerminate(CxBuffer *buffer) { - if (0 == cxBufferPut(buffer, 0)) { - buffer->size = buffer->pos - 1; - return 0; + // try to extend / shrink the buffer + if (buffer->pos >= buffer->capacity) { + if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == 0) { + return -1; + } + if (cxBufferReserve(buffer, buffer->pos + 1)) { + return -1; // LCOV_EXCL_LINE + } } else { - return -1; + buffer->size = buffer->pos; + cxBufferShrink(buffer, 1); + // set the capacity explicitly, in case shrink was skipped due to CoW + buffer->capacity = buffer->size + 1; } + + // check if we are still on read-only memory + if (buffer_copy_on_write(buffer)) return -1; + + // write the terminator and exit + buffer->space[buffer->pos] = '\0'; + return 0; } -size_t cxBufferPutString( - CxBuffer *buffer, - const char *str -) { - return cxBufferWrite(str, 1, strlen(str), buffer); +size_t cx_buffer_put_string(CxBuffer *buffer, cxstring str) { + return cxBufferWrite(str.ptr, 1, str.length, buffer); +} + +size_t cx_buffer_append_string(CxBuffer *buffer, cxstring str) { + return cxBufferAppend(str.ptr, 1, str.length, buffer); } size_t cxBufferRead(
--- a/ucx/compare.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/compare.c Fri Dec 19 17:53:18 2025 +0100 @@ -29,6 +29,7 @@ #include "cx/compare.h" #include <math.h> +#include <string.h> int cx_vcmp_int(int a, int b) { if (a == b) { @@ -289,3 +290,21 @@ return p1 < p2 ? -1 : 1; } } + +int cx_ccmp_memcmp( + const void *ptr1, + const void *ptr2, + void *size +) { + size_t n = *(size_t*)size; + return memcmp(ptr1, ptr2, n); +} + +int cx_ccmp_wrap( + const void *ptr1, + const void *ptr2, + void *w +) { + cx_compare_func_wrapper *wrapper = w; + return wrapper->cmp(ptr1, ptr2); +}
--- a/ucx/cx/allocator.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/allocator.h Fri Dec 19 17:53:18 2025 +0100 @@ -142,17 +142,24 @@ * @return either the specified @p target, a pointer to the allocated memory, * or @c NULL, if any error occurred */ -typedef void*(cx_clone_func)(void *target, const void *source, +typedef void*(*cx_clone_func)(void *target, const void *source, const CxAllocator *allocator, void *data); /** - * Reallocate a previously allocated block and changes the pointer in-place, - * if necessary. + * Returns the system's memory page size. + * + * If the page size cannot be retrieved from the system, + * a default of 4096 bytes is assumed. * - * @note This will use stdlib reallocate and @em not the cxDefaultAllocator. + * @return the system's memory page size in bytes + */ +cx_attr_nodiscard +CX_EXPORT unsigned long cx_system_page_size(void); + +/** + * Reallocate a previously allocated block. * - * @par Error handling - * @c errno will be set by realloc() on failure. + * Internal function - do not use. * * @param mem pointer to the pointer to allocated block * @param n the new size in bytes @@ -164,16 +171,9 @@ CX_EXPORT int cx_reallocate_(void **mem, size_t n); /** - * Reallocate a previously allocated block and changes the pointer in-place, - * if necessary. - * - * The size is calculated by multiplying @p nemb and @p size. + * Reallocate a previously allocated block. * - * @note This will use stdlib reallocate and @em not the cxDefaultAllocator. - * - * @par Error handling - * @c errno will be set by realloc() on failure or when the multiplication of - * @p nmemb and @p size overflows. + * Internal function - do not use. * * @param mem pointer to the pointer to allocated block * @param nmemb the number of elements @@ -261,6 +261,8 @@ * * @note Re-allocating a block allocated by a different allocator is undefined. * + * @attention This function is bug-prone. Consider using cxReallocate(). + * * @param allocator the allocator * @param mem pointer to the previously allocated block * @param n the new size in bytes @@ -271,8 +273,8 @@ CX_EXPORT void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n); /** - * Reallocate the previously allocated block in @p mem, making the new block - * @p n bytes long. + * Reallocate the previously allocated block in @p mem. + * * This function may return the same pointer passed to it if moving * the memory was not necessary. * @@ -282,6 +284,8 @@ * * @note Re-allocating a block allocated by a different allocator is undefined. * + * @attention This function is bug-prone. Consider using cxReallocateArray(). + * * @param allocator the allocator * @param mem pointer to the previously allocated block * @param nmemb the number of elements @@ -294,14 +298,9 @@ void *mem, size_t nmemb, size_t size); /** - * Reallocate 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. + * Reallocate a previously allocated block. * - * @note Re-allocating a block allocated by a different allocator is undefined. - * - * @par Error handling - * @c errno will be set if the underlying realloc function does so. + * Internal function - do not use. * * @param allocator the allocator * @param mem pointer to the pointer to allocated block @@ -332,16 +331,9 @@ cxReallocate_(allocator, (void**)(mem), n) /** - * Reallocate a previously allocated block and changes the pointer in-place, - * if necessary. - * This function acts like cxReallocArray() using the pointer pointed to - * by @p mem. + * Reallocate a previously allocated block. * - * @note Re-allocating a block allocated by a different allocator is undefined. - * - * @par Error handling - * @c errno will be set, if the underlying realloc function does so or the - * multiplication of @p nmemb and @p size overflows. + * Internal function - do not use. * * @param allocator the allocator * @param mem pointer to the pointer to allocated block @@ -377,7 +369,7 @@ cxReallocateArray_(allocator, (void**) (mem), nmemb, size) /** - * Allocate @p nmemb elements of @p n bytes each, all initialized to zero. + * Allocate @p nmemb elements of @p size bytes each, all initialized to zero. * * @param allocator the allocator * @param nmemb the number of elements @@ -400,37 +392,125 @@ CX_EXPORT void *cxZalloc(const CxAllocator *allocator, size_t n); /** + * Allocate @p n bytes of memory. + * * Convenience macro that invokes cxMalloc() with the cxDefaultAllocator. + * + * @param n (@c size_t) the number of bytes + * @return (@c void*) a pointer to the allocated memory */ -#define cxMallocDefault(...) cxMalloc(cxDefaultAllocator, __VA_ARGS__) +#define cxMallocDefault(n) cxMalloc(cxDefaultAllocator, n) + /** + * Allocate @p n bytes of memory and sets every byte to zero. + * * Convenience macro that invokes cxZalloc() with the cxDefaultAllocator. + * + * @param n (@c size_t) the number of bytes + * @return (@c void*) a pointer to the allocated memory */ -#define cxZallocDefault(...) cxZalloc(cxDefaultAllocator, __VA_ARGS__) +#define cxZallocDefault(n) cxZalloc(cxDefaultAllocator, n) + /** + * Allocate @p nmemb elements of @p size bytes each, all initialized to zero. + * * Convenience macro that invokes cxCalloc() with the cxDefaultAllocator. + * + * @param nmemb (@c size_t) the number of elements + * @param size (@c size_t) the size of each element in bytes + * @return (@c void*) a pointer to the allocated memory */ -#define cxCallocDefault(...) cxCalloc(cxDefaultAllocator, __VA_ARGS__) +#define cxCallocDefault(nmemb, size) cxCalloc(cxDefaultAllocator, nmemb, size) + /** + * Reallocate the previously allocated block in @p mem. + * + * This function may return the same pointer passed to it if moving + * the memory was not necessary. + * * Convenience macro that invokes cxRealloc() with the cxDefaultAllocator. + * + * @attention This function is bug-prone. Consider using cxReallocateDefault(). + * + * @param mem (@c void*) pointer to the previously allocated block + * @param n (@c size_t) the new size in bytes + * @return (@c void*) a pointer to the reallocated memory */ -#define cxReallocDefault(...) cxRealloc(cxDefaultAllocator, __VA_ARGS__) +#define cxReallocDefault(mem, n) cxRealloc(cxDefaultAllocator, mem, n) + /** + * Reallocate 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. + * * Convenience macro that invokes cxReallocate() with the cxDefaultAllocator. + * + * @note Re-allocating a block allocated by a different allocator is undefined. + * + * @par Error handling + * @c errno will be set if the underlying realloc function does so. + * + * @param mem (@c void**) pointer to the pointer to allocated block + * @param n (@c size_t) the new size in bytes + * @retval zero success + * @retval non-zero failure */ -#define cxReallocateDefault(...) cxReallocate(cxDefaultAllocator, __VA_ARGS__) -/** - * Convenience macro that invokes cxReallocateArray() with the cxDefaultAllocator. - */ -#define cxReallocateArrayDefault(...) cxReallocateArray(cxDefaultAllocator, __VA_ARGS__) +#define cxReallocateDefault(mem, n) cxReallocate(cxDefaultAllocator, mem, n) + /** - * Convenience macro that invokes cxReallocArray() with the cxDefaultAllocator. + * Reallocate a previously allocated block and changes the pointer in-place, + * if necessary. + * This function acts like cxReallocArray() using the pointer pointed to + * by @p mem. + * + * Convenience macro that invokes cxReallocateArray() with the cxDefaultAllocator. + * + * @note Re-allocating a block allocated by a different allocator is undefined. + * + * @par Error handling + * @c errno will be set, if the underlying realloc function does so or the + * multiplication of @p nmemb and @p size overflows. + * + * @param mem (@c void**) pointer to the pointer to allocated block + * @param nmemb (@c size_t) the number of elements + * @param size (@c size_t) the size of each element + * @retval zero success + * @retval non-zero failure */ -#define cxReallocArrayDefault(...) cxReallocArray(cxDefaultAllocator, __VA_ARGS__) +#define cxReallocateArrayDefault(mem, nmemb, size) \ + cxReallocateArray(cxDefaultAllocator, mem, nmemb, size) + /** - * Convenience macro that invokes cxFree() with the cxDefaultAllocator. + * Reallocate the previously allocated block in @p mem. + * + * Convenience macro that invokes cxReallocArray() with the cxDefaultAllocator. + * + * This function may return the same pointer passed to it if moving + * the memory was not necessary. + * + * The size is calculated by multiplying @p nemb and @p size. + * If that multiplication overflows, this function returns @c NULL, and @c errno + * will be set. + * + * @note Re-allocating a block allocated by a different allocator is undefined. + * + * @attention This function is bug-prone. Consider using cxReallocateArrayDefault(). + * + * @param mem (@c void*) pointer to the previously allocated block + * @param nmemb (@c size_t) the number of elements + * @param size (@c size_t) the size of each element + * @return (@c void*) a pointer to the reallocated memory */ -#define cxFreeDefault(...) cxFree(cxDefaultAllocator, __VA_ARGS__) +#define cxReallocArrayDefault(mem, nmemb, size) cxReallocArray(cxDefaultAllocator, mem, nmemb, size) + +/** + * Free a block of memory. + * + * Convenience function that invokes cxFree() with the cxDefaultAllocator. + * + * @param mem the memory to deallocate + */ +CX_EXPORT void cxFreeDefault(void *mem); #ifdef __cplusplus } // extern "C"
--- a/ucx/cx/array_list.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/array_list.h Fri Dec 19 17:53:18 2025 +0100 @@ -50,617 +50,892 @@ CX_EXPORT extern const unsigned cx_array_swap_sbo_size; /** - * Declares variables for an array that can be used with the convenience macros. - * - * @par Examples - * @code - * // integer array with at most 255 elements - * CX_ARRAY_DECLARE_SIZED(int, myarray, uint8_t) + * Declares a typed array with size and capacity. * - * // array of MyObject* pointers where size and capacity are stored as unsigned int - * CX_ARRAY_DECLARE_SIZED(MyObject*, objects, unsigned int) - * - * // initializing code - * cx_array_initialize(myarray, 16); // reserve space for 16 - * cx_array_initialize(objects, 100); // reserve space for 100 - * @endcode + * @param type the type of the elements + * @param name the name of the array + */ +#define CX_ARRAY(type, name) \ + struct { \ + type *data; \ + size_t size; \ + size_t capacity; \ + } name + +/** + * Internal structure for arrays. * - * @param type the type of the data - * @param name the name of the array - * @param size_type the type of the size (should be uint8_t, uint16_t, uint32_t, or size_t) + * A generalization of array structures declared with CX_ARRAY(). + */ +typedef struct cx_array_s { + /** The array data. */ + void *data; + /** The number of elements. */ + size_t size; + /** The maximum number of elements. */ + size_t capacity; +} CxArray; + +/** + * Initializes an array by allocating memory. * - * @see cx_array_initialize() - * @see cx_array_simple_add() - * @see cx_array_simple_copy() - * @see cx_array_simple_add_sorted() - * @see cx_array_simple_insert_sorted() + * Internal function - do not use manually. + * + * @param allocator the allocator for the array + * @param array a pointer to the array structure + * @param elem_size size of one element + * @param capacity the initial maximum number of elements + * @retval zero allocation was successful + * @retval non-zero allocation failed */ -#define CX_ARRAY_DECLARE_SIZED(type, name, size_type) \ - type * name; \ - /** Array size. */ size_type name##_size; \ - /** Array capacity. */ size_type name##_capacity +cx_attr_nonnull +CX_EXPORT int cx_array_init_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity); /** - * Declares variables for an array that can be used with the convenience macros. - * - * The size and capacity variables will have type @c size_t. - * Use #CX_ARRAY_DECLARE_SIZED() to specify a different type. + * Initializes an array by allocating memory. * - * @par Examples - * @code - * // int array - * CX_ARRAY_DECLARE(int, myarray) + * The size is set to zero. + * + * @attention If the array was already initialized, this will leak memory. + * Use cx_array_reserve() to change the capacity of an initialized array. * - * // initializing code - * cx_array_initialize(myarray, 32); // reserve space for 32 - * @endcode - * - * @param type the type of the data - * @param name the name of the array - * - * @see cx_array_initialize() - * @see cx_array_simple_add() - * @see cx_array_simple_copy() - * @see cx_array_simple_add_sorted() - * @see cx_array_simple_insert_sorted() + * @param allocator (@c CxAllocator*) the allocator for the array + * @param array the name of the array + * @param capacity (@c size_t) the initial maximum number of elements + * @retval zero allocation was successful + * @retval non-zero allocation failed */ -#define CX_ARRAY_DECLARE(type, name) CX_ARRAY_DECLARE_SIZED(type, name, size_t) +#define cx_array_init_a(allocator, array, capacity) cx_array_init_(allocator, (CxArray*)&(array), sizeof((array).data[0]), capacity) /** - * Initializes an array with the given capacity. - * - * The type of the capacity depends on the type used during declaration. + * Initializes an array by allocating memory. * - * @par Examples - * @code - * CX_ARRAY_DECLARE_SIZED(int, arr1, uint8_t) - * CX_ARRAY_DECLARE(int, arr2) // size and capacity are implicitly size_t + * The size is set to zero. * - * // initializing code - * cx_array_initialize(arr1, 500); // error: maximum for uint8_t is 255 - * cx_array_initialize(arr2, 500); // OK - * @endcode - * - * - * The memory for the array is allocated with the cxDefaultAllocator. + * @attention If the array was already initialized, this will leak memory. * * @param array the name of the array - * @param capacity the initial capacity - * @see cx_array_initialize_a() - * @see CX_ARRAY_DECLARE_SIZED() - * @see CX_ARRAY_DECLARE() + * @param capacity (@c size_t) the initial maximum number of elements + * @retval zero allocation was successful + * @retval non-zero allocation failed */ -#define cx_array_initialize(array, capacity) \ - array##_capacity = capacity; \ - array##_size = 0; \ - array = cxMallocDefault(sizeof(array[0]) * capacity) +#define cx_array_init(array, capacity) cx_array_init_a(cxDefaultAllocator, array, capacity) + +/** + * Initializes an array with fixed size memory. + * + * Internal function - do not use manually. + * + * @param array a pointer to the array structure + * @param data the fixed size array + * @param capacity the capacity of the fixed size array + * @param size the number of initialized elements in the fixed size array + */ +cx_attr_nonnull +CX_EXPORT void cx_array_init_fixed_(CxArray *array, const void *data, size_t capacity, size_t size); /** - * Initializes an array with the given capacity using the specified allocator. + * Initializes an array with fixed size memory. + * + * This is useful, for example, when you want to work with memory on the stack + * and only want to move to the heap when the stack memory is not enough. + * + * With the @p num_initialized argument you can specify how many elements in the + * fixed size array are already correctly initialized, which determines the + * initial size of the array. * - * @par Example - * @code - * CX_ARRAY_DECLARE(int, myarray) + * The capacity is determined automatically by the compiler. * + * @attention When you add elements to an array that was initialized with fixed + * size memory, you MUST check the capacity before adding the element and invoke + * cx_array_copy_to_new() when you intend to exceed the capacity. + * + * @attention When you pass a pointer to an array that does not have a fixed + * size, the behavior is unspecified. * - * const CxAllocator *al = // ... - * cx_array_initialize_a(al, myarray, 128); - * // ... - * cxFree(al, myarray); // remember to free with the same allocator - * @endcode + * @param array the name of the array to initialize + * @param fixed_size_array (@c void*) the fixed size array + * @param num_initialized (@c size_t) the number of already initialized elements in the fixed size array + * @see cx_array_copy_to_new() + */ +#define cx_array_init_fixed(array, fixed_size_array, num_initialized) \ + cx_array_init_fixed_((CxArray*)&(array), fixed_size_array, cx_nmemb(fixed_size_array), num_initialized) + +/** + * Changes the capacity of an array. * - * @param allocator (@c CxAllocator*) the allocator - * @param array the name of the array - * @param capacity the initial capacity - * @see cx_array_initialize() - * @see CX_ARRAY_DECLARE_SIZED() - * @see CX_ARRAY_DECLARE() + * Internal function - do not use. + * + * @param allocator the allocator + * @param array a pointer to the array structure + * @param elem_size the size of one element + * @param capacity the new capacity + * @retval zero allocation was successful + * @retval non-zero allocation failed */ -#define cx_array_initialize_a(allocator, array, capacity) \ - array##_capacity = capacity; \ - array##_size = 0; \ - array = cxMalloc(allocator, sizeof(array[0]) * capacity) +cx_attr_nonnull +CX_EXPORT int cx_array_reserve_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity); /** - * Defines a reallocation mechanism for arrays. - * You can create your own, use cx_array_reallocator(), or - * use the #cx_array_default_reallocator. + * Changes the capacity of an array. + * + * If required, the size is reduced to fit into the new capacity. + * + * @param allocator (@c CxAllocator*) the allocator for the array + * @param array the name of the array + * @param capacity (@c size_t) the new maximum number of elements + * @retval zero allocation was successful + * @retval non-zero allocation failed */ -struct cx_array_reallocator_s { - /** - * Reallocates space for the given array. - * - * Implementations are not required to free the original array. - * This allows reallocation of static or stack memory by allocating heap memory - * and copying the array contents; namely when @c stack_ptr in this struct - * is not @c NULL and @p array equals @c stack_ptr. - * - * @param array the array to reallocate - * @param old_capacity the old number of elements - * @param new_capacity the new number of elements - * @param elem_size the size of each element - * @param alloc a reference to this allocator - * @return a pointer to the reallocated memory or @c NULL on failure - */ - void *(*realloc)( void *array, size_t old_capacity, size_t new_capacity, - size_t elem_size, struct cx_array_reallocator_s *alloc); +#define cx_array_reserve_a(allocator, array, capacity) \ + cx_array_reserve_(allocator, (CxArray*)&(array), sizeof((array).data[0]), capacity) - /** - * The allocator that shall be used for the reallocations. - */ - const CxAllocator *allocator; - /** - * Optional pointer to stack memory - * if the array is originally located on the stack. - */ - const void *stack_ptr; -}; +/** + * Changes the capacity of an array. + * + * If required, the size is reduced to fit into the new capacity. + * + * @param array the name of the array + * @param capacity (@c size_t) the new maximum number of elements + * @retval zero allocation was successful + * @retval non-zero allocation failed + */ +#define cx_array_reserve(array, capacity) \ + cx_array_reserve_a(cxDefaultAllocator, array, capacity) /** - * Typedef for the array reallocator struct. + * Copies the array to a new memory region. + * + * Internal function - do not use. + * + * @param allocator the allocator for new new memory + * @param array a pointer to the array structure + * @param elem_size the size of one element + * @param capacity the new capacity + * @retval zero allocation was successful + * @retval non-zero allocation failed */ -typedef struct cx_array_reallocator_s CxArrayReallocator; - -/** - * A default array reallocator that is based on the cxDefaultAllocator. - */ -CX_EXPORT extern CxArrayReallocator *cx_array_default_reallocator; +cx_attr_nonnull +CX_EXPORT int cx_array_copy_to_new_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity); /** - * Creates a new array reallocator. - * - * When @p allocator is @c NULL, the cxDefaultAllocator will be used. + * Copies the array to a new memory region. * - * When @p stack_ptr is not @c NULL, the reallocator is supposed to be used - * @em only for the specific array initially located at @p stack_ptr. - * When reallocation is needed, the reallocator checks if the array is - * still located at @p stack_ptr and copies the contents to the heap. + * This is useful when you have initialized the array with a fixed size memory + * using cx_array_init_fixed(), and now you want to increase the capacity. + * + * @attention When the original memory does not belong to stack memory, and + * you do not have another reference to this memory, it will leak. * - * @note Invoking this function with both arguments being @c NULL will return a - * reallocator that behaves like #cx_array_default_reallocator. - * - * @param allocator the allocator this reallocator shall be based on - * @param stack_ptr the address of the array when the array is initially located - * on the stack or shall not reallocate in place - * @return an array reallocator + * @param allocator (@c CxAllocator*) the allocator for the new memory + * @param array the name of the array + * @param capacity (@c size_t) the new maximum number of elements + * @retval zero allocation was successful + * @retval non-zero allocation failed + * @see cx_array_init_fixed() */ -CX_EXPORT CxArrayReallocator cx_array_reallocator( - const struct cx_allocator_s *allocator, const void *stack_ptr); +#define cx_array_copy_to_new_a(allocator, array, capacity) \ + cx_array_copy_to_new_(allocator, (CxArray*)&(array), sizeof((array).data[0]), capacity) /** - * Reserves memory for additional elements. - * - * This function checks if the @p capacity of the array is sufficient to hold - * at least @p size plus @p elem_count elements. If not, a reallocation is - * performed with the specified @p reallocator. - * You can create your own reallocator by hand, use #cx_array_default_reallocator, - * or use the convenience function cx_array_reallocator() to create a custom reallocator. + * Copies the array to a new memory region. * - * This function can be useful to replace subsequent calls to cx_array_copy() - * with one single cx_array_reserve() and then - after guaranteeing a - * sufficient capacity - use simple memmove() or memcpy(). + * This is useful when you have initialized the array with a fixed size memory + * using cx_array_init_fixed(), and now you want to increase the capacity. * - * The @p width in bytes refers to the size and capacity. - * Both must have the same width. - * Supported are 0, 1, 2, and 4, as well as 8 if running on a 64-bit - * architecture. If set to zero, the native word width is used. + * @attention When the original memory does not belong to stack memory, and + * you do not have another reference to this memory, it will leak. * - * @param array a pointer to the target array - * @param size a pointer to the size of the array - * @param capacity a pointer to the capacity of the array - * @param width the width in bytes for the @p size and @p capacity or zero for default + * @param array the name of the array + * @param capacity (@c size_t) the new maximum number of elements + * @retval zero allocation was successful + * @retval non-zero allocation failed + * @see cx_array_init_fixed() + */ +#define cx_array_copy_to_new(array, capacity) \ + cx_array_copy_to_new_a(cxDefaultAllocator, array, capacity) + +/** + * Inserts data into an array. + * + * Internal function - do not use. + * + * @param allocator the allocator to use for a possible reallocation + * @param array a pointer to the array structure * @param elem_size the size of one element - * @param elem_count the number of expected additional elements - * @param reallocator the array reallocator to use - * (@c NULL defaults to #cx_array_default_reallocator) + * @param index the index where to insert the @p other data + * @param other a pointer to an array of data that shall be inserted + * @param n the number of elements that shall be inserted * @retval zero success - * @retval non-zero failure - * @see cx_array_reallocator() + * @retval non-zero a re-allocation was necessary but failed */ -cx_attr_nonnull_arg(1, 2, 3) -CX_EXPORT int cx_array_reserve(void **array, void *size, void *capacity, - unsigned width, size_t elem_size, size_t elem_count, - CxArrayReallocator *reallocator); +cx_attr_nonnull_arg(1, 2) +CX_EXPORT int cx_array_insert_(const CxAllocator *allocator, CxArray *array, + size_t elem_size, size_t index, const void *other, size_t n); /** - * Copies elements from one array to another. - * - * The elements are copied to the @p target array at the specified @p index, - * overwriting possible elements. The @p index does not need to be in range of - * the current array @p size. If the new index plus the number of elements added - * extends the array's size, the remaining @p capacity is used. + * Appends an element to an array. * - * If the @p capacity is also insufficient to hold the new data, a reallocation - * attempt is made with the specified @p reallocator. - * You can create your own reallocator by hand, use #cx_array_default_reallocator, - * or use the convenience function cx_array_reallocator() to create a custom reallocator. - * - * The @p width in bytes refers to the size and capacity. - * Both must have the same width. - * Supported are 0, 1, 2, and 4, as well as 8 if running on a 64-bit - * architecture. If set to zero, the native word width is used. + * When the capacity is not enough to hold the new element, a re-allocation is attempted. * - * @param target a pointer to the target array - * @param size a pointer to the size of the target array - * @param capacity a pointer to the capacity of the target array - * @param width the width in bytes for the @p size and @p capacity or zero for default - * @param index the index where the copied elements shall be placed - * @param src the source array - * @param elem_size the size of one element - * @param elem_count the number of elements to copy - * @param reallocator the array reallocator to use - * (@c NULL defaults to #cx_array_default_reallocator) + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the element shall be added + * @param element the element that shall be added * @retval zero success - * @retval non-zero failure - * @see cx_array_reallocator() + * @retval non-zero a re-allocation was necessary but failed */ -cx_attr_nonnull_arg(1, 2, 3, 6) -CX_EXPORT int cx_array_copy(void **target, void *size, void *capacity, unsigned width, - size_t index, const void *src, size_t elem_size, size_t elem_count, - CxArrayReallocator *reallocator); +#define cx_array_add_a(allocator, array, element) \ + cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (array).size, (void*)&(element), 1) + +/** + * Appends an element to an array. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. + * + * @param array the name of the array where the element shall be added + * @param element (@c void*) a pointer to the element that shall be added + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_add(array, element) \ + cx_array_add_a(cxDefaultAllocator, array, element) /** - * Convenience macro that uses cx_array_copy() with a default layout and - * the specified reallocator. + * Inserts an element into an array. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. * - * @param reallocator (@c CxArrayReallocator*) the array reallocator to use - * @param array the name of the array (NOT a pointer or alias to the array) - * @param index (@c size_t) the index where the copied elements shall be placed - * @param src (@c void*) the source array - * @param count (@c size_t) the number of elements to copy + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the element shall be inserted + * @param index (@c size_t) the index where to insert the @p element + * @param element the element that shall be inserted * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_copy() + * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_simple_copy_a(reallocator, array, index, src, count) \ - cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \ - sizeof(array##_size), index, src, sizeof((array)[0]), count, \ - reallocator) +#define cx_array_insert_a(allocator, array, index, element) \ + cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), index, (void*)&(element), 1) /** - * Convenience macro that uses cx_array_copy() with a default layout and - * the default reallocator. + * Inserts an element into an array. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. * - * @param array the name of the array (NOT a pointer or alias to the array) - * @param index (@c size_t) the index where the copied elements shall be placed - * @param src (@c void*) the source array - * @param count (@c size_t) the number of elements to copy + * @param array the name of the array where the element shall be inserted + * @param index (@c size_t) the index where to insert the @p element + * @param element (@c void*) a pointer to the element that shall be inserted * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_copy_a() + * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_simple_copy(array, index, src, count) \ - cx_array_simple_copy_a(NULL, array, index, src, count) +#define cx_array_insert(array, index, element) \ + cx_array_insert_a(cxDefaultAllocator, array, index, element) /** - * Convenience macro that uses cx_array_reserve() with a default layout and - * the specified reallocator. + * Inserts data into an array. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. * - * @param reallocator (@c CxArrayReallocator*) the array reallocator to use - * @param array the name of the array (NOT a pointer or alias to the array) - * @param count (@c size_t) the number of expected @em additional elements + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param index (@c size_t) the index where to insert the @p other data + * @param other (@c void*) a pointer to an array of data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_reserve() + * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_simple_reserve_a(reallocator, array, count) \ - cx_array_reserve((void**)&(array), &(array##_size), &(array##_capacity), \ - sizeof(array##_size), sizeof((array)[0]), count, \ - reallocator) +#define cx_array_insert_array_a(allocator, array, index, other, n) \ + cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), index, other, n) /** - * Convenience macro that uses cx_array_reserve() with a default layout and - * the default reallocator. + * Inserts data into an array. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. * - * @param array the name of the array (NOT a pointer or alias to the array) - * @param count (@c size_t) the number of expected additional elements + * @param array the name of the array where the elements shall be inserted + * @param index (@c size_t) the index where to insert the @p other data + * @param other (@c void*) a pointer to an array of data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_reserve_a() + * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_simple_reserve(array, count) \ - cx_array_simple_reserve_a(NULL, array, count) +#define cx_array_insert_array(array, index, other, n) \ + cx_array_insert_array_a(cxDefaultAllocator, array, index, other, n) + +/** + * Appends data to an array. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. + * + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be added + * @param other (@c void*) a pointer to an array of data that shall be added + * @param n (@c size_t) the number of elements that shall be added + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_add_array_a(allocator, array, other, n) \ + cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (array).size, other, n) /** - * Adds an element to an array with the possibility of allocating more space. - * - * The element @p elem is added to the end of the @p target array which contains - * @p size elements, already. The @p capacity must point to a variable denoting - * the current maximum number of elements the array can hold. + * Appends data to an array. * - * If the capacity is insufficient to hold the new element, an attempt to - * increase the @p capacity is made and the new capacity is written back. - * - * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. - * It is important, however, that @p size and @p capacity are pointers to - * variables of the same type. + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. * - * @param target (@c void**) a pointer to the target array - * @param size (@c SIZE_TYPE*) a pointer to the size of the target array - * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array - * @param elem_size (@c size_t) the size of one element - * @param elem (@c void*) a pointer to the element to add - * @param reallocator (@c CxArrayReallocator*) the array reallocator to use + * @param array the name of the array where the elements shall be added + * @param other (@c void*) a pointer to an array of data that shall be added + * @param n (@c size_t) the number of elements that shall be added * @retval zero success - * @retval non-zero failure + * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ - cx_array_copy((void**)(target), size, capacity, sizeof(*(size)), \ - *(size), elem, elem_size, 1, reallocator) +#define cx_array_add_array(array, other, n) \ + cx_array_add_array_a(cxDefaultAllocator, array, other, n) /** - * Convenience macro that uses cx_array_add() with a default layout and - * the specified reallocator. + * Inserts sorted data into a sorted array. + * + * Internal function - do not use. * - * @param reallocator (@c CxArrayReallocator*) the array reallocator to use - * @param array the name of the array (NOT a pointer or alias to the array) - * @param elem the element to add (NOT a pointer, address is automatically taken) + * @param allocator the allocator to use for a possible reallocation + * @param array a pointer to the array structure + * @param elem_size the size of one element + * @param sorted_data a pointer to an array of data that shall be inserted + * @param n the number of elements that shall be inserted + * @param cmp_func the compare function + * @param allow_duplicates @c false if duplicates shall be skipped during insertion * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_add() + * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_simple_add_a(reallocator, array, elem) \ - cx_array_simple_copy_a(reallocator, array, array##_size, &(elem), 1) +cx_attr_nonnull +CX_EXPORT int cx_array_insert_sorted_(const CxAllocator *allocator, CxArray *array, + size_t elem_size, const void *sorted_data, size_t n, + cx_compare_func cmp_func, bool allow_duplicates); /** - * Convenience macro that uses cx_array_add() with a default layout and - * the default reallocator. + * Inserts an element into a sorted array. * - * @param array the name of the array (NOT a pointer or alias to the array) - * @param elem the element to add (NOT a pointer, address is automatically taken) - * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_add_a() - */ -#define cx_array_simple_add(array, elem) \ - cx_array_simple_add_a(cx_array_default_reallocator, array, elem) - -/** - * Inserts a sorted array into another sorted array. + * When the capacity is not enough to hold the new element, a re-allocation is attempted. * - * If either the target or the source array is not already sorted with respect - * to the specified @p cmp_func, the behavior is undefined. + * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. * - * If the capacity is insufficient to hold the new data, a reallocation - * attempt is made. - * You can create your own reallocator by hand, use #cx_array_default_reallocator, - * or use the convenience function cx_array_reallocator() to create a custom reallocator. - * - * @param target a pointer to the target array - * @param size a pointer to the size of the target array - * @param capacity a pointer to the capacity of the target array - * @param cmp_func the compare function for the elements - * @param src the source array - * @param elem_size the size of one element - * @param elem_count the number of elements to insert - * @param reallocator the array reallocator to use - * (@c NULL defaults to #cx_array_default_reallocator) + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param element the element that shall be inserted + * @param cmp_func (@c cx_compare_func) the compare function that establishes the order * @retval zero success - * @retval non-zero failure + * @retval non-zero a re-allocation was necessary but failed */ -cx_attr_nonnull_arg(1, 2, 3, 5) -CX_EXPORT int cx_array_insert_sorted(void **target, size_t *size, size_t *capacity, - cx_compare_func cmp_func, const void *src, size_t elem_size, size_t elem_count, - CxArrayReallocator *reallocator); +#define cx_array_insert_sorted_a(allocator, array, element, cmp_func) \ + cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (void*)&(element), 1, cmp_func, true) /** * Inserts an element into a sorted array. * - * If the target array is not already sorted with respect - * to the specified @p cmp_func, the behavior is undefined. + * When the capacity is not enough to hold the new element, a re-allocation is attempted. + * + * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param array the name of the array where the elements shall be inserted + * @param element the element that shall be inserted + * @param cmp_func (@c cx_compare_func) the compare function that establishes the order + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_sorted(array, element, cmp_func) \ + cx_array_insert_sorted_a(cxDefaultAllocator, array, element, cmp_func) + +/** + * Inserts sorted data into a sorted array. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. * - * If the capacity is not enough to hold the new data, a reallocation - * attempt is made. + * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. * - * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. - * It is important, however, that @p size and @p capacity are pointers to - * variables of the same type. + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func) the compare function that establishes the order + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_sorted_array_a(allocator, array, sorted_data, n, cmp_func) \ + cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, true) + +/** + * Inserts sorted data into a sorted array. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. + * + * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. * - * @param target (@c void**) a pointer to the target array - * @param size (@c SIZE_TYPE*) a pointer to the size of the target array - * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array - * @param elem_size (@c size_t) the size of one element - * @param elem (@c void*) a pointer to the element to add - * @param cmp_func (@c cx_cmp_func) the compare function for the elements - * @param reallocator (@c CxArrayReallocator*) the array reallocator to use + * @param array the name of the array where the elements shall be inserted + * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func) the compare function that establishes the order * @retval zero success - * @retval non-zero failure + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_sorted_array(array, sorted_data, n, cmp_func) \ + cx_array_insert_sorted_array_a(cxDefaultAllocator, array, sorted_data, n, cmp_func) + +/** + * Inserts an element into a sorted array if it is not already contained. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. + * + * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param element the element that shall be inserted + * @param cmp_func (@c cx_compare_func) the compare function that establishes the order + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_add_sorted(target, size, capacity, elem_size, elem, cmp_func, reallocator) \ - cx_array_insert_sorted((void**)(target), size, capacity, cmp_func, elem, elem_size, 1, reallocator) +#define cx_array_insert_unique_a(allocator, array, element, cmp_func) \ + cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (void*)&(element), 1, cmp_func, false) + +/** + * Inserts an element into a sorted array if it is not already contained. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. + * + * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param array the name of the array where the elements shall be inserted + * @param element the element that shall be inserted + * @param cmp_func (@c cx_compare_func) the compare function that establishes the order + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_unique(array, element, cmp_func) \ + cx_array_insert_unique_a(cxDefaultAllocator, array, element, cmp_func) /** - * Convenience macro for cx_array_add_sorted() with a default - * layout and the specified reallocator. + * Inserts sorted data into a sorted array, skipping duplicates. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. + * + * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func) the compare function that establishes the order + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_unique_array_a(allocator, array, sorted_data, n, cmp_func) \ + cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, false) + +/** + * Inserts sorted data into a sorted array, skipping duplicates. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. + * + * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. * - * @param reallocator (@c CxArrayReallocator*) the array reallocator to use - * @param array the name of the array (NOT a pointer or alias to the array) - * @param elem the element to add (NOT a pointer, address is automatically taken) - * @param cmp_func (@c cx_cmp_func) the compare function for the elements + * @param array the name of the array where the elements shall be inserted + * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func) the compare function that establishes the order + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_unique_array(array, sorted_data, n, cmp_func) \ + cx_array_insert_unique_array_a(cxDefaultAllocator, array, sorted_data, n, cmp_func) + +/** + * Inserts sorted data into a sorted array. + * + * Internal function - do not use. + * + * @param allocator the allocator to use for a possible reallocation + * @param array a pointer to the array structure + * @param elem_size the size of one element + * @param sorted_data a pointer to an array of data that shall be inserted + * @param n the number of elements that shall be inserted + * @param cmp_func the compare function + * @param context additional context for the compare function + * @param allow_duplicates @c false if duplicates shall be skipped during insertion * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_add_sorted() + * @retval non-zero a re-allocation was necessary but failed + */ +cx_attr_nonnull_arg(1, 2, 4, 6) +CX_EXPORT int cx_array_insert_sorted_c_(const CxAllocator *allocator, CxArray *array, + size_t elem_size, const void *sorted_data, size_t n, + cx_compare_func2 cmp_func, void *context, bool allow_duplicates); + +/** + * Inserts an element into a sorted array. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. + * + * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param element the element that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_simple_add_sorted_a(reallocator, array, elem, cmp_func) \ - cx_array_add_sorted(&array, &(array##_size), &(array##_capacity), \ - sizeof((array)[0]), &(elem), cmp_func, reallocator) +#define cx_array_insert_sorted_ca(allocator, array, element, cmp_func) \ + cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (void*)&(element), 1, cmp_func, context, true) + +/** + * Inserts an element into a sorted array. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. + * + * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param array the name of the array where the elements shall be inserted + * @param element the element that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_sorted_c(array, element, cmp_func, context) \ + cx_array_insert_sorted_ca(cxDefaultAllocator, array, element, cmp_func, context) + +/** + * Inserts sorted data into a sorted array. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. + * + * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_sorted_array_ca(allocator, array, sorted_data, n, cmp_func, context) \ + cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, context, true) /** - * Convenience macro for cx_array_add_sorted() with a default - * layout and the default reallocator. + * Inserts sorted data into a sorted array. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. + * + * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. * - * @param array the name of the array (NOT a pointer or alias to the array) - * @param elem the element to add (NOT a pointer, address is automatically taken) - * @param cmp_func (@c cx_cmp_func) the compare function for the elements + * @param array the name of the array where the elements shall be inserted + * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_sorted_array_c(array, sorted_data, n, cmp_func, context) \ + cx_array_insert_sorted_array_ca(cxDefaultAllocator, array, sorted_data, n, cmp_func, context) + +/** + * Inserts an element into a sorted array if it is not already contained. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. + * + * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param element the element that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_add_sorted_a() + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_unique_ca(allocator, array, element, cmp_func, context) \ + cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (void*)&(element), 1, cmp_func, context, false) + +/** + * Inserts an element into a sorted array if it is not already contained. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. + * + * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param array the name of the array where the elements shall be inserted + * @param element the element that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_simple_add_sorted(array, elem, cmp_func) \ - cx_array_simple_add_sorted_a(NULL, array, elem, cmp_func) +#define cx_array_insert_unique_c(array, element, cmp_func, context) \ + cx_array_insert_unique_ca(cxDefaultAllocator, array, element, cmp_func, context) + +/** + * Inserts sorted data into a sorted array, skipping duplicates. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. + * + * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_unique_array_ca(allocator, array, sorted_data, n, cmp_func, context) \ + cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, context, false) + +/** + * Inserts sorted data into a sorted array, skipping duplicates. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. + * + * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param array the name of the array where the elements shall be inserted + * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_unique_array_c(array, sorted_data, n, cmp_func, context) \ + cx_array_insert_unique_array_ca(cxDefaultAllocator, array, sorted_data, n, cmp_func, context) /** - * Convenience macro for cx_array_insert_sorted() with a default - * layout and the specified reallocator. + * An alternative to qsort_r() when that is not available on your platform. + * + * If it is available, qsort_r() is used directly. + * + * @param array the array that shall be sorted + * @param nmemb the number of elements in the array + * @param size the size of one element + * @param fn the compare function + * @param context the context for the compare function + */ +CX_EXPORT void cx_array_qsort_c(void *array, size_t nmemb, size_t size, + cx_compare_func2 fn, void *context); + +/** + * Sorts an array. + * + * Internal function - do not use. * - * @param reallocator (@c CxArrayReallocator*) the array reallocator to use - * @param array the name of the array (NOT a pointer or alias to the array) - * @param src (@c void*) pointer to the source array - * @param n (@c size_t) number of elements in the source array - * @param cmp_func (@c cx_cmp_func) the compare function for the elements - * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_insert_sorted() + * @param array a pointer to the array structure + * @param elem_size the size of one element + * @param fn the compare function */ -#define cx_array_simple_insert_sorted_a(reallocator, array, src, n, cmp_func) \ - cx_array_insert_sorted((void**)(&array), &(array##_size), &(array##_capacity), \ - cmp_func, src, sizeof((array)[0]), n, reallocator) +CX_EXPORT void cx_array_sort_(CxArray *array, size_t elem_size, + cx_compare_func fn); + +/** + * Sorts an array. + * + * Internal function - do not use. + * + * @param array a pointer to the array structure + * @param elem_size the size of one element + * @param fn the compare function + * @param context the context for the compare function + */ +CX_EXPORT void cx_array_sort_c_(CxArray *array, size_t elem_size, + cx_compare_func2 fn, void *context); + +/** + * Sorts an array. + * + * @param array the name of the array + * @param fn (@c cx_compare_func) the compare function + * @param context (@c void*) the context for the compare function + */ +#define cx_array_sort(array, fn) \ + cx_array_sort_((CxArray*)&(array), sizeof((array).data[0]), fn) /** - * Convenience macro for cx_array_insert_sorted() with a default - * layout and the default reallocator. + * Sorts an array. + * + * @param array the name of the array + * @param fn (@c cx_compare_func2) the compare function + * @param context (@c void*) the context for the compare function + */ +#define cx_array_sort_c(array, fn, context) \ + cx_array_sort_c_((CxArray*)&(array), sizeof((array).data[0]), fn, context) + +/** + * Creates an iterator over the elements of an array. + * + * Internal function - do not use. + * + * @param array a pointer to the array structure + * @param elem_size the size of one element + * @return an iterator over the elements + */ +cx_attr_nodiscard cx_attr_nonnull +CX_EXPORT CxIterator cx_array_iterator_(CxArray *array, size_t elem_size); + +/** + * Creates an iterator over the elements of an array. + * + * The iterator will yield pointers to the elements. + * + * This iterator cannot be used to remove elements + * because it does not get a modifiable reference to the array's size. * - * @param array the name of the array (NOT a pointer or alias to the array) - * @param src (@c void*) pointer to the source array - * @param n (@c size_t) number of elements in the source array - * @param cmp_func (@c cx_cmp_func) the compare function for the elements - * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_insert_sorted_a() + * @param array the name of the array + * @return an iterator over the elements + * @see cx_array_iterator_ptr() + */ +#define cx_array_iterator(array) \ + cx_array_iterator_((CxArray*)&(array), sizeof((array).data[0])) + +/** + * Creates an iterator over the elements of an array containing pointers. + * + * Internal function - do not use. + * + * @param array the name of the array + * @return an iterator over the elements */ -#define cx_array_simple_insert_sorted(array, src, n, cmp_func) \ - cx_array_simple_insert_sorted_a(NULL, array, src, n, cmp_func) +cx_attr_nodiscard cx_attr_nonnull +CX_EXPORT CxIterator cx_array_iterator_ptr_(CxArray *array); + +/** + * Creates an iterator over the elements of an array containing pointers. + * + * The iterator will yield the elements themselves, which are supposed to + * be pointers. + * + * This iterator cannot be used to remove elements + * because it does not get a modifiable reference to the array's size. + * + * @param array the name of the array + * @return an iterator over the elements + * @see cx_array_iterator() + */ +#define cx_array_iterator_ptr(array) \ + cx_array_iterator_ptr_((CxArray*)&(array)) /** - * Inserts a sorted array into another sorted array, avoiding duplicates. - * - * If either the target or the source array is not already sorted with respect - * to the specified @p cmp_func, the behavior is undefined. + * Removes elements from the array. * - * If the capacity is insufficient to hold the new data, a reallocation - * attempt is made. - * You can create your own reallocator by hand, use #cx_array_default_reallocator, - * or use the convenience function cx_array_reallocator() to create a custom reallocator. + * Internal function - do not use. * - * @param target a pointer to the target array - * @param size a pointer to the size of the target array - * @param capacity a pointer to the capacity of the target array - * @param cmp_func the compare function for the elements - * @param src the source array + * @param array a pointer to the array structure * @param elem_size the size of one element - * @param elem_count the number of elements to insert - * @param reallocator the array reallocator to use - * (@c NULL defaults to #cx_array_default_reallocator) - * @retval zero success - * @retval non-zero failure + * @param index the index of the first element to remove + * @param n the number of elements to remove + * @param fast indicates whether tail elements should be copied into the gap */ -cx_attr_nonnull_arg(1, 2, 3, 5) -CX_EXPORT int cx_array_insert_unique(void **target, size_t *size, size_t *capacity, - cx_compare_func cmp_func, const void *src, size_t elem_size, size_t elem_count, - CxArrayReallocator *reallocator); +cx_attr_nonnull +CX_EXPORT void cx_array_remove_(CxArray *array, size_t elem_size, size_t index, size_t n, bool fast); /** - * Inserts an element into a sorted array if it does not exist. - * - * If the target array is not already sorted with respect - * to the specified @p cmp_func, the behavior is undefined. + * Removes one element from the array. * - * If the capacity is insufficient to hold the new data, a reallocation - * attempt is made. + * Tail elements are all moved by one. If you don't need a stable order + * in the array, consider using cx_array_remove_fast(). * - * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. - * It is important, however, that @p size and @p capacity are pointers to - * variables of the same type. + * If the index is out of bounds, this function does nothing. * - * @param target (@c void**) a pointer to the target array - * @param size (@c SIZE_TYPE*) a pointer to the size of the target array - * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array - * @param elem_size (@c size_t) the size of one element - * @param elem (@c void*) a pointer to the element to add - * @param cmp_func (@c cx_cmp_func) the compare function for the elements - * @param reallocator (@c CxArrayReallocator*) the array reallocator to use - * @retval zero success (also when the element was already present) - * @retval non-zero failure + * @param array the name of the array + * @param index (@c size_t) the index of the element to remove + * @see cx_array_remove_fast() */ -#define cx_array_add_unique(target, size, capacity, elem_size, elem, cmp_func, reallocator) \ - cx_array_insert_unique((void**)(target), size, capacity, cmp_func, elem, elem_size, 1, reallocator) +#define cx_array_remove(array, index) \ + cx_array_remove_((CxArray*)&(array), sizeof((array).data[0]), index, 1, false) + +/** + * Removes one element from the array. + * + * The gap will be filled with a copy of the last element in the array. + * This changes the order of elements. If you want a stable order, + * use cx_array_remove() instead. + * + * If the index is out of bounds, this function does nothing. + * + * @param array the name of the array + * @param index (@c size_t) the index of the element to remove + * @see cx_array_remove() + */ +#define cx_array_remove_fast(array, index) \ + cx_array_remove_((CxArray*)&(array), sizeof((array).data[0]), index, 1, true) /** - * Convenience macro for cx_array_add_unique() with a default - * layout and the specified reallocator. + * Removes multiple elements from the array. + * + * Tail elements are all moved to close the gap. If you don't need a stable + * order in the array, consider using cx_array_remove_array_fast(). * - * @param reallocator (@c CxArrayReallocator*) the array reallocator to use - * @param array the name of the array (NOT a pointer or alias to the array) - * @param elem the element to add (NOT a pointer, address is automatically taken) - * @param cmp_func (@c cx_cmp_func) the compare function for the elements - * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_add_unique() + * If the index is out of bounds, this function does nothing. + * If @n overflows the array, this function removes as many elements as it can. + * + * @param array the name of the array + * @param index (@c size_t) the index of the first element to remove + * @param n (@c size_t) the number of elements to remove + * @see cx_array_remove_array_fast() */ -#define cx_array_simple_add_unique_a(reallocator, array, elem, cmp_func) \ - cx_array_add_unique(&array, &(array##_size), &(array##_capacity), \ - sizeof((array)[0]), &(elem), cmp_func, reallocator) - -/** - * Convenience macro for cx_array_add_unique() with a default - * layout and the default reallocator. - * - * @param array the name of the array (NOT a pointer or alias to the array) - * @param elem the element to add (NOT a pointer, address is automatically taken) - * @param cmp_func (@c cx_cmp_func) the compare function for the elements - * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_add_unique_a() - */ -#define cx_array_simple_add_unique(array, elem, cmp_func) \ - cx_array_simple_add_unique_a(NULL, array, elem, cmp_func) +#define cx_array_remove_array(array, index, n) \ + cx_array_remove_((CxArray*)&(array), sizeof((array).data[0]), index, n, false) /** - * Convenience macro for cx_array_insert_unique() with a default - * layout and the specified reallocator. + * Removes multiple elements from the array. + * + * Tail elements are copied into the gap. If you have more tail elements + * than the number of elements that are removed, this will change the order + * of elements. If you want a stable order, use cx_array_remove_array() instead. * - * @param reallocator (@c CxArrayReallocator*) the array reallocator to use - * @param array the name of the array (NOT a pointer or alias to the array) - * @param src (@c void*) pointer to the source array - * @param n (@c size_t) number of elements in the source array - * @param cmp_func (@c cx_cmp_func) the compare function for the elements - * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_insert_unique() + * If the index is out of bounds, this function does nothing. + * If @n overflows the array, this function removes as many elements as it can. + * + * @param array the name of the array + * @param index (@c size_t) the index of the first element to remove + * @param n (@c size_t) the number of elements to remove + * @see cx_array_remove_array() */ -#define cx_array_simple_insert_unique_a(reallocator, array, src, n, cmp_func) \ - cx_array_insert_unique((void**)(&array), &(array##_size), &(array##_capacity), \ - cmp_func, src, sizeof((array)[0]), n, reallocator) +#define cx_array_remove_array_fast(array, index, n) \ + cx_array_remove_((CxArray*)&(array), sizeof((array).data[0]), index, n, true) /** - * Convenience macro for cx_array_insert_unique() with a default - * layout and the default reallocator. + * Deallocates an array. + * + * Internal function - do not use. + * + * @param allocator (@c CxAllocator*) the allocator which was used to allocate the array + * @param array a pointer to the array structure + */ +cx_attr_nonnull +CX_EXPORT void cx_array_free_(const CxAllocator *allocator, CxArray *array); + +/** + * Deallocates an array. + * + * The structure is reset to zero and can be re-initialized with + * cx_array_inita(). * - * @param array the name of the array (NOT a pointer or alias to the array) - * @param src (@c void*) pointer to the source array - * @param n (@c size_t) number of elements in the source array - * @param cmp_func (@c cx_cmp_func) the compare function for the elements - * @retval zero success - * @retval non-zero failure - * @see CX_ARRAY_DECLARE() - * @see cx_array_simple_insert_unique_a() + * @param array the name of the array */ -#define cx_array_simple_insert_unique(array, src, n, cmp_func) \ - cx_array_simple_insert_unique_a(NULL, array, src, n, cmp_func) +#define cx_array_free(array) cx_array_free_(cxDefaultAllocator, (CxArray*)&(array)) + +/** + * Deallocates an array. + * + * The structure is reset to zero and can be re-initialized with + * cx_array_init_a(). + * + * @param allocator (@c CxAllocator*) the allocator which was used to allocate the array + * @param array the name of the array + */ +#define cx_array_free_a(allocator, array) cx_array_free_(allocator, (CxArray*)&(array)) + /** * Searches the largest lower bound in a sorted array. @@ -669,6 +944,9 @@ * in @p arr that is less or equal to @p elem with respect to @p cmp_func. * When no such element exists, @p size is returned. * + * When such an element exists more than once, the largest index of all those + * elements is returned. + * * If @p elem is contained in the array, this is identical to * #cx_array_binary_search(). * @@ -691,6 +969,9 @@ /** * Searches an item in a sorted array. * + * When such an element exists more than once, the largest index of all those + * elements is returned. + * * If the array is not sorted with respect to the @p cmp_func, the behavior * is undefined. * @@ -715,6 +996,9 @@ * in @p arr that is greater or equal to @p elem with respect to @p cmp_func. * When no such element exists, @p size is returned. * + * When such an element exists more than once, the smallest index of all those + * elements is returned. + * * If @p elem is contained in the array, this is identical to * #cx_array_binary_search(). * @@ -734,6 +1018,91 @@ CX_EXPORT size_t cx_array_binary_search_sup(const void *arr, size_t size, size_t elem_size, const void *elem, cx_compare_func cmp_func); + +/** + * Searches the largest lower bound in a sorted array. + * + * In other words, this function returns the index of the largest element + * in @p arr that is less or equal to @p elem with respect to @p cmp_func. + * When no such element exists, @p size is returned. + * + * When such an element exists more than once, the largest index of all those + * elements is returned. + * + * If @p elem is contained in the array, this is identical to + * #cx_array_binary_search(). + * + * If the array is not sorted with respect to the @p cmp_func, the behavior + * is undefined. + * + * @param arr the array to search + * @param size the size of the array + * @param elem_size the size of one element + * @param elem the element to find + * @param cmp_func the compare function + * @param context the context for the compare function + * @return the index of the largest lower bound, or @p size + * @see cx_array_binary_search_sup() + * @see cx_array_binary_search() + */ +cx_attr_nonnull +CX_EXPORT size_t cx_array_binary_search_inf_c(const void *arr, size_t size, + size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context); + +/** + * Searches an item in a sorted array. + * + * When such an element exists more than once, the largest index of all those + * elements is returned. + * + * If the array is not sorted with respect to the @p cmp_func, the behavior + * is undefined. + * + * @param arr the array to search + * @param size the size of the array + * @param elem_size the size of one element + * @param elem the element to find + * @param cmp_func the compare function + * @param context the context for the compare function + * @return the index of the element in the array, or @p size if the element + * cannot be found + * @see cx_array_binary_search_inf() + * @see cx_array_binary_search_sup() + */ +cx_attr_nonnull +CX_EXPORT size_t cx_array_binary_search_c(const void *arr, size_t size, + size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context); + +/** + * Searches the smallest upper bound in a sorted array. + * + * In other words, this function returns the index of the smallest element + * in @p arr that is greater or equal to @p elem with respect to @p cmp_func. + * When no such element exists, @p size is returned. + * + * When such an element exists more than once, the smallest index of all those + * elements is returned. + * + * If @p elem is contained in the array, this is identical to + * #cx_array_binary_search(). + * + * If the array is not sorted with respect to the @p cmp_func, the behavior + * is undefined. + * + * @param arr the array to search + * @param size the size of the array + * @param elem_size the size of one element + * @param elem the element to find + * @param cmp_func the compare function + * @param context the context for the compare function + * @return the index of the smallest upper bound, or @p size + * @see cx_array_binary_search_inf() + * @see cx_array_binary_search() + */ +cx_attr_nonnull +CX_EXPORT size_t cx_array_binary_search_sup_c(const void *arr, size_t size, + size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context); + /** * Swaps two array elements. * @@ -750,13 +1119,10 @@ * * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of * copies of the added elements, and the compare function will be automatically set - * to cx_cmp_ptr(), if none is given. + * to cx_cmp_ptr(). * * @param allocator the allocator for allocating the list memory * (if @c NULL, the cxDefaultAllocator will be used) - * @param comparator the comparator for the elements - * (if @c NULL, and the list is not storing pointers, sort and find - * functions will not work) * @param elem_size the size of each element in bytes * @param initial_capacity the initial number of elements the array can store * @return the created list @@ -765,25 +1131,7 @@ cx_attr_malloc cx_attr_dealloc(cxListFree, 1) CX_EXPORT CxList *cxArrayListCreate(const CxAllocator *allocator, - cx_compare_func comparator, size_t elem_size, size_t initial_capacity); - -/** - * Allocates an array list for storing elements with @p elem_size bytes each. - * - * The list will use the cxDefaultAllocator and @em NO compare function. - * If you want to call functions that need a compare function, you have to - * set it immediately after creation or use cxArrayListCreate(). - * - * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of - * copies of the added elements and the compare function will be automatically set - * to cx_cmp_ptr(). - * - * @param elem_size (@c size_t) the size of each element in bytes - * @param initial_capacity (@c size_t) the initial number of elements the array can store - * @return the created list - */ -#define cxArrayListCreateSimple(elem_size, initial_capacity) \ - cxArrayListCreate(NULL, NULL, elem_size, initial_capacity) + size_t elem_size, size_t initial_capacity); #ifdef __cplusplus } // extern "C"
--- a/ucx/cx/buffer.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/buffer.h Fri Dec 19 17:53:18 2025 +0100 @@ -48,6 +48,7 @@ #include "common.h" #include "allocator.h" +#include "string.h" #ifdef __cplusplus extern "C" { @@ -89,6 +90,13 @@ #define CX_BUFFER_COPY_ON_EXTEND 0x08 /** + * If this flag is enabled, the buffer will never free its contents regardless of #CX_BUFFER_FREE_CONTENTS. + * + * This is useful, for example, when you want to keep a pointer to the data after destroying the buffer. + */ +#define CX_BUFFER_DO_NOT_FREE 0x10 + +/** * Function pointer for cxBufferWrite that is compatible with cx_write_func. * @see cx_write_func */ @@ -99,59 +107,6 @@ */ #define cxBufferReadFunc ((cx_read_func) cxBufferRead) -/** - * Configuration for automatic flushing. - */ -struct cx_buffer_flush_config_s { - /** - * The buffer may not extend beyond this threshold before starting to flush. - * - * Only used when the buffer uses #CX_BUFFER_AUTO_EXTEND. - * The threshold will be the maximum capacity the buffer is extended to - * before flushing. - */ - size_t threshold; - /** - * The block size for the elements to flush. - */ - size_t blksize; - /** - * The maximum number of blocks to flush in one cycle. - * - * @attention while it is guaranteed that cxBufferFlush() will not flush - * more blocks, this is not necessarily the case for cxBufferWrite(). - * After performing a flush cycle, cxBufferWrite() will retry the write - * operation and potentially trigger another flush cycle, until the - * flush target accepts no more data. - */ - size_t blkmax; - - /** - * The target for the write function. - */ - void *target; - - /** - * The write-function used for flushing. - * If NULL, the flushed content gets discarded. - */ - cx_write_func wfunc; -}; - -/** - * Type alias for the flush configuration struct. - * - * @code - * struct cx_buffer_flush_config_s { - * size_t threshold; - * size_t blksize; - * size_t blkmax; - * void *target; - * cx_write_func wfunc; - * }; - * @endcode - */ -typedef struct cx_buffer_flush_config_s CxBufferFlushConfig; /** Structure for the UCX buffer data. */ struct cx_buffer_s { @@ -168,16 +123,12 @@ }; /** The allocator to use for automatic memory management. */ const CxAllocator *allocator; - /** - * Optional flush configuration - * - * @see cxBufferEnableFlushing() - */ - CxBufferFlushConfig *flush; /** Current position of the buffer. */ size_t pos; /** Current capacity (i.e. maximum size) of the buffer. */ size_t capacity; + /** Maximum capacity that this buffer may grow to. */ + size_t max_capacity; /** Current size of the buffer content. */ size_t size; /** @@ -217,35 +168,18 @@ * space will be leaking after the copy-on-write operation. * * @param buffer the buffer to initialize + * @param allocator the allocator this buffer shall use for automatic + * memory management + * (if @c NULL, the cxDefaultAllocator will be used) * @param space pointer to the memory area, or @c NULL to allocate * new memory * @param capacity the capacity of the buffer - * @param allocator the allocator this buffer shall use for automatic - * memory management - * (if @c NULL, the cxDefaultAllocator will be used) * @param flags buffer features (see cx_buffer_s.flags) * @return zero on success, non-zero if a required allocation failed */ cx_attr_nonnull_arg(1) -CX_EXPORT int cxBufferInit(CxBuffer *buffer, void *space, size_t capacity, - const CxAllocator *allocator, int flags); - -/** - * Configures the buffer for flushing. - * - * Flushing can happen automatically when data is written - * to the buffer (see cxBufferWrite()) or manually when - * cxBufferFlush() is called. - * - * @param buffer the buffer - * @param config the flush configuration - * @retval zero success - * @retval non-zero failure - * @see cxBufferFlush() - * @see cxBufferWrite() - */ -cx_attr_nonnull -CX_EXPORT int cxBufferEnableFlushing(CxBuffer *buffer, CxBufferFlushConfig config); +CX_EXPORT int cxBufferInit(CxBuffer *buffer, const CxAllocator *allocator, + void *space, size_t capacity, int flags); /** * Destroys the buffer contents. @@ -285,18 +219,18 @@ * Then this function will allocate the space and enforce * the #CX_BUFFER_FREE_CONTENTS flag. * + * @param allocator the allocator to use for allocating the structure and the automatic + * memory management within the buffer + * (if @c NULL, the cxDefaultAllocator will be used) * @param space pointer to the memory area, or @c NULL to allocate * new memory * @param capacity the capacity of the buffer - * @param allocator the allocator to use for allocating the structure and the automatic - * memory management within the buffer - * (if @c NULL, the cxDefaultAllocator will be used) * @param flags buffer features (see cx_buffer_s.flags) * @return a pointer to the buffer on success, @c NULL if a required allocation failed */ cx_attr_malloc cx_attr_dealloc(cxBufferFree, 1) cx_attr_nodiscard -CX_EXPORT CxBuffer *cxBufferCreate(void *space, size_t capacity, - const CxAllocator *allocator, int flags); +CX_EXPORT CxBuffer *cxBufferCreate(const CxAllocator *allocator, void *space, + size_t capacity, int flags); /** * Shifts the contents of the buffer by the given offset. @@ -388,6 +322,20 @@ CX_EXPORT int cxBufferSeek(CxBuffer *buffer, off_t offset, int whence); /** + * Discards items from the end of the buffer. + * + * When the current position points to a byte that gets discarded, + * the position is set to the buffer size. + * + * @param buffer the buffer + * @param size the size of one item + * @param nitems the number of items to discard + * @return the actual number of discarded items + */ +cx_attr_nonnull +CX_EXPORT size_t cxBufferPop(CxBuffer *buffer, size_t size, size_t nitems); + +/** * Clears the buffer by resetting the position and deleting the data. * * The data is deleted by zeroing it with a call to memset(). @@ -425,11 +373,51 @@ cx_attr_nonnull cx_attr_nodiscard CX_EXPORT bool cxBufferEof(const CxBuffer *buffer); +/** + * Ensures that the buffer has the required capacity. + * + * If the current capacity is not sufficient, the buffer will be extended. + * If the current capacity is larger, the buffer is shrunk and superfluous + * content is discarded. + * + * This function will reserve no more bytes than requested, in contrast to + * cxBufferMinimumCapacity(), which may reserve more bytes to improve the + * number of future necessary reallocations. + * + * @param buffer the buffer + * @param capacity the required capacity for this buffer + * @retval zero on success + * @retval non-zero on allocation failure + * @see cxBufferShrink() + * @see cxBufferMinimumCapacity() + */ +cx_attr_nonnull +CX_EXPORT int cxBufferReserve(CxBuffer *buffer, size_t capacity); + +/** + * Limits the buffer's capacity. + * + * If the current capacity is already larger, this function fails and returns + * non-zero. + * + * The capacity limit will affect auto-extension features, as well as future + * calls to cxBufferMinimumCapacity() and cxBufferReserve(). + * + * @param buffer the buffer + * @param capacity the maximum allowed capacity for this buffer + * @retval zero the limit is applied + * @retval non-zero the new limit is smaller than the current capacity + * @see cxBufferReserve() + * @see cxBufferMinimumCapacity() + */ +cx_attr_nonnull +CX_EXPORT int cxBufferMaximumCapacity(CxBuffer *buffer, size_t capacity); /** * Ensures that the buffer has a minimum capacity. * - * If the current capacity is not sufficient, the buffer will be extended. + * If the current capacity is not sufficient, the buffer will be generously + * extended. * * The new capacity will be a power of two until the system's page size is reached. * Then, the new capacity will be a multiple of the page size. @@ -438,6 +426,8 @@ * @param capacity the minimum required capacity for this buffer * @retval zero the capacity was already sufficient or successfully increased * @retval non-zero on allocation failure + * @see cxBufferMaximumCapacity() + * @see cxBufferReserve() * @see cxBufferShrink() */ cx_attr_nonnull @@ -457,6 +447,7 @@ * * @param buffer the buffer * @param reserve the number of bytes that shall remain reserved + * @see cxBufferReserve() * @see cxBufferMinimumCapacity() */ cx_attr_nonnull @@ -465,33 +456,13 @@ /** * Writes data to a CxBuffer. * - * If automatic flushing is not enabled, the data is simply written into the - * buffer at the current position, and the position of the buffer is increased - * by the number of bytes written. - * - * If flushing is enabled and the buffer needs to flush, the data is flushed to - * the target until the target signals that it cannot take more data by - * returning zero via the respective write function. In that case, the remaining - * data in this buffer is shifted to the beginning of this buffer so that the - * newly available space can be used to append as much data as possible. - * - * This function only stops writing more elements when the flush target and this - * buffer are both incapable of taking more data or all data has been written. + * If auto-extension is enabled, the buffer's capacity is automatically + * increased when it is not large enough to hold all data. + * By default, the capacity grows indefinitely, unless limited with + * cxBufferMaximumCapacity(). + * When auto-extension fails, this function writes no data and returns zero. * - * If, after flushing, the number of items that shall be written still exceeds - * the capacity or flush threshold, this function tries to write all items directly - * to the flush target, if possible. - * - * The number returned by this function is the number of elements from - * @c ptr that could be written to either the flush target or the buffer. - * That means it does @em not include the number of items that were already in - * the buffer and were also flushed during the process. - * - * @attention - * When @p size is larger than one and the contents of the buffer are not aligned - * with @p size, flushing stops after all complete items have been flushed, leaving - * the misaligned part in the buffer. - * Afterward, this function only writes as many items as possible to the buffer. + * The position of the buffer is moved alongside the written data. * * @note The signature is compatible with the fwrite() family of functions. * @@ -531,62 +502,6 @@ size_t nitems, CxBuffer *buffer); /** - * Performs a single flush-run on the specified buffer. - * - * Does nothing when the position in the buffer is zero. - * Otherwise, the data until the current position minus - * one is considered for flushing. - * Note carefully that flushing will never exceed the - * current @em position, even when the size of the - * buffer is larger than the current position. - * - * One flush run will try to flush @c blkmax many - * blocks of size @c blksize until either the @p buffer - * has no more data to flush or the write function - * used for flushing returns zero. - * - * The buffer is shifted left for that many bytes - * the flush operation has successfully flushed. - * - * @par Example 1 - * Assume you have a buffer with size 340 and you are - * at position 200. The flush configuration is - * @c blkmax=4 and @c blksize=64 . - * Assume that the entire flush operation is successful. - * All 200 bytes on the left-hand-side from the current - * position are written. - * That means the size of the buffer is now 140 and the - * position is zero. - * - * @par Example 2 - * Same as Example 1, but now the @c blkmax is 1. - * The size of the buffer is now 276, and the position is 136. - * - * @par Example 3 - * Same as Example 1, but now assume the flush target - * only accepts 100 bytes before returning zero. - * That means the flush operation manages to flush - * one complete block and one partial block, ending - * up with a buffer with size 240 and position 100. - * - * @remark Just returns zero when flushing was not enabled with - * cxBufferEnableFlushing(). - * - * @remark When the buffer uses copy-on-write, the memory - * is copied first, before attempting any flush. - * This is, however, considered an erroneous use of the - * buffer because it makes little sense to put - * readonly data into an UCX buffer for flushing instead - * of writing it directly to the target. - * - * @param buffer the buffer - * @return the number of successfully flushed bytes - * @see cxBufferEnableFlushing() - */ -cx_attr_nonnull -CX_EXPORT size_t cxBufferFlush(CxBuffer *buffer); - -/** * Reads data from a CxBuffer. * * The position of the buffer is increased by the number of bytes read. @@ -610,8 +525,9 @@ * * The least significant byte of the argument is written to the buffer. If the * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled, - * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature - * is disabled or the buffer extension fails, @c EOF is returned. + * the buffer capacity is extended, unless a limit set by + * cxBufferMaximumCapacity() is reached. + * If the feature is disabled or the buffer extension fails, @c EOF is returned. * * On successful writing, the position of the buffer is increased. * @@ -620,8 +536,8 @@ * * @param buffer the buffer to write to * @param c the character to write - * @return the byte that has been written or @c EOF when the end of the stream is - * reached, and automatic extension is not enabled or not possible + * @return the byte that has been written or @c EOF when the end of the + * stream is reached, and automatic extension is not enabled or not possible * @see cxBufferTerminate() */ cx_attr_nonnull @@ -630,28 +546,61 @@ /** * Writes a terminating zero to a buffer at the current position. * - * If successful, sets the size to the current position and advances the position by one. + * If successful, also sets the size to the current position and shrinks the buffer. * * The purpose of this function is to have the written data ready to be used as * a C string with the buffer's size being the length of that string. * * @param buffer the buffer to write to * @return zero, if the terminator could be written, non-zero otherwise + * @see cxBufferShrink() */ cx_attr_nonnull CX_EXPORT int cxBufferTerminate(CxBuffer *buffer); /** - * Writes a string to a buffer. - * - * This is a convenience function for <code>cxBufferWrite(str, 1, strlen(str), buffer)</code>. + * Internal function - do not use. * * @param buffer the buffer - * @param str the zero-terminated string + * @param str the string * @return the number of bytes written + * @see cxBufferPutString() + */ +cx_attr_nonnull +CX_EXPORT size_t cx_buffer_put_string(CxBuffer *buffer, cxstring str); + +/** + * Writes a string to a buffer with cxBufferWrite(). + * + * @param buffer (@c CxBuffer*) the buffer + * @param str (any string) the zero-terminated string + * @return (@c size_t) the number of bytes written + * @see cxBufferWrite() + * @see cx_strcast() */ -cx_attr_nonnull cx_attr_cstr_arg(2) -CX_EXPORT size_t cxBufferPutString(CxBuffer *buffer, const char *str); +#define cxBufferPutString(buffer, str) cx_buffer_put_string(buffer, cx_strcast(str)) + +/** + * Internal function - do not use. + * + * @param buffer the buffer + * @param str the string + * @return the number of bytes written + * @see cxBufferPutString() + */ +cx_attr_nonnull +CX_EXPORT size_t cx_buffer_append_string(CxBuffer *buffer, cxstring str); + +/** + * Appends a string to a buffer with cxBufferAppend(). + * + * @param buffer (@c CxBuffer*) the buffer + * @param str (any string) the zero-terminated string + * @return (@c size_t) the number of bytes written + * @see cxBufferAppend() + * @see cx_strcast() + */ +#define cxBufferAppendString(buffer, str) cx_buffer_append_string(buffer, cx_strcast(str)) /** * Gets a character from a buffer.
--- a/ucx/cx/collection.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/collection.h Fri Dec 19 17:53:18 2025 +0100 @@ -58,10 +58,6 @@ */ const CxAllocator *allocator; /** - * The comparator function for the elements. - */ - cx_compare_func cmpfunc; - /** * The size of each element. */ size_t elem_size; @@ -70,6 +66,19 @@ */ size_t size; /** + * A two-argument comparator function for the elements. + */ + cx_compare_func simple_cmp; + /** + * A three-argument comparator function for the elements. + * If specified, this function has precedence over the @c simple_cmp function. + */ + cx_compare_func2 advanced_cmp; + /** + * A pointer to custom data for the @c advanced_cmp function + */ + void *cmp_data; + /** * An optional simple destructor for the collection's elements. * * @attention Read the documentation of the particular collection implementation @@ -139,6 +148,25 @@ */ #define cxCollectionStoresPointers(c) ((c)->collection.store_pointer) + +/** + * Convenience macro for adding indirection to an element if the collection is storing pointers. + * + * @param c a pointer to a struct that contains #CX_COLLECTION_BASE + * @param elem the pointer that shall be taken the address from, if the collection is storing pointers + * @return if the collection is storing pointers, takes the address of @p elem, otherwise returns @p elem + */ +#define cx_ref(c, elem) (cxCollectionStoresPointers(c) ? ((void*)&(elem)) : (elem)) + +/** + * Convenience macro for dereferencing an element if the collection is storing pointers. + * + * @param c a pointer to a struct that contains #CX_COLLECTION_BASE + * @param elem a pointer to the collection element + * @return if the collection is storing pointers, dereferences @p elem, otherwise returns @p elem + */ +#define cx_deref(c, elem) (cxCollectionStoresPointers(c) ? *((void**)(elem)) : (elem)) + /** * Indicates whether the collection can guarantee that the stored elements are currently sorted. * @@ -154,29 +182,89 @@ #define cxCollectionSorted(c) ((c)->collection.sorted || (c)->collection.size == 0) /** - * Sets the compare function for a collection. + * Sets a simple compare function for a collection. + * + * Erases a possible advanced compare function. + * If you want to set both, because you want to access the simple function + * in your advanced function, you must set the simple function first. + * + * @param c a pointer to a struct that contains #CX_COLLECTION_BASE + * @param func (@c cx_compare_func) the compare function + */ +#define cxSetCompareFunc(c, func) \ + (c)->collection.simple_cmp = (cx_compare_func)(func); \ + (c)->collection.advanced_cmp = NULL + +/** + * Sets an advanced compare function that supports custom data for a collection. + * + * @param c a pointer to a struct that contains #CX_COLLECTION_BASE + * @param func (@c cx_compare_func2) the compare function + * @param data (@c void*) the pointer to custom data that is passed to the compare function + */ +#define cxSetAdvancedCompareFunc(c, func, data) \ + (c)->collection.advanced_cmp = (cx_compare_func2) func; \ + (c)->collection.cmp_data = data + +/** + * Invokes the simple comparator function for two elements. + * + * Usually only used by collection implementations. There should be no need + * to invoke this macro manually. * * @param c a pointer to a struct that contains #CX_COLLECTION_BASE - * @param func (@c cx_compare_func) the compare function that shall be used by @c c + * @param left (@c void*) pointer to data + * @param right (@c void*) pointer to data + */ +#define cx_invoke_simple_compare_func(c, left, right) \ + (c)->collection.simple_cmp(left, right) + +/** + * Invokes the advanced comparator function for two elements. + * + * Usually only used by collection implementations. There should be no need + * to invoke this macro manually. + * + * @param c a pointer to a struct that contains #CX_COLLECTION_BASE + * @param left (@c void*) pointer to data + * @param right (@c void*) pointer to data */ -#define cxCollectionCompareFunc(c, func) (c)->collection.cmpfunc = (func) +#define cx_invoke_advanced_compare_func(c, left, right) \ + (c)->collection.advanced_cmp(left, right, (c)->collection.cmp_data) + + +/** + * Invokes the configured comparator function for two elements. + * + * Usually only used by collection implementations. There should be no need + * to invoke this macro manually. + * + * @param c a pointer to a struct that contains #CX_COLLECTION_BASE + * @param left (@c void*) pointer to data + * @param right (@c void*) pointer to data + */ +#define cx_invoke_compare_func(c, left, right) \ + (((c)->collection.advanced_cmp) ? \ + cx_invoke_advanced_compare_func(c,left,right) : \ + cx_invoke_simple_compare_func(c,left,right)) /** * Sets a simple destructor function for this collection. * * @param c a pointer to a struct that contains #CX_COLLECTION_BASE - * @param destr the destructor function + * @param destr (@c cx_destructor_func) the destructor function */ -#define cxDefineDestructor(c, destr) \ +#define cxSetDestructor(c, destr) \ (c)->collection.simple_destructor = (cx_destructor_func) destr /** - * Sets a simple destructor function for this collection. + * Sets an advanced destructor function for this collection. * * @param c a pointer to a struct that contains #CX_COLLECTION_BASE - * @param destr the destructor function + * @param destr (@c cx_destructor_func2) the destructor function + * @param data (@c void*) the additional data the advanced destructor is invoked with */ -#define cxDefineAdvancedDestructor(c, destr, data) \ +#define cxSetAdvancedDestructor(c, destr, data) \ (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \ (c)->collection.destructor_data = data
--- a/ucx/cx/common.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/common.h Fri Dec 19 17:53:18 2025 +0100 @@ -80,10 +80,10 @@ #define UCX_COMMON_H /** Major UCX version as integer constant. */ -#define UCX_VERSION_MAJOR 3 +#define UCX_VERSION_MAJOR 4 /** Minor UCX version as integer constant. */ -#define UCX_VERSION_MINOR 1 +#define UCX_VERSION_MINOR 0 /** Version constant which ensures to increase monotonically. */ #define UCX_VERSION (((UCX_VERSION_MAJOR)<<16)|UCX_VERSION_MINOR) @@ -284,6 +284,9 @@ */ #define CX_INLINE __attribute__((always_inline)) static inline #else +/** + * Declares a function to be inlined. + */ #define CX_INLINE static inline #endif /**
--- a/ucx/cx/compare.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/compare.h Fri Dec 19 17:53:18 2025 +0100 @@ -57,6 +57,13 @@ typedef int (*cx_compare_func)(const void *left, const void *right); /** + * A comparator function comparing two arbitrary values. + * + * Functions with this signature allow specifying a pointer to custom data. + */ +typedef int (*cx_compare_func2)(const void *left, const void *right, void *data); + +/** * Compares two integers of type int. * * @note the parameters deliberately have type @c void* to be @@ -527,6 +534,41 @@ cx_attr_nonnull cx_attr_nodiscard CX_EXPORT int cx_cmp_ptr(const void *ptr1, const void *ptr2); +/** + * A @c cx_compare_func2 compatible wrapper for @c memcmp(). + * + * @param ptr1 pointer one + * @param ptr2 pointer two + * @param n (@c size_t*) a pointer to the length + * @return the result of @c memcmp() + */ +cx_attr_nonnull cx_attr_nodiscard +CX_EXPORT int cx_ccmp_memcmp(const void *ptr1, const void *ptr2, void *n); + +/** Wraps a compare function for cx_ccmp_wrap. */ +typedef struct { + /** The wrapped compare function */ + cx_compare_func cmp; +} cx_compare_func_wrapper; + +/** + * A @c cx_compare_func2 wrapper for a @c cx_compare_func(). + * + * This is not strictly compatible with a @c cx_compare_func2 because + * ISO C does not define conversions between function and object pointers. + * + * But it works on all tested platforms to cast a pointer to this function to + * a @c cx_compare_func2. + * + * @param ptr1 pointer one + * @param ptr2 pointer two + * @param cmp_wrapper a pointer to a @c cx_compare_func_wrapper + * @return the result of the invoked compare function + * @see cx_compare_func_wrapper_s + */ +cx_attr_nonnull cx_attr_nodiscard +CX_EXPORT int cx_ccmp_wrap(const void *ptr1, const void *ptr2, void* cmp_wrapper); + #ifdef __cplusplus } // extern "C" #endif
--- a/ucx/cx/hash_map.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/hash_map.h Fri Dec 19 17:53:18 2025 +0100 @@ -88,22 +88,6 @@ size_t itemsize, size_t buckets); /** - * Creates a new hash map with a default number of buckets. - * - * If @p elem_size is #CX_STORE_POINTERS, the created map stores pointers instead of - * copies of the added elements. - * - * @note Iterators provided by this hash map implementation provide the remove operation. - * The index value of an iterator is incremented when the iterator advanced without - * removing an entry. - * In other words, when the iterator is finished, @c index==size . - * - * @param itemsize (@c size_t) the size of one element - * @return (@c CxMap*) a pointer to the new hash map - */ -#define cxHashMapCreateSimple(itemsize) cxHashMapCreate(NULL, itemsize, 0) - -/** * Increases the number of buckets, if necessary. * * The load threshold is @c 0.75*buckets. If the element count exceeds the load
--- a/ucx/cx/iterator.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/iterator.h Fri Dec 19 17:53:18 2025 +0100 @@ -214,26 +214,15 @@ * use cxIteratorPtr() to create an iterator which directly * yields the stored pointers. * - * While the iterator is in use, the array may only be altered by removing - * elements through #cxIteratorFlagRemoval(). Every other change to the array - * will bring this iterator to an undefined state. - * - * When @p remove_keeps_order is set to @c false, removing an element will only - * move the last element to the position of the removed element, instead of - * moving all subsequent elements by one. Usually, when the order of elements is - * not important, this parameter should be set to @c false. - * * @param array a pointer to the array (can be @c NULL) * @param elem_size the size of one array element * @param elem_count the number of elements in the array - * @param remove_keeps_order @c true if the order of elements must be preserved - * when removing an element * @return an iterator for the specified array * @see cxIteratorPtr() */ cx_attr_nodiscard CX_EXPORT CxIterator cxIterator(const void *array, - size_t elem_size, size_t elem_count, bool remove_keeps_order); + size_t elem_size, size_t elem_count); /** * Creates an iterator for the specified plain pointer array. @@ -243,25 +232,13 @@ * hand, an iterator created with cxIterator() would return the * addresses of those pointers within the array). * - * While the iterator is in use, the array may only be altered by removing - * elements through #cxIteratorFlagRemoval(). Every other change to the array - * will bring this iterator to an undefined state. - * - * When @p remove_keeps_order is set to @c false, removing an element will only - * move the last element to the position of the removed element, instead of - * moving all subsequent elements by one. Usually, when the order of elements is - * not important, this parameter should be set to @c false. - * * @param array a pointer to the array (can be @c NULL) * @param elem_count the number of elements in the array - * @param remove_keeps_order @c true if the order of elements must be preserved - * when removing an element * @return an iterator for the specified array * @see cxIterator() */ cx_attr_nodiscard -CX_EXPORT CxIterator cxIteratorPtr(const void *array, size_t elem_count, - bool remove_keeps_order); +CX_EXPORT CxIterator cxIteratorPtr(const void *array, size_t elem_count); #ifdef __cplusplus } // extern "C"
--- a/ucx/cx/json.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/json.h Fri Dec 19 17:53:18 2025 +0100 @@ -41,8 +41,7 @@ #include "string.h" #include "buffer.h" #include "array_list.h" - -#include <string.h> +#include "map.h" #ifdef __cplusplus extern "C" { @@ -184,13 +183,10 @@ typedef struct cx_json_value_s CxJsonValue; /** - * Type alias for the JSON array struct. + * Type alias for the map representing a JSON object. + * The map contains pointers of type @c CxJsonValue. */ -typedef struct cx_json_array_s CxJsonArray; -/** - * Type alias for the JSON object struct. - */ -typedef struct cx_json_object_s CxJsonObject; +typedef CxMap* CxJsonObject; /** * Type alias for a JSON string. */ @@ -209,49 +205,6 @@ typedef enum cx_json_literal CxJsonLiteral; /** - * Type alias for a key/value pair in a JSON object. - */ -typedef struct cx_json_obj_value_s CxJsonObjValue; - -/** - * JSON array structure. - */ -struct cx_json_array_s { - /** - * The array data. - */ - CX_ARRAY_DECLARE(CxJsonValue*, array); -}; - -/** - * JSON object structure. - */ -struct cx_json_object_s { - /** - * The key/value entries. - */ - CX_ARRAY_DECLARE(CxJsonObjValue, values); - /** - * The original indices to reconstruct the order in which the members were added. - */ - size_t *indices; -}; - -/** - * Structure for a key/value entry in a JSON object. - */ -struct cx_json_obj_value_s { - /** - * The key (or name in JSON terminology) of the value. - */ - cxmutstr name; - /** - * The value. - */ - CxJsonValue *value; -}; - -/** * Structure for a JSON value. */ struct cx_json_value_s { @@ -274,7 +227,7 @@ /** * The array data if the type is #CX_JSON_ARRAY. */ - CxJsonArray array; + CX_ARRAY(CxJsonValue*, array); /** * The object data if the type is #CX_JSON_OBJECT. */ @@ -295,7 +248,7 @@ * The literal type if the type is #CX_JSON_LITERAL. */ CxJsonLiteral literal; - } value; + }; }; /** @@ -329,6 +282,7 @@ * The allocator used for produced JSON values. */ const CxAllocator *allocator; + /** * The input buffer. */ @@ -349,21 +303,21 @@ CxJsonValue *parsed; /** - * A pointer to an intermediate state of a currently parsed object member. + * The name of a not yet completely parsed object member. * * Never access this value manually. */ - CxJsonObjValue uncompleted_member; + cxmutstr uncompleted_member_name; /** * State stack. */ - CX_ARRAY_DECLARE_SIZED(int, states, unsigned); + CX_ARRAY(int, states); /** * Value buffer stack. */ - CX_ARRAY_DECLARE_SIZED(CxJsonValue*, vbuf, unsigned); + CX_ARRAY(CxJsonValue*, vbuf); /** * Internally reserved memory for the state stack. @@ -439,10 +393,6 @@ */ bool pretty; /** - * Set false to output the members in the order in which they were added. - */ - bool sort_members; - /** * The maximum number of fractional digits in a number value. * The default value is 6 and values larger than 15 are reduced to 15. * Note that the actual number of digits may be lower, depending on the concrete number. @@ -508,6 +458,33 @@ CX_EXPORT int cxJsonWrite(void* target, const CxJsonValue* value, cx_write_func wfunc, const CxJsonWriter* settings); + +/** + * Produces a compact string representation of the specified JSON value. + * + * @param allocator the allocator for the string + * @param value the JSON value + * @return the produced string + * @see cxJsonWrite() + * @see cxJsonWriterCompact() + * @see cxJsonToPrettyString() + */ +cx_attr_nonnull_arg(2) +CX_EXPORT cxmutstr cxJsonToString(const CxAllocator *allocator, CxJsonValue *value); + +/** + * Produces a pretty string representation of the specified JSON value. + * + * @param allocator the allocator for the string + * @param value the JSON value + * @return the produced string + * @see cxJsonWrite() + * @see cxJsonWriterPretty() + * @see cxJsonToString() + */ +cx_attr_nonnull_arg(2) +CX_EXPORT cxmutstr cxJsonToPrettyString(const CxAllocator *allocator, CxJsonValue *value); + /** * Initializes the JSON interface. * @@ -530,8 +507,8 @@ /** * Destroys and re-initializes the JSON interface. * - * You might want to use this to reset the parser after - * encountering a syntax error. + * You must use this to reset the parser after encountering a syntax error + * if you want to continue using it. * * @param json the JSON interface */ @@ -556,7 +533,7 @@ * @retval non-zero internal allocation error * @see cxJsonFill() */ -cx_attr_nonnull cx_attr_access_r(2, 3) +cx_attr_nonnull_arg(1) cx_attr_access_r(2, 3) CX_EXPORT int cxJsonFilln(CxJson *json, const char *buf, size_t len); @@ -592,6 +569,36 @@ */ #define cxJsonFill(json, str) cx_json_fill(json, cx_strcast(str)) + +/** + * Internal function - use cxJsonFromString() instead. + * + * @param allocator the allocator for the JSON value + * @param str the string to parse + * @param value a pointer where the JSON value shall be stored to + * @return status code + */ +cx_attr_nonnull_arg(3) +CX_EXPORT CxJsonStatus cx_json_from_string(const CxAllocator *allocator, + cxstring str, CxJsonValue **value); + +/** + * Parses a string into a JSON value. + * + * @param allocator (@c CxAllocator*) the allocator for the JSON value + * @param str (any string) the string to parse + * @param value (@c CxJsonValue**) a pointer where the JSON value shall be stored to + * @retval CX_JSON_NO_ERROR success + * @retval CX_JSON_NO_DATA the string was empty or blank + * @retval CX_JSON_INCOMPLETE_DATA the string unexpectedly ended + * @retval CX_JSON_BUFFER_ALLOC_FAILED allocating internal buffer space failed + * @retval CX_JSON_VALUE_ALLOC_FAILED allocating memory for the CxJsonValue failed + * @retval CX_JSON_FORMAT_ERROR_NUMBER the JSON text contains an illegally formatted number + * @retval CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN JSON syntax error + */ +#define cxJsonFromString(allocator, str, value) \ + cx_json_from_string(allocator, cx_strcast(str), value) + /** * Creates a new (empty) JSON object. * @@ -606,13 +613,16 @@ /** * Creates a new (empty) JSON array. * + * Optionally, this function already allocates memory with the given capacity. + * * @param allocator the allocator to use + * @param capacity optional capacity or zero if it's unknown how many elements the array will have * @return the new JSON array or @c NULL if allocation fails * @see cxJsonObjPutArr() * @see cxJsonArrAddValues() */ cx_attr_nodiscard -CX_EXPORT CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator); +CX_EXPORT CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator, size_t capacity); /** * Creates a new JSON number value. @@ -641,28 +651,27 @@ /** * Creates a new JSON string. * + * Internal function - use cxJsonCreateString() instead. + * * @param allocator the allocator to use * @param str the string data * @return the new JSON value or @c NULL if allocation fails - * @see cxJsonCreateString() * @see cxJsonObjPutString() - * @see cxJsonArrAddStrings() + * @see cxJsonArrAddCxStrings() */ -cx_attr_nodiscard cx_attr_nonnull_arg(2) cx_attr_cstr_arg(2) -CX_EXPORT CxJsonValue* cxJsonCreateString(const CxAllocator* allocator, const char *str); +cx_attr_nodiscard +CX_EXPORT CxJsonValue* cx_json_create_string(const CxAllocator* allocator, cxstring str); /** * Creates a new JSON string. * - * @param allocator the allocator to use - * @param str the string data - * @return the new JSON value or @c NULL if allocation fails - * @see cxJsonCreateCxString() - * @see cxJsonObjPutCxString() + * @param allocator (@c CxAllocator*) the allocator to use + * @param str the string + * @return (@c CxJsonValue*) the new JSON value or @c NULL if allocation fails + * @see cxJsonObjPutString() * @see cxJsonArrAddCxStrings() */ -cx_attr_nodiscard -CX_EXPORT CxJsonValue* cxJsonCreateCxString(const CxAllocator* allocator, cxstring str); +#define cxJsonCreateString(allocator, str) cx_json_create_string(allocator, cx_strcast(str)) /** * Creates a new JSON literal. @@ -760,10 +769,7 @@ /** * Adds or replaces a value within a JSON object. * - * The value will be directly added and not copied. - * - * @note If a value with the specified @p name already exists, - * it will be (recursively) freed with its own allocator. + * Internal function - use cxJsonObjPut(). * * @param obj the JSON object * @param name the name of the value @@ -772,11 +778,29 @@ * @retval non-zero allocation failure */ cx_attr_nonnull -CX_EXPORT int cxJsonObjPut(CxJsonValue* obj, cxstring name, CxJsonValue* child); +CX_EXPORT int cx_json_obj_put(CxJsonValue* obj, cxstring name, CxJsonValue* child); + +/** + * Adds or replaces a value within a JSON object. + * + * The value will be directly added and not copied. + * + * @note If a value with the specified @p name already exists, + * it will be (recursively) freed with its own allocator. + * + * @param obj (@c CxJsonValue*) the JSON object + * @param name (any string) the name of the value + * @param child (@c CxJsonValue*) the value + * @retval zero success + * @retval non-zero allocation failure + */ +#define cxJsonObjPut(obj, name, child) cx_json_obj_put(obj, cx_strcast(name), child) /** * Creates a new JSON object and adds it to an existing object. * + * Internal function - use cxJsonObjPutObj(). + * * @param obj the target JSON object * @param name the name of the new value * @return the new value or @c NULL if allocation fails @@ -784,23 +808,51 @@ * @see cxJsonCreateObj() */ cx_attr_nonnull -CX_EXPORT CxJsonValue* cxJsonObjPutObj(CxJsonValue* obj, cxstring name); +CX_EXPORT CxJsonValue* cx_json_obj_put_obj(CxJsonValue* obj, cxstring name); + +/** + * Creates a new JSON object and adds it to an existing object. + * + * @param obj (@c CxJsonValue*) the target JSON object + * @param name (any string) the name of the new value + * @return (@c CxJsonValue*) the new value or @c NULL if allocation fails + * @see cxJsonObjPut() + * @see cxJsonCreateObj() + */ +#define cxJsonObjPutObj(obj, name) cx_json_obj_put_obj(obj, cx_strcast(name)) /** * Creates a new JSON array and adds it to an object. * + * Internal function - use cxJsonObjPutArr(). + * * @param obj the target JSON object * @param name the name of the new value + * @param capacity optional initial capacity * @return the new value or @c NULL if allocation fails * @see cxJsonObjPut() * @see cxJsonCreateArr() */ cx_attr_nonnull -CX_EXPORT CxJsonValue* cxJsonObjPutArr(CxJsonValue* obj, cxstring name); +CX_EXPORT CxJsonValue* cx_json_obj_put_arr(CxJsonValue* obj, cxstring name, size_t capacity); + +/** + * Creates a new JSON array and adds it to an object. + * + * @param obj (@c CxJsonValue*) the target JSON object + * @param name (any string) the name of the new value + * @param capacity (@c size_t) optional initial capacity + * @return (@c CxJsonValue*) the new value or @c NULL if allocation fails + * @see cxJsonObjPut() + * @see cxJsonCreateArr() + */ +#define cxJsonObjPutArr(obj, name, capacity) cx_json_obj_put_arr(obj, cx_strcast(name), capacity) /** * Creates a new JSON number and adds it to an object. * + * Internal function - use cxJsonObjPutNumber(). + * * @param obj the target JSON object * @param name the name of the new value * @param num the numeric value @@ -809,11 +861,25 @@ * @see cxJsonCreateNumber() */ cx_attr_nonnull -CX_EXPORT CxJsonValue* cxJsonObjPutNumber(CxJsonValue* obj, cxstring name, double num); +CX_EXPORT CxJsonValue* cx_json_obj_put_number(CxJsonValue* obj, cxstring name, double num); + +/** + * Creates a new JSON number and adds it to an object. + * + * @param obj (@c CxJsonValue*) the target JSON object + * @param name (any string) the name of the new value + * @param num (@c double) the numeric value + * @return (@c CxJsonValue*) the new value or @c NULL if allocation fails + * @see cxJsonObjPut() + * @see cxJsonCreateNumber() + */ +#define cxJsonObjPutNumber(obj, name, num) cx_json_obj_put_number(obj, cx_strcast(name), num) /** * Creates a new JSON number, based on an integer, and adds it to an object. * + * Internal function - use cxJsonObjPutInteger(). + * * @param obj the target JSON object * @param name the name of the new value * @param num the numeric value @@ -822,12 +888,24 @@ * @see cxJsonCreateInteger() */ cx_attr_nonnull -CX_EXPORT CxJsonValue* cxJsonObjPutInteger(CxJsonValue* obj, cxstring name, int64_t num); +CX_EXPORT CxJsonValue* cx_json_obj_put_integer(CxJsonValue* obj, cxstring name, int64_t num); + +/** + * Creates a new JSON number, based on an integer, and adds it to an object. + * + * @param obj (@c CxJsonValue*) the target JSON object + * @param name (any string) the name of the new value + * @param num (@c int64_t) the numeric value + * @return (@c CxJsonValue*) the new value or @c NULL if allocation fails + * @see cxJsonObjPut() + * @see cxJsonCreateInteger() + */ +#define cxJsonObjPutInteger(obj, name, num) cx_json_obj_put_integer(obj, cx_strcast(name), num) /** * Creates a new JSON string and adds it to an object. * - * The string data is copied. + * Internal function - use cxJsonObjPutString() * * @param obj the target JSON object * @param name the name of the new value @@ -836,27 +914,28 @@ * @see cxJsonObjPut() * @see cxJsonCreateString() */ -cx_attr_nonnull cx_attr_cstr_arg(3) -CX_EXPORT CxJsonValue* cxJsonObjPutString(CxJsonValue* obj, cxstring name, const char* str); +cx_attr_nonnull +CX_EXPORT CxJsonValue* cx_json_obj_put_string(CxJsonValue* obj, cxstring name, cxstring str); /** * Creates a new JSON string and adds it to an object. * * The string data is copied. * - * @param obj the target JSON object - * @param name the name of the new value - * @param str the string data - * @return the new value or @c NULL if allocation fails + * @param obj (@c CxJsonValue*) the target JSON object + * @param name (any string) the name of the new value + * @param str (any string) the string data + * @return (@c CxJsonValue*) the new value or @c NULL if allocation fails * @see cxJsonObjPut() - * @see cxJsonCreateCxString() + * @see cxJsonCreateString() */ -cx_attr_nonnull -CX_EXPORT CxJsonValue* cxJsonObjPutCxString(CxJsonValue* obj, cxstring name, cxstring str); +#define cxJsonObjPutString(obj, name, str) cx_json_obj_put_string(obj, cx_strcast(name), cx_strcast(str)) /** * Creates a new JSON literal and adds it to an object. * + * Internal function - use cxJsonObjPutLiteral(). + * * @param obj the target JSON object * @param name the name of the new value * @param lit the type of literal @@ -865,7 +944,19 @@ * @see cxJsonCreateLiteral() */ cx_attr_nonnull -CX_EXPORT CxJsonValue* cxJsonObjPutLiteral(CxJsonValue* obj, cxstring name, CxJsonLiteral lit); +CX_EXPORT CxJsonValue* cx_json_obj_put_literal(CxJsonValue* obj, cxstring name, CxJsonLiteral lit); + +/** + * Creates a new JSON literal and adds it to an object. + * + * @param obj (@c CxJsonValue*) the target JSON object + * @param name (any string) the name of the new value + * @param lit (@c CxJsonLiteral) the type of literal + * @return (@c CxJsonValue*) the new value or @c NULL if allocation fails + * @see cxJsonObjPut() + * @see cxJsonCreateLiteral() + */ +#define cxJsonObjPutLiteral(obj, name, lit) cx_json_obj_put_literal(obj, cx_strcast(name), lit) /** * Recursively deallocates the memory of a JSON value. @@ -998,7 +1089,7 @@ */ cx_attr_nonnull CX_INLINE bool cxJsonIsBool(const CxJsonValue *value) { - return cxJsonIsLiteral(value) && value->value.literal != CX_JSON_NULL; + return cxJsonIsLiteral(value) && value->literal != CX_JSON_NULL; } /** @@ -1015,7 +1106,7 @@ */ cx_attr_nonnull CX_INLINE bool cxJsonIsTrue(const CxJsonValue *value) { - return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_TRUE; + return cxJsonIsLiteral(value) && value->literal == CX_JSON_TRUE; } /** @@ -1032,7 +1123,7 @@ */ cx_attr_nonnull CX_INLINE bool cxJsonIsFalse(const CxJsonValue *value) { - return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_FALSE; + return cxJsonIsLiteral(value) && value->literal == CX_JSON_FALSE; } /** @@ -1045,7 +1136,7 @@ */ cx_attr_nonnull CX_INLINE bool cxJsonIsNull(const CxJsonValue *value) { - return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL; + return cxJsonIsLiteral(value) && value->literal == CX_JSON_NULL; } /** @@ -1123,7 +1214,7 @@ */ cx_attr_nonnull CX_INLINE bool cxJsonAsBool(const CxJsonValue *value) { - return value->value.literal == CX_JSON_TRUE; + return value->literal == CX_JSON_TRUE; } /** @@ -1137,7 +1228,7 @@ */ cx_attr_nonnull CX_INLINE size_t cxJsonArrSize(const CxJsonValue *value) { - return value->value.array.array_size; + return value->array.size; } /** @@ -1188,10 +1279,24 @@ CX_EXPORT CxIterator cxJsonArrIter(const CxJsonValue *value); /** - * Returns an iterator over the JSON object members. + * Returns the size of a JSON object. + * + * If the @p value is not a JSON object, the behavior is undefined. * - * The iterator yields values of type @c CxJsonObjValue* which - * contain the name and value of the member. + * @param value the JSON value + * @return the size of the object, i.e., the number of key/value pairs + * @see cxJsonIsObject() + */ +cx_attr_nonnull +CX_INLINE size_t cxJsonObjSize(const CxJsonValue *value) { + return cxCollectionSize(value->object); +} + +/** + * Returns a map iterator over the JSON object members. + * + * The iterator yields values of type @c CxMapEntry* which + * contain the name and the @c CxJsonObjValue* of the member. * * If the @p value is not a JSON object, the behavior is undefined. * @@ -1200,7 +1305,7 @@ * @see cxJsonIsObject() */ cx_attr_nonnull cx_attr_nodiscard -CX_EXPORT CxIterator cxJsonObjIter(const CxJsonValue *value); +CX_EXPORT CxMapIterator cxJsonObjIter(const CxJsonValue *value); /** * Internal function, do not use. @@ -1251,6 +1356,66 @@ */ #define cxJsonObjRemove(value, name) cx_json_obj_remove(value, cx_strcast(name)) +/** + * Performs a deep comparison of two JSON values. + * + * The order of object members is ignored during comparison. + * + * @param json the JSON value + * @param other the other JSON value that the JSON value is compared to + * @retval zero the values are equal (except for ordering of object members) + * @retval non-zero the values differ + */ +CX_EXPORT int cxJsonCompare(const CxJsonValue *json, const CxJsonValue *other); + + +/** + * Creates a deep copy of the specified JSON value. + * + * If you need a @c cx_clone_func compatible version, see cxJsonCloneFunc(). + * + * @note when you are cloning @c NULL, you will get a pointer to a statically + * allocated value which represents nothing. + * + * @param value the value to be cloned + * @param allocator the allocator for the new value + * @return the new value or @c NULL if any allocation was unsuccessful + * @see cxJsonCloneFunc() + */ +cx_attr_nodiscard +CX_EXPORT CxJsonValue* cxJsonClone(const CxJsonValue* value, + const CxAllocator* allocator); + + +/** + * A @c cx_clone_func compatible version of cxJsonClone(). + * + * Internal function - use cxJsonCloneFunc() to get a properly casted function pointer. + * + * @param target the target memory or @c NULL + * @param source the value to be cloned + * @param allocator the allocator for the new value + * @param data unused + * @return the new value or @c NULL if any allocation was unsuccessful + * @see cxJsonClone() + */ +cx_attr_nodiscard +CX_EXPORT CxJsonValue* cx_json_clone_func( + CxJsonValue* target, const CxJsonValue* source, + const CxAllocator* allocator, void *data); + +/** + * A @c cx_clone_func compatible version of cxJsonClone(). + * + * @param target (@c CxJsonValue*) the target memory or @c NULL + * @param source (@c CxJsonValue*) the value to be cloned + * @param allocator (@c CxAllocator*) the allocator for the new value + * @param data unused + * @return the new value or @c NULL if any allocation was unsuccessful + * @see cxJsonClone() + */ +#define cxJsonCloneFunc ((cx_clone_func) cx_json_clone_func) + #ifdef __cplusplus } #endif
--- a/ucx/cx/kv_list.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/kv_list.h Fri Dec 19 17:53:18 2025 +0100 @@ -49,7 +49,7 @@ * * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of * copies of the added elements, and the compare function will be automatically set - * to cx_cmp_ptr() if none is given. + * to cx_cmp_ptr(). * * After creating the list, it can also be used as a map after converting the pointer * to a CxMap pointer with cxKvListAsMap(). @@ -58,9 +58,6 @@ * * @param allocator the allocator for allocating the list nodes * (if @c NULL, the cxDefaultAllocator will be used) - * @param comparator the comparator for the elements - * (if @c NULL, and the list is not storing pointers, sort and find - * functions will not work) * @param elem_size the size of each element in bytes * @return the created list * @see cxKvListAsMap() @@ -68,14 +65,14 @@ */ cx_attr_nodiscard cx_attr_malloc cx_attr_dealloc(cxListFree, 1) CX_EXPORT CxList *cxKvListCreate(const CxAllocator *allocator, - cx_compare_func comparator, size_t elem_size); + size_t elem_size); /** * Allocates a linked list with a lookup-map for storing elements with @p elem_size bytes each. * * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of * copies of the added elements, and the compare function will be automatically set - * to cx_cmp_ptr() if none is given. + * to cx_cmp_ptr(). * * This function creates the list with cxKvListCreate() and immediately applies * cxKvListAsMap(). If you want to use the returned object as a list, you can call @@ -83,9 +80,6 @@ * * @param allocator the allocator for allocating the list nodes * (if @c NULL, the cxDefaultAllocator will be used) - * @param comparator the comparator for the elements - * (if @c NULL, and the list is not storing pointers, sort and find - * functions will not work) * @param elem_size the size of each element in bytes * @return the created list wrapped into the CxMap interface * @see cxKvListAsMap() @@ -93,52 +87,7 @@ */ cx_attr_nodiscard cx_attr_malloc cx_attr_dealloc(cxMapFree, 1) CX_EXPORT CxMap *cxKvListCreateAsMap(const CxAllocator *allocator, - cx_compare_func comparator, size_t elem_size); - -/** - * Allocates a linked list with a lookup-map for storing elements with @p elem_size bytes each. - * - * The list will use cxDefaultAllocator and no comparator function. If you want - * to call functions that need a comparator, you must either set one immediately - * after list creation or use cxKvListCreate(). - * - * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of - * copies of the added elements, and the compare function will be automatically set - * to cx_cmp_ptr(). - * - * After creating the list, it can also be used as a map after converting the pointer - * to a CxMap pointer with cxKvListAsMap(). - * When you want to use the list interface again, you can also convert the map pointer back - * with cxKvListAsList(). - * - * @param elem_size (@c size_t) the size of each element in bytes - * @return (@c CxList*) the created list - * @see cxKvListAsMap() - * @see cxKvListAsList() - */ -#define cxKvListCreateSimple(elem_size) cxKvListCreate(NULL, NULL, elem_size) - -/** - * Allocates a linked list with a lookup-map for storing elements with @p elem_size bytes each. - * - * The list will use cxDefaultAllocator and no comparator function. If you want - * to call functions that need a comparator, you must either set one immediately - * after list creation or use cxKvListCreate(). - * - * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of - * copies of the added elements, and the compare function will be automatically set - * to cx_cmp_ptr(). - * - * This macro behaves as if the list was created with cxKvListCreateSimple() and - * immediately followed up by cxKvListAsMap(). - * If you want to use the returned object as a list, you can call cxKvListAsList() later. - * - * @param elem_size (@c size_t) the size of each element in bytes - * @return (@c CxMap*) the created list wrapped into the CxMap interface - * @see cxKvListAsMap() - * @see cxKvListAsList() - */ -#define cxKvListCreateAsMapSimple(elem_size) cxKvListCreateAsMap(NULL, NULL, elem_size) + size_t elem_size); /** * Converts a map pointer belonging to a key-value-List back to the original list pointer. @@ -152,7 +101,7 @@ /** * Converts a map pointer belonging to a key-value-List back to the original list pointer. * - * @param list a list created by cxKvListCreate() or cxKvListCreateSimple() + * @param list a list created by cxKvListCreate() * @return a map pointer that lets you use the list as if it was a map */ cx_attr_nodiscard cx_attr_nonnull cx_attr_returns_nonnull
--- a/ucx/cx/linked_list.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/linked_list.h Fri Dec 19 17:53:18 2025 +0100 @@ -62,7 +62,14 @@ */ off_t loc_data; /** + * Location of extra data (optional). + * Negative when no extra data is requested. + * @see cx_linked_list_extra_data() + */ + off_t loc_extra; + /** * Additional bytes to allocate @em behind the payload (e.g. for metadata). + * @see cx_linked_list_extra_data() */ size_t extra_data_len; /** @@ -80,36 +87,33 @@ * * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of * copies of the added elements, and the compare function will be automatically set - * to cx_cmp_ptr() if none is given. + * to cx_cmp_ptr(). * * @param allocator the allocator for allocating the list nodes * (if @c NULL, the cxDefaultAllocator will be used) - * @param comparator the comparator for the elements - * (if @c NULL, and the list is not storing pointers, sort and find - * functions will not work) * @param elem_size the size of each element in bytes * @return the created list */ cx_attr_nodiscard cx_attr_malloc cx_attr_dealloc(cxListFree, 1) CX_EXPORT CxList *cxLinkedListCreate(const CxAllocator *allocator, - cx_compare_func comparator, size_t elem_size); + size_t elem_size); /** - * Allocates a linked list for storing elements with @p elem_size bytes each. + * Instructs the linked list to reserve extra data in each node. * - * The list will use cxDefaultAllocator and no comparator function. If you want - * to call functions that need a comparator, you must either set one immediately - * after list creation or use cxLinkedListCreate(). + * The extra data will be aligned and placed behind the element data. + * The exact location in the node is stored in the @c loc_extra field + * of the linked list. * - * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of - * copies of the added elements, and the compare function will be automatically set - * to cx_cmp_ptr(). + * You should usually not use this function except when you are creating an + * own linked-list implementation that is based on the UCX linked list and + * needs to store extra data in each node. * - * @param elem_size (@c size_t) the size of each element in bytes - * @return (@c CxList*) the created list + * @param list the list (must be a linked list) + * @param len the length of the extra data */ -#define cxLinkedListCreateSimple(elem_size) \ - cxLinkedListCreate(NULL, NULL, elem_size) +cx_attr_nonnull +CX_EXPORT void cx_linked_list_extra_data(cx_linked_list *list, size_t len); /** * Finds the node at a certain index. @@ -138,16 +142,34 @@ * @param start a pointer to the start node * @param loc_advance the location of the pointer to advance * @param loc_data the location of the @c data pointer within your node struct - * @param cmp_func a compare function to compare @p elem against the node data * @param elem a pointer to the element to find * @param found_index an optional pointer where the index of the found node * (given that @p start has index 0) is stored + * @param cmp_func a compare function to compare @p elem against the node data * @return a pointer to the found node or @c NULL if no matching node was found */ -cx_attr_nonnull_arg(1, 4, 5) +cx_attr_nonnull_arg(1, 4, 6) CX_EXPORT void *cx_linked_list_find(const void *start, ptrdiff_t loc_advance, - ptrdiff_t loc_data, cx_compare_func cmp_func, const void *elem, - size_t *found_index); + ptrdiff_t loc_data, const void *elem, size_t *found_index, + cx_compare_func cmp_func); + +/** + * Finds the node containing an element within a linked list. + * + * @param start a pointer to the start node + * @param loc_advance the location of the pointer to advance + * @param loc_data the location of the @c data pointer within your node struct + * @param elem a pointer to the element to find + * @param found_index an optional pointer where the index of the found node + * (given that @p start has index 0) is stored + * @param cmp_func a compare function to compare @p elem against the node data + * @param context additional context for the compare function + * @return a pointer to the found node or @c NULL if no matching node was found + */ +cx_attr_nonnull_arg(1, 4, 6) +CX_EXPORT void *cx_linked_list_find_c(const void *start, ptrdiff_t loc_advance, + ptrdiff_t loc_data, const void *elem, size_t *found_index, + cx_compare_func2 cmp_func, void *context); /** * Finds the first node in a linked list. @@ -370,6 +392,92 @@ ptrdiff_t loc_prev, ptrdiff_t loc_next, void *insert_begin, cx_compare_func cmp_func); /** + * Inserts a node into a sorted linked list. + * The new node must not be part of any list yet. + * + * If the list starting with the node pointed to by @p begin is not sorted + * already, the behavior is undefined. + * + * @param begin a pointer to the beginning node pointer (required) + * @param end a pointer to the end node pointer (if your list has one) + * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) + * @param loc_next the location of a @c next pointer within your node struct (required) + * @param new_node a pointer to the node that shall be inserted + * @param cmp_func a compare function that will receive the node pointers + * @param context context for the compare function + */ +cx_attr_nonnull_arg(1, 5, 6) +CX_EXPORT void cx_linked_list_insert_sorted_c(void **begin, void **end, + ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node, cx_compare_func2 cmp_func, void *context); + +/** + * Inserts a chain of nodes into a sorted linked list. + * The chain must not be part of any list yet. + * + * If either the list starting with the node pointed to by @p begin or the list + * starting with @p insert_begin is not sorted, the behavior is undefined. + * + * @attention In contrast to cx_linked_list_insert_chain(), the source chain + * will be broken and inserted into the target list so that the resulting list + * will be sorted according to @p cmp_func. That means each node in the source + * chain may be re-linked with nodes from the target list. + * + * @param begin a pointer to the beginning node pointer (required) + * @param end a pointer to the end node pointer (if your list has one) + * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) + * @param loc_next the location of a @c next pointer within your node struct (required) + * @param insert_begin a pointer to the first node of the chain that shall be inserted + * @param cmp_func a compare function that will receive the node pointers + * @param context context for the compare function + */ +cx_attr_nonnull_arg(1, 5, 6) +CX_EXPORT void cx_linked_list_insert_sorted_chain_c(void **begin, void **end, + ptrdiff_t loc_prev, ptrdiff_t loc_next, void *insert_begin, cx_compare_func2 cmp_func, void *context); + +/** + * Inserts a node into a sorted linked list if no other node with the same value already exists. + * The new node must not be part of any list yet. + * + * If the list starting with the node pointed to by @p begin is not sorted + * already, the behavior is undefined. + * + * @param begin a pointer to the beginning node pointer (required) + * @param end a pointer to the end node pointer (if your list has one) + * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) + * @param loc_next the location of a @c next pointer within your node struct (required) + * @param new_node a pointer to the node that shall be inserted + * @param cmp_func a compare function that will receive the node pointers + * @retval zero when the node was inserted + * @retval non-zero when a node with the same value already exists + */ +cx_attr_nonnull_arg(1, 5, 6) +CX_EXPORT int cx_linked_list_insert_unique_c(void **begin, void **end, + ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node, cx_compare_func2 cmp_func, void *context); + +/** + * Inserts a chain of nodes into a sorted linked list, avoiding duplicates. + * The chain must not be part of any list yet. + * + * If either the list starting with the node pointed to by @p begin or the list + * starting with @p insert_begin is not sorted, the behavior is undefined. + * + * @attention In contrast to cx_linked_list_insert_sorted(), not all nodes of the + * chain might be added. This function returns a new chain consisting of all the duplicates. + * + * @param begin a pointer to the beginning node pointer (required) + * @param end a pointer to the end node pointer (if your list has one) + * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) + * @param loc_next the location of a @c next pointer within your node struct (required) + * @param insert_begin a pointer to the first node of the chain that shall be inserted + * @param cmp_func a compare function that will receive the node pointers + * @param context context for the compare function + * @return a pointer to a new chain with all duplicates that were not inserted (or @c NULL when there were no duplicates) + */ +cx_attr_nonnull_arg(1, 5, 6) cx_attr_nodiscard +CX_EXPORT void *cx_linked_list_insert_unique_chain_c(void **begin, void **end, + ptrdiff_t loc_prev, ptrdiff_t loc_next, void *insert_begin, cx_compare_func2 cmp_func, void *context); + +/** * Removes a chain of nodes from the linked list. * * If one of the nodes to remove is the beginning (resp. end) node of the list, and if @p begin (resp. @p end) @@ -430,16 +538,6 @@ /** * Sorts a linked list based on a comparison function. * - * This function can work with linked lists of the following structure: - * @code - * typedef struct node node; - * struct node { - * node* prev; - * node* next; - * my_payload data; - * } - * @endcode - * * @note This is a recursive function with at most logarithmic recursion depth. * * @param begin a pointer to the beginning node pointer (required) @@ -453,6 +551,23 @@ CX_EXPORT void cx_linked_list_sort(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, ptrdiff_t loc_data, cx_compare_func cmp_func); +/** + * Sorts a linked list based on a comparison function. + * + * @note This is a recursive function with at most logarithmic recursion depth. + * + * @param begin a pointer to the beginning node pointer (required) + * @param end a pointer to the end node pointer (optional) + * @param loc_prev the location of a @c prev pointer within your node struct (negative if not present) + * @param loc_next the location of a @c next pointer within your node struct (required) + * @param loc_data the location of the @c data pointer within your node struct + * @param cmp_func the compare function defining the sort order + * @param context additional context for the compare function + */ +cx_attr_nonnull_arg(1, 6) +CX_EXPORT void cx_linked_list_sort_c(void **begin, void **end, + ptrdiff_t loc_prev, ptrdiff_t loc_next, ptrdiff_t loc_data, cx_compare_func2 cmp_func, void *context); + /** * Compares two lists element wise. @@ -472,6 +587,23 @@ ptrdiff_t loc_advance, ptrdiff_t loc_data, cx_compare_func cmp_func); /** + * Compares two lists element wise. + * + * @attention Both lists must have the same structure. + * + * @param begin_left the beginning of the left list (@c NULL denotes an empty list) + * @param begin_right the beginning of the right list (@c NULL denotes an empty list) + * @param loc_advance the location of the pointer to advance + * @param loc_data the location of the @c data pointer within your node struct + * @param cmp_func the function to compare the elements + * @return the first non-zero result of invoking @p cmp_func or: negative if the left list is smaller than the + * right list, positive if the left list is larger than the right list, zero if both lists are equal. + */ +cx_attr_nonnull_arg(5) +CX_EXPORT int cx_linked_list_compare_c(const void *begin_left, const void *begin_right, + ptrdiff_t loc_advance, ptrdiff_t loc_data, cx_compare_func2 cmp_func, void *context); + +/** * Reverses the order of the nodes in a linked list. * * @param begin a pointer to the beginning node pointer (required)
--- a/ucx/cx/list.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/list.h Fri Dec 19 17:53:18 2025 +0100 @@ -60,10 +60,6 @@ * The list class definition. */ const cx_list_class *cl; - /** - * The actual implementation in case the list class is delegating. - */ - const cx_list_class *climpl; }; /** @@ -170,6 +166,12 @@ void (*reverse)(struct cx_list_s *list); /** + * Optional member function for changing the capacity. + * If the list does not support overallocation, this can be set to @c NULL. + */ + int (*change_capacity)(struct cx_list_s *list, size_t new_capacity); + + /** * Member function for returning an iterator pointing to the specified index. */ struct cx_iterator_s (*iterator)(const struct cx_list_s *list, size_t index, bool backward); @@ -285,30 +287,26 @@ * The purpose of this function is to be called in the initialization code * of your list to set certain members correctly. * - * This is particularly important when you want your list to support - * #CX_STORE_POINTERS as @p elem_size. This function will wrap the list - * class accordingly and make sure that you can implement your list as if - * it was only storing objects, and the wrapper will automatically enable - * the feature of storing pointers. + * This is particularly useful when you want your list to support + * #CX_STORE_POINTERS as @p elem_size. * * @par Example * * @code * CxList *myCustomListCreate( * const CxAllocator *allocator, - * cx_compare_func comparator, * size_t elem_size * ) { * if (allocator == NULL) { * allocator = cxDefaultAllocator; * } * - * MyCustomList *list = cxCalloc(allocator, 1, sizeof(MyCustomList)); + * MyCustomList *list = cxZalloc(allocator, sizeof(MyCustomList)); * if (list == NULL) return NULL; * * // initialize * cx_list_init((CxList*)list, &my_custom_list_class, - * allocator, comparator, elem_size); + * allocator, elem_size); * * // ... some more custom stuff ... * @@ -319,13 +317,24 @@ * @param list the list to initialize * @param cl the list class * @param allocator the allocator for the elements - * @param comparator a compare function for the elements * @param elem_size the size of one element */ cx_attr_nonnull_arg(1, 2, 3) CX_EXPORT void cx_list_init(struct cx_list_s *list, struct cx_list_class_s *cl, const struct cx_allocator_s *allocator, - cx_compare_func comparator, size_t elem_size); + size_t elem_size); + +/** + * A @c cx_compare_func2 compatible wrapper for the compare functions of a list. + * + * @param left first element + * @param right second element + * @param list the list which is comparing the elements + * @return the comparison result + */ +cx_attr_nonnull +CX_EXPORT int cx_list_compare_wrapper( + const void *left, const void *right, void *list); /** * Returns the number of elements currently stored in the list. @@ -978,7 +987,7 @@ * @param data optional additional data that is passed to the clone function * @retval zero when all elements were successfully cloned * @retval non-zero when an allocation error occurred - * @see cxListUnion() + * @see cxListCloneShallow() */ cx_attr_nonnull_arg(1, 2, 3) CX_EXPORT int cxListClone(CxList *dst, const CxList *src, @@ -1001,6 +1010,7 @@ * @param data optional additional data that is passed to the clone function * @retval zero when the elements were successfully cloned * @retval non-zero when an allocation error occurred + * @see cxListDifferenceShallow() */ cx_attr_nonnull_arg(1, 2, 3, 4) CX_EXPORT int cxListDifference(CxList *dst, @@ -1024,6 +1034,7 @@ * @param data optional additional data that is passed to the clone function * @retval zero when the elements were successfully cloned * @retval non-zero when an allocation error occurred + * @see cxListIntersectionShallow() */ cx_attr_nonnull_arg(1, 2, 3, 4) CX_EXPORT int cxListIntersection(CxList *dst, const CxList *src, const CxList *other, @@ -1048,12 +1059,136 @@ * @param data optional additional data that is passed to the clone function * @retval zero when the elements were successfully cloned * @retval non-zero when an allocation error occurred - * @see cxListClone() + * @see cxListUnionShallow() */ cx_attr_nonnull_arg(1, 2, 3, 4) CX_EXPORT int cxListUnion(CxList *dst, const CxList *src, const CxList *other, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); +/** + * Performs a shallow clone of one list into another. + * + * This function uses the default allocator, if needed, and performs + * shallow clones with @c memcpy(). + * + * If the destination list already contains elements, the cloned elements + * are appended to that list. + * + * @attention If the cloned elements need to be destroyed by a destructor + * function, you must make sure that the destination list also uses this + * destructor function. + * + * @param dst the destination list + * @param src the source list + * @retval zero when all elements were successfully cloned + * @retval non-zero when an allocation error occurred + * @see cxListClone() + */ +cx_attr_nonnull +CX_EXPORT int cxListCloneShallow(CxList *dst, const CxList *src); + +/** + * Clones elements from a list only if they are not present in another list. + * + * This function uses the default allocator, if needed, and performs + * shallow clones with @c memcpy(). + * + * If the @p minuend does not contain duplicates, this is equivalent to adding + * the set difference to the destination list. + * + * This function is optimized for the case when both the @p minuend and the + * @p subtrahend are sorted. + * + * @param dst the destination list + * @param minuend the list to subtract elements from + * @param subtrahend the elements that shall be subtracted + * @retval zero when the elements were successfully cloned + * @retval non-zero when an allocation error occurred + * @see cxListDifference() + */ +cx_attr_nonnull +CX_EXPORT int cxListDifferenceShallow(CxList *dst, + const CxList *minuend, const CxList *subtrahend); + +/** + * Clones elements from a list only if they are also present in another list. + * + * This function uses the default allocator, if needed, and performs + * shallow clones with @c memcpy(). + * + * This function is optimized for the case when both the @p src and the + * @p other list are sorted. + * + * If the destination list already contains elements, the intersection is appended + * to that list. + * + * @param dst the destination list + * @param src the list to clone the elements from + * @param other the list to check the elements for existence + * @retval zero when the elements were successfully cloned + * @retval non-zero when an allocation error occurred + * @see cxListIntersection() + */ +cx_attr_nonnull +CX_EXPORT int cxListIntersectionShallow(CxList *dst, const CxList *src, const CxList *other); + +/** + * Performs a deep clone of one list into another, skipping duplicates. + * + * This function uses the default allocator, if needed, and performs + * shallow clones with @c memcpy(). + * + * This function is optimized for the case when both the @p src and the + * @p other list are sorted. + * In that case, the union will also be sorted. + * + * If the destination list already contains elements, the union is appended + * to that list. In that case the destination is not necessarily sorted. + * + * @param dst the destination list + * @param src the primary source list + * @param other the other list, where elements are only cloned from + * when they are not in @p src + * @retval zero when the elements were successfully cloned + * @retval non-zero when an allocation error occurred + * @see cxListUnion() + */ +cx_attr_nonnull +CX_EXPORT int cxListUnionShallow(CxList *dst, const CxList *src, const CxList *other); + +/** + * Asks the list to reserve enough memory for a given total number of elements. + * + * List implementations are free to choose if reserving memory upfront makes + * sense. + * For example, array-based implementations usually do support reserving memory + * for additional elements while linked lists usually don't. + * + * @note When the requested capacity is smaller than the current size, + * this function returns zero without performing any action. + * + * @param list the list + * @param capacity the expected total number of elements + * @retval zero on success or when overallocation is not supported + * @retval non-zero when an allocation error occurred + * @see cxListShrink() + */ +cx_attr_nonnull +CX_EXPORT int cxListReserve(CxList *list, size_t capacity); + +/** + * Advises the list to free any overallocated memory. + * + * Lists that do not support overallocation simply return zero. + * + * This function usually returns zero, except for very special and custom + * list and/or allocator implementations where freeing memory can fail. + * + * @param list the list + * @return usually zero + */ +cx_attr_nonnull +CX_EXPORT int cxListShrink(CxList *list); #ifdef __cplusplus } // extern "C"
--- a/ucx/cx/map.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/map.h Fri Dec 19 17:53:18 2025 +0100 @@ -183,9 +183,9 @@ * Add or overwrite an element. * If the @p value is @c NULL, the implementation * shall only allocate memory instead of adding an existing value to the map. - * Returns a pointer to the allocated memory or @c NULL if allocation fails. + * Returns a map entry where the pointer to the key is @c NULL if allocation fails. */ - void *(*put)(CxMap *map, CxHashKey key, void *value); + CxMapEntry (*put)(CxMap *map, CxHashKey key, void *value); /** * Returns an element. @@ -357,8 +357,6 @@ * @param map the map * @param key the key * @return the pointer to the allocated memory or @c NULL if allocation fails - * @retval zero success - * @retval non-zero value on memory allocation failure * @see cxMapEmplace() */ cx_attr_nonnull @@ -379,8 +377,6 @@ * @param map (@c CxMap*) the map * @param key (any supported key type) the key * @return the pointer to the allocated memory or @c NULL if allocation fails - * @retval zero success - * @retval non-zero value on memory allocation failure * @see CX_HASH_KEY() */ #define cxMapEmplace(map, key) cx_map_emplace(map, CX_HASH_KEY(key)) @@ -603,6 +599,139 @@ CX_EXPORT int cxMapUnion(CxMap *dst, const CxMap *src, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data); +/** + * Performs a shallow clone of one map into another. + * + * This function uses the default allocator, if needed, and performs + * shallow clones with @c memcpy(). + * + * If the destination map already contains entries, the cloned entries + * are added to that map, possibly overwriting existing elements when + * the keys already exist. + * + * When elements in the destination map need to be replaced, any destructor + * function is called on the replaced elements before replacing them. + * + * @attention If the cloned elements need to be destroyed by a destructor + * function, you must make sure that the destination map also uses this + * destructor function. + * + * @param dst the destination map + * @param src the source map + * @retval zero when all elements were successfully cloned + * @retval non-zero when an allocation error occurred + * @see cxMapClone() + */ +cx_attr_nonnull +CX_EXPORT int cxMapCloneShallow(CxMap *dst, const CxMap *src); + +/** + * Clones entries of a map if their key is not present in another map. + * + * This function uses the default allocator, if needed, and performs + * shallow clones with @c memcpy(). + * + * @param dst the destination map + * @param minuend the map to subtract the entries from + * @param subtrahend the map containing the elements to be subtracted + * @retval zero when the elements were successfully cloned + * @retval non-zero when an allocation error occurred + */ +cx_attr_nonnull +CX_EXPORT int cxMapDifferenceShallow(CxMap *dst, const CxMap *minuend, const CxMap *subtrahend); + +/** + * Clones entries of a map if their key is not present in a list. + * + * This function uses the default allocator, if needed, and performs + * shallow clones with @c memcpy(). + * + * Note that the list must contain keys of type @c CxKey + * (or pointers to such keys) and must use @c cx_hash_key_cmp + * as the compare function. + * Generic key types cannot be processed in this case. + * + * @param dst the destination map + * @param src the source map + * @param keys the list of @c CxKey items + * @retval zero when the elements were successfully cloned + * @retval non-zero when an allocation error occurred + * @see cxMapListDifference() + */ +cx_attr_nonnull +CX_EXPORT int cxMapListDifferenceShallow(CxMap *dst, const CxMap *src, const CxList *keys); + + +/** + * Clones entries of a map only if their key is present in another map. + * + * This function uses the default allocator, if needed, and performs + * shallow clones with @c memcpy(). + * + * @param dst the destination map + * @param src the map to clone the entries from + * @param other the map to check for existence of the keys + * @retval zero when the elements were successfully cloned + * @retval non-zero when an allocation error occurred + */ +cx_attr_nonnull +CX_EXPORT int cxMapIntersectionShallow(CxMap *dst, const CxMap *src, const CxMap *other); + +/** + * Clones entries of a map only if their key is present in a list. + * + * This function uses the default allocator, if needed, and performs + * shallow clones with @c memcpy(). + * + * Note that the list must contain keys of type @c CxKey + * (or pointers to such keys) and must use @c cx_hash_key_cmp + * as the compare function. + * Generic key types cannot be processed in this case. + * + * @param dst the destination map + * @param src the source map + * @param keys the list of @c CxKey items + * @retval zero when the elements were successfully cloned + * @retval non-zero when an allocation error occurred + */ +cx_attr_nonnull +CX_EXPORT int cxMapListIntersectionShallow(CxMap *dst, const CxMap *src, const CxList *keys); + +/** + * Clones entries into a map if their key does not exist yet. + * + * This function uses the default allocator, if needed, and performs + * shallow clones with @c memcpy(). + * + * If you want to calculate the union of two maps into a fresh new map, + * you can proceed as follows: + * 1. Clone the first map into a fresh, empty map. + * 2. Use this function to clone the second map into the result from step 1. + * + * @param dst the destination map + * @param src the map to clone the entries from + * @retval zero when the elements were successfully cloned + * @retval non-zero when an allocation error occurred + */ +cx_attr_nonnull +CX_EXPORT int cxMapUnionShallow(CxMap *dst, const CxMap *src); + + +/** + * Compares the entries of two maps. + * + * @param map the map + * @param other the other map that the first map is compared to + * @retval zero when both maps have the same key sets + * and the values are pairwise equivalent + * @retval negative when the first @p map has fewer keys than the @p other map + * @retval positive when the first @p map has more keys than the @p other map + * @retval non-zero (unspecified whether positive or negative) when the size + * of both maps is equal but a key or a value is different + */ +cx_attr_nonnull +CX_EXPORT int cxMapCompare(const CxMap *map, const CxMap *other); + #ifdef __cplusplus } // extern "C" #endif
--- a/ucx/cx/properties.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/properties.h Fri Dec 19 17:53:18 2025 +0100 @@ -41,9 +41,6 @@ #include "map.h" #include "buffer.h" -#include <stdio.h> -#include <string.h> - #ifdef __cplusplus extern "C" { #endif @@ -76,13 +73,10 @@ */ char comment3; - /* + /** * The character, when appearing at the end of a line, continues that line. * This is '\' by default. */ - /** - * Reserved for future use. - */ char continuation; }; @@ -141,23 +135,16 @@ */ CX_PROPERTIES_BUFFER_ALLOC_FAILED, /** - * Initializing the properties source failed. - * - * @see cx_properties_read_init_func + * A file operation failed. + * Only for cxPropertiesLoad(). + * It is system-specific if errno is set. */ - CX_PROPERTIES_READ_INIT_FAILED, + CX_PROPERTIES_FILE_ERROR, /** - * Reading from a properties source failed. - * - * @see cx_properties_read_func + * A map operation failed. + * Only for cxPropertiesLoad(). */ - CX_PROPERTIES_READ_FAILED, - /** - * Sinking a k/v-pair failed. - * - * @see cx_properties_sink_func - */ - CX_PROPERTIES_SINK_FAILED, + CX_PROPERTIES_MAP_ERROR, }; /** @@ -190,134 +177,6 @@ */ typedef struct cx_properties_s CxProperties; - -/** - * Typedef for a properties sink. - */ -typedef struct cx_properties_sink_s CxPropertiesSink; - -/** - * A function that consumes a k/v-pair in a sink. - * - * The sink could be a map, and the sink function would be calling - * a map function to store the k/v-pair. - * - * @param prop the properties interface that wants to sink a k/v-pair - * @param sink the sink - * @param key the key - * @param value the value - * @retval zero success - * @retval non-zero sinking the k/v-pair failed - */ -typedef int(*cx_properties_sink_func)( - CxProperties *prop, - CxPropertiesSink *sink, - cxstring key, - cxstring value -); - -/** - * Defines a sink for k/v-pairs. - */ -struct cx_properties_sink_s { - /** - * The sink object. - */ - void *sink; - /** - * Optional custom data. - */ - void *data; - /** - * A function for consuming k/v-pairs into the sink. - */ - cx_properties_sink_func sink_func; -}; - - -/** - * Typedef for a properties source. - */ -typedef struct cx_properties_source_s CxPropertiesSource; - -/** - * A function that reads data from a source. - * - * When the source is depleted, implementations SHALL provide an empty - * string in the @p target and return zero. - * A non-zero return value is only permitted in case of an error. - * - * The meaning of the optional parameters is implementation-dependent. - * - * @param prop the properties interface that wants to read from the source - * @param src the source - * @param target a string buffer where the read data shall be stored - * @retval zero success - * @retval non-zero reading the data failed - */ -typedef int(*cx_properties_read_func)( - CxProperties *prop, - CxPropertiesSource *src, - cxstring *target -); - -/** - * A function that may initialize additional memory for the source. - * - * @param prop the properties interface that wants to read from the source - * @param src the source - * @retval zero initialization was successful - * @retval non-zero otherwise - */ -typedef int(*cx_properties_read_init_func)( - CxProperties *prop, - CxPropertiesSource *src -); - -/** - * A function that cleans memory initialized by the read_init_func. - * - * @param prop the properties interface that wants to read from the source - * @param src the source - */ -typedef void(*cx_properties_read_clean_func)( - CxProperties *prop, - CxPropertiesSource *src -); - -/** - * Defines a properties source. - */ -struct cx_properties_source_s { - /** - * The source object. - * - * For example, a file stream or a string. - */ - void *src; - /** - * Optional additional data pointer. - */ - void *data_ptr; - /** - * Optional size information. - */ - size_t data_size; - /** - * A function that reads data from the source. - */ - cx_properties_read_func read_func; - /** - * Optional function that may prepare the source for reading data. - */ - cx_properties_read_init_func read_init_func; - /** - * Optional function that cleans additional memory allocated by the - * read_init_func. - */ - cx_properties_read_clean_func read_clean_func; -}; - /** * Initialize a properties interface. * @@ -465,100 +324,83 @@ CX_EXPORT CxPropertiesStatus cxPropertiesNext(CxProperties *prop, cxstring *key, cxstring *value); /** - * Creates a properties sink for an UCX map. - * - * The values stored in the map will be pointers to freshly allocated, - * zero-terminated C strings (@c char*), which means the @p map should have been - * created with #CX_STORE_POINTERS. - * - * The cxDefaultAllocator will be used unless you specify a custom - * allocator in the optional @c data field of the returned sink. - * - * @param map the map that shall consume the k/v-pairs. - * @return the sink - * @see cxPropertiesLoad() + * The size of the stack memory that `cxPropertiesLoad()` will reserve with `cxPropertiesUseStack()`. */ -cx_attr_nonnull cx_attr_nodiscard -CX_EXPORT CxPropertiesSink cxPropertiesMapSink(CxMap *map); +CX_EXPORT extern const unsigned cx_properties_load_buf_size; /** - * Creates a properties source based on an UCX string. - * - * @param str the string - * @return the properties source - * @see cxPropertiesLoad() + * The size of the stack memory that `cxPropertiesLoad()` will use to read contents from the file. */ -cx_attr_nodiscard -CX_EXPORT CxPropertiesSource cxPropertiesStringSource(cxstring str); - -/** - * Creates a properties source based on C string with the specified length. - * - * @param str the string - * @param len the length - * @return the properties source - * @see cxPropertiesLoad() - */ -cx_attr_nonnull cx_attr_nodiscard cx_attr_access_r(1, 2) -CX_EXPORT CxPropertiesSource cxPropertiesCstrnSource(const char *str, size_t len); +CX_EXPORT extern const unsigned cx_properties_load_fill_size; /** - * Creates a properties source based on a C string. - * - * The length will be determined with strlen(), so the string MUST be - * zero-terminated. + * Internal function - use cxPropertiesLoad() instead. * - * @param str the string - * @return the properties source - * @see cxPropertiesLoad() + * @param allocator the allocator for the values + * @param filename the file name + * @param target the target map + * @param config the parser config + * @return status code */ -cx_attr_nonnull cx_attr_nodiscard cx_attr_cstr_arg(1) -CX_EXPORT CxPropertiesSource cxPropertiesCstrSource(const char *str); +cx_attr_nonnull_arg(3) +CX_EXPORT CxPropertiesStatus cx_properties_load(const CxAllocator *allocator, + cxstring filename, CxMap *target, CxPropertiesConfig config); /** - * Creates a properties source based on an FILE. - * - * @param file the file - * @param chunk_size how many bytes may be read in one operation + * Loads properties from a file and inserts them into a map. * - * @return the properties source - * @see cxPropertiesLoad() - */ -cx_attr_nonnull cx_attr_nodiscard cx_attr_access_r(1) -CX_EXPORT CxPropertiesSource cxPropertiesFileSource(FILE *file, size_t chunk_size); - - -/** - * Loads properties data from a source and transfers it to a sink. + * Entries are added to the map, possibly overwriting existing entries. + * + * The map must either store pointers of type @c char*, or elements of type cxmutstr. + * Any other configuration is not supported. * - * This function tries to read as much data from the source as possible. - * When the source was completely consumed and at least on k/v-pair was found, - * the return value will be #CX_PROPERTIES_NO_ERROR. - * When the source was consumed but no k/v-pairs were found, the return value - * will be #CX_PROPERTIES_NO_DATA. - * In case the source data ends unexpectedly, the #CX_PROPERTIES_INCOMPLETE_DATA - * is returned. In that case you should call this function again with the same - * sink and either an updated source or the same source if the source is able to - * yield the missing data. + * @note When the parser finds an error, all successfully parsed keys before the error + * are added to the map nonetheless. * - * The other result codes apply, according to their description. - * - * @param prop the properties interface - * @param sink the sink - * @param source the source - * @retval CX_PROPERTIES_NO_ERROR (zero) a key/value pair was found - * @retval CX_PROPERTIES_READ_INIT_FAILED initializing the source failed - * @retval CX_PROPERTIES_READ_FAILED reading from the source failed - * @retval CX_PROPERTIES_SINK_FAILED sinking the properties into the sink failed - * @retval CX_PROPERTIES_NO_DATA the source did not provide any key/value pairs - * @retval CX_PROPERTIES_INCOMPLETE_DATA the source did not provide enough data + * @param allocator the allocator for the values that will be stored in the map + * @param filename (any string) the absolute or relative path to the file + * @param target (@c CxMap*) the map where the properties shall be added + * @param config the parser config + * @retval CX_PROPERTIES_NO_ERROR (zero) at least one key/value pair was found + * @retval CX_PROPERTIES_NO_DATA the file is syntactically OK, but does not contain properties + * @retval CX_PROPERTIES_INCOMPLETE_DATA unexpected end of file * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed + * @retval CX_PROPERTIES_FILE_ERROR a file operation failed; depending on the system @c errno might be set + * @retval CX_PROPERTIES_MAP_ERROR storing a key/value pair in the map failed + * @see cxPropertiesLoadDefault() */ -cx_attr_nonnull -CX_EXPORT CxPropertiesStatus cxPropertiesLoad(CxProperties *prop, - CxPropertiesSink sink, CxPropertiesSource source); +#define cxPropertiesLoad(allocator, filename, target, config) \ + cx_properties_load(allocator, cx_strcast(filename), target, config) + +/** + * Loads properties from a file and inserts them into a map with a default config. + * + * Entries are added to the map, possibly overwriting existing entries. + * + * The map must either store pointers of type @c char*, or elements of type cxmutstr. + * Any other configuration is not supported. + * + * @note When the parser finds an error, all successfully parsed keys before the error + * are added to the map nonetheless. + * + * @param allocator the allocator for the values that will be stored in the map + * @param filename (any string) the absolute or relative path to the file + * @param target (@c CxMap*) the map where the properties shall be added + * @retval CX_PROPERTIES_NO_ERROR (zero) at least one key/value pair was found + * @retval CX_PROPERTIES_NO_DATA the file is syntactically OK, but does not contain properties + * @retval CX_PROPERTIES_INCOMPLETE_DATA unexpected end of file + * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key + * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter + * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed + * @retval CX_PROPERTIES_FILE_ERROR a file operation failed; depending on the system @c errno might be set + * @retval CX_PROPERTIES_MAP_ERROR storing a key/value pair in the map failed + * @see cxPropertiesLoad() + */ +#define cxPropertiesLoadDefault(allocator, filename, target) \ + cx_properties_load(allocator, cx_strcast(filename), target, cx_properties_config_default) + #ifdef __cplusplus } // extern "C"
--- a/ucx/cx/string.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/string.h Fri Dec 19 17:53:18 2025 +0100 @@ -39,6 +39,8 @@ #include "common.h" #include "allocator.h" +#include <string.h> + /** Expands a UCX string as printf arguments. */ #define CX_SFMT(s) (int) (s).length, (s).ptr @@ -137,30 +139,6 @@ */ typedef struct cx_strtok_ctx_s CxStrtokCtx; -#ifdef __cplusplus -extern "C" { - -/** - * A literal initializer for an UCX string structure. - * - * @param literal the string literal - */ -#define CX_STR(literal) cxstring{literal, sizeof(literal) - 1} - -#else // __cplusplus - -/** - * A literal initializer for an UCX string structure. - * - * The argument MUST be a string (const char*) @em literal. - * - * @param literal the string literal - */ -#define CX_STR(literal) ((cxstring){literal, sizeof(literal) - 1}) - -#endif - - /** * Wraps a mutable string that must be zero-terminated. * @@ -179,7 +157,12 @@ * @see cx_mutstrn() */ cx_attr_nodiscard cx_attr_cstr_arg(1) -CX_EXPORT cxmutstr cx_mutstr(char *cstring); +CX_INLINE cxmutstr cx_mutstr(char *cstring) { + cxmutstr str; + str.ptr = cstring; + str.length = cstring == NULL ? 0 : strlen(cstring); + return str; +} /** * Wraps a string that does not need to be zero-terminated. @@ -198,7 +181,12 @@ * @see cx_mutstr() */ cx_attr_nodiscard cx_attr_access_rw(1, 2) -CX_EXPORT cxmutstr cx_mutstrn(char *cstring, size_t length); +CX_INLINE cxmutstr cx_mutstrn(char *cstring, size_t length) { + cxmutstr str; + str.ptr = cstring; + str.length = length; + return str; +} /** * Wraps a string that must be zero-terminated. @@ -218,7 +206,12 @@ * @see cx_strn() */ cx_attr_nodiscard cx_attr_cstr_arg(1) -CX_EXPORT cxstring cx_str(const char *cstring); +CX_INLINE cxstring cx_str(const char *cstring) { + cxstring str; + str.ptr = cstring; + str.length = cstring == NULL ? 0 : strlen(cstring); + return str; +} /** @@ -238,10 +231,14 @@ * @see cx_str() */ cx_attr_nodiscard cx_attr_access_r(1, 2) -CX_EXPORT cxstring cx_strn(const char *cstring, size_t length); +CX_INLINE cxstring cx_strn(const char *cstring, size_t length) { + cxstring str; + str.ptr = cstring; + str.length = length; + return str; +} #ifdef __cplusplus -} // extern "C" cx_attr_nodiscard CX_CPPDECL cxstring cx_strcast(cxmutstr str) { return cx_strn(str.ptr, str.length); @@ -256,7 +253,7 @@ } cx_attr_nodiscard CX_CPPDECL cxstring cx_strcast(const unsigned char *str) { - return cx_str(static_cast<const char*>(str)); + return cx_str(reinterpret_cast<const char*>(str)); } extern "C" { #else
--- a/ucx/cx/tree.h Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/cx/tree.h Fri Dec 19 17:53:18 2025 +0100 @@ -88,6 +88,7 @@ ptrdiff_t loc_next; /** * The total number of distinct nodes that have been passed so far. + * This includes the current node. */ size_t counter; /** @@ -185,6 +186,7 @@ ptrdiff_t loc_next; /** * The total number of distinct nodes that have been passed so far. + * This includes the currently visited node. */ size_t counter; /** @@ -641,17 +643,13 @@ * Structure for holding the base data of a tree. */ struct cx_tree_s { + CX_COLLECTION_BASE; /** * The tree class definition. */ const cx_tree_class *cl; /** - * Allocator to allocate new nodes. - */ - const CxAllocator *allocator; - - /** * A pointer to the root node. * * Will be @c NULL when @c size is 0. @@ -669,21 +667,6 @@ cx_tree_node_create_func node_create; /** - * An optional simple destructor for the tree nodes. - */ - cx_destructor_func simple_destructor; - - /** - * An optional advanced destructor for the tree nodes. - */ - cx_destructor_func2 advanced_destructor; - - /** - * The pointer to additional data that is passed to the advanced destructor. - */ - void *destructor_data; - - /** * A function to compare two nodes. */ cx_tree_search_func search; @@ -694,11 +677,6 @@ cx_tree_search_data_func search_data; /** - * The number of currently stored elements. - */ - size_t size; - - /** * Offset in the node struct for the parent pointer. */ ptrdiff_t loc_parent; @@ -1094,7 +1072,7 @@ * @see cxTreeIterate() */ cx_attr_nonnull cx_attr_nodiscard -CxTreeVisitor cxTreeVisit(CxTree *tree); +CX_EXPORT CxTreeVisitor cxTreeVisit(CxTree *tree); /** * Sets the (new) parent of the specified child.
--- a/ucx/hash_map.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/hash_map.c Fri Dec 19 17:53:18 2025 +0100 @@ -78,7 +78,7 @@ cxFree(map->collection.allocator, map); } -static void *cx_hash_map_put( +static CxMapEntry cx_hash_map_put( CxMap *map, CxHashKey key, void *value @@ -117,7 +117,7 @@ allocator, sizeof(struct cx_hash_map_element_s) + map->collection.elem_size ); - if (e == NULL) return NULL; + if (e == NULL) return (CxMapEntry){NULL, NULL}; // LCOV_EXCL_LINE // write the value if (value == NULL) { @@ -130,10 +130,10 @@ // copy the key void *kd = cxMalloc(allocator, key.len); - if (kd == NULL) { + if (kd == NULL) { // LCOV_EXCL_START cxFree(allocator, e); - return NULL; - } + return (CxMapEntry){NULL, NULL}; + } // LCOV_EXCL_STOP memcpy(kd, key.data, key.len); e->key.data = kd; e->key.len = key.len; @@ -152,8 +152,8 @@ map->collection.size++; } - // return pointer to the element - return elm->data; + // return the entry + return (CxMapEntry){&elm->key, elm->data}; } static void cx_hash_map_unlink( @@ -414,8 +414,7 @@ buckets = 16; } - struct cx_hash_map_s *map = cxCalloc(allocator, 1, - sizeof(struct cx_hash_map_s)); + struct cx_hash_map_s *map = cxZalloc(allocator, sizeof(struct cx_hash_map_s)); if (map == NULL) return NULL; // initialize hash map members @@ -433,9 +432,12 @@ if (itemsize > 0) { map->base.collection.elem_size = itemsize; + map->base.collection.advanced_cmp = cx_ccmp_memcmp; + map->base.collection.cmp_data = &map->base.collection.elem_size; } else { map->base.collection.elem_size = sizeof(void *); map->base.collection.store_pointer = true; + map->base.collection.simple_cmp = cx_cmp_ptr; } return (CxMap *) map; @@ -447,15 +449,16 @@ size_t new_bucket_count = (map->collection.size * 5) >> 1; if (new_bucket_count < hash_map->bucket_count) { + // LCOV_EXCL_START errno = EOVERFLOW; return 1; - } + } // LCOV_EXCL_STOP struct cx_hash_map_element_s **new_buckets = cxCalloc( map->collection.allocator, new_bucket_count, sizeof(struct cx_hash_map_element_s *) ); - if (new_buckets == NULL) return 1; + if (new_buckets == NULL) return 1; // LCOV_EXCL_LINE // iterate through the elements and assign them to their new slots for (size_t slot = 0; slot < hash_map->bucket_count; slot++) {
--- a/ucx/iterator.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/iterator.c Fri Dec 19 17:53:18 2025 +0100 @@ -29,6 +29,7 @@ #include "cx/iterator.h" #include <string.h> +#include <assert.h> static bool cx_iter_valid(const void *it) { const struct cx_iterator_s *iter = it; @@ -45,51 +46,14 @@ return *(void**)iter->elem_handle; } -static void cx_iter_next_fast(void *it) { +static void cx_iter_next(void *it) { struct cx_iterator_s *iter = it; - if (iter->base.remove) { - iter->base.remove = false; - iter->elem_count--; - // only move the last element when we are not currently aiming - // at the last element already - if (iter->index < iter->elem_count) { - void *last = ((char *) iter->src_handle) - + iter->elem_count * iter->elem_size; - memcpy(iter->elem_handle, last, iter->elem_size); - } - } else { - iter->index++; - iter->elem_handle = ((char *) iter->elem_handle) + iter->elem_size; - } + assert(!iter->base.remove); + iter->index++; + iter->elem_handle = ((char *) iter->elem_handle) + iter->elem_size; } -static void cx_iter_next_slow(void *it) { - struct cx_iterator_s *iter = it; - if (iter->base.remove) { - iter->base.remove = false; - iter->elem_count--; - - // number of elements to move - size_t remaining = iter->elem_count - iter->index; - if (remaining > 0) { - memmove( - iter->elem_handle, - ((char *) iter->elem_handle) + iter->elem_size, - remaining * iter->elem_size - ); - } - } else { - iter->index++; - iter->elem_handle = ((char *) iter->elem_handle) + iter->elem_size; - } -} - -CxIterator cxIterator( - const void *array, - size_t elem_size, - size_t elem_count, - bool remove_keeps_order -) { +CxIterator cxIterator(const void *array, size_t elem_size, size_t elem_count) { CxIterator iter; iter.index = 0; @@ -99,19 +63,18 @@ iter.elem_count = array == NULL ? 0 : elem_count; iter.base.valid = cx_iter_valid; iter.base.current = cx_iter_current; - iter.base.next = remove_keeps_order ? cx_iter_next_slow : cx_iter_next_fast; + iter.base.next = cx_iter_next; + iter.base.valid_impl = NULL; + iter.base.current_impl = NULL; + iter.base.next_impl = NULL; iter.base.remove = false; iter.base.allow_remove = true; return iter; } -CxIterator cxIteratorPtr( - const void *array, - size_t elem_count, - bool remove_keeps_order -) { - CxIterator iter = cxIterator(array, sizeof(void*), elem_count, remove_keeps_order); +CxIterator cxIteratorPtr(const void *array, size_t elem_count) { + CxIterator iter = cxIterator(array, sizeof(void*), elem_count); iter.base.current = cx_iter_current_ptr; return iter; }
--- a/ucx/json.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/json.c Fri Dec 19 17:53:18 2025 +0100 @@ -27,6 +27,7 @@ */ #include "cx/json.h" +#include "cx/kv_list.h" #include <string.h> #include <assert.h> @@ -41,90 +42,10 @@ static CxJsonValue cx_json_value_nothing = {.type = CX_JSON_NOTHING}; -static int json_cmp_objvalue(const void *l, const void *r) { - const CxJsonObjValue *left = l; - const CxJsonObjValue *right = r; - return cx_strcmp(cx_strcast(left->name), cx_strcast(right->name)); -} - -static size_t json_find_objvalue(const CxJsonValue *obj, cxstring name) { - assert(obj->type == CX_JSON_OBJECT); - CxJsonObjValue kv_dummy; - kv_dummy.name = cx_mutstrn((char*) name.ptr, name.length); - return cx_array_binary_search( - obj->value.object.values, - obj->value.object.values_size, - sizeof(CxJsonObjValue), - &kv_dummy, - json_cmp_objvalue - ); -} - -static int json_add_objvalue(CxJsonValue *objv, CxJsonObjValue member) { - assert(objv->type == CX_JSON_OBJECT); - const CxAllocator * const al = objv->allocator; - CxJsonObject *obj = &(objv->value.object); - - // determine the index where we need to insert the new member - size_t index = cx_array_binary_search_sup( - obj->values, - obj->values_size, - sizeof(CxJsonObjValue), - &member, json_cmp_objvalue - ); - - // is the name already present? - if (index < obj->values_size && 0 == json_cmp_objvalue(&member, &obj->values[index])) { - // free the original value - cx_strfree_a(al, &obj->values[index].name); - cxJsonValueFree(obj->values[index].value); - // replace the item - obj->values[index] = member; - - // nothing more to do - return 0; - } - - // determine the old capacity and reserve for one more element - CxArrayReallocator arealloc = cx_array_reallocator(al, NULL); - size_t oldcap = obj->values_capacity; - if (cx_array_simple_reserve_a(&arealloc, obj->values, 1)) return 1; - - // check the new capacity, if we need to realloc the index array - size_t newcap = obj->values_capacity; - if (newcap > oldcap) { - if (cxReallocateArray(al, &obj->indices, newcap, sizeof(size_t))) { - return 1; - } - } - - // check if append or insert - if (index < obj->values_size) { - // move the other elements - memmove( - &obj->values[index+1], - &obj->values[index], - (obj->values_size - index) * sizeof(CxJsonObjValue) - ); - // increase indices for the moved elements - for (size_t i = 0; i < obj->values_size ; i++) { - if (obj->indices[i] >= index) { - obj->indices[i]++; - } - } - } - - // insert the element and set the index - obj->values[index] = member; - obj->indices[obj->values_size] = index; - obj->values_size++; - - return 0; -} - static void token_destroy(CxJsonToken *token) { if (token->allocated) { cx_strfree(&token->content); + token->allocated = false; } } @@ -190,8 +111,8 @@ ttype = CX_JSON_TOKEN_STRING; } else { cxstring s = cx_strcast(str); - if (!cx_strcmp(s, CX_STR("true")) || !cx_strcmp(s, CX_STR("false")) - || !cx_strcmp(s, CX_STR("null"))) { + if (!cx_strcmp(s, "true") || !cx_strcmp(s, "false") + || !cx_strcmp(s, "null")) { ttype = CX_JSON_TOKEN_LITERAL; } else { ttype = token_numbertype(str.ptr, str.length); @@ -307,7 +228,9 @@ } } - if (ttype != CX_JSON_NO_TOKEN) { + if (ttype == CX_JSON_NO_TOKEN) { + return CX_JSON_NO_DATA; + } else { // uncompleted token size_t uncompleted_len = json->buffer.size - token_part_start; if (json->uncompleted.tokentype == CX_JSON_NO_TOKEN) { @@ -334,9 +257,8 @@ } // advance the buffer position - we saved the stuff in the uncompleted token json->buffer.pos += uncompleted_len; + return CX_JSON_INCOMPLETE_DATA; } - - return CX_JSON_INCOMPLETE_DATA; } // converts a Unicode codepoint to utf8 @@ -437,7 +359,7 @@ } else if (c == 'u') { char utf8buf[4]; unsigned utf8len = unescape_unicode_string( - cx_strn(str.ptr + i - 1, str.length + 1 - i), + cx_strn(str.ptr + i - 1, str.length - i), utf8buf ); if(utf8len > 0) { @@ -456,7 +378,7 @@ } else { // TODO: discuss the behavior for unrecognized escape sequences // most parsers throw an error here - we just ignore it - result.ptr[result.length++] = '\\'; + result.ptr[result.length++] = '\\'; // LCOV_EXCL_LINE } result.ptr[result.length++] = c; @@ -473,7 +395,7 @@ return result; } -static cxmutstr escape_string(cxmutstr str, bool escape_slash) { +static cxmutstr escape_string(cxstring str, bool escape_slash) { // note: this function produces the string without enclosing quotes // the reason is that we don't want to allocate memory just for that CxBuffer buf = {0}; @@ -488,7 +410,7 @@ size_t capa = str.length + 32; char *space = cxMallocDefault(capa); if (space == NULL) return cx_mutstrn(NULL, 0); - cxBufferInit(&buf, space, capa, NULL, CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&buf, NULL, space, capa, CX_BUFFER_AUTO_EXTEND); cxBufferWrite(str.ptr, 1, i, &buf); all_printable = false; } @@ -519,11 +441,27 @@ cxBufferPut(&buf, c); } } - if (!all_printable) { - str = cx_mutstrn(buf.space, buf.size); + cxmutstr ret; + if (all_printable) { + // don't copy the string when we don't need to escape anything + ret = cx_mutstrn((char*)str.ptr, str.length); + } else { + ret = cx_mutstrn(buf.space, buf.size); } cxBufferDestroy(&buf); - return str; + return ret; +} + +static CxJsonObject json_create_object_map(const CxAllocator *allocator) { + CxMap *map = cxKvListCreateAsMap(allocator, CX_STORE_POINTERS); + if (map == NULL) return NULL; // LCOV_EXCL_LINE + cxSetCompareFunc(map, cxJsonCompare); + cxSetDestructor(map, cxJsonValueFree); + return map; +} + +static void json_free_object_map(CxJsonObject obj) { + cxMapFree(obj); } static CxJsonValue* json_create_value(CxJson *json, CxJsonValueType type) { @@ -534,33 +472,30 @@ v->type = type; v->allocator = json->allocator; if (type == CX_JSON_ARRAY) { - cx_array_initialize_a(json->allocator, v->value.array.array, 16); - if (v->value.array.array == NULL) goto create_json_value_exit_error; // LCOV_EXCL_LINE + if (cx_array_init_a(json->allocator, v->array, 16)) { + goto create_json_value_exit_error; // LCOV_EXCL_LINE + } } else if (type == CX_JSON_OBJECT) { - cx_array_initialize_a(json->allocator, v->value.object.values, 16); - v->value.object.indices = cxCalloc(json->allocator, 16, sizeof(size_t)); - if (v->value.object.values == NULL || - v->value.object.indices == NULL) - goto create_json_value_exit_error; // LCOV_EXCL_LINE + v->object = json_create_object_map(json->allocator); + if (v->object == NULL) goto create_json_value_exit_error; // LCOV_EXCL_LINE } // add the new value to a possible parent - if (json->vbuf_size > 0) { - CxJsonValue *parent = json->vbuf[json->vbuf_size - 1]; + if (json->vbuf.size > 0) { + CxJsonValue *parent = json->vbuf.data[json->vbuf.size - 1]; assert(parent != NULL); if (parent->type == CX_JSON_ARRAY) { - CxArrayReallocator value_realloc = cx_array_reallocator(json->allocator, NULL); - if (cx_array_simple_add_a(&value_realloc, parent->value.array.array, v)) { + if (cx_array_add_a(json->allocator, parent->array, v)) { goto create_json_value_exit_error; // LCOV_EXCL_LINE } } else if (parent->type == CX_JSON_OBJECT) { // the member was already created after parsing the name - assert(json->uncompleted_member.name.ptr != NULL); - json->uncompleted_member.value = v; - if (json_add_objvalue(parent, json->uncompleted_member)) { + // store the pointer of the uncompleted value in the map + assert(json->uncompleted_member_name.ptr != NULL); + if (cxMapPut(parent->object, json->uncompleted_member_name, v)) { goto create_json_value_exit_error; // LCOV_EXCL_LINE } - json->uncompleted_member.name = (cxmutstr) {NULL, 0}; + cx_strfree_a(json->allocator, &json->uncompleted_member_name); } else { assert(false); // LCOV_EXCL_LINE } @@ -568,10 +503,19 @@ // add the new value to the stack, if it is an array or object if (type == CX_JSON_ARRAY || type == CX_JSON_OBJECT) { - CxArrayReallocator vbuf_realloc = cx_array_reallocator(NULL, json->vbuf_internal); - if (cx_array_simple_add_a(&vbuf_realloc, json->vbuf, v)) { - goto create_json_value_exit_error; // LCOV_EXCL_LINE + if (json->vbuf.size >= json->vbuf.capacity) { + int alloc_error; + if (json->vbuf.data == json->vbuf_internal) { + alloc_error = cx_array_copy_to_new(json->vbuf, json->vbuf.size+1); + } else { + alloc_error = cx_array_reserve(json->vbuf, json->vbuf.size+1); + } + if (alloc_error) { + goto create_json_value_exit_error; // LCOV_EXCL_LINE + } } + json->vbuf.data[json->vbuf.size] = v; + json->vbuf.size++; } // if currently no value is parsed, this is now the value of interest @@ -605,29 +549,23 @@ memset(json, 0, sizeof(CxJson)); json->allocator = allocator; - json->states = json->states_internal; - json->states_capacity = cx_nmemb(json->states_internal); - json->states[0] = JP_STATE_VALUE_BEGIN; - json->states_size = 1; - - json->vbuf = json->vbuf_internal; - json->vbuf_capacity = cx_nmemb(json->vbuf_internal); + cx_array_init_fixed(json->states, json->states_internal, 1); + json->states.data[0] = JP_STATE_VALUE_BEGIN; + cx_array_init_fixed(json->vbuf, json->vbuf_internal, 0); } void cxJsonDestroy(CxJson *json) { cxBufferDestroy(&json->buffer); - if (json->states != json->states_internal) { - cxFreeDefault(json->states); + if (json->states.data != json->states_internal) { + cx_array_free(json->states); } - if (json->vbuf != json->vbuf_internal) { - cxFreeDefault(json->vbuf); + if (json->vbuf.data != json->vbuf_internal) { + cx_array_free(json->vbuf); } cxJsonValueFree(json->parsed); json->parsed = NULL; - if (json->uncompleted_member.name.ptr != NULL) { - cx_strfree_a(json->allocator, &json->uncompleted_member.name); - json->uncompleted_member = (CxJsonObjValue){{NULL, 0}, NULL}; - } + token_destroy(&json->uncompleted); + cx_strfree_a(json->allocator, &json->uncompleted_member_name); } void cxJsonReset(CxJson *json) { @@ -640,8 +578,9 @@ if (cxBufferEof(&json->buffer)) { // reinitialize the buffer cxBufferDestroy(&json->buffer); - cxBufferInit(&json->buffer, (char*) buf, size, - NULL, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_COPY_ON_WRITE); + if (buf == NULL) buf = ""; // buffer must not be initialized with NULL + cxBufferInit(&json->buffer, NULL, (char*) buf, + size, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_COPY_ON_WRITE); json->buffer.size = size; return 0; } else { @@ -650,9 +589,9 @@ } static void json_add_state(CxJson *json, int state) { - // we have guaranteed the necessary space with cx_array_simple_reserve() + // we have guaranteed the necessary space // therefore, we can safely add the state in the simplest way possible - json->states[json->states_size++] = state; + json->states.data[json->states.size++] = state; } #define return_rec(code) \ @@ -673,13 +612,21 @@ } // pop the current state - assert(json->states_size > 0); - int state = json->states[--json->states_size]; + assert(json->states.size > 0); + int state = json->states.data[--json->states.size]; - // guarantee that at least two more states fit on the stack - CxArrayReallocator state_realloc = cx_array_reallocator(NULL, json->states_internal); - if (cx_array_simple_reserve_a(&state_realloc, json->states, 2)) { - return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE + // guarantee that at least two more states fit into the array + const size_t required_states_depth = json->states.size + 2; + if (required_states_depth >= json->states.capacity) { + int alloc_error; + if (json->states.data == json->states_internal) { + alloc_error = cx_array_copy_to_new(json->states, required_states_depth); + } else { + alloc_error = cx_array_reserve(json->states, required_states_depth); + } + if (alloc_error) { + return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE + } } @@ -711,6 +658,16 @@ json_add_state(json, JP_STATE_OBJ_NAME_OR_CLOSE); return_rec(CX_JSON_NO_ERROR); } + case CX_JSON_TOKEN_END_ARRAY: { + if (state == JP_STATE_VALUE_BEGIN_AR) { + // discard the array from the value buffer + json->vbuf.size--; + json->states.size--; + return_rec(CX_JSON_NO_ERROR); + } else { + return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); + } + } case CX_JSON_TOKEN_STRING: { if ((vbuf = json_create_value(json, CX_JSON_STRING)) == NULL) { return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE @@ -719,7 +676,7 @@ if (str.ptr == NULL) { return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE } - vbuf->value.string = str; + vbuf->string = str; return_rec(CX_JSON_NO_ERROR); } case CX_JSON_TOKEN_INTEGER: @@ -729,12 +686,13 @@ return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE } if (type == CX_JSON_INTEGER) { - if (cx_strtoi64(token.content, &vbuf->value.integer, 10)) { + if (cx_strtoi64(token.content, &vbuf->integer, 10)) { return_rec(CX_JSON_FORMAT_ERROR_NUMBER); } } else { - if (cx_strtod(token.content, &vbuf->value.number)) { - return_rec(CX_JSON_FORMAT_ERROR_NUMBER); + if (cx_strtod(token.content, &vbuf->number)) { + // TODO: at the moment this is unreachable, because the tokenizer is already stricter than cx_strtod() + return_rec(CX_JSON_FORMAT_ERROR_NUMBER); // LCOV_EXCL_LINE } } return_rec(CX_JSON_NO_ERROR); @@ -743,12 +701,12 @@ if ((vbuf = json_create_value(json, CX_JSON_LITERAL)) == NULL) { return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE } - if (0 == cx_strcmp(cx_strcast(token.content), cx_str("true"))) { - vbuf->value.literal = CX_JSON_TRUE; - } else if (0 == cx_strcmp(cx_strcast(token.content), cx_str("false"))) { - vbuf->value.literal = CX_JSON_FALSE; + if (0 == cx_strcmp(token.content, "true")) { + vbuf->literal = CX_JSON_TRUE; + } else if (0 == cx_strcmp(token.content, "false")) { + vbuf->literal = CX_JSON_FALSE; } else { - vbuf->value.literal = CX_JSON_NULL; + vbuf->literal = CX_JSON_NULL; } return_rec(CX_JSON_NO_ERROR); } @@ -763,7 +721,7 @@ return_rec(CX_JSON_NO_ERROR); } else if (token.tokentype == CX_JSON_TOKEN_END_ARRAY) { // discard the array from the value buffer - json->vbuf_size--; + json->vbuf.size--; return_rec(CX_JSON_NO_ERROR); } else { return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); @@ -771,7 +729,7 @@ } else if (state == JP_STATE_OBJ_NAME_OR_CLOSE || state == JP_STATE_OBJ_NAME) { if (state == JP_STATE_OBJ_NAME_OR_CLOSE && token.tokentype == CX_JSON_TOKEN_END_OBJECT) { // discard the obj from the value buffer - json->vbuf_size--; + json->vbuf.size--; return_rec(CX_JSON_NO_ERROR); } else { // expect string @@ -784,9 +742,9 @@ if (name.ptr == NULL) { return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE } - assert(json->uncompleted_member.name.ptr == NULL); - json->uncompleted_member.name = name; - assert(json->vbuf_size > 0); + assert(json->uncompleted_member_name.ptr == NULL); + json->uncompleted_member_name = name; + assert(json->vbuf.size > 0); // next state json_add_state(json, JP_STATE_OBJ_COLON); @@ -807,7 +765,7 @@ return_rec(CX_JSON_NO_ERROR); } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) { // discard the obj from the value buffer - json->vbuf_size--; + json->vbuf.size--; return_rec(CX_JSON_NO_ERROR); } else { return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); @@ -815,34 +773,34 @@ } else { // should be unreachable assert(false); - return_rec(-1); + return_rec(-1); // LCOV_EXCL_LINE } } CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value) { - // check if buffer has been filled + // initialize output value + *value = &cx_json_value_nothing; + + // check if the buffer has been filled if (json->buffer.space == NULL) { return CX_JSON_NULL_DATA; } - // initialize output value - *value = &cx_json_value_nothing; - // parse data CxJsonStatus result; do { result = json_parse(json); - if (result == CX_JSON_NO_ERROR && json->states_size == 1) { + if (result == CX_JSON_NO_ERROR && json->states.size == 1) { // final state reached - assert(json->states[0] == JP_STATE_VALUE_END); - assert(json->vbuf_size == 0); + assert(json->states.data[0] == JP_STATE_VALUE_END); + assert(json->vbuf.size == 0); // write output value *value = json->parsed; json->parsed = NULL; // re-initialize state machine - json->states[0] = JP_STATE_VALUE_BEGIN; + json->states.data[0] = JP_STATE_VALUE_BEGIN; return CX_JSON_NO_ERROR; } @@ -851,36 +809,60 @@ // the parser might think there is no data // but when we did not reach the final state, // we know that there must be more to come - if (result == CX_JSON_NO_DATA && json->states_size > 1) { + if (result == CX_JSON_NO_DATA && json->states.size > 1) { return CX_JSON_INCOMPLETE_DATA; } return result; } +CxJsonStatus cx_json_from_string(const CxAllocator *allocator, + cxstring str, CxJsonValue **value) { + *value = &cx_json_value_nothing; + CxJson parser; + cxJsonInit(&parser, allocator); + if (cxJsonFill(&parser, str)) { + // LCOV_EXCL_START + cxJsonDestroy(&parser); + return CX_JSON_BUFFER_ALLOC_FAILED; + // LCOV_EXCL_STOP + } + CxJsonStatus status = cxJsonNext(&parser, value); + // check if we consume the total string + CxJsonValue *chk_value = NULL; + CxJsonStatus chk_status = CX_JSON_NO_DATA; + if (status == CX_JSON_NO_ERROR) { + chk_status = cxJsonNext(&parser, &chk_value); + } + cxJsonDestroy(&parser); + if (chk_status == CX_JSON_NO_DATA) { + return status; + } else { + cxJsonValueFree(*value); + // if chk_value is nothing, the free is harmless + cxJsonValueFree(chk_value); + *value = &cx_json_value_nothing; + return CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN; + } + +} + void cxJsonValueFree(CxJsonValue *value) { if (value == NULL || value->type == CX_JSON_NOTHING) return; switch (value->type) { case CX_JSON_OBJECT: { - CxJsonObject obj = value->value.object; - for (size_t i = 0; i < obj.values_size; i++) { - cxJsonValueFree(obj.values[i].value); - cx_strfree_a(value->allocator, &obj.values[i].name); - } - cxFree(value->allocator, obj.values); - cxFree(value->allocator, obj.indices); + json_free_object_map(value->object); break; } case CX_JSON_ARRAY: { - CxJsonArray array = value->value.array; - for (size_t i = 0; i < array.array_size; i++) { - cxJsonValueFree(array.array[i]); + for (size_t i = 0; i < value->array.size; i++) { + cxJsonValueFree(value->array.data[i]); } - cxFree(value->allocator, array.array); + cx_array_free_a(value->allocator, value->array); break; } case CX_JSON_STRING: { - cxFree(value->allocator, value->value.string.ptr); + cxFree(value->allocator, value->string.ptr); break; } default: { @@ -896,15 +878,8 @@ if (v == NULL) return NULL; v->allocator = allocator; v->type = CX_JSON_OBJECT; - cx_array_initialize_a(allocator, v->value.object.values, 16); - if (v->value.object.values == NULL) { // LCOV_EXCL_START - cxFree(allocator, v); - return NULL; - // LCOV_EXCL_STOP - } - v->value.object.indices = cxCalloc(allocator, 16, sizeof(size_t)); - if (v->value.object.indices == NULL) { // LCOV_EXCL_START - cxFree(allocator, v->value.object.values); + v->object = json_create_object_map(allocator); + if (v->object == NULL) { // LCOV_EXCL_START cxFree(allocator, v); return NULL; // LCOV_EXCL_STOP @@ -912,14 +887,23 @@ return v; } -CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator) { +CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator, size_t capacity) { if (allocator == NULL) allocator = cxDefaultAllocator; CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); if (v == NULL) return NULL; v->allocator = allocator; v->type = CX_JSON_ARRAY; - cx_array_initialize_a(allocator, v->value.array.array, 16); - if (v->value.array.array == NULL) { cxFree(allocator, v); return NULL; } + if (capacity > 0) { + if (cx_array_init_a(allocator, v->array, capacity)) { + // LCOV_EXCL_START + cxFree(allocator, v); + return NULL; + // LCOV_EXCL_STOP + } + } else { + v->array.data = NULL; + v->array.size = v->array.capacity = 0; + } return v; } @@ -929,7 +913,7 @@ if (v == NULL) return NULL; v->allocator = allocator; v->type = CX_JSON_NUMBER; - v->value.number = num; + v->number = num; return v; } @@ -939,15 +923,11 @@ if (v == NULL) return NULL; v->allocator = allocator; v->type = CX_JSON_INTEGER; - v->value.integer = num; + v->integer = num; return v; } -CxJsonValue* cxJsonCreateString(const CxAllocator* allocator, const char* str) { - return cxJsonCreateCxString(allocator, cx_str(str)); -} - -CxJsonValue* cxJsonCreateCxString(const CxAllocator* allocator, cxstring str) { +CxJsonValue* cx_json_create_string(const CxAllocator* allocator, cxstring str) { if (allocator == NULL) allocator = cxDefaultAllocator; CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); if (v == NULL) return NULL; @@ -955,7 +935,7 @@ v->type = CX_JSON_STRING; cxmutstr s = cx_strdup_a(allocator, str); if (s.ptr == NULL) { cxFree(allocator, v); return NULL; } - v->value.string = s; + v->string = s; return v; } @@ -965,7 +945,7 @@ if (v == NULL) return NULL; v->allocator = allocator; v->type = CX_JSON_LITERAL; - v->value.literal = lit; + v->literal = lit; return v; } @@ -1020,7 +1000,7 @@ CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*)); if (values == NULL) return -1; for (size_t i = 0; i < count; i++) { - values[i] = cxJsonCreateCxString(arr->allocator, str[i]); + values[i] = cxJsonCreateString(arr->allocator, str[i]); if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } } int ret = cxJsonArrAddValues(arr, values, count); @@ -1041,70 +1021,50 @@ } int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue* const* val, size_t count) { - CxArrayReallocator value_realloc = cx_array_reallocator(arr->allocator, NULL); assert(arr->type == CX_JSON_ARRAY); - return cx_array_simple_copy_a(&value_realloc, - arr->value.array.array, - arr->value.array.array_size, - val, count - ); + return cx_array_add_array_a(arr->allocator, arr->array, val, count); } -int cxJsonObjPut(CxJsonValue* obj, cxstring name, CxJsonValue* child) { - cxmutstr k = cx_strdup_a(obj->allocator, name); - if (k.ptr == NULL) return -1; - CxJsonObjValue kv = {k, child}; - if (json_add_objvalue(obj, kv)) { - cx_strfree_a(obj->allocator, &k); - return 1; - } else { - return 0; - } +int cx_json_obj_put(CxJsonValue* obj, cxstring name, CxJsonValue* child) { + return cxMapPut(obj->object, name, child); } -CxJsonValue* cxJsonObjPutObj(CxJsonValue* obj, cxstring name) { +CxJsonValue* cx_json_obj_put_obj(CxJsonValue* obj, cxstring name) { CxJsonValue* v = cxJsonCreateObj(obj->allocator); if (v == NULL) return NULL; if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } return v; } -CxJsonValue* cxJsonObjPutArr(CxJsonValue* obj, cxstring name) { - CxJsonValue* v = cxJsonCreateArr(obj->allocator); +CxJsonValue* cx_json_obj_put_arr(CxJsonValue* obj, cxstring name, size_t capacity) { + CxJsonValue* v = cxJsonCreateArr(obj->allocator, capacity); if (v == NULL) return NULL; if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } return v; } -CxJsonValue* cxJsonObjPutNumber(CxJsonValue* obj, cxstring name, double num) { +CxJsonValue* cx_json_obj_put_number(CxJsonValue* obj, cxstring name, double num) { CxJsonValue* v = cxJsonCreateNumber(obj->allocator, num); if (v == NULL) return NULL; if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } return v; } -CxJsonValue* cxJsonObjPutInteger(CxJsonValue* obj, cxstring name, int64_t num) { +CxJsonValue* cx_json_obj_put_integer(CxJsonValue* obj, cxstring name, int64_t num) { CxJsonValue* v = cxJsonCreateInteger(obj->allocator, num); if (v == NULL) return NULL; if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } return v; } -CxJsonValue* cxJsonObjPutString(CxJsonValue* obj, cxstring name, const char* str) { +CxJsonValue* cx_json_obj_put_string(CxJsonValue* obj, cxstring name, cxstring str) { CxJsonValue* v = cxJsonCreateString(obj->allocator, str); if (v == NULL) return NULL; if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } return v; } -CxJsonValue* cxJsonObjPutCxString(CxJsonValue* obj, cxstring name, cxstring str) { - CxJsonValue* v = cxJsonCreateCxString(obj->allocator, str); - if (v == NULL) return NULL; - if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } - return v; -} - -CxJsonValue* cxJsonObjPutLiteral(CxJsonValue* obj, cxstring name, CxJsonLiteral lit) { +CxJsonValue* cx_json_obj_put_literal(CxJsonValue* obj, cxstring name, CxJsonLiteral lit) { CxJsonValue* v = cxJsonCreateLiteral(obj->allocator, lit); if (v == NULL) return NULL; if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL;} @@ -1112,98 +1072,75 @@ } CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index) { - if (index >= value->value.array.array_size) { + if (index >= value->array.size) { return &cx_json_value_nothing; } - return value->value.array.array[index]; + return value->array.data[index]; } CxJsonValue *cxJsonArrRemove(CxJsonValue *value, size_t index) { - if (index >= value->value.array.array_size) { + if (index >= value->array.size) { return NULL; } - CxJsonValue *ret = value->value.array.array[index]; - // TODO: replace with a low level cx_array_remove() - size_t count = value->value.array.array_size - index - 1; - if (count > 0) { - memmove(value->value.array.array + index, value->value.array.array + index + 1, count * sizeof(CxJsonValue*)); - } - value->value.array.array_size--; + CxJsonValue *ret = value->array.data[index]; + cx_array_remove(value->array, index); return ret; } char *cxJsonAsString(const CxJsonValue *value) { - return value->value.string.ptr; + return value->string.ptr; } cxstring cxJsonAsCxString(const CxJsonValue *value) { - return cx_strcast(value->value.string); + return cx_strcast(value->string); } cxmutstr cxJsonAsCxMutStr(const CxJsonValue *value) { - return value->value.string; + return value->string; } double cxJsonAsDouble(const CxJsonValue *value) { if (value->type == CX_JSON_INTEGER) { - return (double) value->value.integer; + return (double) value->integer; } else { - return value->value.number; + return value->number; } } int64_t cxJsonAsInteger(const CxJsonValue *value) { if (value->type == CX_JSON_INTEGER) { - return value->value.integer; + return value->integer; } else { - return (int64_t) value->value.number; + return (int64_t) value->number; } } CxIterator cxJsonArrIter(const CxJsonValue *value) { - return cxIteratorPtr( - value->value.array.array, - value->value.array.array_size, - true // arrays need to keep order - ); + return cx_array_iterator_ptr(value->array); } -CxIterator cxJsonObjIter(const CxJsonValue *value) { - return cxIterator( - value->value.object.values, - sizeof(CxJsonObjValue), - value->value.object.values_size, - true // TODO: objects do not always need to keep order - ); +CxMapIterator cxJsonObjIter(const CxJsonValue *value) { + return cxMapIterator(value->object); } CxJsonValue *cx_json_obj_get(const CxJsonValue *value, cxstring name) { - size_t index = json_find_objvalue(value, name); - if (index >= value->value.object.values_size) { + CxJsonValue *v = cxMapGet(value->object, name); + if (v == NULL) { return &cx_json_value_nothing; } else { - return value->value.object.values[index].value; + return v; } } CxJsonValue *cx_json_obj_remove(CxJsonValue *value, cxstring name) { - size_t index = json_find_objvalue(value, name); - if (index >= value->value.object.values_size) { - return NULL; - } else { - CxJsonObjValue kv = value->value.object.values[index]; - cx_strfree_a(value->allocator, &kv.name); - // TODO: replace with cx_array_remove() / cx_array_remove_fast() - value->value.object.values_size--; - memmove(value->value.object.values + index, value->value.object.values + index + 1, (value->value.object.values_size - index) * sizeof(CxJsonObjValue)); - return kv.value; - } + CxJsonValue *v = NULL; + cxMapRemoveAndGet(value->object, name, &v); + return v; } CxJsonWriter cxJsonWriterCompact(void) { return (CxJsonWriter) { false, - true, 6, false, 4, @@ -1214,7 +1151,6 @@ CxJsonWriter cxJsonWriterPretty(bool use_spaces) { return (CxJsonWriter) { true, - true, 6, use_spaces, 4, @@ -1279,17 +1215,8 @@ expected++; } depth++; - size_t elem_count = value->value.object.values_size; - for (size_t look_idx = 0; look_idx < elem_count; look_idx++) { - // get the member either via index array or directly - size_t elem_idx = settings->sort_members - ? look_idx - : value->value.object.indices[look_idx]; - CxJsonObjValue *member = &value->value.object.values[elem_idx]; - if (settings->sort_members) { - depth++;depth--; - } - + CxMapIterator member_iter = cxJsonObjIter(value); + cx_foreach(const CxMapEntry *, member, member_iter) { // possible indentation if (settings->pretty) { if (cx_json_writer_indent(target, wfunc, settings, depth)) { @@ -1299,26 +1226,27 @@ // the name actual += wfunc("\"", 1, 1, target); - cxmutstr name = escape_string(member->name, settings->escape_slash); + cxstring key = cx_strn(member->key->data, member->key->len); + cxmutstr name = escape_string(key, settings->escape_slash); actual += wfunc(name.ptr, 1, name.length, target); - if (name.ptr != member->name.ptr) { - cx_strfree(&name); - } actual += wfunc("\"", 1, 1, target); const char *obj_name_sep = ": "; if (settings->pretty) { actual += wfunc(obj_name_sep, 1, 2, target); - expected += 4 + member->name.length; + expected += 4 + name.length; } else { actual += wfunc(obj_name_sep, 1, 1, target); - expected += 3 + member->name.length; + expected += 3 + name.length; + } + if (name.ptr != key.ptr) { + cx_strfree(&name); } // the value if (cx_json_write_rec(target, member->value, wfunc, settings, depth)) return 1; // end of object-value - if (look_idx < elem_count - 1) { + if (member_iter.index < member_iter.elem_count - 1) { const char *obj_value_sep = ",\n"; if (settings->pretty) { actual += wfunc(obj_value_sep, 1, 2, target); @@ -1350,7 +1278,9 @@ if (cx_json_write_rec( target, element, wfunc, settings, depth) - ) return 1; + ) { + return 1; // LCOV_EXCL_LINE + } if (iter.index < iter.elem_count - 1) { const char *arr_value_sep = ", "; @@ -1369,13 +1299,14 @@ } case CX_JSON_STRING: { actual += wfunc("\"", 1, 1, target); - cxmutstr str = escape_string(value->value.string, settings->escape_slash); + cxmutstr str = escape_string(cx_strcast(value->string), + settings->escape_slash); actual += wfunc(str.ptr, 1, str.length, target); - if (str.ptr != value->value.string.ptr) { + actual += wfunc("\"", 1, 1, target); + expected += 2 + str.length; + if (str.ptr != value->string.ptr) { cx_strfree(&str); } - actual += wfunc("\"", 1, 1, target); - expected += 2 + value->value.string.length; break; } case CX_JSON_NUMBER: { @@ -1383,7 +1314,7 @@ // because of the way how %g is defined, we need to // double the precision and truncate ourselves precision = 1 + (precision > 15 ? 30 : 2 * precision); - snprintf(numbuf, 40, "%.*g", precision, value->value.number); + snprintf(numbuf, 40, "%.*g", precision, value->number); char *dot, *exp; unsigned char max_digits; // find the decimal separator and hope that it's one of . or , @@ -1447,17 +1378,17 @@ break; } case CX_JSON_INTEGER: { - snprintf(numbuf, 32, "%" PRIi64, value->value.integer); + snprintf(numbuf, 32, "%" PRIi64, value->integer); size_t len = strlen(numbuf); actual += wfunc(numbuf, 1, len, target); expected += len; break; } case CX_JSON_LITERAL: { - if (value->value.literal == CX_JSON_TRUE) { + if (value->literal == CX_JSON_TRUE) { actual += wfunc("true", 1, 4, target); expected += 4; - } else if (value->value.literal == CX_JSON_FALSE) { + } else if (value->literal == CX_JSON_FALSE) { actual += wfunc("false", 1, 5, target); expected += 5; } else { @@ -1495,3 +1426,144 @@ } return cx_json_write_rec(target, value, wfunc, settings, 0); } + +static cxmutstr cx_json_to_string(CxJsonValue *value, const CxAllocator *allocator, CxJsonWriter *writer) { + if (allocator == NULL) allocator = cxDefaultAllocator; + CxBuffer buffer; + if (cxBufferInit(&buffer, allocator, NULL, 128, + CX_BUFFER_AUTO_EXTEND | CX_BUFFER_DO_NOT_FREE)) { + return (cxmutstr){NULL, 0}; + } + if (cx_json_write_rec(&buffer, value, cxBufferWriteFunc, writer, 0) + || cxBufferTerminate(&buffer)) { + // LCOV_EXCL_START + buffer.flags &= ~CX_BUFFER_DO_NOT_FREE; + cxBufferDestroy(&buffer); + return (cxmutstr){NULL, 0}; + // LCOV_EXCL_STOP + } else { + cxmutstr str = cx_mutstrn(buffer.space, buffer.size); + cxBufferDestroy(&buffer); + return str; + } + +} + +cxmutstr cxJsonToString(const CxAllocator *allocator, CxJsonValue *value) { + CxJsonWriter writer = cxJsonWriterCompact(); + return cx_json_to_string(value, allocator, &writer); +} + +cxmutstr cxJsonToPrettyString(const CxAllocator *allocator, CxJsonValue *value) { + CxJsonWriter writer = cxJsonWriterPretty(true); + return cx_json_to_string(value, allocator, &writer); +} + +int cxJsonCompare(const CxJsonValue *json, const CxJsonValue *other) { + if (json == other) return 0; + if (json == NULL || other == NULL) return -1; + if (json->type != other->type) { + if (!cxJsonIsNumber(json)) return -1; + if (!cxJsonIsNumber(other)) return -1; + } + switch (json->type) { + case CX_JSON_NOTHING: + return 0; + case CX_JSON_OBJECT: + return cxMapCompare(json->object, other->object); + case CX_JSON_ARRAY: + if (json->array.size != other->array.size) return -1; + for (size_t i = 0; i < json->array.size; i++) { + const int d = cxJsonCompare(json->array.data[i], other->array.data[i]); + if (d != 0) return d; + } + return 0; + case CX_JSON_STRING: + return cx_strcmp(json->string, other->string); + case CX_JSON_INTEGER: + if (other->type == CX_JSON_INTEGER) { + return cx_vcmp_int64(json->integer, other->integer); + } else { + return cx_vcmp_double(cxJsonAsDouble(json), other->number); + } + case CX_JSON_NUMBER: + return cx_vcmp_double(json->number, cxJsonAsDouble(other)); + case CX_JSON_LITERAL: + return json->literal == other->literal ? 0 : -1; + default: + // LCOV_EXCL_START + // unreachable + assert(false); + return -1; + // LCOV_EXCL_STOP + } +} + +CxJsonValue* cxJsonClone(const CxJsonValue* value, const CxAllocator* allocator) { + return cx_json_clone_func(NULL, value, allocator, NULL); +} + +CxJsonValue* cx_json_clone_func(CxJsonValue* target, const CxJsonValue* source, + const CxAllocator* allocator, cx_attr_unused void *data) { + if (source == NULL || source->type == CX_JSON_NOTHING) { + return &cx_json_value_nothing; + } + if (allocator == NULL) allocator = cxDefaultAllocator; + +#define return_value(v) { \ + CxJsonValue *ret = v; \ + if (target == NULL) { \ + return ret; \ + } else { \ + *target = *ret; \ + cxFree(allocator, ret); \ + return target; \ + } \ + } + + switch (source->type) { + case CX_JSON_OBJECT: { + CxJsonValue *obj = cxJsonCreateObj(allocator); + if (obj == NULL) return NULL; // LCOV_EXCL_LINE + if (cxMapClone(obj->object, source->object, cxJsonCloneFunc, allocator, NULL)) { + // LCOV_EXCL_START + cxJsonValueFree(obj); + return NULL; + // LCOV_EXCL_STOP + } + return_value(obj); + } + case CX_JSON_ARRAY: { + const size_t elem_count = source->array.size; + CxJsonValue *arr = cxJsonCreateArr(allocator, elem_count); + if (arr == NULL) return NULL; // LCOV_EXCL_LINE + arr->array.size = elem_count; + for (size_t i = 0 ; i < elem_count ; i++) { + CxJsonValue *e = cx_json_clone_func(NULL, source->array.data[i], allocator, NULL); + if (e == NULL) { + // LCOV_EXCL_START + cxJsonValueFree(arr); + return NULL; + // LCOV_EXCL_STOP + } + arr->array.data[i] = e; + } + return_value(arr); + } + case CX_JSON_STRING: + return_value(cxJsonCreateString(allocator, source->string)); + case CX_JSON_INTEGER: + return_value(cxJsonCreateInteger(allocator, source->integer)); + case CX_JSON_NUMBER: + return_value(cxJsonCreateNumber(allocator, source->number)); + case CX_JSON_LITERAL: + return_value(cxJsonCreateLiteral(allocator, source->literal)); + default: + // LCOV_EXCL_START + // unreachable + assert(false); + return NULL; + // LCOV_EXCL_STOP + } +#undef return_value +}
--- a/ucx/kv_list.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/kv_list.c Fri Dec 19 17:53:18 2025 +0100 @@ -98,7 +98,7 @@ } static CxHashKey *cx_kv_list_loc_key(cx_kv_list *list, void *node_data) { - return (CxHashKey*)((char*)node_data + list->list.base.collection.elem_size); + return (CxHashKey*)((char*)node_data - list->list.loc_data + list->list.loc_extra); } static void cx_kvl_deallocate(struct cx_list_s *list) { @@ -221,11 +221,12 @@ size_t index; cx_linked_list *ll = &kv_list->list; - char *node = cx_linked_list_find( + char *node = cx_linked_list_find_c( ll->begin, ll->loc_next, ll->loc_data, - list->collection.cmpfunc, elem, - &index + elem, &index, + cx_list_compare_wrapper, + list ); if (node == NULL) { return list->collection.size; @@ -285,6 +286,7 @@ static void cx_kvl_map_deallocate(struct cx_map_s *map) { cx_kv_list *kv_list = ((struct cx_kv_list_map_s*)map)->list; + cx_kv_list_update_destructors(kv_list); kv_list->map_methods->deallocate(map); kv_list->list_methods->deallocate(&kv_list->list.base); } @@ -296,41 +298,7 @@ kv_list->map_methods->clear(map); } -static void *cx_kvl_map_put(CxMap *map, CxHashKey key, void *value) { - cx_kv_list *kv_list = ((struct cx_kv_list_map_s*)map)->list; - // if the hash has not yet been computed, do it now - if (key.hash == 0) { - cx_hash_murmur(&key); - } - - // reserve memory in the map first - void **map_data = kv_list->map_methods->put(map, key, NULL); - if (map_data == NULL) return NULL; // LCOV_EXCL_LINE - - // insert the data into the list (which most likely destroys the sorted property) - kv_list->list.base.collection.sorted = false; - void *node_data = kv_list->list_methods->insert_element( - &kv_list->list.base, kv_list->list.base.collection.size, - kv_list->list.base.collection.store_pointer ? &value : value); - if (node_data == NULL) { // LCOV_EXCL_START - // non-destructively remove the key again - kv_list->map_methods->remove(&kv_list->map->map_base.base, key, &map_data); - return NULL; - } // LCOV_EXCL_STOP - - // write the node pointer to the map entry - *map_data = node_data; - - // copy the key to the node data - CxHashKey *key_ptr = cx_kv_list_loc_key(kv_list, node_data); - *key_ptr = key; - - // we must return node_data here and not map_data, - // because the node_data is the actual element of this collection - return node_data; -} - -void *cx_kvl_map_get(const CxMap *map, CxHashKey key) { +static void *cx_kvl_map_get(const CxMap *map, CxHashKey key) { cx_kv_list *kv_list = ((struct cx_kv_list_map_s*)map)->list; void *node_data = kv_list->map_methods->get(map, key); if (node_data == NULL) return NULL; // LCOV_EXCL_LINE @@ -338,7 +306,7 @@ return kv_list->list.base.collection.store_pointer ? *(void**)node_data : node_data; } -int cx_kvl_map_remove(CxMap *map, CxHashKey key, void *targetbuf) { +static int cx_kvl_map_remove(CxMap *map, CxHashKey key, void *targetbuf) { cx_kv_list *kv_list = ((struct cx_kv_list_map_s*)map)->list; void *node_data; @@ -381,6 +349,43 @@ return 0; } +static CxMapEntry cx_kvl_map_put(CxMap *map, CxHashKey key, void *value) { + cx_kv_list *kv_list = ((struct cx_kv_list_map_s*)map)->list; + // if the hash has not yet been computed, do it now + if (key.hash == 0) { + cx_hash_murmur(&key); + } + + // remove any existing element first + cx_kvl_map_remove(map, key, NULL); + + // now reserve new memory in the map + CxMapEntry map_entry = kv_list->map_methods->put(map, key, NULL); + if (map_entry.key == NULL) return (CxMapEntry){NULL, NULL}; // LCOV_EXCL_LINE + + // insert the data into the list (which most likely destroys the sorted property) + kv_list->list.base.collection.sorted = false; + void *node_data = kv_list->list_methods->insert_element( + &kv_list->list.base, kv_list->list.base.collection.size, + kv_list->list.base.collection.store_pointer ? &value : value); + if (node_data == NULL) { // LCOV_EXCL_START + // non-destructively remove the key again + void *dummy; + kv_list->map_methods->remove(&kv_list->map->map_base.base, key, &dummy); + return (CxMapEntry){NULL, NULL}; + } // LCOV_EXCL_STOP + + // write the node pointer to the map entry + *(void**)map_entry.value = node_data; + + // copy the key to the node data + CxHashKey *key_ptr = cx_kv_list_loc_key(kv_list, node_data); + *key_ptr = *map_entry.key; + + // we must return an entry that points to the node data! + return (CxMapEntry ){key_ptr, node_data}; +} + static void *cx_kvl_iter_current_entry(const void *it) { const CxMapIterator *iter = it; return (void*)&iter->entry; @@ -455,7 +460,7 @@ return iter->elem != NULL; } -CxMapIterator cx_kvl_map_iterator(const CxMap *map, enum cx_map_iterator_type type) { +static CxMapIterator cx_kvl_map_iterator(const CxMap *map, enum cx_map_iterator_type type) { CxMapIterator iter = {0}; iter.type = type; @@ -509,6 +514,15 @@ return iter; } +static int cx_kvl_change_capacity(struct cx_list_s *list, + cx_attr_unused size_t cap) { + // since our backing list is a linked list, we don't need to do much here, + // but rehashing the map is quite useful + cx_kv_list *kv_list = (cx_kv_list*)list; + cxMapRehash(&kv_list->map->map_base.base); + return 0; +} + static cx_list_class cx_kv_list_class = { cx_kvl_deallocate, cx_kvl_insert_element, @@ -524,6 +538,7 @@ cx_kvl_sort, NULL, cx_kvl_reverse, + cx_kvl_change_capacity, cx_kvl_iterator, }; @@ -538,7 +553,6 @@ CxList *cxKvListCreate( const CxAllocator *allocator, - cx_compare_func comparator, size_t elem_size ) { if (allocator == NULL) { @@ -546,10 +560,10 @@ } // create a normal linked list and a normal hash map, first - CxList *list = cxLinkedListCreate(allocator, comparator, elem_size); + CxList *list = cxLinkedListCreate(allocator, elem_size); if (list == NULL) return NULL; // LCOV_EXCL_LINE cx_linked_list *ll = (cx_linked_list*)list; - ll->extra_data_len = sizeof(CxHashKey); + cx_linked_list_extra_data(ll, sizeof(CxHashKey)); CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0); if (map == NULL) { // LCOV_EXCL_START cxListFree(list); @@ -586,23 +600,17 @@ // remember the base methods and override them kv_list->map_methods = map->cl; map->cl = &cx_kv_map_class; - if (list->climpl == NULL) { - kv_list->list_methods = list->cl; - list->cl = &cx_kv_list_class; - } else { - kv_list->list_methods = list->climpl; - list->climpl = &cx_kv_list_class; - } + kv_list->list_methods = list->cl; + list->cl = &cx_kv_list_class; return list; } CxMap *cxKvListCreateAsMap( const CxAllocator *allocator, - cx_compare_func comparator, size_t elem_size ) { - CxList *list = cxKvListCreate(allocator, comparator, elem_size); + CxList *list = cxKvListCreate(allocator, elem_size); return list == NULL ? NULL : cxKvListAsMap(list); } @@ -635,14 +643,14 @@ return 1; } - // add the key to the map; - if (NULL == kv_list->map_methods->put(&kv_list->map->map_base.base, key, node_data)) { - return 1; // LCOV_EXCL_LINE - } + // add the key to the map + const CxMapEntry entry = kv_list->map_methods->put( + &kv_list->map->map_base.base, key, node_data); + if (entry.key == NULL) return 1; // LCOV_EXCL_LINE // write the key to the list's node CxHashKey *loc_key = cx_kv_list_loc_key(kv_list, node_data); - *loc_key = key; + *loc_key = *entry.key; return 0; } @@ -684,22 +692,23 @@ cx_kv_list *kv_list = (cx_kv_list*)list; // reserve memory in the map - void **map_data = kv_list->map_methods->put(&kv_list->map->map_base.base, key, NULL); - if (map_data == NULL) return 1; // LCOV_EXCL_LINE + CxMapEntry map_entry = kv_list->map_methods->put(&kv_list->map->map_base.base, key, NULL); + if (map_entry.key == NULL) return 1; // LCOV_EXCL_LINE // insert the node void *node_data = kv_list->list_methods->insert_element(&kv_list->list.base, index, kv_list->list.base.collection.store_pointer ? &value : value); if (node_data == NULL) { // LCOV_EXCL_START // non-destructively remove the key again - kv_list->map_methods->remove(&kv_list->map->map_base.base, key, &map_data); + void *dummy; + kv_list->map_methods->remove(&kv_list->map->map_base.base, key, &dummy); return 1; } // LCOV_EXCL_STOP - *map_data = node_data; + *(void**)map_entry.value = node_data; // write the key to the node CxHashKey *loc_key = cx_kv_list_loc_key(kv_list, node_data); - *loc_key = key; + *loc_key = *map_entry.key; return 0; }
--- a/ucx/linked_list.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/linked_list.c Fri Dec 19 17:53:18 2025 +0100 @@ -31,6 +31,15 @@ #include <string.h> #include <assert.h> +#if __STDC_VERSION__ < 202311L +// we cannot simply include stdalign.h +// because Solaris is not entirely C11 complaint +#ifndef __alignof_is_defined +#define alignof _Alignof +#define __alignof_is_defined 1 +#endif +#endif + // LOW LEVEL LINKED LIST FUNCTIONS #define CX_LL_PTR(cur, off) (*(void**)(((char*)(cur))+(off))) @@ -56,13 +65,14 @@ return (void *) cur; } -void *cx_linked_list_find( +void *cx_linked_list_find_c( const void *start, ptrdiff_t loc_advance, ptrdiff_t loc_data, - cx_compare_func cmp_func, const void *elem, - size_t *found_index + size_t *found_index, + cx_compare_func2 cmp_func, + void *context ) { assert(start != NULL); assert(loc_advance >= 0); @@ -73,7 +83,7 @@ size_t index = 0; do { void *current = ll_data(node); - if (cmp_func(current, elem) == 0) { + if (cmp_func(current, elem, context) == 0) { if (found_index != NULL) { *found_index = index; } @@ -85,6 +95,19 @@ return NULL; } +void *cx_linked_list_find( + const void *start, + ptrdiff_t loc_advance, + ptrdiff_t loc_data, + const void *elem, + size_t *found_index, + cx_compare_func cmp_func +) { + cx_compare_func_wrapper wrapper = {cmp_func}; + return cx_linked_list_find_c(start, loc_advance, loc_data, + elem, found_index, cx_ccmp_wrap, &wrapper); +} + void *cx_linked_list_first( const void *node, ptrdiff_t loc_prev @@ -231,26 +254,14 @@ } } -void cx_linked_list_insert_sorted( - void **begin, - void **end, - ptrdiff_t loc_prev, - ptrdiff_t loc_next, - void *new_node, - cx_compare_func cmp_func -) { - assert(ll_next(new_node) == NULL); - cx_linked_list_insert_sorted_chain( - begin, end, loc_prev, loc_next, new_node, cmp_func); -} - static void *cx_linked_list_insert_sorted_chain_impl( void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *insert_begin, - cx_compare_func cmp_func, + cx_compare_func2 cmp_func, + void *context, bool allow_duplicates ) { assert(begin != NULL); @@ -267,7 +278,7 @@ // determine the new start { - int d = source_original == NULL ? 1 : cmp_func(source_original, source_argument); + int d = source_original == NULL ? 1 : cmp_func(source_original, source_argument, context); if (d <= 0) { // the new chain starts with the original chain new_begin = new_end = source_original; @@ -293,7 +304,7 @@ // now successively compare the elements and add them to the correct chains while (source_original != NULL && source_argument != NULL) { - int d = cmp_func(source_original, source_argument); + int d = cmp_func(source_original, source_argument, context); if (d <= 0) { // the original is not larger, add it to the chain cx_linked_list_link(new_end, source_original, loc_prev, loc_next); @@ -318,7 +329,7 @@ } else { // the original is larger, append the source argument to the chain // check if we must discard the source argument as duplicate - if (!allow_duplicates && cmp_func(new_end, source_argument) == 0) { + if (!allow_duplicates && cmp_func(new_end, source_argument, context) == 0) { if (dup_end == NULL) { dup_begin = dup_end = source_argument; } else { @@ -347,7 +358,7 @@ } else { // otherwise we must check one-by-one while (source_argument != NULL) { - if (cmp_func(new_end, source_argument) == 0) { + if (cmp_func(new_end, source_argument, context) == 0) { if (dup_end == NULL) { dup_begin = dup_end = source_argument; } else { @@ -385,6 +396,19 @@ return dup_begin; } +void cx_linked_list_insert_sorted( + void **begin, + void **end, + ptrdiff_t loc_prev, + ptrdiff_t loc_next, + void *new_node, + cx_compare_func cmp_func +) { + assert(ll_next(new_node) == NULL); + cx_linked_list_insert_sorted_chain( + begin, end, loc_prev, loc_next, new_node, cmp_func); +} + void cx_linked_list_insert_sorted_chain( void **begin, void **end, @@ -393,9 +417,10 @@ void *insert_begin, cx_compare_func cmp_func ) { + cx_compare_func_wrapper wrapper = {cmp_func}; cx_linked_list_insert_sorted_chain_impl( begin, end, loc_prev, loc_next, - insert_begin, cmp_func, true); + insert_begin, cx_ccmp_wrap, &wrapper, true); } int cx_linked_list_insert_unique( @@ -419,9 +444,66 @@ void *insert_begin, cx_compare_func cmp_func ) { + cx_compare_func_wrapper wrapper = {cmp_func}; return cx_linked_list_insert_sorted_chain_impl( begin, end, loc_prev, loc_next, - insert_begin, cmp_func, false); + insert_begin, cx_ccmp_wrap, &wrapper, false); +} + +void cx_linked_list_insert_sorted_c( + void **begin, + void **end, + ptrdiff_t loc_prev, + ptrdiff_t loc_next, + void *new_node, + cx_compare_func2 cmp_func, + void *context +) { + assert(ll_next(new_node) == NULL); + cx_linked_list_insert_sorted_chain_c( + begin, end, loc_prev, loc_next, new_node, cmp_func, context); +} + +void cx_linked_list_insert_sorted_chain_c( + void **begin, + void **end, + ptrdiff_t loc_prev, + ptrdiff_t loc_next, + void *insert_begin, + cx_compare_func2 cmp_func, + void *context +) { + cx_linked_list_insert_sorted_chain_impl( + begin, end, loc_prev, loc_next, + insert_begin, cmp_func, context, true); +} + +int cx_linked_list_insert_unique_c( + void **begin, + void **end, + ptrdiff_t loc_prev, + ptrdiff_t loc_next, + void *new_node, + cx_compare_func2 cmp_func, + void *context +) { + assert(ll_next(new_node) == NULL); + return NULL != cx_linked_list_insert_unique_chain_c( + begin, end, loc_prev, loc_next, new_node, cmp_func, context); +} + +void *cx_linked_list_insert_unique_chain_c( + void **begin, + void **end, + ptrdiff_t loc_prev, + ptrdiff_t loc_next, + void *insert_begin, + cx_compare_func2 cmp_func, + void *context +) { + return cx_linked_list_insert_sorted_chain_impl( + begin, end, loc_prev, loc_next, + insert_begin, cmp_func, context, false); } size_t cx_linked_list_remove_chain( @@ -502,6 +584,8 @@ #endif static void cx_linked_list_sort_merge( + void **begin, + void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, ptrdiff_t loc_data, @@ -509,21 +593,20 @@ void *ls, void *le, void *re, - cx_compare_func cmp_func, - void **begin, - void **end + cx_compare_func2 cmp_func, + void *context ) { void *sbo[CX_LINKED_LIST_SORT_SBO_SIZE]; void **sorted = length >= CX_LINKED_LIST_SORT_SBO_SIZE ? cxMallocDefault(sizeof(void *) * length) : sbo; - if (sorted == NULL) abort(); + if (sorted == NULL) abort(); // LCOV_EXCL_LINE void *rc, *lc; lc = ls; rc = le; size_t n = 0; while (lc && lc != le && rc != re) { - if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) { + if (cmp_func(ll_data(lc), ll_data(rc), context) <= 0) { sorted[n] = lc; lc = ll_next(lc); } else { @@ -557,13 +640,14 @@ } } -void cx_linked_list_sort( // NOLINT(misc-no-recursion) - purposely recursive function +void cx_linked_list_sort_c( // NOLINT(misc-no-recursion) - purposely recursive function void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, ptrdiff_t loc_data, - cx_compare_func cmp_func + cx_compare_func2 cmp_func, + void *context ) { assert(begin != NULL); assert(loc_next >= 0); @@ -581,7 +665,7 @@ // check how many elements are already sorted lc = ls; size_t ln = 1; - while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) { + while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc), context) > 0) { lc = ll_next(lc); ln++; } @@ -593,7 +677,7 @@ size_t rn = 1; rc = le; // skip already sorted elements - while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) { + while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc), context) > 0) { rc = ll_next(rc); rn++; } @@ -601,27 +685,65 @@ // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them void *sorted_begin, *sorted_end; - cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, + cx_linked_list_sort_merge(&sorted_begin, &sorted_end, + loc_prev, loc_next, loc_data, ln + rn, ls, le, re, cmp_func, - &sorted_begin, &sorted_end); + context); // Something left? Sort it! size_t remainder_length = cx_linked_list_size(re, loc_next); if (remainder_length > 0) { void *remainder = re; - cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, cmp_func); + cx_linked_list_sort_c(&remainder, NULL, loc_prev, loc_next, loc_data, cmp_func, context); // merge sorted list with (also sorted) remainder - cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, + cx_linked_list_sort_merge(&sorted_begin, &sorted_end, + loc_prev, loc_next, loc_data, ln + rn + remainder_length, sorted_begin, remainder, NULL, cmp_func, - &sorted_begin, &sorted_end); + context); } *begin = sorted_begin; if (end) *end = sorted_end; } } +void cx_linked_list_sort( + void **begin, + void **end, + ptrdiff_t loc_prev, + ptrdiff_t loc_next, + ptrdiff_t loc_data, + cx_compare_func cmp_func +) { + cx_compare_func_wrapper wrapper = {cmp_func}; + cx_linked_list_sort_c(begin, end, loc_prev, loc_next, loc_data, cx_ccmp_wrap, &wrapper); +} + +int cx_linked_list_compare_c( + const void *begin_left, + const void *begin_right, + ptrdiff_t loc_advance, + ptrdiff_t loc_data, + cx_compare_func2 cmp_func, + void *context +) { + const void *left = begin_left, *right = begin_right; + + while (left != NULL && right != NULL) { + const void *left_data = ll_data(left); + const void *right_data = ll_data(right); + int result = cmp_func(left_data, right_data, context); + if (result != 0) return result; + left = ll_advance(left); + right = ll_advance(right); + } + + if (left != NULL) { return 1; } + else if (right != NULL) { return -1; } + else { return 0; } +} + int cx_linked_list_compare( const void *begin_left, const void *begin_right, @@ -629,20 +751,9 @@ ptrdiff_t loc_data, cx_compare_func cmp_func ) { - const void *left = begin_left, *right = begin_right; - - while (left != NULL && right != NULL) { - const void *left_data = ll_data(left); - const void *right_data = ll_data(right); - int result = cmp_func(left_data, right_data); - if (result != 0) return result; - left = ll_advance(left); - right = ll_advance(right); - } - - if (left != NULL) { return 1; } - else if (right != NULL) { return -1; } - else { return 0; } + cx_compare_func_wrapper wrapper = {cmp_func}; + return cx_linked_list_compare_c(begin_left, begin_right, + loc_advance, loc_data, cx_ccmp_wrap, &wrapper); } void cx_linked_list_reverse( @@ -692,8 +803,13 @@ } static void *cx_ll_malloc_node(const cx_linked_list *list) { - return cxZalloc(list->base.collection.allocator, - list->loc_data + list->base.collection.elem_size + list->extra_data_len); + size_t n; + if (list->extra_data_len == 0) { + n = list->loc_data + list->base.collection.elem_size; + } else { + n = list->loc_extra + list->extra_data_len; + } + return cxZalloc(list->base.collection.allocator, n); } static int cx_ll_insert_at( @@ -784,13 +900,11 @@ } } -static _Thread_local cx_compare_func cx_ll_insert_sorted_cmp_func; -static _Thread_local off_t cx_ll_insert_sorted_loc_data; - -static int cx_ll_insert_sorted_cmp_helper(const void *l, const void *r) { - const char *left = (const char*)l + cx_ll_insert_sorted_loc_data; - const char *right = (const char*)r + cx_ll_insert_sorted_loc_data; - return cx_ll_insert_sorted_cmp_func(left, right); +static int cx_ll_insert_sorted_cmp_helper(const void *l, const void *r, void *c) { + cx_linked_list *list = c; + const char *left = (const char*)l + list->loc_data; + const char *right = (const char*)r + list->loc_data; + return cx_list_compare_wrapper(left, right, list); } static size_t cx_ll_insert_sorted_impl( @@ -825,29 +939,19 @@ } CX_LL_PTR(prev, ll->loc_next) = NULL; - // invoke the low level function - cx_ll_insert_sorted_cmp_func = list->collection.cmpfunc; - cx_ll_insert_sorted_loc_data = ll->loc_data; - if (allow_duplicates) { - cx_linked_list_insert_sorted_chain( - &ll->begin, - &ll->end, - ll->loc_prev, - ll->loc_next, - chain, - cx_ll_insert_sorted_cmp_helper - ); - list->collection.size += inserted; - } else { - void *duplicates = cx_linked_list_insert_unique_chain( - &ll->begin, - &ll->end, - ll->loc_prev, - ll->loc_next, - chain, - cx_ll_insert_sorted_cmp_helper - ); - list->collection.size += inserted; + // invoke the low-level function + void *duplicates = cx_linked_list_insert_sorted_chain_impl( + &ll->begin, + &ll->end, + ll->loc_prev, + ll->loc_next, + chain, + cx_ll_insert_sorted_cmp_helper, + list, + allow_duplicates + ); + list->collection.size += inserted; + if (!allow_duplicates) { // free the nodes that did not make it into the list while (duplicates != NULL) { void *next = CX_LL_PTR(duplicates, ll->loc_next); @@ -1076,12 +1180,12 @@ size_t index; cx_linked_list *ll = (cx_linked_list *) list; - char *node = cx_linked_list_find( + char *node = cx_linked_list_find_c( ll->begin, ll->loc_next, ll->loc_data, - list->collection.cmpfunc, elem, - &index - ); + elem, &index, + cx_list_compare_wrapper, + list); if (node == NULL) { return list->collection.size; } @@ -1097,9 +1201,9 @@ static void cx_ll_sort(struct cx_list_s *list) { cx_linked_list *ll = (cx_linked_list *) list; - cx_linked_list_sort(&ll->begin, &ll->end, + cx_linked_list_sort_c(&ll->begin, &ll->end, ll->loc_prev, ll->loc_next, ll->loc_data, - list->collection.cmpfunc); + cx_list_compare_wrapper, list); } static void cx_ll_reverse(struct cx_list_s *list) { @@ -1115,9 +1219,9 @@ cx_linked_list *right = (cx_linked_list *) other; assert(left->loc_next == right->loc_next); assert(left->loc_data == right->loc_data); - return cx_linked_list_compare(left->begin, right->begin, + return cx_linked_list_compare_c(left->begin, right->begin, left->loc_next, left->loc_data, - list->collection.cmpfunc); + cx_list_compare_wrapper, (void*)list); } static bool cx_ll_iter_valid(const void *it) { @@ -1215,7 +1319,7 @@ return result; } else { if (cx_ll_insert_element(list, list->collection.size, elem) == NULL) { - return 1; + return 1; // LCOV_EXCL_LINE } iter->elem_count++; iter->index = list->collection.size; @@ -1252,12 +1356,12 @@ cx_ll_sort, cx_ll_compare, cx_ll_reverse, + NULL, // no overallocation supported cx_ll_iterator, }; CxList *cxLinkedListCreate( const CxAllocator *allocator, - cx_compare_func comparator, size_t elem_size ) { if (allocator == NULL) { @@ -1266,12 +1370,22 @@ cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list)); if (list == NULL) return NULL; - list->extra_data_len = 0; list->loc_prev = 0; list->loc_next = sizeof(void*); list->loc_data = sizeof(void*)*2; + list->loc_extra = -1; + list->extra_data_len = 0; cx_list_init((CxList*)list, &cx_linked_list_class, - allocator, comparator, elem_size); + allocator, elem_size); return (CxList *) list; } + +void cx_linked_list_extra_data(cx_linked_list *list, size_t len) { + list->extra_data_len = len; + + off_t loc_extra = list->loc_data + list->base.collection.elem_size; + size_t alignment = alignof(void*); + size_t padding = alignment - (loc_extra % alignment); + list->loc_extra = loc_extra + padding; +}
--- a/ucx/list.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/list.c Fri Dec 19 17:53:18 2025 +0100 @@ -31,189 +31,35 @@ #include <string.h> #include <assert.h> -// <editor-fold desc="Store Pointers Functionality"> - -static _Thread_local cx_compare_func cx_pl_cmpfunc_impl; +// we don't want to include the full array_list.h. +// therefore, we only forward declare the one function we want to use +CX_EXPORT void cx_array_qsort_c(void *array, size_t nmemb, size_t size, + cx_compare_func2 fn, void *context); -static int cx_pl_cmpfunc( - const void *l, - const void *r -) { - // l and r are guaranteed to be non-NULL pointing to the list's memory - void *const *lptr = l; - void *const *rptr = r; - const void *left = *lptr; - const void *right = *rptr; - if (left == NULL) { - // NULL is smaller than any value except NULL - return right == NULL ? 0 : -1; - } else if (right == NULL) { - // any value is larger than NULL - return 1; - } - return cx_pl_cmpfunc_impl(left, right); -} - -static void cx_pl_hack_cmpfunc(const struct cx_list_s *list) { - // cast away const - this is the hacky thing - struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; - cx_pl_cmpfunc_impl = l->cmpfunc; - l->cmpfunc = cx_pl_cmpfunc; -} - -static void cx_pl_unhack_cmpfunc(const struct cx_list_s *list) { - // cast away const - this is the hacky thing - struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; - l->cmpfunc = cx_pl_cmpfunc_impl; -} - -static void cx_pl_destructor(struct cx_list_s *list) { - list->climpl->deallocate(list); -} -static void *cx_pl_insert_element( - struct cx_list_s *list, - size_t index, - const void *element -) { - return list->climpl->insert_element(list, index, &element); -} - -static size_t cx_pl_insert_array( - struct cx_list_s *list, - size_t index, - const void *array, - size_t n -) { - return list->climpl->insert_array(list, index, array, n); -} - -static size_t cx_pl_insert_sorted( - struct cx_list_s *list, - const void *array, - size_t n -) { - cx_pl_hack_cmpfunc(list); - size_t result = list->climpl->insert_sorted(list, array, n); - cx_pl_unhack_cmpfunc(list); - return result; -} - -static size_t cx_pl_insert_unique( - struct cx_list_s *list, - const void *array, - size_t n -) { - cx_pl_hack_cmpfunc(list); - size_t result = list->climpl->insert_unique(list, array, n); - cx_pl_unhack_cmpfunc(list); - return result; -} - -static int cx_pl_insert_iter( - struct cx_iterator_s *iter, - const void *elem, - int prepend -) { - struct cx_list_s *list = iter->src_handle; - return list->climpl->insert_iter(iter, &elem, prepend); +int cx_list_compare_wrapper(const void *l, const void *r, void *c) { + CxList *list = c; + const void *left; + const void *right; + if (cxCollectionStoresPointers(list)) { + left = *(void**)l; + right = *(void**)r; + // for historic reasons, we are handling the NULL case here + // because every UCX compare function does not support NULL arguments + if (left == NULL) { + if (right == NULL) return 0; + return -1; + } else if (right == NULL) { + return 1; + } + } else { + left = l; + right = r; + } + return cx_invoke_compare_func(list, left, right); } -static size_t cx_pl_remove( - struct cx_list_s *list, - size_t index, - size_t num, - void *targetbuf -) { - return list->climpl->remove(list, index, num, targetbuf); -} - -static void cx_pl_clear(struct cx_list_s *list) { - list->climpl->clear(list); -} - -static int cx_pl_swap( - struct cx_list_s *list, - size_t i, - size_t j -) { - return list->climpl->swap(list, i, j); -} - -static void *cx_pl_at( - const struct cx_list_s *list, - size_t index -) { - void **ptr = list->climpl->at(list, index); - return ptr == NULL ? NULL : *ptr; -} - -static size_t cx_pl_find_remove( - struct cx_list_s *list, - const void *elem, - bool remove -) { - cx_pl_hack_cmpfunc(list); - size_t ret = list->climpl->find_remove(list, &elem, remove); - cx_pl_unhack_cmpfunc(list); - return ret; -} - -static void cx_pl_sort(struct cx_list_s *list) { - cx_pl_hack_cmpfunc(list); - list->climpl->sort(list); - cx_pl_unhack_cmpfunc(list); -} - -static int cx_pl_compare( - const struct cx_list_s *list, - const struct cx_list_s *other -) { - cx_pl_hack_cmpfunc(list); - int ret = list->climpl->compare(list, other); - cx_pl_unhack_cmpfunc(list); - return ret; -} - -static void cx_pl_reverse(struct cx_list_s *list) { - list->climpl->reverse(list); -} - -static void *cx_pl_iter_current(const void *it) { - const struct cx_iterator_s *iter = it; - void **ptr = iter->base.current_impl(it); - return ptr == NULL ? NULL : *ptr; -} - -static struct cx_iterator_s cx_pl_iterator( - const struct cx_list_s *list, - size_t index, - bool backwards -) { - struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards); - iter.base.current_impl = iter.base.current; - iter.base.current = cx_pl_iter_current; - return iter; -} - -static cx_list_class cx_pointer_list_class = { - cx_pl_destructor, - cx_pl_insert_element, - cx_pl_insert_array, - cx_pl_insert_sorted, - cx_pl_insert_unique, - cx_pl_insert_iter, - cx_pl_remove, - cx_pl_clear, - cx_pl_swap, - cx_pl_at, - cx_pl_find_remove, - cx_pl_sort, - cx_pl_compare, - cx_pl_reverse, - cx_pl_iterator, -}; -// </editor-fold> +#define cx_list_compare_wrapper(l, r, c) cx_list_compare_wrapper(l, r, (void*)c) // <editor-fold desc="empty list implementation"> @@ -267,33 +113,31 @@ cx_emptyl_noop, NULL, cx_emptyl_noop, + NULL, cx_emptyl_iterator, }; CxList cx_empty_list = { { NULL, - NULL, 0, 0, NULL, NULL, NULL, + NULL, + NULL, + NULL, false, true, }, &cx_empty_list_class, - NULL }; CxList *const cxEmptyList = &cx_empty_list; // </editor-fold> -#define invoke_list_func(name, list, ...) \ - ((list)->climpl == NULL ? (list)->cl->name : (list)->climpl->name) \ - (list, __VA_ARGS__) - size_t cx_list_default_insert_array( struct cx_list_s *list, size_t index, @@ -303,8 +147,7 @@ const char *src = data; size_t i = 0; for (; i < n; i++) { - if (NULL == invoke_list_func( - insert_element, list, index + i, src) + if (NULL == list->cl->insert_element(list, index + i, src) ) { return i; // LCOV_EXCL_LINE } @@ -325,7 +168,6 @@ if (n == 0) return 0; size_t elem_size = list->collection.elem_size; - cx_compare_func cmp = list->collection.cmpfunc; const char *src = sorted_data; // track indices and number of inserted items @@ -333,19 +175,19 @@ // search the list for insertion points while (di < list->collection.size) { - const void *list_elm = invoke_list_func(at, list, di); + const void *list_elm = list->cl->at(list, di); // compare the current list element with the first source element // if less, skip the list elements // if equal, skip the list elements and optionally the source elements { - int d = cmp(list_elm, src); + int d = cx_list_compare_wrapper(list_elm, src, list); if (d <= 0) { if (!allow_duplicates && d == 0) { src += elem_size; si++; processed++; // we also count duplicates for the return value - while (si < n && cmp(list_elm, src) == 0) { + while (si < n && cx_list_compare_wrapper(list_elm, src, list) == 0) { src += elem_size; si++; processed++; @@ -365,7 +207,7 @@ while (++si < n) { if (!allow_duplicates) { // skip duplicates within the source - if (cmp(next, next + elem_size) == 0) { + if (cx_list_compare_wrapper(next, next + elem_size, list) == 0) { next += elem_size; skip++; continue; @@ -379,7 +221,7 @@ } next += elem_size; // once we become larger than the list elem, break - if (cmp(list_elm, next) <= 0) { + if (cx_list_compare_wrapper(list_elm, next, list) <= 0) { break; } // otherwise, we can insert one more @@ -388,11 +230,11 @@ // insert the elements at location si if (ins == 1) { - if (NULL == invoke_list_func(insert_element, list, di, src)) { + if (NULL == list->cl->insert_element(list, di, src)) { return processed; // LCOV_EXCL_LINE } } else { - size_t r = invoke_list_func(insert_array, list, di, src, ins); + size_t r = list->cl->insert_array(list, di, src, ins); if (r < ins) { return processed + r; // LCOV_EXCL_LINE } @@ -410,13 +252,13 @@ // insert remaining items if (si < n) { if (allow_duplicates) { - processed += invoke_list_func(insert_array, list, di, src, n - si); + processed += list->cl->insert_array(list, di, src, n - si); } else { - const void *last = di == 0 ? NULL : invoke_list_func(at, list, di - 1); + const void *last = di == 0 ? NULL : list->cl->at(list, di - 1); for (; si < n; si++) { // skip duplicates within the source - if (last == NULL || cmp(last, src) != 0) { - if (NULL == invoke_list_func(insert_element, list, di, src)) { + if (last == NULL || cx_list_compare_wrapper(last, src, list) != 0) { + if (NULL == list->cl->insert_element(list, di, src)) { return processed; // LCOV_EXCL_LINE } last = src; @@ -456,19 +298,18 @@ // copy elements from source array char *loc = tmp; for (size_t i = 0; i < list_size; i++) { - void *src = invoke_list_func(at, list, i); + void *src = list->cl->at(list, i); memcpy(loc, src, elem_size); loc += elem_size; } // qsort - qsort(tmp, list_size, elem_size, - list->collection.cmpfunc); + cx_array_qsort_c(tmp, list_size, elem_size, cx_list_compare_wrapper, list); // copy elements back loc = tmp; for (size_t i = 0; i < list_size; i++) { - void *dest = invoke_list_func(at, list, i); + void *dest = list->cl->at(list, i); memcpy(dest, loc, elem_size); loc += elem_size; } @@ -486,8 +327,8 @@ void *tmp = cxMallocDefault(elem_size); if (tmp == NULL) return 1; // LCOV_EXCL_LINE - void *ip = invoke_list_func(at, list, i); - void *jp = invoke_list_func(at, list, j); + void *ip = list->cl->at(list, i); + void *jp = list->cl->at(list, j); memcpy(tmp, ip, elem_size); memcpy(ip, jp, elem_size); @@ -502,22 +343,24 @@ struct cx_list_s *list, struct cx_list_class_s *cl, const struct cx_allocator_s *allocator, - cx_compare_func comparator, size_t elem_size ) { list->cl = cl; list->collection.allocator = allocator; - list->collection.cmpfunc = comparator; + list->collection.size = 0; + list->collection.sorted = false; // should be set by the implementation if (elem_size > 0) { list->collection.elem_size = elem_size; + list->collection.simple_cmp = NULL; + list->collection.advanced_cmp = cx_ccmp_memcmp; + list->collection.cmp_data = &list->collection.elem_size; + list->collection.store_pointer = false; } else { list->collection.elem_size = sizeof(void *); - if (list->collection.cmpfunc == NULL) { - list->collection.cmpfunc = cx_cmp_ptr; - } + list->collection.simple_cmp = cx_cmp_ptr; + list->collection.advanced_cmp = NULL; + list->collection.cmp_data = NULL; list->collection.store_pointer = true; - list->climpl = list->cl; - list->cl = &cx_pointer_list_class; } } @@ -525,33 +368,28 @@ const CxList *list, const CxList *other ) { + // check if we cannot use the list internal function bool cannot_optimize = false; // if one is storing pointers but the other is not cannot_optimize |= list->collection.store_pointer ^ other->collection.store_pointer; - // if one class is wrapped but the other is not - cannot_optimize |= (list->climpl == NULL) ^ (other->climpl == NULL); - - // if the compare functions do not match or both are NULL - if (!cannot_optimize) { - cx_compare_func list_cmp = (cx_compare_func) (list->climpl != NULL ? - list->climpl->compare : list->cl->compare); - cx_compare_func other_cmp = (cx_compare_func) (other->climpl != NULL ? - other->climpl->compare : other->cl->compare); - cannot_optimize |= list_cmp != other_cmp; - cannot_optimize |= list_cmp == NULL; - } + // check if the lists are incompatible or this list does not implement compare + cx_compare_func list_cmp = (cx_compare_func) list->cl->compare; + cx_compare_func other_cmp = (cx_compare_func) other->cl->compare; + cannot_optimize |= list_cmp != other_cmp; + cannot_optimize |= list_cmp == NULL; if (cannot_optimize) { // lists are definitely different - cannot use internal compare function if (list->collection.size == other->collection.size) { - CxIterator left = list->cl->iterator(list, 0, false); - CxIterator right = other->cl->iterator(other, 0, false); + CxIterator left = cxListIterator(list); + CxIterator right = cxListIterator(other); for (size_t i = 0; i < list->collection.size; i++) { void *leftValue = cxIteratorCurrent(left); void *rightValue = cxIteratorCurrent(right); - int d = list->collection.cmpfunc(leftValue, rightValue); + // values are already unwrapped, invoke immediately + int d = cx_invoke_compare_func(list, leftValue, rightValue); if (d != 0) { return d; } @@ -574,7 +412,7 @@ int cxListAdd(CxList *list, const void *elem) { list->collection.sorted = false; - return list->cl->insert_element(list, list->collection.size, elem) == NULL; + return list->cl->insert_element(list, list->collection.size, cx_ref(list, elem)) == NULL; } size_t cxListAddArray(CxList *list, const void *array, size_t n) { @@ -584,7 +422,7 @@ int cxListInsert(CxList *list, size_t index, const void *elem) { list->collection.sorted = false; - return list->cl->insert_element(list, index, elem) == NULL; + return list->cl->insert_element(list, index, cx_ref(list, elem)) == NULL; } void *cxListEmplaceAt(CxList *list, size_t index) { @@ -611,11 +449,6 @@ iter.index = 0; // replace the valid function to abort iteration when c is reached iter.base.valid = cx_list_emplace_iterator_valid; - // if we are storing pointers, we want to return the pure pointers. - // therefore, we must unwrap the "current" method - if (list->collection.store_pointer) { - iter.base.current = iter.base.current_impl; - } return iter; } @@ -626,15 +459,13 @@ int cxListInsertSorted(CxList *list, const void *elem) { assert(cxCollectionSorted(list)); list->collection.sorted = true; - const void *data = list->collection.store_pointer ? &elem : elem; - return list->cl->insert_sorted(list, data, 1) == 0; + return list->cl->insert_sorted(list, cx_ref(list, elem), 1) == 0; } int cxListInsertUnique(CxList *list, const void *elem) { if (cxCollectionSorted(list)) { list->collection.sorted = true; - const void *data = list->collection.store_pointer ? &elem : elem; - return list->cl->insert_unique(list, data, 1) == 0; + return list->cl->insert_unique(list, cx_ref(list, elem), 1) == 0; } else { if (cxListContains(list, elem)) { return 0; @@ -663,8 +494,7 @@ const char *source = array; for (size_t i = 0 ; i < n; i++) { // note: this also checks elements added in a previous iteration - const void *data = list->collection.store_pointer ? - *((const void**)source) : source; + const void *data = cx_deref(list, source); if (!cxListContains(list, data)) { if (cxListAdd(list, data)) { return i; // LCOV_EXCL_LINE @@ -677,15 +507,15 @@ } int cxListInsertAfter(CxIterator *iter, const void *elem) { - CxList* list = (CxList*)iter->src_handle; + CxList* list = iter->src_handle; list->collection.sorted = false; - return list->cl->insert_iter(iter, elem, 0); + return list->cl->insert_iter(iter, cx_ref(list, elem), 0); } int cxListInsertBefore(CxIterator *iter, const void *elem) { - CxList* list = (CxList*)iter->src_handle; + CxList* list = iter->src_handle; list->collection.sorted = false; - return list->cl->insert_iter(iter, elem, 1); + return list->cl->insert_iter(iter, cx_ref(list, elem), 1); } int cxListRemove(CxList *list, size_t index) { @@ -724,15 +554,17 @@ } void *cxListAt(const CxList *list, size_t index) { - return list->cl->at(list, index); + void *result = list->cl->at(list, index); + if (result == NULL) return NULL; + return cx_deref(list, result); } void *cxListFirst(const CxList *list) { - return list->cl->at(list, 0); + return cxListAt(list, 0); } void *cxListLast(const CxList *list) { - return list->cl->at(list, list->collection.size - 1); + return cxListAt(list, list->collection.size - 1); } int cxListSet(CxList *list, size_t index, const void *elem) { @@ -741,8 +573,7 @@ } if (list->collection.store_pointer) { - // For pointer collections, always use climpl - void **target = list->climpl->at(list, index); + void **target = list->cl->at(list, index); *target = (void *)elem; } else { void *target = list->cl->at(list, index); @@ -752,32 +583,48 @@ return 0; } +static void *cx_pl_iter_current(const void *it) { + const struct cx_iterator_s *iter = it; + void **ptr = iter->base.current_impl(it); + return ptr == NULL ? NULL : *ptr; +} + +CX_INLINE CxIterator cx_pl_iter_wrap(const CxList *list, CxIterator iter) { + if (cxCollectionStoresPointers(list)) { + iter.base.current_impl = iter.base.current; + iter.base.current = cx_pl_iter_current; + return iter; + } else { + return iter; + } +} + CxIterator cxListIteratorAt(const CxList *list, size_t index) { if (list == NULL) list = cxEmptyList; - return list->cl->iterator(list, index, false); + return cx_pl_iter_wrap(list, list->cl->iterator(list, index, false)); } CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index) { if (list == NULL) list = cxEmptyList; - return list->cl->iterator(list, index, true); + return cx_pl_iter_wrap(list, list->cl->iterator(list, index, true)); } CxIterator cxListIterator(const CxList *list) { if (list == NULL) list = cxEmptyList; - return list->cl->iterator(list, 0, false); + return cx_pl_iter_wrap(list, list->cl->iterator(list, 0, false)); } CxIterator cxListBackwardsIterator(const CxList *list) { if (list == NULL) list = cxEmptyList; - return list->cl->iterator(list, list->collection.size - 1, true); + return cx_pl_iter_wrap(list, list->cl->iterator(list, list->collection.size - 1, true)); } size_t cxListFind(const CxList *list, const void *elem) { - return list->cl->find_remove((CxList*)list, elem, false); + return list->cl->find_remove((CxList*)list, cx_ref(list, elem), false); } bool cxListContains(const CxList* list, const void* elem) { - return list->cl->find_remove((CxList*)list, elem, false) < list->collection.size; + return list->cl->find_remove((CxList*)list, cx_ref(list, elem), false) < list->collection.size; } bool cxListIndexValid(const CxList *list, size_t index) { @@ -785,7 +632,7 @@ } size_t cxListFindRemove(CxList *list, const void *elem) { - return list->cl->find_remove(list, elem, true); + return list->cl->find_remove(list, cx_ref(list, elem), true); } void cxListSort(CxList *list) { @@ -819,6 +666,15 @@ list->collection.advanced_destructor = destr2_bak; } +static void* cx_list_shallow_clone_func(void *dst, const void *src, const CxAllocator *al, void *data) { + size_t elem_size = *(size_t*)data; + if (dst == NULL) dst = cxMalloc(al, elem_size); + if (dst != NULL) memcpy(dst, src, elem_size); + return dst; +} + +#define use_shallow_clone_func(list) cx_list_shallow_clone_func, NULL, (void*)&((list)->collection.elem_size) + int cxListClone(CxList *dst, const CxList *src, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; @@ -858,6 +714,11 @@ return 1; } + // set the sorted flag when we know it's sorted + if (orig_size == 0 && src->collection.sorted) { + dst->collection.sorted = true; + } + return 0; } @@ -878,8 +739,7 @@ int d; if (cxIteratorValid(sub_iter)) { sub_elem = cxIteratorCurrent(sub_iter); - cx_compare_func cmp = subtrahend->collection.cmpfunc; - d = cmp(sub_elem, min_elem); + d = cx_list_compare_wrapper(sub_elem, min_elem, subtrahend); } else { // no more elements in the subtrahend, // i.e., the min_elem is larger than any elem of the subtrahend @@ -947,7 +807,7 @@ while (cxIteratorValid(src_iter) && cxIteratorValid(other_iter)) { void *src_elem = cxIteratorCurrent(src_iter); void *other_elem = cxIteratorCurrent(other_iter); - int d = src->collection.cmpfunc(src_elem, other_elem); + int d = cx_list_compare_wrapper(src_elem, other_elem, src); if (d == 0) { // is contained, clone it void **dst_mem = cxListEmplace(dst); @@ -1006,7 +866,7 @@ CxIterator src_iter = cxListIterator(src); CxIterator other_iter = cxListIterator(other); while (cxIteratorValid(src_iter) || cxIteratorValid(other_iter)) { - void *src_elem, *other_elem; + void *src_elem = NULL, *other_elem = NULL; int d; if (!cxIteratorValid(src_iter)) { other_elem = cxIteratorCurrent(other_iter); @@ -1017,39 +877,33 @@ } else { src_elem = cxIteratorCurrent(src_iter); other_elem = cxIteratorCurrent(other_iter); - d = src->collection.cmpfunc(src_elem, other_elem); + d = cx_list_compare_wrapper(src_elem, other_elem, src); } - if (d <= 0) { - // source element is smaller or equal, clone it - void **dst_mem = cxListEmplace(dst); - void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; - void* dst_ptr = clone_func(target, src_elem, clone_allocator, data); - if (dst_ptr == NULL) { - cx_list_pop_uninitialized_elements(dst, 1); - return 1; - } - if (cxCollectionStoresPointers(dst)) { - *dst_mem = dst_ptr; - } + void *clone_from; + if (d < 0) { + // source element is smaller clone it + clone_from = src_elem; cxIteratorNext(src_iter); - // if the other element was equal, skip it - if (d == 0) { - cxIteratorNext(other_iter); - } + } else if (d == 0) { + // both elements are equal, clone from the source, skip other + clone_from = src_elem; + cxIteratorNext(src_iter); + cxIteratorNext(other_iter); } else { // the other element is smaller, clone it - void **dst_mem = cxListEmplace(dst); - void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; - void* dst_ptr = clone_func(target, other_elem, clone_allocator, data); - if (dst_ptr == NULL) { - cx_list_pop_uninitialized_elements(dst, 1); - return 1; - } - if (cxCollectionStoresPointers(dst)) { - *dst_mem = dst_ptr; - } + clone_from = other_elem; cxIteratorNext(other_iter); } + void **dst_mem = cxListEmplace(dst); + void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; + void* dst_ptr = clone_func(target, clone_from, clone_allocator, data); + if (dst_ptr == NULL) { + cx_list_pop_uninitialized_elements(dst, 1); + return 1; + } + if (cxCollectionStoresPointers(dst)) { + *dst_mem = dst_ptr; + } } // if dst was empty, it is now guaranteed to be sorted @@ -1078,3 +932,36 @@ return 0; } + +int cxListCloneShallow(CxList *dst, const CxList *src) { + return cxListClone(dst, src, use_shallow_clone_func(src)); +} + +int cxListDifferenceShallow(CxList *dst, const CxList *minuend, const CxList *subtrahend) { + return cxListDifference(dst, minuend, subtrahend, use_shallow_clone_func(minuend)); +} + +int cxListIntersectionShallow(CxList *dst, const CxList *src, const CxList *other) { + return cxListIntersection(dst, src, other, use_shallow_clone_func(src)); +} + +int cxListUnionShallow(CxList *dst, const CxList *src, const CxList *other) { + return cxListUnion(dst, src, other, use_shallow_clone_func(src)); +} + +int cxListReserve(CxList *list, size_t capacity) { + if (list->cl->change_capacity == NULL) { + return 0; + } + if (capacity <= cxCollectionSize(list)) { + return 0; + } + return list->cl->change_capacity(list, capacity); +} + +int cxListShrink(CxList *list) { + if (list->cl->change_capacity == NULL) { + return 0; + } + return list->cl->change_capacity(list, cxCollectionSize(list)); +}
--- a/ucx/map.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/map.c Fri Dec 19 17:53:18 2025 +0100 @@ -70,12 +70,14 @@ CxMap cx_empty_map = { { NULL, - NULL, 0, 0, NULL, NULL, NULL, + NULL, + NULL, + NULL, false, true }, @@ -110,11 +112,13 @@ } int cx_map_put(CxMap *map, CxHashKey key, void *value) { - return map->cl->put(map, key, value) == NULL; + return map->cl->put(map, key, value).key == NULL; } void *cx_map_emplace(CxMap *map, CxHashKey key) { - return map->cl->put(map, key, NULL); + const CxMapEntry entry = map->cl->put(map, key, NULL); + if (entry.key == NULL) return NULL; + return entry.value; } void *cx_map_get(const CxMap *map, CxHashKey key) { @@ -140,6 +144,15 @@ map->collection.advanced_destructor = destr2_bak; } +static void* cx_map_shallow_clone_func(void *dst, const void *src, const CxAllocator *al, void *data) { + size_t elem_size = *(size_t*)data; + if (dst == NULL) dst = cxMalloc(al, elem_size); + if (dst != NULL) memcpy(dst, src, elem_size); + return dst; +} + +#define use_shallow_clone_func(map) cx_map_shallow_clone_func, NULL, (void*)&((map)->collection.elem_size) + int cxMapClone(CxMap *dst, const CxMap *src, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; @@ -293,3 +306,56 @@ } return 0; } + +int cxMapCloneShallow(CxMap *dst, const CxMap *src) { + return cxMapClone(dst, src, use_shallow_clone_func(src)); +} + +int cxMapDifferenceShallow(CxMap *dst, const CxMap *minuend, const CxMap *subtrahend) { + return cxMapDifference(dst, minuend, subtrahend, use_shallow_clone_func(minuend)); +} + +int cxMapListDifferenceShallow(CxMap *dst, const CxMap *src, const CxList *keys) { + return cxMapListDifference(dst, src, keys, use_shallow_clone_func(src)); +} + +int cxMapIntersectionShallow(CxMap *dst, const CxMap *src, const CxMap *other) { + return cxMapIntersection(dst, src, other, use_shallow_clone_func(src)); +} + +int cxMapListIntersectionShallow(CxMap *dst, const CxMap *src, const CxList *keys) { + return cxMapListIntersection(dst, src, keys, use_shallow_clone_func(src)); +} + +int cxMapUnionShallow(CxMap *dst, const CxMap *src) { + return cxMapUnion(dst, src, use_shallow_clone_func(src)); +} + +int cxMapCompare(const CxMap *map, const CxMap *other) { + // compare map sizes + const size_t size_left = cxMapSize(map); + const size_t size_right = cxMapSize(other); + if (size_left < size_right) { + return -1; + } else if (size_left > size_right) { + return 1; + } + + // iterate through the first map + CxMapIterator iter = cxMapIterator(map); + cx_foreach(const CxMapEntry *, entry, iter) { + const void *value_left = entry->value; + const void *value_right = cxMapGet(other, *entry->key); + // if the other map does not have the key, we are done + if (value_right == NULL) { + return -1; + } + // compare the values + const int d = cx_invoke_compare_func(map, value_left, value_right); + if (d != 0) { + return d; + } + } + + return 0; +}
--- a/ucx/printf.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/printf.c Fri Dec 19 17:53:18 2025 +0100 @@ -61,8 +61,10 @@ va_copy(ap2, ap); int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap); if (ret < 0) { + // LCOV_EXCL_START va_end(ap2); return ret; + // LCOV_EXCL_STOP } else if (ret < CX_PRINTF_SBO_SIZE) { va_end(ap2); return (int) wfc(buf, 1, ret, stream); @@ -121,8 +123,10 @@ if (s.ptr) { ret = vsnprintf(s.ptr, len, fmt, ap2); if (ret < 0) { + // LCOV_EXCL_START cxFree(a, s.ptr); s.ptr = NULL; + // LCOV_EXCL_STOP } else { s.length = (size_t) ret; } @@ -162,7 +166,7 @@ if (ptr) { int newret = vsnprintf(ptr, newlen, fmt, ap2); if (newret < 0) { - cxFree(alloc, ptr); + cxFree(alloc, ptr); // LCOV_EXCL_LINE } else { *len = newlen; *str = ptr; @@ -207,7 +211,7 @@ if (ptr) { int newret = vsnprintf(ptr, newlen, fmt, ap2); if (newret < 0) { - cxFree(alloc, ptr); + cxFree(alloc, ptr); // LCOV_EXCL_LINE } else { *len = newlen; *str = ptr;
--- a/ucx/properties.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/properties.c Fri Dec 19 17:53:18 2025 +0100 @@ -29,12 +29,15 @@ #include "cx/properties.h" #include <assert.h> +#include <stdio.h> +#include <string.h> +#include <ctype.h> const CxPropertiesConfig cx_properties_config_default = { - '=', - '#', - '\0', - '\0', + '=', + '#', + '\0', + '\0', '\\', }; @@ -65,8 +68,8 @@ if (cxBufferEof(&prop->input)) { // destroy a possible previously initialized buffer cxBufferDestroy(&prop->input); - cxBufferInit(&prop->input, (void*) buf, len, - NULL, CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&prop->input, NULL, (void*) buf, + len, CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_AUTO_EXTEND); prop->input.size = len; } else { if (cxBufferAppend(buf, 1, len, &prop->input) < len) return -1; @@ -79,7 +82,7 @@ char *buf, size_t capacity ) { - cxBufferInit(&prop->buffer, buf, capacity, NULL, CX_BUFFER_COPY_ON_EXTEND); + cxBufferInit(&prop->buffer, NULL, buf, capacity, CX_BUFFER_COPY_ON_EXTEND); } CxPropertiesStatus cxPropertiesNext( @@ -94,13 +97,30 @@ // a pointer to the buffer we want to read from CxBuffer *current_buffer = &prop->input; - + + char comment1 = prop->config.comment1; + char comment2 = prop->config.comment2; + char comment3 = prop->config.comment3; + char delimiter = prop->config.delimiter; + char continuation = prop->config.continuation; + // check if we have rescued data if (!cxBufferEof(&prop->buffer)) { // check if we can now get a complete line cxstring input = cx_strn(prop->input.space + prop->input.pos, prop->input.size - prop->input.pos); cxstring nl = cx_strchr(input, '\n'); + while (nl.length > 0) { + // check for line continuation + char previous = nl.ptr > input.ptr ? nl.ptr[-1] : prop->buffer.space[prop->buffer.size-1]; + if (previous == continuation) { + // this nl is a line continuation, check the next newline + nl = cx_strchr(cx_strsubs(nl, 1), '\n'); + } else { + break; + } + } + if (nl.length > 0) { // we add as much data to the rescue buffer as we need // to complete the line @@ -120,19 +140,14 @@ // still not enough data, copy input buffer to internal buffer if (cxBufferAppend(input.ptr, 1, input.length, &prop->buffer) < input.length) { - return CX_PROPERTIES_BUFFER_ALLOC_FAILED; + return CX_PROPERTIES_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE } // reset the input buffer (make way for a re-fill) cxBufferReset(&prop->input); return CX_PROPERTIES_INCOMPLETE_DATA; } } - - char comment1 = prop->config.comment1; - char comment2 = prop->config.comment2; - char comment3 = prop->config.comment3; - char delimiter = prop->config.delimiter; - + // get one line and parse it while (!cxBufferEof(current_buffer)) { const char *buf = current_buffer->space + current_buffer->pos; @@ -145,6 +160,7 @@ size_t delimiter_index = 0; size_t comment_index = 0; bool has_comment = false; + bool has_continuation = false; size_t i = 0; char c = 0; @@ -159,6 +175,9 @@ if (delimiter_index == 0 && !has_comment) { delimiter_index = i; } + } else if (delimiter_index > 0 && c == continuation && i+1 < len && buf[i+1] == '\n') { + has_continuation = true; + i++; } else if (c == '\n') { break; } @@ -171,7 +190,7 @@ assert(cxBufferEof(&prop->buffer)); if (prop->buffer.space == NULL) { // initialize a rescue buffer, if the user did not provide one - cxBufferInit(&prop->buffer, NULL, 256, NULL, CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&prop->buffer, NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND); } else { // from a previous rescue there might be already read data // reset the buffer to avoid unnecessary buffer extension @@ -223,10 +242,53 @@ k = cx_strtrim(k); val = cx_strtrim(val); if (k.length > 0) { + current_buffer->pos += i + 1; + assert(current_buffer->pos <= current_buffer->size); + assert(current_buffer != &prop->buffer || current_buffer->pos == current_buffer->size); + + if (has_continuation) { + char *ptr = (char*)val.ptr; + if (current_buffer != &prop->buffer) { + // move value to the rescue buffer + if (prop->buffer.space == NULL) { + cxBufferInit(&prop->buffer, NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND); + } + prop->buffer.size = 0; + prop->buffer.pos = 0; + if (cxBufferWrite(val.ptr, 1, val.length, &prop->buffer) != val.length) { + return CX_PROPERTIES_BUFFER_ALLOC_FAILED; + } + val.ptr = prop->buffer.space; + ptr = prop->buffer.space; + } + // value.ptr is now inside the rescue buffer and we can + // remove the continuation character from the value + bool trim = false; + size_t x = 0; + for(size_t j=0;j<val.length;j++) { + c = ptr[j]; + if (j+1 < val.length && c == '\\' && ptr[j+1] == '\n') { + // skip continuation and newline character + j++; + trim = true; // enable trim in the next line + continue; + } + if (j > x) { + if (trim) { + if (isspace((unsigned char)c)) { + continue; + } + trim = false; + } + ptr[x] = c; + } + x++; + } + val.length = x; + } *key = k; *value = val; - current_buffer->pos += i + 1; - assert(current_buffer->pos <= current_buffer->size); + return CX_PROPERTIES_NO_ERROR; } else { return CX_PROPERTIES_INVALID_EMPTY_KEY; @@ -241,180 +303,96 @@ return CX_PROPERTIES_NO_DATA; } -static int cx_properties_sink_map( - cx_attr_unused CxProperties *prop, - CxPropertiesSink *sink, - cxstring key, - cxstring value -) { - CxMap *map = sink->sink; - CxAllocator *alloc = sink->data; - cxmutstr v = cx_strdup_a(alloc, value); - int r = cxMapPut(map, key, v.ptr); - if (r != 0) cx_strfree_a(alloc, &v); - return r; -} - -CxPropertiesSink cxPropertiesMapSink(CxMap *map) { - CxPropertiesSink sink; - sink.sink = map; - sink.data = (void*) cxDefaultAllocator; - sink.sink_func = cx_properties_sink_map; - return sink; -} +#ifndef CX_PROPERTIES_LOAD_FILL_SIZE +#define CX_PROPERTIES_LOAD_FILL_SIZE 1024 +#endif +const unsigned cx_properties_load_fill_size = CX_PROPERTIES_LOAD_FILL_SIZE; +#ifndef CX_PROPERTIES_LOAD_BUF_SIZE +#define CX_PROPERTIES_LOAD_BUF_SIZE 256 +#endif +const unsigned cx_properties_load_buf_size = CX_PROPERTIES_LOAD_BUF_SIZE; -static int cx_properties_read_string( - CxProperties *prop, - CxPropertiesSource *src, - cxstring *target -) { - if (prop->input.space == src->src) { - // when the input buffer already contains the string - // we have nothing more to provide - target->length = 0; - } else { - target->ptr = src->src; - target->length = src->data_size; +CxPropertiesStatus cx_properties_load(const CxAllocator *allocator, + cxstring filename, CxMap *target, CxPropertiesConfig config) { + if (allocator == NULL) { + allocator = cxDefaultAllocator; } - return 0; -} - -static int cx_properties_read_file( - cx_attr_unused CxProperties *prop, - CxPropertiesSource *src, - cxstring *target -) { - target->ptr = src->data_ptr; - target->length = fread(src->data_ptr, 1, src->data_size, src->src); - return ferror((FILE*)src->src); -} - -static int cx_properties_read_init_file( - cx_attr_unused CxProperties *prop, - CxPropertiesSource *src -) { - src->data_ptr = cxMallocDefault(src->data_size); - if (src->data_ptr == NULL) return 1; - return 0; -} -static void cx_properties_read_clean_file( - cx_attr_unused CxProperties *prop, - CxPropertiesSource *src -) { - cxFreeDefault(src->data_ptr); -} - -CxPropertiesSource cxPropertiesStringSource(cxstring str) { - CxPropertiesSource src; - src.src = (void*) str.ptr; - src.data_size = str.length; - src.data_ptr = NULL; - src.read_func = cx_properties_read_string; - src.read_init_func = NULL; - src.read_clean_func = NULL; - return src; -} - -CxPropertiesSource cxPropertiesCstrnSource(const char *str, size_t len) { - CxPropertiesSource src; - src.src = (void*) str; - src.data_size = len; - src.data_ptr = NULL; - src.read_func = cx_properties_read_string; - src.read_init_func = NULL; - src.read_clean_func = NULL; - return src; -} + // sanity check for the map + const bool use_cstring = cxCollectionStoresPointers(target); + if (!use_cstring && cxCollectionElementSize(target) != sizeof(cxmutstr)) { + return CX_PROPERTIES_MAP_ERROR; + } -CxPropertiesSource cxPropertiesCstrSource(const char *str) { - CxPropertiesSource src; - src.src = (void*) str; - src.data_size = strlen(str); - src.data_ptr = NULL; - src.read_func = cx_properties_read_string; - src.read_init_func = NULL; - src.read_clean_func = NULL; - return src; -} + // create a duplicate to guarantee zero-termination + cxmutstr fname = cx_strdup(filename); + if (fname.ptr == NULL) { + return CX_PROPERTIES_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE + } -CxPropertiesSource cxPropertiesFileSource(FILE *file, size_t chunk_size) { - CxPropertiesSource src; - src.src = file; - src.data_size = chunk_size; - src.data_ptr = NULL; - src.read_func = cx_properties_read_file; - src.read_init_func = cx_properties_read_init_file; - src.read_clean_func = cx_properties_read_clean_file; - return src; -} - -CxPropertiesStatus cxPropertiesLoad( - CxProperties *prop, - CxPropertiesSink sink, - CxPropertiesSource source -) { - assert(source.read_func != NULL); - assert(sink.sink_func != NULL); - - // initialize reader - if (source.read_init_func != NULL) { - if (source.read_init_func(prop, &source)) { - return CX_PROPERTIES_READ_INIT_FAILED; - } + // open the file + FILE *f = fopen(fname.ptr, "r"); + if (f == NULL) { + cx_strfree(&fname); + return CX_PROPERTIES_FILE_ERROR; } - // transfer the data from the source to the sink + // initialize the parser + char linebuf[cx_properties_load_buf_size]; + char fillbuf[cx_properties_load_fill_size]; CxPropertiesStatus status; - CxPropertiesStatus kv_status = CX_PROPERTIES_NO_DATA; - bool found = false; + CxProperties parser; + cxPropertiesInit(&parser, config); + cxPropertiesUseStack(&parser, linebuf, cx_properties_load_buf_size); + + // read/fill/parse loop + status = CX_PROPERTIES_NO_DATA; + size_t keys_found = 0; while (true) { - // read input - cxstring input; - if (source.read_func(prop, &source, &input)) { - status = CX_PROPERTIES_READ_FAILED; + size_t r = fread(fillbuf, 1, cx_properties_load_fill_size, f); + if (ferror(f)) { + status = CX_PROPERTIES_FILE_ERROR; + break; + } + if (r == 0) { break; } - - // no more data - break - if (input.length == 0) { - if (found) { - // something was found, check the last kv_status - if (kv_status == CX_PROPERTIES_INCOMPLETE_DATA) { - status = CX_PROPERTIES_INCOMPLETE_DATA; - } else { - status = CX_PROPERTIES_NO_ERROR; - } - } else { - // nothing found - status = CX_PROPERTIES_NO_DATA; - } + if (cxPropertiesFilln(&parser, fillbuf, r)) { + status = CX_PROPERTIES_BUFFER_ALLOC_FAILED; break; } - - // set the input buffer and read the k/v-pairs - cxPropertiesFill(prop, input); - - do { - cxstring key, value; - kv_status = cxPropertiesNext(prop, &key, &value); - if (kv_status == CX_PROPERTIES_NO_ERROR) { - found = true; - if (sink.sink_func(prop, &sink, key, value)) { - kv_status = CX_PROPERTIES_SINK_FAILED; + cxstring key, value; + while (true) { + status = cxPropertiesNext(&parser, &key, &value); + if (status != CX_PROPERTIES_NO_ERROR) { + break; + } else { + cxmutstr v = cx_strdup_a(allocator, value); + if (v.ptr == NULL) { + status = CX_PROPERTIES_MAP_ERROR; + break; } + void *mv = use_cstring ? (void*)v.ptr : &v; + if (cxMapPut(target, key, mv)) { + cx_strfree(&v); + status = CX_PROPERTIES_MAP_ERROR; + break; + } + keys_found++; } - } while (kv_status == CX_PROPERTIES_NO_ERROR); - - if (kv_status > CX_PROPERTIES_OK) { - status = kv_status; + } + if (status > CX_PROPERTIES_OK) { break; } } - if (source.read_clean_func != NULL) { - source.read_clean_func(prop, &source); + // cleanup and exit + fclose(f); + cxPropertiesDestroy(&parser); + cx_strfree(&fname); + if (status == CX_PROPERTIES_NO_DATA && keys_found > 0) { + return CX_PROPERTIES_NO_ERROR; + } else { + return status; } - - return status; }
--- a/ucx/string.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/string.c Fri Dec 19 17:53:18 2025 +0100 @@ -25,7 +25,8 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -#ifdef MEMRCHR_NEED_GNU + +#ifdef WITH_MEMRCHR #define _GNU_SOURCE #endif @@ -46,28 +47,6 @@ #define cx_strcasecmp_impl strncasecmp #endif -cxmutstr cx_mutstr(char *cstring) { - return (cxmutstr) {cstring, cstring == NULL ? 0 : strlen(cstring)}; -} - -cxmutstr cx_mutstrn( - char *cstring, - size_t length -) { - return (cxmutstr) {cstring, length}; -} - -cxstring cx_str(const char *cstring) { - return (cxstring) {cstring, cstring == NULL ? 0 : strlen(cstring)}; -} - -cxstring cx_strn( - const char *cstring, - size_t length -) { - return (cxstring) {cstring, length}; -} - void cx_strfree(cxmutstr *str) { if (str == NULL) return; cxFreeDefault(str->ptr); @@ -91,7 +70,7 @@ cxstring src ) { if (cxReallocate(alloc, &dest->ptr, src.length + 1)) { - return 1; + return 1; // LCOV_EXCL_LINE } memcpy(dest->ptr, src.ptr, src.length); @@ -137,7 +116,7 @@ size_t slen = str.length; for (size_t i = 0; i < count; i++) { cxstring s = va_arg(ap, cxstring); - if (slen > SIZE_MAX - str.length) overflow = true; + if (slen > SIZE_MAX - s.length) overflow = true; slen += s.length; } va_end(ap); @@ -156,10 +135,10 @@ } else { newstr = cxRealloc(alloc, str.ptr, slen + 1); } - if (newstr == NULL) { + if (newstr == NULL) { // LCOV_EXCL_START va_end(ap2); return (cxmutstr) {NULL, 0}; - } + } // LCOV_EXCL_STOP str.ptr = newstr; // concatenate strings @@ -521,10 +500,12 @@ cxMalloc(allocator, string.length + 1), string.length }; + // LCOV_EXCL_START if (result.ptr == NULL) { result.length = 0; return result; } + // LCOV_EXCL_STOP memcpy(result.ptr, string.ptr, string.length); result.ptr[string.length] = '\0'; return result;
--- a/ucx/tree.c Sun Dec 07 20:16:59 2025 +0100 +++ b/ucx/tree.c Fri Dec 19 17:53:18 2025 +0100 @@ -28,8 +28,6 @@ #include "cx/tree.h" -#include "cx/array_list.h" - #include <assert.h> #define CX_TREE_PTR(cur, off) (*(void**)(((char*)(cur))+(off))) @@ -352,7 +350,16 @@ } } else { // node has children, push the first child onto the stack and enter it - cx_array_simple_add(iter->stack, children); + if (iter->stack_size >= iter->stack_capacity) { + const size_t newcap = iter->stack_capacity + 8; + if (cxReallocArrayDefault(&iter->stack, newcap, sizeof(void*))) { + // we cannot return an error in this function + abort(); // LCOV_EXCL_LINE + } + iter->stack_capacity = newcap; + } + iter->stack[iter->stack_size] = children; + iter->stack_size++; iter->node = children; iter->counter++; } @@ -566,7 +573,7 @@ ptrdiff_t loc_next ) { *cnode = cfunc(src, cdata); - if (*cnode == NULL) return 1; + if (*cnode == NULL) return 1; // LCOV_EXCL_LINE cx_tree_zero_pointers(*cnode, cx_tree_ptr_locations); void *match = NULL; @@ -627,7 +634,7 @@ // create the new node void *new_node = cfunc(elem, cdata); - if (new_node == NULL) return processed; + if (new_node == NULL) return processed; // LCOV_EXCL_LINE cx_tree_zero_pointers(new_node, cx_tree_ptr_locations); // start searching from current node @@ -717,7 +724,7 @@ } // otherwise, create iterator and hand over to other function - CxIterator iter = cxIterator(src, elem_size, num, false); + CxIterator iter = cxIterator(src, elem_size, num); return cx_tree_add_iter(cxIteratorRef(iter), num, sfunc, cfunc, cdata, failed, root, loc_parent, loc_children, loc_last_child, @@ -731,18 +738,18 @@ void *node; if (tree->root == NULL) { node = tree->node_create(data, tree); - if (node == NULL) return 1; + if (node == NULL) return 1; // LCOV_EXCL_LINE cx_tree_zero_pointers(node, cx_tree_node_layout(tree)); tree->root = node; - tree->size = 1; + tree->collection.size = 1; return 0; } int result = cx_tree_add(data, tree->search, tree->node_create, tree, &node, tree->root, cx_tree_node_layout(tree)); if (0 == result) { - tree->size++; + tree->collection.size++; } else { - cxFree(tree->allocator, node); + cxFree(tree->collection.allocator, node); } return result; } @@ -758,7 +765,7 @@ // use the first element from the iter to create the root node void **eptr = iter->current(iter); void *node = tree->node_create(*eptr, tree); - if (node == NULL) return 0; + if (node == NULL) return 0; // LCOV_EXCL_LINE cx_tree_zero_pointers(node, cx_tree_node_layout(tree)); tree->root = node; ins = 1; @@ -767,9 +774,9 @@ void *failed; ins += cx_tree_add_iter(iter, n, tree->search, tree->node_create, tree, &failed, tree->root, cx_tree_node_layout(tree)); - tree->size += ins; + tree->collection.size += ins; if (ins < n) { - cxFree(tree->allocator, failed); + cxFree(tree->collection.allocator, failed); } return ins; } @@ -780,7 +787,7 @@ const void *data, size_t depth ) { - if (tree->root == NULL) return NULL; + if (tree->root == NULL) return NULL; // LCOV_EXCL_LINE void *found; if (0 == cx_tree_search_data( @@ -818,24 +825,21 @@ assert(search_func != NULL); assert(search_data_func != NULL); - CxTree *tree = cxMalloc(allocator, sizeof(CxTree)); - if (tree == NULL) return NULL; + CxTree *tree = cxZalloc(allocator, sizeof(CxTree)); + if (tree == NULL) return NULL; // LCOV_EXCL_LINE tree->cl = &cx_tree_default_class; - tree->allocator = allocator; + tree->collection.allocator = allocator; tree->node_create = create_func; tree->search = search_func; tree->search_data = search_data_func; - tree->simple_destructor = NULL; - tree->advanced_destructor = (cx_destructor_func2) cxFree; - tree->destructor_data = (void *) allocator; + tree->collection.advanced_destructor = (cx_destructor_func2) cxFree; + tree->collection.destructor_data = (void *) allocator; tree->loc_parent = loc_parent; tree->loc_children = loc_children; tree->loc_last_child = loc_last_child; tree->loc_prev = loc_prev; tree->loc_next = loc_next; - tree->root = NULL; - tree->size = 0; return tree; } @@ -845,7 +849,7 @@ if (tree->root != NULL) { cxTreeClear(tree); } - cxFree(tree->allocator, tree); + cxFree(tree->collection.allocator, tree); } CxTree *cxTreeCreateWrapped(const CxAllocator *allocator, void *root, @@ -856,47 +860,41 @@ } assert(root != NULL); - CxTree *tree = cxMalloc(allocator, sizeof(CxTree)); - if (tree == NULL) return NULL; + CxTree *tree = cxZalloc(allocator, sizeof(CxTree)); + if (tree == NULL) return NULL; // LCOV_EXCL_LINE tree->cl = &cx_tree_default_class; // set the allocator anyway, just in case... - tree->allocator = allocator; - tree->node_create = NULL; - tree->search = NULL; - tree->search_data = NULL; - tree->simple_destructor = NULL; - tree->advanced_destructor = NULL; - tree->destructor_data = NULL; + tree->collection.allocator = allocator; tree->loc_parent = loc_parent; tree->loc_children = loc_children; tree->loc_last_child = loc_last_child; tree->loc_prev = loc_prev; tree->loc_next = loc_next; tree->root = root; - tree->size = cxTreeSubtreeSize(tree, root); + tree->collection.size = cxTreeSubtreeSize(tree, root); return tree; } void cxTreeSetParent(CxTree *tree, void *parent, void *child) { size_t loc_parent = tree->loc_parent; if (tree_parent(child) == NULL) { - tree->size++; + tree->collection.size++; } cx_tree_link(parent, child, cx_tree_node_layout(tree)); } void cxTreeAddChildNode(CxTree *tree, void *parent, void *child) { cx_tree_link(parent, child, cx_tree_node_layout(tree)); - tree->size++; + tree->collection.size++; } int cxTreeAddChild(CxTree *tree, void *parent, const void *data) { void *node = tree->node_create(data, tree); - if (node == NULL) return 1; + if (node == NULL) return 1; // LCOV_EXCL_LINE cx_tree_zero_pointers(node, cx_tree_node_layout(tree)); cx_tree_link(parent, node, cx_tree_node_layout(tree)); - tree->size++; + tree->collection.size++; return 0; } @@ -911,7 +909,7 @@ size_t cxTreeInsertArray(CxTree *tree, const void *data, size_t elem_size, size_t n) { if (n == 0) return 0; if (n == 1) return 0 == cxTreeInsert(tree, data) ? 1 : 0; - CxIterator iter = cxIterator(data, elem_size, n, false); + CxIterator iter = cxIterator(data, elem_size, n); return cxTreeInsertIter(tree, cxIteratorRef(iter), n); } @@ -948,7 +946,7 @@ } size_t cxTreeSize(CxTree *tree) { - return tree->size; + return tree->collection.size; } size_t cxTreeDepth(CxTree *tree) { @@ -1002,7 +1000,7 @@ if (loc_last_child >= 0) tree_last_child(node) = NULL; // the tree now has one member less - tree->size--; + tree->collection.size--; return 0; } @@ -1010,12 +1008,12 @@ void cxTreeRemoveSubtree(CxTree *tree, void *node) { if (node == tree->root) { tree->root = NULL; - tree->size = 0; + tree->collection.size = 0; return; } size_t subtree_size = cxTreeSubtreeSize(tree, node); cx_tree_unlink(node, cx_tree_node_layout(tree)); - tree->size -= subtree_size; + tree->collection.size -= subtree_size; } int cxTreeDestroyNode( @@ -1025,12 +1023,7 @@ ) { int result = cxTreeRemoveNode(tree, node, relink_func); if (result == 0) { - if (tree->simple_destructor) { - tree->simple_destructor(node); - } - if (tree->advanced_destructor) { - tree->advanced_destructor(tree->destructor_data, node); - } + cx_invoke_destructor(tree, node); return 0; } else { return result; @@ -1045,15 +1038,10 @@ ); cx_foreach(void *, child, iter) { if (iter.exiting) { - if (tree->simple_destructor) { - tree->simple_destructor(child); - } - if (tree->advanced_destructor) { - tree->advanced_destructor(tree->destructor_data, child); - } + cx_invoke_destructor(tree, child); } } - tree->size -= iter.counter; + tree->collection.size -= iter.counter; if (node == tree->root) { tree->root = NULL; } @@ -1071,6 +1059,7 @@ cxFreeDefault(q); q = next; } + visitor->queue_next = visitor->queue_last = NULL; } CxTreeIterator cxTreeIterateSubtree(CxTree *tree, void *node, bool visit_on_exit) {