src/cx/allocator.h

changeset 963
2f601274bbac
parent 935
312fb24c14de
equal deleted inserted replaced
962:cd418898af5c 963:2f601274bbac
151 ); 151 );
152 152
153 /** 153 /**
154 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. 154 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
155 * 155 *
156 * The size is calculated by multiplying \p nemb and \p size.
157 *
158 * \par Error handling
159 * \c errno will be set by realloc() on failure or when the multiplication of
160 * \p nmemb and \p size overflows.
161 *
162 * @param mem pointer to the pointer to allocated block
163 * @param nmemb the number of elements
164 * @param size the size of each element
165 * @return zero on success, non-zero on failure
166 */
167 __attribute__((__nonnull__))
168 int cx_reallocatearray(
169 void **mem,
170 size_t nmemb,
171 size_t size
172 );
173
174 /**
175 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
176 *
156 * \par Error handling 177 * \par Error handling
157 * \c errno will be set by realloc() on failure. 178 * \c errno will be set by realloc() on failure.
158 * 179 *
159 * @param mem pointer to the pointer to allocated block 180 * @param mem pointer to the pointer to allocated block
160 * @param n the new size in bytes 181 * @param n the new size in bytes
161 * @return zero on success, non-zero on failure 182 * @return zero on success, non-zero on failure
162 */ 183 */
163 #define cx_reallocate(mem, n) cx_reallocate((void**)(mem), n) 184 #define cx_reallocate(mem, n) cx_reallocate((void**)(mem), n)
185
186 /**
187 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
188 *
189 * The size is calculated by multiplying \p nemb and \p size.
190 *
191 * \par Error handling
192 * \c errno will be set by realloc() on failure or when the multiplication of
193 * \p nmemb and \p size overflows.
194 *
195 * @param mem pointer to the pointer to allocated block
196 * @param nmemb the number of elements
197 * @param size the size of each element
198 * @return zero on success, non-zero on failure
199 */
200 #define cx_reallocatearray(mem, nmemb, size) cx_reallocatearray((void**)(mem), nmemb, size)
164 201
165 /** 202 /**
166 * Allocate \p n bytes of memory. 203 * Allocate \p n bytes of memory.
167 * 204 *
168 * @param allocator the allocator 205 * @param allocator the allocator
169 * @param n the number of bytes 206 * @param n the number of bytes
170 * @return a pointer to the allocated memory 207 * @return a pointer to the allocated memory
171 */ 208 */
172 __attribute__((__malloc__)) 209 __attribute__((__malloc__))
210 __attribute__((__nonnull__))
173 __attribute__((__alloc_size__(2))) 211 __attribute__((__alloc_size__(2)))
174 void *cxMalloc( 212 void *cxMalloc(
175 const CxAllocator *allocator, 213 const CxAllocator *allocator,
176 size_t n 214 size_t n
177 ); 215 );
187 * @param mem pointer to the previously allocated block 225 * @param mem pointer to the previously allocated block
188 * @param n the new size in bytes 226 * @param n the new size in bytes
189 * @return a pointer to the re-allocated memory 227 * @return a pointer to the re-allocated memory
190 */ 228 */
191 __attribute__((__warn_unused_result__)) 229 __attribute__((__warn_unused_result__))
230 __attribute__((__nonnull__(1)))
192 __attribute__((__alloc_size__(3))) 231 __attribute__((__alloc_size__(3)))
193 void *cxRealloc( 232 void *cxRealloc(
194 const CxAllocator *allocator, 233 const CxAllocator *allocator,
195 void *mem, 234 void *mem,
196 size_t n 235 size_t n
197 ); 236 );
198 237
199 /** 238 /**
239 * Re-allocate the previously allocated block in \p mem, making the new block \p n bytes long.
240 * This function may return the same pointer that was passed to it, if moving the memory
241 * was not necessary.
242 *
243 * The size is calculated by multiplying \p nemb and \p size.
244 * If that multiplication overflows, this function returns \c NULL and \c errno will be set.
245 *
246 * \note Re-allocating a block allocated by a different allocator is undefined.
247 *
248 * @param allocator the allocator
249 * @param mem pointer to the previously allocated block
250 * @param nmemb the number of elements
251 * @param size the size of each element
252 * @return a pointer to the re-allocated memory
253 */
254 __attribute__((__warn_unused_result__))
255 __attribute__((__nonnull__(1)))
256 __attribute__((__alloc_size__(3, 4)))
257 void *cxReallocArray(
258 const CxAllocator *allocator,
259 void *mem,
260 size_t nmemb,
261 size_t size
262 );
263
264 /**
200 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. 265 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
201 * This function acts like cxRealloc() using the pointer pointed to by \p mem. 266 * This function acts like cxRealloc() using the pointer pointed to by \p mem.
202 * 267 *
203 * \note Re-allocating a block allocated by a different allocator is undefined. 268 * \note Re-allocating a block allocated by a different allocator is undefined.
204 * 269 *
214 int cxReallocate( 279 int cxReallocate(
215 const CxAllocator *allocator, 280 const CxAllocator *allocator,
216 void **mem, 281 void **mem,
217 size_t n 282 size_t n
218 ); 283 );
284
285 /**
286 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
287 * This function acts like cxRealloc() using the pointer pointed to by \p mem.
288 *
289 * \note Re-allocating a block allocated by a different allocator is undefined.
290 *
291 * \par Error handling
292 * \c errno will be set, if the underlying realloc function does so.
293 *
294 * @param allocator the allocator
295 * @param mem pointer to the pointer to allocated block
296 * @param n the new size in bytes
297 * @return zero on success, non-zero on failure
298 */
299 #define cxReallocate(allocator, mem, n) cxReallocate(allocator, (void**)(mem), n)
300
301 /**
302 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
303 * This function acts like cxReallocArray() using the pointer pointed to by \p mem.
304 *
305 * \note Re-allocating a block allocated by a different allocator is undefined.
306 *
307 * \par Error handling
308 * \c errno will be set, if the underlying realloc function does so or the
309 * multiplication of \p nmemb and \p size overflows.
310 *
311 * @param allocator the allocator
312 * @param mem pointer to the pointer to allocated block
313 * @param nmemb the number of elements
314 * @param size the size of each element
315 * @return zero on success, non-zero on failure
316 */
317 __attribute__((__nonnull__))
318 int cxReallocateArray(
319 const CxAllocator *allocator,
320 void **mem,
321 size_t nmemb,
322 size_t size
323 );
324
325 /**
326 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
327 * This function acts like cxReallocArray() using the pointer pointed to by \p mem.
328 *
329 * \note Re-allocating a block allocated by a different allocator is undefined.
330 *
331 * \par Error handling
332 * \c errno will be set, if the underlying realloc function does so or the
333 * multiplication of \p nmemb and \p size overflows.
334 *
335 * @param allocator the allocator
336 * @param mem pointer to the pointer to allocated block
337 * @param nmemb the number of elements
338 * @param size the size of each element
339 * @return zero on success, non-zero on failure
340 */
341 #define cxReallocateArray(allocator, mem, nmemb, size) \
342 cxReallocateArray(allocator, (void**) (mem), nmemb, size)
219 343
220 /** 344 /**
221 * Allocate \p nelem elements of \p n bytes each, all initialized to zero. 345 * Allocate \p nelem elements of \p n bytes each, all initialized to zero.
222 * 346 *
223 * @param allocator the allocator 347 * @param allocator the allocator
224 * @param nelem the number of elements 348 * @param nelem the number of elements
225 * @param n the size of each element in bytes 349 * @param n the size of each element in bytes
226 * @return a pointer to the allocated memory 350 * @return a pointer to the allocated memory
227 */ 351 */
228 __attribute__((__malloc__)) 352 __attribute__((__malloc__))
353 __attribute__((__nonnull__))
229 __attribute__((__alloc_size__(2, 3))) 354 __attribute__((__alloc_size__(2, 3)))
230 void *cxCalloc( 355 void *cxCalloc(
231 const CxAllocator *allocator, 356 const CxAllocator *allocator,
232 size_t nelem, 357 size_t nelem,
233 size_t n 358 size_t n
239 * \note Freeing a block of a different allocator is undefined. 364 * \note Freeing a block of a different allocator is undefined.
240 * 365 *
241 * @param allocator the allocator 366 * @param allocator the allocator
242 * @param mem a pointer to the block to free 367 * @param mem a pointer to the block to free
243 */ 368 */
244 __attribute__((__nonnull__)) 369 __attribute__((__nonnull__(1)))
245 void cxFree( 370 void cxFree(
246 const CxAllocator *allocator, 371 const CxAllocator *allocator,
247 void *mem 372 void *mem
248 ); 373 );
249 374

mercurial