ucx
UAP Common Extensions
Loading...
Searching...
No Matches
allocator.h
Go to the documentation of this file.
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 */
33#ifndef UCX_ALLOCATOR_H
34#define UCX_ALLOCATOR_H
35
36#include "common.h"
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
45typedef struct {
49 void *(*malloc)(
50 void *data,
51 size_t n
52 );
53
57 void *(*realloc)(
58 void *data,
59 void *mem,
60 size_t n
61 );
62
66 void *(*calloc)(
67 void *data,
68 size_t nmemb,
69 size_t size
70 );
71
75 void (*free)(
76 void *data,
77 void *mem
78 );
80
94
99
104extern const CxAllocator * const cxDefaultAllocator;
105
116typedef void (*cx_destructor_func)(void *memory);
117
129typedef void (*cx_destructor_func2)(
130 void *data,
131 void *memory
132);
133
151 void **mem,
152 size_t n
153);
154
176 void **mem,
177 size_t nmemb,
178 size_t size
179);
180
194#define cx_reallocate(mem, n) cx_reallocate_((void**)(mem), n)
195
212#define cx_reallocatearray(mem, nmemb, size) \
213 cx_reallocatearray_((void**)(mem), nmemb, size)
214
226 const CxAllocator *allocator,
227 void *mem
228);
229
244 const CxAllocator *allocator,
245 size_t n
246);
247
267 const CxAllocator *allocator,
268 void *mem,
269 size_t n
270);
271
296 const CxAllocator *allocator,
297 void *mem,
298 size_t nmemb,
299 size_t size
300);
301
322 const CxAllocator *allocator,
323 void **mem,
324 size_t n
325);
326
343#define cxReallocate(allocator, mem, n) \
344 cxReallocate_(allocator, (void**)(mem), n)
345
369 const CxAllocator *allocator,
370 void **mem,
371 size_t nmemb,
372 size_t size
373);
374
394#define cxReallocateArray(allocator, mem, nmemb, size) \
395 cxReallocateArray_(allocator, (void**) (mem), nmemb, size)
396
412 const CxAllocator *allocator,
413 size_t nmemb,
414 size_t size
415);
416
417#ifdef __cplusplus
418} // extern "C"
419#endif
420
421#endif // UCX_ALLOCATOR_H
int cxReallocate_(const CxAllocator *allocator, void **mem, size_t n)
Reallocate a previously allocated block and changes the pointer in-place, if necessary.
void(* cx_destructor_func2)(void *data, void *memory)
Function pointer type for destructor functions.
Definition allocator.h:129
int cx_reallocatearray_(void **mem, size_t nmemb, size_t size)
Reallocate a previously allocated block and changes the pointer in-place, if necessary.
void cxFree(const CxAllocator *allocator, void *mem)
Free a block allocated by this allocator.
void(* cx_destructor_func)(void *memory)
Function pointer type for destructor functions.
Definition allocator.h:116
void * cxMalloc(const CxAllocator *allocator, size_t n)
Allocate n bytes of memory.
void * cxReallocArray(const CxAllocator *allocator, void *mem, size_t nmemb, size_t size)
Reallocate the previously allocated block in mem, making the new block n bytes long.
int cx_reallocate_(void **mem, size_t n)
Reallocate a previously allocated block and changes the pointer in-place, if necessary.
const CxAllocator *const cxDefaultAllocator
A default allocator using standard library malloc() etc.
void * cxCalloc(const CxAllocator *allocator, size_t nmemb, size_t size)
Allocate nmemb elements of n bytes each, all initialized to zero.
void * cxRealloc(const CxAllocator *allocator, void *mem, size_t n)
Reallocate the previously allocated block in mem, making the new block n bytes long.
int cxReallocateArray_(const CxAllocator *allocator, void **mem, size_t nmemb, size_t size)
Reallocate a previously allocated block and changes the pointer in-place, if necessary.
Common definitions and feature checks.
#define cx_attr_export
Only used for building Windows DLLs.
Definition common.h:285
#define cx_attr_allocsize(...)
Specifies the parameters from which the allocation size is calculated.
Definition common.h:178
#define cx_attr_nonnull
All pointer arguments must be non-NULL.
Definition common.h:136
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:265
#define cx_attr_malloc
The attributed function always returns freshly allocated memory.
Definition common.h:151
#define cx_attr_dealloc_ucx
Shortcut to specify cxFree() as deallocator.
Definition common.h:173
#define cx_attr_nonnull_arg(...)
The specified pointer arguments must be non-NULL.
Definition common.h:141
The class definition for an allocator.
Definition allocator.h:45
Structure holding the data for an allocator.
Definition allocator.h:84
void * data
A pointer to the data this allocator uses.
Definition allocator.h:92
cx_allocator_class * cl
A pointer to the instance of the allocator class.
Definition allocator.h:88