25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 #include "cx/allocator.h" |
29 #include "cx/allocator.h" |
|
30 #include "cx/utils.h" |
|
31 |
|
32 #include <errno.h> |
30 |
33 |
31 __attribute__((__malloc__, __alloc_size__(2))) |
34 __attribute__((__malloc__, __alloc_size__(2))) |
32 static void *cx_malloc_stdlib( |
35 static void *cx_malloc_stdlib( |
33 __attribute__((__unused__)) void *d, |
36 __attribute__((__unused__)) void *d, |
34 size_t n |
37 size_t n |
104 size_t n |
128 size_t n |
105 ) { |
129 ) { |
106 return allocator->cl->realloc(allocator->data, mem, n); |
130 return allocator->cl->realloc(allocator->data, mem, n); |
107 } |
131 } |
108 |
132 |
|
133 void *cxReallocArray( |
|
134 const CxAllocator *allocator, |
|
135 void *mem, |
|
136 size_t nmemb, |
|
137 size_t size |
|
138 ) { |
|
139 size_t n; |
|
140 if (cx_szmul(nmemb, size, &n)) { |
|
141 errno = ENOMEM; |
|
142 return NULL; |
|
143 } else { |
|
144 return allocator->cl->realloc(allocator->data, mem, n); |
|
145 } |
|
146 } |
|
147 |
|
148 #undef cxReallocate |
109 int cxReallocate( |
149 int cxReallocate( |
110 const CxAllocator *allocator, |
150 const CxAllocator *allocator, |
111 void **mem, |
151 void **mem, |
112 size_t n |
152 size_t n |
113 ) { |
153 ) { |
114 void *nmem = allocator->cl->realloc(allocator->data, *mem, n); |
154 void *nmem = allocator->cl->realloc(allocator->data, *mem, n); |
|
155 if (nmem == NULL) { |
|
156 return 1; |
|
157 } else { |
|
158 *mem = nmem; |
|
159 return 0; |
|
160 } |
|
161 } |
|
162 |
|
163 #undef cxReallocateArray |
|
164 int cxReallocateArray( |
|
165 const CxAllocator *allocator, |
|
166 void **mem, |
|
167 size_t nmemb, |
|
168 size_t size |
|
169 ) { |
|
170 void *nmem = cxReallocArray(allocator, *mem, nmemb, size); |
115 if (nmem == NULL) { |
171 if (nmem == NULL) { |
116 return 1; |
172 return 1; |
117 } else { |
173 } else { |
118 *mem = nmem; |
174 *mem = nmem; |
119 return 0; |
175 return 0; |