implement toggle button (WinUI3) newapi

Sun, 01 Oct 2023 12:08:09 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 01 Oct 2023 12:08:09 +0200
branch
newapi
changeset 187
24ce2c326d85
parent 186
5db4979bf482
child 188
fbbae6738252

implement toggle button (WinUI3)

.hgignore file | annotate | diff | comparison | revisions
make/vs/testapp/main.c file | annotate | diff | comparison | revisions
make/vs/ucx/ucx.vcxproj file | annotate | diff | comparison | revisions
make/vs/ucx/ucx.vcxproj.filters file | annotate | diff | comparison | revisions
ucx/allocator.c file | annotate | diff | comparison | revisions
ucx/array_list.c file | annotate | diff | comparison | revisions
ucx/basic_mempool.c file | annotate | diff | comparison | revisions
ucx/cx/allocator.h file | annotate | diff | comparison | revisions
ucx/cx/basic_mempool.h file | annotate | diff | comparison | revisions
ucx/cx/collection.h file | annotate | diff | comparison | revisions
ucx/cx/common.h.orig file | annotate | diff | comparison | revisions
ucx/cx/hash_map.h file | annotate | diff | comparison | revisions
ucx/cx/iterator.h file | annotate | diff | comparison | revisions
ucx/cx/list.h file | annotate | diff | comparison | revisions
ucx/cx/map.h file | annotate | diff | comparison | revisions
ucx/cx/mempool.h file | annotate | diff | comparison | revisions
ucx/cx/tree.h file | annotate | diff | comparison | revisions
ucx/cx/utils.h file | annotate | diff | comparison | revisions
ucx/linked_list.c file | annotate | diff | comparison | revisions
ucx/mempool.c file | annotate | diff | comparison | revisions
ucx/tree.c file | annotate | diff | comparison | revisions
ucx/utils.c file | annotate | diff | comparison | revisions
ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/context.h file | annotate | diff | comparison | revisions
ui/common/document.c file | annotate | diff | comparison | revisions
ui/ui/button.h file | annotate | diff | comparison | revisions
ui/ui/container.h file | annotate | diff | comparison | revisions
ui/winui/button.cpp file | annotate | diff | comparison | revisions
ui/winui/button.h file | annotate | diff | comparison | revisions
ui/winui/window.cpp file | annotate | diff | comparison | revisions
--- a/.hgignore	Sun Oct 01 09:23:47 2023 +0200
+++ b/.hgignore	Sun Oct 01 12:08:09 2023 +0200
@@ -3,7 +3,7 @@
 relre:^core$
 relre:^make/vs/.vs/.*
 relre:^make/vs/packages/.*
-relre:^make/vs/testapp/testapp\.vcxproj\.user
+relre:^make/vs/.*vcxproj\.user
 relre:^ui/winui/winui\.vcxproj\.user
 relre:^ui/wpf/UIcore/obj
 relre:^ui/wpf/UIwrapper/.vs
--- a/make/vs/testapp/main.c	Sun Oct 01 09:23:47 2023 +0200
+++ b/make/vs/testapp/main.c	Sun Oct 01 12:08:09 2023 +0200
@@ -34,14 +34,33 @@
 
 #include <ui/ui.h>
 
+typedef struct WindowData {
+    UiInteger* check;
+    UiInteger* toggle;
+} WindowData;
 
 void action1(UiEvent* event, void* data) {
     char* action = data;
-    printf("data: %s!\n", data);
+    
+    WindowData* wdata = event->window;
+    int64_t is_checked = wdata->check->get(wdata->check);
+
+    printf("data: %s %d\n", data, is_checked);
+}
+
+void action_set_checkbox(UiEvent* event, void* data) {
+    char* action = data;
+
+    WindowData* wdata = event->window;
+    wdata->check->set(wdata->check, 1);
 }
 
 void application_startup(UiEvent* event, void* data) {
     UiObject* obj = ui_window("Test", NULL);
+    WindowData* wdata = ui_malloc(obj->ctx, sizeof(WindowData));
+    obj->window = wdata;
+    wdata->check = ui_int_new(obj->ctx, "check");
+    wdata->toggle = ui_int_new(obj->ctx, "toggle");
 
     UI_GRID_SP(obj, 10, 5, 20) {
         ui_button(obj, .label="Button1", .onclick=action1, .onclickdata="action1");
@@ -53,7 +72,11 @@
         ui_button(obj, .label="Button5", .onclick=action1, .onclickdata="action5", .colspan=2);
         ui_newline(obj);
 
-        ui_button(obj, .label="Very Long Button Label Text ____________ Test", .onclick=action1, .onclickdata="test");
+        ui_button(obj, .label="Very Long Button Label Text ____________ Test", .onclick=action_set_checkbox);
+        ui_newline(obj);
+
+        ui_checkbox(obj, .label = "Option 1", .value = wdata->check);
+        ui_togglebutton(obj, .label = "Option 2", .value = wdata->toggle);
     }
 
     ui_show(obj);
--- a/make/vs/ucx/ucx.vcxproj	Sun Oct 01 09:23:47 2023 +0200
+++ b/make/vs/ucx/ucx.vcxproj	Sun Oct 01 12:08:09 2023 +0200
@@ -134,7 +134,6 @@
   <ItemGroup>
     <ClCompile Include="..\..\..\ucx\allocator.c" />
     <ClCompile Include="..\..\..\ucx\array_list.c" />
-    <ClCompile Include="..\..\..\ucx\basic_mempool.c" />
     <ClCompile Include="..\..\..\ucx\buffer.c" />
     <ClCompile Include="..\..\..\ucx\compare.c" />
     <ClCompile Include="..\..\..\ucx\hash_key.c" />
@@ -142,15 +141,14 @@
     <ClCompile Include="..\..\..\ucx\linked_list.c" />
     <ClCompile Include="..\..\..\ucx\list.c" />
     <ClCompile Include="..\..\..\ucx\map.c" />
+    <ClCompile Include="..\..\..\ucx\mempool.c" />
     <ClCompile Include="..\..\..\ucx\printf.c" />
     <ClCompile Include="..\..\..\ucx\string.c" />
-    <ClCompile Include="..\..\..\ucx\tree.c" />
     <ClCompile Include="..\..\..\ucx\utils.c" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\..\ucx\cx\allocator.h" />
     <ClInclude Include="..\..\..\ucx\cx\array_list.h" />
-    <ClInclude Include="..\..\..\ucx\cx\basic_mempool.h" />
     <ClInclude Include="..\..\..\ucx\cx\buffer.h" />
     <ClInclude Include="..\..\..\ucx\cx\collection.h" />
     <ClInclude Include="..\..\..\ucx\cx\common.h" />
@@ -164,7 +162,6 @@
     <ClInclude Include="..\..\..\ucx\cx\mempool.h" />
     <ClInclude Include="..\..\..\ucx\cx\printf.h" />
     <ClInclude Include="..\..\..\ucx\cx\string.h" />
-    <ClInclude Include="..\..\..\ucx\cx\tree.h" />
     <ClInclude Include="..\..\..\ucx\cx\utils.h" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
--- a/make/vs/ucx/ucx.vcxproj.filters	Sun Oct 01 09:23:47 2023 +0200
+++ b/make/vs/ucx/ucx.vcxproj.filters	Sun Oct 01 12:08:09 2023 +0200
@@ -21,9 +21,6 @@
     <ClCompile Include="..\..\..\ucx\array_list.c">
       <Filter>Quelldateien</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\..\ucx\basic_mempool.c">
-      <Filter>Quelldateien</Filter>
-    </ClCompile>
     <ClCompile Include="..\..\..\ucx\buffer.c">
       <Filter>Quelldateien</Filter>
     </ClCompile>
@@ -51,10 +48,10 @@
     <ClCompile Include="..\..\..\ucx\string.c">
       <Filter>Quelldateien</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\..\ucx\tree.c">
+    <ClCompile Include="..\..\..\ucx\utils.c">
       <Filter>Quelldateien</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\..\ucx\utils.c">
+    <ClCompile Include="..\..\..\ucx\mempool.c">
       <Filter>Quelldateien</Filter>
     </ClCompile>
   </ItemGroup>
@@ -65,9 +62,6 @@
     <ClInclude Include="..\..\..\ucx\cx\array_list.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\..\ucx\cx\basic_mempool.h">
-      <Filter>Headerdateien</Filter>
-    </ClInclude>
     <ClInclude Include="..\..\..\ucx\cx\buffer.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
@@ -107,9 +101,6 @@
     <ClInclude Include="..\..\..\ucx\cx\string.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\..\ucx\cx\tree.h">
-      <Filter>Headerdateien</Filter>
-    </ClInclude>
     <ClInclude Include="..\..\..\ucx\cx\utils.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
--- a/ucx/allocator.c	Sun Oct 01 09:23:47 2023 +0200
+++ b/ucx/allocator.c	Sun Oct 01 12:08:09 2023 +0200
@@ -75,6 +75,20 @@
 };
 CxAllocator *cxDefaultAllocator = &cx_default_allocator;
 
+
+int cx_reallocate(
+        void **mem,
+        size_t n
+) {
+    void *nmem = realloc(*mem, n);
+    if (nmem == NULL) {
+        return 1;
+    } else {
+        *mem = nmem;
+        return 0;
+    }
+}
+
 // IMPLEMENTATION OF HIGH LEVEL API
 
 void *cxMalloc(
--- a/ucx/array_list.c	Sun Oct 01 09:23:47 2023 +0200
+++ b/ucx/array_list.c	Sun Oct 01 12:08:09 2023 +0200
@@ -103,7 +103,7 @@
 }
 
 #ifndef CX_ARRAY_SWAP_SBO_SIZE
-#define CX_ARRAY_SWAP_SBO_SIZE 512
+#define CX_ARRAY_SWAP_SBO_SIZE 128
 #endif
 
 void cx_array_swap(
--- a/ucx/basic_mempool.c	Sun Oct 01 09:23:47 2023 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,235 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "cx/basic_mempool.h"
-#include "cx/utils.h"
-#include <string.h>
-
-#define of_chk_(n) if (SIZE_MAX - sizeof(cx_destructor_func) < (n)) return NULL
-
-/** Internal structure for denoting pooled memory. */
-typedef struct {
-    /** The destructor. */
-    cx_destructor_func destructor;
-    /**
-     * Access to the first byte of the polled memory.
-     */
-    char c;
-} cx_basic_mempool_memory;
-
-static int cx_basic_mempool_chcap(
-        struct cx_basic_mempool_s *pool,
-        size_t newcap
-) {
-    if (newcap < pool->ndata) {
-        return 1;
-    }
-
-    size_t newcapsz;
-    if (cx_szmul(newcap, sizeof(void *), &newcapsz)) {
-        return 1;
-    }
-
-    void **data = realloc(pool->data, newcapsz);
-    if (data) {
-        pool->data = data;
-        pool->size = newcap;
-        return 0;
-    } else {
-        return 1;
-    }
-}
-
-void *cx_malloc_basic_mempool(
-        void *data,
-        size_t n
-) {
-    of_chk_(n);
-    struct cx_basic_mempool_s *pool = data;
-
-    if (pool->ndata >= pool->size) {
-        size_t newcap = pool->size * 2;
-        if (newcap < pool->size || cx_basic_mempool_chcap(pool, newcap)) {
-            return NULL;
-        }
-    }
-
-    cx_basic_mempool_memory *mem = malloc(sizeof(cx_destructor_func) + n);
-    if (mem == NULL) {
-        return NULL;
-    }
-
-    mem->destructor = NULL;
-    pool->data[pool->ndata] = mem;
-    pool->ndata++;
-
-    return &(mem->c);
-}
-
-void *cx_calloc_basic_mempool(
-        void *data,
-        size_t nelem,
-        size_t elsize
-) {
-    size_t msz;
-    if (cx_szmul(nelem, elsize, &msz)) {
-        return NULL;
-    }
-    void *ptr = cx_malloc_basic_mempool(data, msz);
-    if (ptr == NULL) {
-        return NULL;
-    }
-    memset(ptr, 0, nelem * elsize);
-    return ptr;
-}
-
-void *cx_realloc_basic_mempool(
-        void *data,
-        void *ptr,
-        size_t n
-) {
-    of_chk_(n);
-    struct cx_basic_mempool_s *pool = data;
-
-    char *mem = ((char *) ptr) - sizeof(cx_destructor_func);
-    char *newm = (char *) realloc(mem, n + sizeof(cx_destructor_func));
-    if (newm == NULL) {
-        return NULL;
-    }
-    if (mem != newm) {
-        cx_for_n(i, pool->ndata) {
-            if (pool->data[i] == mem) {
-                pool->data[i] = newm;
-                return newm + sizeof(cx_destructor_func);
-            }
-        }
-        abort();
-    } else {
-        return newm + sizeof(cx_destructor_func);
-    }
-}
-
-void cx_free_basic_mempool(
-        void *data,
-        void *ptr
-) {
-    struct cx_basic_mempool_s *pool = data;
-
-    cx_basic_mempool_memory *mem = (cx_basic_mempool_memory *)
-            ((char *) ptr - sizeof(cx_destructor_func));
-    cx_for_n(i, pool->ndata) {
-        if (mem == pool->data[i]) {
-            if (mem->destructor != NULL) {
-                mem->destructor(&(mem->c));
-            }
-            free(mem);
-            size_t last_index = pool->ndata - 1;
-            if (i != last_index) {
-                pool->data[i] = pool->data[last_index];
-                pool->data[last_index] = NULL;
-            }
-            pool->ndata--;
-            return;
-        }
-    }
-    abort();
-}
-
-void cx_basic_mempool_destroy(CxMempool *p) {
-    struct cx_basic_mempool_s *pool = (struct cx_basic_mempool_s *) p;
-    cx_basic_mempool_memory *mem;
-    cx_for_n(i, pool->ndata) {
-        mem = (cx_basic_mempool_memory *) pool->data[i];
-        if (mem) {
-            if (mem->destructor) {
-                mem->destructor(&(mem->c));
-            }
-            free(mem);
-        }
-    }
-    free(pool->data);
-    free((void *) p->allocator);
-    free(pool);
-}
-
-void cx_basic_mempool_set_destr(
-        __attribute__((__unused__)) CxMempool *pool,
-        void *ptr,
-        cx_destructor_func func
-) {
-    *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func;
-}
-
-static cx_allocator_class cx_basic_mempool_allocator_class = {
-        cx_malloc_basic_mempool,
-        cx_realloc_basic_mempool,
-        cx_calloc_basic_mempool,
-        cx_free_basic_mempool
-};
-
-static cx_mempool_class cx_basic_mempool_class = {
-        cx_basic_mempool_destroy,
-        cx_basic_mempool_set_destr,
-};
-
-CxMempool *cxBasicMempoolCreate(size_t capacity) {
-    size_t poolsize;
-    if (cx_szmul(capacity, sizeof(void *), &poolsize)) {
-        return NULL;
-    }
-
-    struct cx_basic_mempool_s *pool =
-            malloc(sizeof(struct cx_basic_mempool_s));
-    if (pool == NULL) {
-        return NULL;
-    }
-
-
-    CxAllocator *provided_allocator = malloc(sizeof(CxAllocator));
-    if (!provided_allocator) {
-        free(pool);
-        return NULL;
-    }
-    provided_allocator->cl = &cx_basic_mempool_allocator_class;
-    provided_allocator->data = pool;
-
-    pool->base.cl = &cx_basic_mempool_class;
-    pool->base.allocator = provided_allocator;
-
-    pool->data = malloc(poolsize);
-    if (pool->data == NULL) {
-        free(provided_allocator);
-        free(pool);
-        return NULL;
-    }
-
-    pool->ndata = 0;
-    pool->size = capacity;
-
-    return (CxMempool *) pool;
-}
--- a/ucx/cx/allocator.h	Sun Oct 01 09:23:47 2023 +0200
+++ b/ucx/cx/allocator.h	Sun Oct 01 12:08:09 2023 +0200
@@ -133,6 +133,22 @@
 ) __attribute__((__nonnull__(2)));
 
 /**
+ * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
+ *
+ * \par Error handling
+ * \c errno will be set by realloc() on failure.
+ *
+ * @param mem pointer to the pointer to allocated block
+ * @param n the new size in bytes
+ * @return zero on success, non-zero on failure
+ */
+int cx_reallocate(
+        void **mem,
+        size_t n
+)
+__attribute__((__nonnull__));
+
+/**
  * Allocate \p n bytes of memory.
  *
  * @param allocator the allocator
@@ -169,7 +185,6 @@
 /**
  * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
  * This function acts like cxRealloc() using the pointer pointed to by \p mem.
- * On success, the pointer is changed to the new location (in case the
  *
  * \note Re-allocating a block allocated by a different allocator is undefined.
  *
--- a/ucx/cx/basic_mempool.h	Sun Oct 01 09:23:47 2023 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-/**
- * \file basic_mempool.h
- * \brief Implementation of a basic memory pool.
- * \author Mike Becker
- * \author Olaf Wintermann
- * \version 3.0
- * \copyright 2-Clause BSD License
- */
-
-#ifndef UCX_BASIC_MEMPOOL_H
-#define UCX_BASIC_MEMPOOL_H
-
-#include "mempool.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * Basic array-based memory pool.
- */
-struct cx_basic_mempool_s {
-    /** Inherit base structure members. */
-    CxMempool base;
-
-    /** List of pointers to pooled memory. */
-    void **data;
-
-    /** Number of pooled memory items. */
-    size_t ndata;
-
-    /** Memory pool size. */
-    size_t size;
-};
-
-/**
- * Creates a basic array-based memory pool.
- *
- * @param capacity the initial capacity of the pool
- * @return the created memory pool or \c NULL if allocation failed
- */
-__attribute__((__warn_unused_result__))
-CxMempool *cxBasicMempoolCreate(size_t capacity);
-
-#ifdef __cplusplus
-} // extern "C"
-#endif
-
-#endif // UCX_BASIC_MEMPOOL_H
--- a/ucx/cx/collection.h	Sun Oct 01 09:23:47 2023 +0200
+++ b/ucx/cx/collection.h	Sun Oct 01 12:08:09 2023 +0200
@@ -127,6 +127,15 @@
     (c)->store_pointer ? (*((void **) (e))) : (e))
 
 
+/**
+ * Invokes all available destructor functions for a specific element.
+ *
+ * Usually only used by collection implementations. There should be no need
+ * to invoke this macro manually.
+ *
+ * @param c the collection
+ * @param e the element
+ */
 #define cx_invoke_destructor(c, e) \
     if ((c)->simple_destructor) cx_invoke_simple_destructor(c,e); \
     if ((c)->advanced_destructor) cx_invoke_advanced_destructor(c,e)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ucx/cx/common.h.orig	Sun Oct 01 12:08:09 2023 +0200
@@ -0,0 +1,138 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * \file common.h
+ *
+ * \brief Common definitions and feature checks.
+ *
+ * \author Mike Becker
+ * \author Olaf Wintermann
+ * \version 3.0
+ * \copyright 2-Clause BSD License
+ *
+ * \mainpage UAP Common Extensions
+ * Library with common and useful functions, macros and data structures.
+ * <p>
+ * Latest available source:<br>
+ * <a href="https://sourceforge.net/projects/ucx/files/">https://sourceforge.net/projects/ucx/files/</a>
+ * </p>
+ *
+ * <p>
+ * Repositories:<br>
+ * <a href="https://sourceforge.net/p/ucx/code">https://sourceforge.net/p/ucx/code</a>
+ * -&nbsp;or&nbsp;-
+ * <a href="https://develop.uap-core.de/hg/ucx">https://develop.uap-core.de/hg/ucx</a>
+ * </p>
+ *
+ * <h2>LICENCE</h2>
+ *
+ * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef UCX_COMMON_H
+#define UCX_COMMON_H
+
+/** Major UCX version as integer constant. */
+#define UCX_VERSION_MAJOR   3
+
+/** Minor UCX version as integer constant. */
+#define UCX_VERSION_MINOR   0
+
+/** Version constant which ensures to increase monotonically. */
+#define UCX_VERSION (((UCX_VERSION_MAJOR)<<16)|UCX_VERSION_MINOR)
+
+#define __attribute__(...) 
+
+#include <stdlib.h>
+#include <stddef.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+/**
+ * Function pointer compatible with fwrite-like functions.
+ */
+typedef size_t (*cx_write_func)(
+        void const *,
+        size_t,
+        size_t,
+        void *
+);
+
+/**
+ * Function pointer compatible with fread-like functions.
+ */
+typedef size_t (*cx_read_func)(
+        void *,
+        size_t,
+        size_t,
+        void *
+);
+
+#ifdef _WIN32
+
+#ifdef __MINGW32__
+#include <sys/types.h>
+#endif // __MINGW32__
+
+#else // !_WIN32
+
+#include <sys/types.h>
+
+#endif // _WIN32
+
+#ifndef __GNUC__
+/**
+ * Removes GNU C attributes where they are not supported.
+ */
+#define __attribute__(x)
+#endif
+
+#endif // UCX_COMMON_H
--- a/ucx/cx/hash_map.h	Sun Oct 01 09:23:47 2023 +0200
+++ b/ucx/cx/hash_map.h	Sun Oct 01 12:08:09 2023 +0200
@@ -74,7 +74,7 @@
  * cxMapStorePointers() was called immediately after creation.
  *
  * @note Iterators provided by this hash map implementation provide the remove operation.
- * The index value of an iterator is the incremented when the iterator advanced without removal.
+ * The index value of an iterator is incremented when the iterator advanced without removal.
  * In other words, when the iterator is finished, \c index==size .
  *
  * @param allocator the allocator to use
@@ -96,7 +96,7 @@
  * cxMapStorePointers() was called immediately after creation.
  *
  * @note Iterators provided by this hash map implementation provide the remove operation.
- * The index value of an iterator is the incremented when the iterator advanced without removal.
+ * The index value of an iterator is incremented when the iterator advanced without removal.
  * In other words, when the iterator is finished, \c index==size .
  *
  * @param itemsize the size of one element
--- a/ucx/cx/iterator.h	Sun Oct 01 09:23:47 2023 +0200
+++ b/ucx/cx/iterator.h	Sun Oct 01 12:08:09 2023 +0200
@@ -204,13 +204,13 @@
 
 /**
  * Iterator value type.
- * An iterator points to a certain element in an (possibly unbounded) chain of elements.
+ * An iterator points to a certain element in a (possibly unbounded) chain of elements.
  * Iterators that are based on collections (which have a defined "first" element), are supposed
  * to be "position-aware", which means that they keep track of the current index within the collection.
  *
  * @note Objects that are pointed to by an iterator are always mutable through that iterator. However,
  * this iterator cannot mutate the collection itself (add or remove elements) and any mutation of the
- * collection by other means make this iterator invalid (regardless of what cxIteratorValid() returns).
+ * collection by other means makes this iterator invalid (regardless of what cxIteratorValid() returns).
  *
  * @see CxMutIterator
  */
--- a/ucx/cx/list.h	Sun Oct 01 09:23:47 2023 +0200
+++ b/ucx/cx/list.h	Sun Oct 01 12:08:09 2023 +0200
@@ -77,7 +77,7 @@
     void (*destructor)(struct cx_list_s *list);
 
     /**
-     * Member function for inserting a single elements.
+     * Member function for inserting a single element.
      * Implementors SHOULD see to performant implementations for corner cases.
      */
     int (*insert_element)(
@@ -145,7 +145,7 @@
     );
 
     /**
-     * Member function for sorting the list in place.
+     * Member function for sorting the list in-place.
      */
     void (*sort)(struct cx_list_s *list);
 
@@ -584,7 +584,7 @@
 }
 
 /**
- * Sorts the list in place.
+ * Sorts the list in-place.
  *
  * \remark The underlying sort algorithm is implementation defined.
  *
--- a/ucx/cx/map.h	Sun Oct 01 09:23:47 2023 +0200
+++ b/ucx/cx/map.h	Sun Oct 01 12:08:09 2023 +0200
@@ -146,6 +146,11 @@
     void *value;
 };
 
+/**
+ * A shared instance of an empty map.
+ *
+ * Writing to that map is undefined.
+ */
 extern CxMap *const cxEmptyMap;
 
 /**
--- a/ucx/cx/mempool.h	Sun Oct 01 09:23:47 2023 +0200
+++ b/ucx/cx/mempool.h	Sun Oct 01 12:08:09 2023 +0200
@@ -44,24 +44,31 @@
 extern "C" {
 #endif
 
-/**
- * Memory pool class type.
- */
-typedef struct cx_mempool_class_s cx_mempool_class;
+/** Internal structure for pooled memory. */
+struct cx_mempool_memory_s;
 
 /**
  * The basic structure of a memory pool.
  * Should be the first member of an actual memory pool implementation.
  */
 struct cx_mempool_s {
+    /** The provided allocator. */
+    CxAllocator const *allocator;
+
     /**
-     * The pool class definition.
+     * A destructor that shall be automatically registered for newly allocated memory.
+     * This destructor MUST NOT free the memory.
      */
-    cx_mempool_class *cl;
-    /**
-     * The provided allocator.
-     */
-    CxAllocator const *allocator;
+    cx_destructor_func auto_destr;
+
+    /** Array of pooled memory. */
+    struct cx_mempool_memory_s **data;
+
+    /** Number of pooled memory items. */
+    size_t size;
+
+    /** Memory pool capacity. */
+    size_t capacity;
 };
 
 /**
@@ -70,50 +77,70 @@
 typedef struct cx_mempool_s CxMempool;
 
 /**
- * The class definition for a memory pool.
+ * Creates an array-based memory pool with a shared destructor function.
+ *
+ * This destructor MUST NOT free the memory.
+ *
+ * @param capacity the initial capacity of the pool
+ * @param destr the destructor function to use for allocated memory
+ * @return the created memory pool or \c NULL if allocation failed
  */
-struct cx_mempool_class_s {
-    /** Member function for destroying the pool. */
-    __attribute__((__nonnull__))
-    void (*destroy)(CxMempool *pool);
-
-    /** Member function for setting a destructor. */
-    __attribute__((__nonnull__))
-    void (*set_destructor)(
-            CxMempool *pool,
-            void *memory,
-            cx_destructor_func fnc
-    );
-};
-
+__attribute__((__warn_unused_result__))
+CxMempool *cxMempoolCreate(size_t capacity, cx_destructor_func destr);
 
 /**
- * Destroys a memory pool including their contents.
+ * Creates a basic array-based memory pool.
+ *
+ * @param capacity the initial capacity of the pool
+ * @return the created memory pool or \c NULL if allocation failed
+ */
+__attribute__((__warn_unused_result__))
+static inline CxMempool *cxBasicMempoolCreate(size_t capacity) {
+    return cxMempoolCreate(capacity, NULL);
+}
+
+/**
+ * Destroys a memory pool and frees the managed memory.
  *
  * @param pool the memory pool to destroy
  */
 __attribute__((__nonnull__))
-static inline void cxMempoolDestroy(CxMempool *pool) {
-    pool->cl->destroy(pool);
-}
+void cxMempoolDestroy(CxMempool *pool);
 
 /**
- * Sets a destructor function for an allocated memory object.
+ * Sets the destructor function for a specific allocated memory object.
  *
- * If the memory is not managed by the pool, the behavior is undefined.
+ * If the memory is not managed by a UCX memory pool, the behavior is undefined.
+ * The destructor MUST NOT free the memory.
  *
- * @param pool the pool
- * @param memory the objected allocated in the pool
+ * @param memory the object allocated in the pool
  * @param fnc the destructor function
  */
 __attribute__((__nonnull__))
-static inline void cxMempoolSetDestructor(
-        CxMempool *pool,
+void cxMempoolSetDestructor(
         void *memory,
         cx_destructor_func fnc
-) {
-    pool->cl->set_destructor(pool, memory, fnc);
-}
+);
+
+/**
+ * Registers foreign memory with this pool.
+ *
+ * The destructor, in contrast to memory allocated by the pool, MUST free the memory.
+ *
+ * A small portion of memory will be allocated to register the information in the pool.
+ * If that allocation fails, this function will return non-zero.
+ *
+ * @param pool the pool
+ * @param memory the object allocated in the pool
+ * @param destr the destructor function
+ * @return zero on success, non-zero on failure
+ */
+__attribute__((__nonnull__))
+int cxMempoolRegister(
+        CxMempool *pool,
+        void *memory,
+        cx_destructor_func destr
+);
 
 #ifdef __cplusplus
 } // extern "C"
--- a/ucx/cx/tree.h	Sun Oct 01 09:23:47 2023 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,136 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-/**
- * \file tree.h
- * \brief Interface for tree implementations.
- * \author Olaf Wintermann
- * \author Mike Becker
- * \version 3.0
- * \copyright 2-Clause BSD License
- */
-
-#ifndef UCX_TREE_H
-#define UCX_TREE_H
-
-#include "common.h"
-#include "allocator.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * Adds a sibling to the current tree node.
- *
- * In case your struct does not have a \p prev or a \p parent pointer,
- * specify a negative location. The location of the \p next pointer is
- * mandatory.
- *
- * \attention Do not use this function to add siblings in a tree where the
- * nodes store a pointer to the last sibling because that would not be modified by this function.
- *
- * \remark If yo do not provide a location to the parent pointer, a call to this function is
- * effectively the same as a call to cx_linked_list_add().
- *
- * @param node a pointer to the node
- * @param loc_prev the location of a \c prev pointer within your node struct
- * @param loc_next the location of a \c next pointer within your node struct
- * @param loc_parent the location of a \c parent pointer within your node struct
- * @param new_node the new node that shall be added as a sibling
- */
-void cx_tree_add_sibling(void *node,
-                         ptrdiff_t loc_prev, ptrdiff_t loc_next,
-                         ptrdiff_t loc_parent,
-                         void *new_node)
-__attribute__((__nonnull__));
-
-/**
- * Adds a node to the list of children.
- *
- * \par Example with a full structure
- * A full tree node structure may look like this:
- * \code
- * typedef struct MyTreeNode MyTreeNode;
- * struct MyTreeNode {
- *   MyTreeNode* parent;
- *   MyTreeNode* first_child;
- *   MyTreeNode* last_child;
- *   MyTreeNode* prev_sibling;
- *   MyTreeNode* next_sibling;
- *   // ...contents...
- * }
- * \endcode
- * Adding a new child to a node with the above structure can be performed with the following call:
- * \code
- * MyTreeNode *node, *child; // given
- * cx_tree_add_child(&node->first_child, &node->last_child,
- *                   offsetof(MyTreeNode, prev_sibling), offsetof(MyTreeNode, next_sibling),
- *                   child, offsetof(MyTreeNode, parent), node);
- * \endcode
- *
- * \par Example with a reduced structure
- * The minimal reasonable structure with parent pointer looks like this:
- * \code
- * typedef struct MyTreeNode MyTreeNode;
- * struct MyTreeNode {
- *   MyTreeNode* parent;
- *   MyTreeNode* children;
- *   MyTreeNode* next_sibling;
- *   // ...contents...
- * }
- * \endcode
- * This simplifies the function call to:
- * \code
- * MyTreeNode *node, *child; // given
- * cx_tree_add_child(&node->children, NULL, -1, offsetof(MyTreeNode, next_sibling),
- *                   child, offsetof(MyTreeNode, parent), node);
- * \endcode
- *
- * \remark If your tree structure does not possess a parent pointer, a call to this function is
- * effectively the same as a call to cx_linked_list_add().
- *
- * @param children_begin a pointer to the begin node pointer (if your list has one)
- * @param children_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
- * @param loc_next the location of a \c next pointer within your node struct
- * @param new_node a pointer to the node that shall be appended
- * @param loc_parent the location of a \c parent pointer within your node struct
- * @param parent the parent node
- */
-void cx_tree_add_child(void **children_begin, void **children_end,
-                       ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node,
-                       ptrdiff_t loc_parent, void *parent)
-__attribute__((__nonnull__ (5)));
-
-
-#ifdef __cplusplus
-} // extern "C"
-#endif
-
-#endif // UCX_TREE_H
-
--- a/ucx/cx/utils.h	Sun Oct 01 09:23:47 2023 +0200
+++ b/ucx/cx/utils.h	Sun Oct 01 12:08:09 2023 +0200
@@ -183,7 +183,6 @@
  * @param dest the destination stream
  * @param rfnc the read function
  * @param wfnc the write function
- * @param n the maximum number of bytes that shall be copied.
  * @return total number of bytes copied
  */
 #define cx_stream_copy(src, dest, rfnc, wfnc) \
--- a/ucx/linked_list.c	Sun Oct 01 09:23:47 2023 +0200
+++ b/ucx/linked_list.c	Sun Oct 01 12:08:09 2023 +0200
@@ -607,7 +607,7 @@
 }
 
 #ifndef CX_LINKED_LIST_SWAP_SBO_SIZE
-#define CX_LINKED_LIST_SWAP_SBO_SIZE 16
+#define CX_LINKED_LIST_SWAP_SBO_SIZE 128
 #endif
 
 static int cx_ll_swap(
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ucx/mempool.c	Sun Oct 01 12:08:09 2023 +0200
@@ -0,0 +1,232 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "cx/mempool.h"
+#include "cx/utils.h"
+#include <string.h>
+
+struct cx_mempool_memory_s {
+    /** The destructor. */
+    cx_destructor_func destructor;
+    /** The actual memory. */
+    char c[];
+};
+
+static void *cx_mempool_malloc(
+        void *p,
+        size_t n
+) {
+    struct cx_mempool_s *pool = p;
+
+    if (pool->size >= pool->capacity) {
+        size_t newcap = pool->capacity - (pool->capacity % 16) + 16;
+        struct cx_mempool_memory_s **newdata = realloc(pool->data, newcap*sizeof(struct cx_mempool_memory_s*));
+        if (newdata == NULL) {
+            return NULL;
+        }
+        pool->data = newdata;
+        pool->capacity = newcap;
+    }
+
+    struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n);
+    if (mem == NULL) {
+        return NULL;
+    }
+
+    mem->destructor = pool->auto_destr;
+    pool->data[pool->size] = mem;
+    pool->size++;
+
+    return mem->c;
+}
+
+static void *cx_mempool_calloc(
+        void *p,
+        size_t nelem,
+        size_t elsize
+) {
+    size_t msz;
+    if (cx_szmul(nelem, elsize, &msz)) {
+        return NULL;
+    }
+    void *ptr = cx_mempool_malloc(p, msz);
+    if (ptr == NULL) {
+        return NULL;
+    }
+    memset(ptr, 0, nelem * elsize);
+    return ptr;
+}
+
+static void *cx_mempool_realloc(
+        void *p,
+        void *ptr,
+        size_t n
+) {
+    struct cx_mempool_s *pool = p;
+
+    struct cx_mempool_memory_s *mem, *newm;
+    mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func));
+    newm = realloc(mem, n + sizeof(cx_destructor_func));
+
+    if (newm == NULL) {
+        return NULL;
+    }
+    if (mem != newm) {
+        cx_for_n(i, pool->size) {
+            if (pool->data[i] == mem) {
+                pool->data[i] = newm;
+                return ((char*)newm) + sizeof(cx_destructor_func);
+            }
+        }
+        abort();
+    } else {
+        return ptr;
+    }
+}
+
+static void cx_mempool_free(
+        void *p,
+        void *ptr
+) {
+    struct cx_mempool_s *pool = p;
+
+    struct cx_mempool_memory_s *mem = (struct cx_mempool_memory_s *)
+            ((char *) ptr - sizeof(cx_destructor_func));
+
+    cx_for_n(i, pool->size) {
+        if (mem == pool->data[i]) {
+            if (mem->destructor) {
+                mem->destructor(mem->c);
+            }
+            free(mem);
+            size_t last_index = pool->size - 1;
+            if (i != last_index) {
+                pool->data[i] = pool->data[last_index];
+                pool->data[last_index] = NULL;
+            }
+            pool->size--;
+            return;
+        }
+    }
+    abort();
+}
+
+void cxMempoolDestroy(CxMempool *pool) {
+    struct cx_mempool_memory_s *mem;
+    cx_for_n(i, pool->size) {
+        mem = pool->data[i];
+        if (mem->destructor) {
+            mem->destructor(mem->c);
+        }
+        free(mem);
+    }
+    free(pool->data);
+    free((void*) pool->allocator);
+    free(pool);
+}
+
+void cxMempoolSetDestructor(
+        void *ptr,
+        cx_destructor_func func
+) {
+    *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func;
+}
+
+struct cx_mempool_foreign_mem_s {
+    cx_destructor_func destr;
+    void* mem;
+};
+
+static void cx_mempool_destr_foreign_mem(void* ptr) {
+    struct cx_mempool_foreign_mem_s *fm = ptr;
+    fm->destr(fm->mem);
+}
+
+int cxMempoolRegister(
+        CxMempool *pool,
+        void *memory,
+        cx_destructor_func destr
+) {
+    struct cx_mempool_foreign_mem_s *fm = cx_mempool_malloc(
+            pool,
+            sizeof(struct cx_mempool_foreign_mem_s)
+    );
+    if (fm == NULL) return 1;
+
+    fm->mem = memory;
+    fm->destr = destr;
+    *(cx_destructor_func *) ((char *) fm - sizeof(cx_destructor_func)) = cx_mempool_destr_foreign_mem;
+
+    return 0;
+}
+
+static cx_allocator_class cx_mempool_allocator_class = {
+        cx_mempool_malloc,
+        cx_mempool_realloc,
+        cx_mempool_calloc,
+        cx_mempool_free
+};
+
+CxMempool *cxMempoolCreate(
+        size_t capacity,
+        cx_destructor_func destr
+) {
+    size_t poolsize;
+    if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) {
+        return NULL;
+    }
+
+    struct cx_mempool_s *pool =
+            malloc(sizeof(struct cx_mempool_s));
+    if (pool == NULL) {
+        return NULL;
+    }
+
+    CxAllocator *provided_allocator = malloc(sizeof(CxAllocator));
+    if (provided_allocator == NULL) {
+        free(pool);
+        return NULL;
+    }
+    provided_allocator->cl = &cx_mempool_allocator_class;
+    provided_allocator->data = pool;
+
+    pool->allocator = provided_allocator;
+
+    pool->data = malloc(poolsize);
+    if (pool->data == NULL) {
+        free(provided_allocator);
+        free(pool);
+        return NULL;
+    }
+
+    pool->size = 0;
+    pool->capacity = capacity;
+    pool->auto_destr = destr;
+
+    return (CxMempool *) pool;
+}
--- a/ucx/tree.c	Sun Oct 01 09:23:47 2023 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,52 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "cx/tree.h"
-#include "cx/linked_list.h"
-
-#define CX_TR_PTR(cur, off) *((void**)(((char*)(cur))+(off)))
-
-void cx_tree_add_sibling(void *node, ptrdiff_t loc_prev, ptrdiff_t loc_next, ptrdiff_t loc_parent, void *new_node) {
-    cx_linked_list_add(&node, NULL, loc_prev, loc_next, new_node);
-
-    // optional parent link
-    if (loc_parent >= 0) {
-        CX_TR_PTR(new_node, loc_parent) = CX_TR_PTR(node, loc_parent);
-    }
-}
-
-void cx_tree_add_child(void **children_begin, void **children_end,
-                       ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node,
-                       ptrdiff_t loc_parent, void *parent) {
-    cx_linked_list_add(children_begin, children_end, loc_prev, loc_next, new_node);
-
-    // optional parent link
-    if (loc_parent >= 0) {
-        CX_TR_PTR(new_node, loc_parent) = parent;
-    }
-}
--- a/ucx/utils.c	Sun Oct 01 09:23:47 2023 +0200
+++ b/ucx/utils.c	Sun Oct 01 12:08:09 2023 +0200
@@ -28,8 +28,13 @@
 
 #include "cx/utils.h"
 
+#ifndef CX_STREAM_BCOPY_BUF_SIZE
 #define CX_STREAM_BCOPY_BUF_SIZE 8192
+#endif
+
+#ifndef CX_STREAM_COPY_BUF_SIZE
 #define CX_STREAM_COPY_BUF_SIZE 1024
+#endif
 
 size_t cx_stream_bncopy(
         void *src,
--- a/ui/common/context.c	Sun Oct 01 09:23:47 2023 +0200
+++ b/ui/common/context.c	Sun Oct 01 12:08:09 2023 +0200
@@ -34,7 +34,7 @@
 
 #include <cx/array_list.h>
 #include <cx/compare.h>
-#include <cx/basic_mempool.h>
+#include <cx/mempool.h>
 
 #include "context.h"
 #include "../ui/window.h"
@@ -45,23 +45,24 @@
 
 void uic_init_global_context(void) {
     CxMempool *mp = cxBasicMempoolCreate(32);
-    global_context = uic_context(NULL, mp->allocator);
+    global_context = uic_context(NULL, mp);
 }
 
 UiContext* ui_global_context(void) {
     return global_context;
 }
 
-UiContext* uic_context(UiObject *toplevel, const CxAllocator *a) {
-    UiContext *ctx = cxMalloc(a, sizeof(UiContext));
+UiContext* uic_context(UiObject *toplevel, CxMempool *mp) {
+    UiContext *ctx = cxMalloc(mp->allocator, sizeof(UiContext));
     memset(ctx, 0, sizeof(UiContext));
-    ctx->allocator = a;
+    ctx->mp = mp;
+    ctx->allocator = mp->allocator;
     ctx->obj = toplevel;
-    ctx->vars = cxHashMapCreate(a, CX_STORE_POINTERS, 16);
+    ctx->vars = cxHashMapCreate(mp->allocator, CX_STORE_POINTERS, 16);
     
-    ctx->documents = cxLinkedListCreate(a, cx_cmp_intptr, CX_STORE_POINTERS);
-    ctx->group_widgets = cxLinkedListCreate(a, NULL, sizeof(UiGroupWidget));
-    ctx->groups = cxArrayListCreate(a, cx_cmp_int, sizeof(int), 32);
+    ctx->documents = cxLinkedListCreate(mp->allocator, cx_cmp_intptr, CX_STORE_POINTERS);
+    ctx->group_widgets = cxLinkedListCreate(mp->allocator, NULL, sizeof(UiGroupWidget));
+    ctx->groups = cxArrayListCreate(mp->allocator, cx_cmp_int, sizeof(int), 32);
     
     ctx->attach_document = uic_context_attach_document;
     ctx->detach_document2 = uic_context_detach_document2;
@@ -195,6 +196,8 @@
     var->from = NULL;
     var->from_ctx = ctx;
 
+    cxMempoolRegister(ctx->mp, var, (cx_destructor_func)uic_unbind_var);
+
     if(!ctx->vars_unbound) {
         ctx->vars_unbound = cxHashMapCreate(ctx->allocator, CX_STORE_POINTERS, 16);
     }
@@ -203,6 +206,15 @@
     return var;
 }
 
+UiVar* uic_create_value_var(UiContext* ctx, void* value) {
+    UiVar *var = (UiVar*)ui_malloc(ctx, sizeof(UiVar));
+    var->from = NULL;
+    var->from_ctx = NULL;
+    var->value = value;
+    var->type = UI_VAR_SPECIAL;
+    return var;
+}
+
 void* uic_create_value(UiContext *ctx, UiVarType type) {
     void *val = NULL;
     switch(type) {
--- a/ui/common/context.h	Sun Oct 01 09:23:47 2023 +0200
+++ b/ui/common/context.h	Sun Oct 01 12:08:09 2023 +0200
@@ -60,6 +60,7 @@
 struct UiContext {
     UiContext     *parent;
     UiObject      *obj;
+    CxMempool *mp;
     const CxAllocator *allocator;
     
     void          *document;
@@ -102,7 +103,7 @@
 
 void uic_init_global_context(void);
 
-UiContext* uic_context(UiObject *toplevel, const CxAllocator *a);
+UiContext* uic_context(UiObject *toplevel, CxMempool *mp);
 UiContext* uic_root_context(UiContext *ctx);
 void uic_context_set_document(UiContext *ctx, void *document); // deprecated
 void uic_context_detach_document(UiContext *ctx); // deprecated
@@ -113,6 +114,7 @@
 
 UiVar* uic_get_var(UiContext *ctx, const char *name);
 UiVar* uic_create_var(UiContext *ctx, const char *name, UiVarType type);
+UiVar* uic_create_value_var(UiContext *ctx, void *value);
 void* uic_create_value(UiContext *ctx, UiVarType type);
 
 void uic_copy_binding(UiVar *from, UiVar *to, UiBool copytodoc);
--- a/ui/common/document.c	Sun Oct 01 09:23:47 2023 +0200
+++ b/ui/common/document.c	Sun Oct 01 12:08:09 2023 +0200
@@ -33,7 +33,7 @@
 #include "document.h"
 
 #include <cx/hash_map.h>
-#include <cx/basic_mempool.h>
+#include <cx/mempool.h>
 
 static CxMap *documents;
 
--- a/ui/ui/button.h	Sun Oct 01 09:23:47 2023 +0200
+++ b/ui/ui/button.h	Sun Oct 01 12:08:09 2023 +0200
@@ -42,24 +42,32 @@
     int colspan;
     int rowspan;
 
-    char* label;
-    char* stockid;
+    const char* label;
+    const char* stockid;
     ui_callback onclick;
     void* onclickdata;
 } UiButtonArgs;
 
 typedef struct UiToggleArgs {
-    char* label;
-    char* stockid;
+    UiTri fill;
+    UiBool hexpand;
+    UiBool vexpand;
+    int colspan;
+    int rowspan;
+
+    const char* label;
+    const char* stockid;
     UiInteger* value;
-    char* varname;
+    const char* varname;
 } UiToggleArgs;
    
 #define ui_button(obj, ...) ui_button_create(obj, (UiButtonArgs){ __VA_ARGS__ } )
+#define ui_togglebutton(obj, ...) ui_togglebutton_create(obj, (UiToggleArgs){ __VA_ARGS__ } )
 #define ui_checkbox(obj, ...) ui_checkbox_create(obj, (UiToggleArgs){ __VA_ARGS__ } )
 #define ui_radiobutton(obj, ...) ui_checkbox_create(obj, (UiToggleArgs){ __VA_ARGS__ } )
 
 UIWIDGET ui_button_create(UiObject* obj, UiButtonArgs args);
+UIWIDGET ui_togglebutton_create(UiObject* obj, UiToggleArgs args);
 UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args);
 UIWIDGET ui_radiobutton_create(UiObject* obj, UiToggleArgs);
 
--- a/ui/ui/container.h	Sun Oct 01 09:23:47 2023 +0200
+++ b/ui/ui/container.h	Sun Oct 01 12:08:09 2023 +0200
@@ -92,7 +92,8 @@
     if(args.hexpand) ui_layout_hexpand(obj, 1); \
     if(args.vexpand) ui_layout_vexpand(obj, 1); \
     if(args.colspan > 0) ui_layout_colspan(obj, args.colspan); \
-    if(args.rowspan > 0) ui_layout_rowspan(obj, args.rowspan);
+    if(args.rowspan > 0) ui_layout_rowspan(obj, args.rowspan); \
+    /*force caller to add ';'*/(void)0
 
 
 #ifdef __cplusplus
--- a/ui/winui/button.cpp	Sun Oct 01 09:23:47 2023 +0200
+++ b/ui/winui/button.cpp	Sun Oct 01 12:08:09 2023 +0200
@@ -40,17 +40,26 @@
 using namespace Microsoft::UI::Xaml::Controls;
 using namespace Windows::UI::Xaml::Interop;
 using namespace winrt::Windows::Foundation;
+using namespace winrt::Microsoft::UI::Xaml::Controls::Primitives;
+
+
+
+static void set_button_label(ButtonBase button, const char* label, const char* stockid) {
+	if (label) {
+		wchar_t* wlabel = str2wstr(label, nullptr);
+		button.Content(box_value(wlabel));
+		free(wlabel);
+	}
+	// TODO: stockid
+}
+
 
 UIWIDGET ui_button_create(UiObject* obj, UiButtonArgs args) {
 	UiObject* current = uic_current_obj(obj);
 
 	// create button with label
 	Button button = Button();
-	if (args.label) {
-		wchar_t* wlabel = str2wstr(args.label, nullptr);
-		button.Content(box_value(wlabel));
-		free(wlabel);
-	}
+	set_button_label(button, args.label, args.stockid);
 
 	// create toolkit wrapper object and register destructor
 	UIElement elm = button;
@@ -77,15 +86,72 @@
 
 	// add button to current container
 	UI_APPLY_LAYOUT1(current, args);
+
 	current->container->Add(button, false);
 
 	return widget;
 }
 
+static UIWIDGET create_togglebutton(UiObject *obj, ToggleButton button, UiToggleArgs args) {
+	UiObject* current = uic_current_obj(obj);
+
+	// set label
+	set_button_label(button, args.label, args.stockid);
+
+	// create toolkit wrapper object and register destructor
+	UIElement elm = button;
+	UiWidget* widget = new UiWidget(elm);
+	ui_context_add_widget_destructor(current->ctx, widget);
+
+	// bind variable
+	UiVar* var = nullptr;
+	if (args.value) {
+		var = uic_create_value_var(current->ctx, args.value);
+	}
+	else if (args.varname) {
+		var = uic_create_var(obj->ctx, args.varname, UI_VAR_INTEGER);
+	}
+	if (var) {
+		UiInteger* value = (UiInteger*)var->value;
+		value->obj = widget;
+		value->get = ui_toggle_button_get;
+		value->set = ui_toggle_button_set;
+	}
+
+	// add button to current container
+	UI_APPLY_LAYOUT1(current, args);
+
+	current->container->Add(button, false);
+
+	return widget;
+}
+
+UIWIDGET ui_togglebutton_create(UiObject* obj, UiToggleArgs args) {
+	ToggleButton button = ToggleButton();
+	return create_togglebutton(obj, button, args);
+}
+
 UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args) {
-	return nullptr;
+	CheckBox button = CheckBox();
+	return create_togglebutton(obj, button, args);
 }
 
 UIWIDGET ui_radiobutton_create(UiObject* obj, UiToggleArgs) {
 	return nullptr;
 }
+
+
+int64_t ui_toggle_button_get(UiInteger* integer) {
+	UiWidget* widget = (UiWidget*)integer->obj;
+	ToggleButton toggleButton = widget->uielement.as<ToggleButton>();
+	int val = toggleButton.IsChecked().GetBoolean();
+	integer->value = val;
+	return val;
+}
+
+void ui_toggle_button_set(UiInteger* integer, int64_t value) {
+	UiWidget* widget = (UiWidget*)integer->obj;
+	ToggleButton toggleButton = widget->uielement.as<ToggleButton>();
+	toggleButton.IsChecked((bool)value);
+	integer->value = value;
+}
--- a/ui/winui/button.h	Sun Oct 01 09:23:47 2023 +0200
+++ b/ui/winui/button.h	Sun Oct 01 12:08:09 2023 +0200
@@ -31,4 +31,8 @@
 #include "toolkit.h"
 #include "../ui/container.h"
 
-#include "../ui/button.h"
\ No newline at end of file
+#include "../ui/button.h"
+
+extern "C" int64_t ui_toggle_button_get(UiInteger * integer);
+
+extern "C" void ui_toggle_button_set(UiInteger * integer, int64_t value);
--- a/ui/winui/window.cpp	Sun Oct 01 09:23:47 2023 +0200
+++ b/ui/winui/window.cpp	Sun Oct 01 12:08:09 2023 +0200
@@ -45,7 +45,7 @@
 
 #include <stdlib.h>
 
-#include <cx/basic_mempool.h>
+#include <cx/mempool.h>
 
 using namespace winrt;
 using namespace Microsoft::UI::Xaml;
@@ -62,7 +62,7 @@
 	CxMempool* mp = cxBasicMempoolCreate(256);
 	UiObject* obj = (UiObject*)cxCalloc(mp->allocator, 1, sizeof(UiObject));
 
-	obj->ctx = uic_context(obj, mp->allocator);
+	obj->ctx = uic_context(obj, mp);
 	obj->window = window_data;
 
 	Window window = Window();

mercurial