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 __attribute__((__warn_unused_result__));
63
67 void *(*calloc)(
68 void *data,
69 size_t nelem,
70 size_t n
71 );
72
76 void (*free)(
77 void *data,
78 void *mem
79 )
80 __attribute__((__nonnull__));
82
94 void *data;
95};
96
101
106
117typedef void (*cx_destructor_func)(void *memory) __attribute__((__nonnull__));
118
130typedef void (*cx_destructor_func2)(
131 void *data,
132 void *memory
133) __attribute__((__nonnull__(2)));
134
146 void **mem,
147 size_t n
148)
149__attribute__((__nonnull__));
150
159 CxAllocator const *allocator,
160 size_t n
161)
162__attribute__((__malloc__))
163__attribute__((__alloc_size__(2)));
164
178 CxAllocator const *allocator,
179 void *mem,
180 size_t n
181)
182__attribute__((__warn_unused_result__))
183__attribute__((__alloc_size__(3)));
184
200 CxAllocator const *allocator,
201 void **mem,
202 size_t n
203)
204__attribute__((__nonnull__));
205
215 CxAllocator const *allocator,
216 size_t nelem,
217 size_t n
218)
219__attribute__((__malloc__))
220__attribute__((__alloc_size__(2, 3)));
221
231 CxAllocator const *allocator,
232 void *mem
233)
234__attribute__((__nonnull__));
235
236#ifdef __cplusplus
237} // extern "C"
238#endif
239
240#endif // UCX_ALLOCATOR_H
void cxFree(CxAllocator const *allocator, void *mem)
Free a block allocated by this allocator.
CxAllocator * cxDefaultAllocator
A default allocator using standard library malloc() etc.
void(* cx_destructor_func2)(void *data, void *memory)
Function pointer type for destructor functions.
Definition: allocator.h:130
void * cxRealloc(CxAllocator const *allocator, void *mem, size_t n)
Re-allocate the previously allocated block in mem, making the new block n bytes long.
void(* cx_destructor_func)(void *memory)
Function pointer type for destructor functions.
Definition: allocator.h:117
int cx_reallocate(void **mem, size_t n)
Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
void * cxMalloc(CxAllocator const *allocator, size_t n)
Allocate n bytes of memory.
void * cxCalloc(CxAllocator const *allocator, size_t nelem, size_t n)
Allocate nelem elements of n bytes each, all initialized to zero.
int cxReallocate(CxAllocator const *allocator, void **mem, size_t n)
Re-allocate a previously allocated block and changes the pointer in-place, if necessary.
Common definitions and feature checks.
#define __attribute__(x)
Removes GNU C attributes where they are not supported.
Definition: common.h:127
The class definition for an allocator.
Definition: allocator.h:45
Structure holding the data for an allocator.
Definition: allocator.h:86
void * data
A pointer to the data this allocator uses.
Definition: allocator.h:94
cx_allocator_class * cl
A pointer to the instance of the allocator class.
Definition: allocator.h:90