Thu, 29 May 2025 14:41:02 +0200
add missing UIEXPORT to arg wrapper functions
| 174 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 28 | ||
| 29 | #include "cx/allocator.h" | |
| 30 | ||
| 440 | 31 | #include <errno.h> |
| 32 | ||
| 174 | 33 | static void *cx_malloc_stdlib( |
| 440 | 34 | cx_attr_unused void *d, |
| 174 | 35 | size_t n |
| 36 | ) { | |
| 37 | return malloc(n); | |
| 38 | } | |
| 39 | ||
| 40 | static void *cx_realloc_stdlib( | |
| 440 | 41 | cx_attr_unused void *d, |
| 174 | 42 | void *mem, |
| 43 | size_t n | |
| 44 | ) { | |
| 45 | return realloc(mem, n); | |
| 46 | } | |
| 47 | ||
| 48 | static void *cx_calloc_stdlib( | |
| 440 | 49 | cx_attr_unused void *d, |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
50 | size_t nmemb, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
51 | size_t size |
| 174 | 52 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
53 | return calloc(nmemb, size); |
| 174 | 54 | } |
| 55 | ||
| 56 | static void cx_free_stdlib( | |
| 440 | 57 | cx_attr_unused void *d, |
| 174 | 58 | void *mem |
| 59 | ) { | |
| 60 | free(mem); | |
| 61 | } | |
| 62 | ||
| 63 | static cx_allocator_class cx_default_allocator_class = { | |
| 64 | cx_malloc_stdlib, | |
| 65 | cx_realloc_stdlib, | |
| 66 | cx_calloc_stdlib, | |
| 67 | cx_free_stdlib | |
| 68 | }; | |
| 69 | ||
| 70 | struct cx_allocator_s cx_default_allocator = { | |
| 71 | &cx_default_allocator_class, | |
| 72 | NULL | |
| 73 | }; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
74 | const CxAllocator * const cxDefaultAllocator = &cx_default_allocator; |
| 174 | 75 | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
76 | int cx_reallocate_( |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
77 | void **mem, |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
78 | size_t n |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
79 | ) { |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
80 | void *nmem = realloc(*mem, n); |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
81 | if (nmem == NULL) { |
| 440 | 82 | return 1; // LCOV_EXCL_LINE |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
83 | } else { |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
84 | *mem = nmem; |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
85 | return 0; |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
86 | } |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
87 | } |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
88 | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
89 | int cx_reallocatearray_( |
| 440 | 90 | void **mem, |
| 91 | size_t nmemb, | |
| 92 | size_t size | |
| 93 | ) { | |
| 94 | size_t n; | |
| 95 | if (cx_szmul(nmemb, size, &n)) { | |
| 96 | errno = EOVERFLOW; | |
| 97 | return 1; | |
| 98 | } else { | |
| 99 | void *nmem = realloc(*mem, n); | |
| 100 | if (nmem == NULL) { | |
| 101 | return 1; // LCOV_EXCL_LINE | |
| 102 | } else { | |
| 103 | *mem = nmem; | |
| 104 | return 0; | |
| 105 | } | |
| 106 | } | |
| 107 | } | |
| 108 | ||
| 174 | 109 | // IMPLEMENTATION OF HIGH LEVEL API |
| 110 | ||
| 111 | void *cxMalloc( | |
| 324 | 112 | const CxAllocator *allocator, |
| 174 | 113 | size_t n |
| 114 | ) { | |
| 115 | return allocator->cl->malloc(allocator->data, n); | |
| 116 | } | |
| 117 | ||
| 118 | void *cxRealloc( | |
| 324 | 119 | const CxAllocator *allocator, |
| 174 | 120 | void *mem, |
| 121 | size_t n | |
| 122 | ) { | |
| 123 | return allocator->cl->realloc(allocator->data, mem, n); | |
| 124 | } | |
| 125 | ||
| 440 | 126 | void *cxReallocArray( |
| 127 | const CxAllocator *allocator, | |
| 128 | void *mem, | |
| 129 | size_t nmemb, | |
| 130 | size_t size | |
| 131 | ) { | |
| 132 | size_t n; | |
| 133 | if (cx_szmul(nmemb, size, &n)) { | |
| 134 | errno = EOVERFLOW; | |
| 135 | return NULL; | |
| 136 | } else { | |
| 137 | return allocator->cl->realloc(allocator->data, mem, n); | |
| 138 | } | |
| 139 | } | |
| 140 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
141 | int cxReallocate_( |
| 324 | 142 | const CxAllocator *allocator, |
| 174 | 143 | void **mem, |
| 144 | size_t n | |
| 145 | ) { | |
| 146 | void *nmem = allocator->cl->realloc(allocator->data, *mem, n); | |
| 147 | if (nmem == NULL) { | |
| 440 | 148 | return 1; // LCOV_EXCL_LINE |
| 149 | } else { | |
| 150 | *mem = nmem; | |
| 151 | return 0; | |
| 152 | } | |
| 153 | } | |
| 154 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
155 | int cxReallocateArray_( |
| 440 | 156 | const CxAllocator *allocator, |
| 157 | void **mem, | |
| 158 | size_t nmemb, | |
| 159 | size_t size | |
| 160 | ) { | |
| 161 | void *nmem = cxReallocArray(allocator, *mem, nmemb, size); | |
| 162 | if (nmem == NULL) { | |
| 163 | return 1; // LCOV_EXCL_LINE | |
| 174 | 164 | } else { |
| 165 | *mem = nmem; | |
| 166 | return 0; | |
| 167 | } | |
| 168 | } | |
| 169 | ||
| 170 | void *cxCalloc( | |
| 324 | 171 | const CxAllocator *allocator, |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
172 | size_t nmemb, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
173 | size_t size |
| 174 | 174 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
175 | return allocator->cl->calloc(allocator->data, nmemb, size); |
| 174 | 176 | } |
| 177 | ||
| 178 | void cxFree( | |
| 324 | 179 | const CxAllocator *allocator, |
| 174 | 180 | void *mem |
| 181 | ) { | |
| 182 | allocator->cl->free(allocator->data, mem); | |
| 183 | } |