ucx/mempool.c

Sun, 05 Jan 2025 22:00:39 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 05 Jan 2025 22:00:39 +0100
changeset 440
7c4b9cba09ca
parent 187
24ce2c326d85
permissions
-rw-r--r--

update ucx

187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "cx/mempool.h"
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
30
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
32 #include <errno.h>
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 struct cx_mempool_memory_s {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 /** The destructor. */
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 cx_destructor_func destructor;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 /** The actual memory. */
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 char c[];
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 };
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 static void *cx_mempool_malloc(
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 void *p,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 size_t n
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 ) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 struct cx_mempool_s *pool = p;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 if (pool->size >= pool->capacity) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 size_t newcap = pool->capacity - (pool->capacity % 16) + 16;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
49 size_t newmsize;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
50 if (pool->capacity > newcap || cx_szmul(newcap,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
51 sizeof(struct cx_mempool_memory_s*), &newmsize)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
52 errno = EOVERFLOW;
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 return NULL;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
55 struct cx_mempool_memory_s **newdata = realloc(pool->data, newmsize);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
56 if (newdata == NULL) return NULL;
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 pool->data = newdata;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 pool->capacity = newcap;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
62 if (mem == NULL) return NULL;
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 mem->destructor = pool->auto_destr;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 pool->data[pool->size] = mem;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 pool->size++;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 return mem->c;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 static void *cx_mempool_calloc(
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 void *p,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 size_t nelem,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 size_t elsize
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 ) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 size_t msz;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 if (cx_szmul(nelem, elsize, &msz)) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
78 errno = EOVERFLOW;
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 return NULL;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 void *ptr = cx_mempool_malloc(p, msz);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
82 if (ptr == NULL) return NULL;
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 memset(ptr, 0, nelem * elsize);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 return ptr;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 static void *cx_mempool_realloc(
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 void *p,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 void *ptr,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 size_t n
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 ) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 struct cx_mempool_s *pool = p;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 struct cx_mempool_memory_s *mem, *newm;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func));
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 newm = realloc(mem, n + sizeof(cx_destructor_func));
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
98 if (newm == NULL) return NULL;
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 if (mem != newm) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
100 for (size_t i = 0; i < pool->size; i++) {
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 if (pool->data[i] == mem) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 pool->data[i] = newm;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 return ((char*)newm) + sizeof(cx_destructor_func);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
106 abort(); // LCOV_EXCL_LINE
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 } else {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 return ptr;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 static void cx_mempool_free(
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 void *p,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 void *ptr
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 ) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
116 if (!ptr) return;
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 struct cx_mempool_s *pool = p;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 struct cx_mempool_memory_s *mem = (struct cx_mempool_memory_s *)
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 ((char *) ptr - sizeof(cx_destructor_func));
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
122 for (size_t i = 0; i < pool->size; i++) {
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 if (mem == pool->data[i]) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 if (mem->destructor) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 mem->destructor(mem->c);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 free(mem);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 size_t last_index = pool->size - 1;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 if (i != last_index) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 pool->data[i] = pool->data[last_index];
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 pool->data[last_index] = NULL;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 pool->size--;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 return;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
137 abort(); // LCOV_EXCL_LINE
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
140 void cxMempoolFree(CxMempool *pool) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
141 if (pool == NULL) return;
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 struct cx_mempool_memory_s *mem;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
143 for (size_t i = 0; i < pool->size; i++) {
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 mem = pool->data[i];
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 if (mem->destructor) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 mem->destructor(mem->c);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 free(mem);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 free(pool->data);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 free((void*) pool->allocator);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 free(pool);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 void cxMempoolSetDestructor(
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 void *ptr,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 cx_destructor_func func
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 ) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
162 void cxMempoolRemoveDestructor(void *ptr) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
163 *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
164 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
165
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 struct cx_mempool_foreign_mem_s {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 cx_destructor_func destr;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 void* mem;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 };
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 static void cx_mempool_destr_foreign_mem(void* ptr) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 struct cx_mempool_foreign_mem_s *fm = ptr;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 fm->destr(fm->mem);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 int cxMempoolRegister(
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 CxMempool *pool,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 void *memory,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 cx_destructor_func destr
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 ) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 struct cx_mempool_foreign_mem_s *fm = cx_mempool_malloc(
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 pool,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 sizeof(struct cx_mempool_foreign_mem_s)
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 );
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 if (fm == NULL) return 1;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 fm->mem = memory;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 fm->destr = destr;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 *(cx_destructor_func *) ((char *) fm - sizeof(cx_destructor_func)) = cx_mempool_destr_foreign_mem;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 return 0;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 static cx_allocator_class cx_mempool_allocator_class = {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 cx_mempool_malloc,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 cx_mempool_realloc,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 cx_mempool_calloc,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 cx_mempool_free
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 };
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 CxMempool *cxMempoolCreate(
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 size_t capacity,
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 cx_destructor_func destr
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 ) {
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 size_t poolsize;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
207 errno = EOVERFLOW;
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 return NULL;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 }
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 struct cx_mempool_s *pool =
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 malloc(sizeof(struct cx_mempool_s));
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
213 if (pool == NULL) return NULL;
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 CxAllocator *provided_allocator = malloc(sizeof(CxAllocator));
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
216 if (provided_allocator == NULL) { // LCOV_EXCL_START
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 free(pool);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 return NULL;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
219 } // LCOV_EXCL_STOP
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 provided_allocator->cl = &cx_mempool_allocator_class;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 provided_allocator->data = pool;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 pool->allocator = provided_allocator;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 pool->data = malloc(poolsize);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
226 if (pool->data == NULL) { // LCOV_EXCL_START
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 free(provided_allocator);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 free(pool);
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 return NULL;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
230 } // LCOV_EXCL_STOP
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 pool->size = 0;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 pool->capacity = capacity;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 pool->auto_destr = destr;
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
236 return pool;
187
24ce2c326d85 implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 }

mercurial