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 */
32
33#ifndef UCX_ALLOCATOR_H
34#define UCX_ALLOCATOR_H
35
36#include "common.h"
37
41typedef struct {
45 void *(*malloc)(void *data, size_t n);
46
50 void *(*realloc)(void *data, void *mem, size_t n);
51
55 void *(*calloc)(void *data, size_t nmemb, size_t size);
56
60 void (*free)(void *data, void *mem);
62
76
81
85CX_EXPORT extern const CxAllocator * const cxStdlibAllocator;
86
92
103typedef void (*cx_destructor_func)(void *memory);
104
116typedef void (*cx_destructor_func2)(void *data, void *memory);
117
118
141typedef void*(*cx_clone_func)(void *target, const void *source,
142 const CxAllocator *allocator, void *data);
143
153unsigned long cx_system_page_size(void);
154
167int cx_reallocate_(void **mem, size_t n);
168
182int cx_reallocatearray_(void **mem, size_t nmemb, size_t size);
183
199#define cx_reallocate(mem, n) cx_reallocate_((void**)(mem), n)
200
219#define cx_reallocatearray(mem, nmemb, size) \
220 cx_reallocatearray_((void**)(mem), nmemb, size)
221
228#define cx_zalloc(n) calloc(1, n)
229
239void cxFree(const CxAllocator *allocator, void *mem);
240
250void *cxMalloc(const CxAllocator *allocator, size_t n);
251
269void *cxRealloc(const CxAllocator *allocator, void *mem, size_t n);
270
293void *cxReallocArray(const CxAllocator *allocator,
294 void *mem, size_t nmemb, size_t size);
295
308int cxReallocate_(const CxAllocator *allocator, void **mem, size_t n);
309
326#define cxReallocate(allocator, mem, n) \
327 cxReallocate_(allocator, (void**)(mem), n)
328
342int cxReallocateArray_(const CxAllocator *allocator,
343 void **mem, size_t nmemb, size_t size);
344
364#define cxReallocateArray(allocator, mem, nmemb, size) \
365 cxReallocateArray_(allocator, (void**) (mem), nmemb, size)
366
377void *cxCalloc(const CxAllocator *allocator, size_t nmemb, size_t size);
378
388void *cxZalloc(const CxAllocator *allocator, size_t n);
389
398#define cxMallocDefault(n) cxMalloc(cxDefaultAllocator, n)
399
408#define cxZallocDefault(n) cxZalloc(cxDefaultAllocator, n)
409
419#define cxCallocDefault(nmemb, size) cxCalloc(cxDefaultAllocator, nmemb, size)
420
435#define cxReallocDefault(mem, n) cxRealloc(cxDefaultAllocator, mem, n)
436
454#define cxReallocateDefault(mem, n) cxReallocate(cxDefaultAllocator, mem, n)
455
476#define cxReallocateArrayDefault(mem, nmemb, size) \
477 cxReallocateArray(cxDefaultAllocator, mem, nmemb, size)
478
500#define cxReallocArrayDefault(mem, nmemb, size) cxReallocArray(cxDefaultAllocator, mem, nmemb, size)
501
510void cxFreeDefault(void *mem);
511
512#endif // UCX_ALLOCATOR_H
unsigned long cx_system_page_size(void)
Returns the system's memory page size.
int cxReallocate_(const CxAllocator *allocator, void **mem, size_t n)
Reallocate a previously allocated block.
void(* cx_destructor_func2)(void *data, void *memory)
Function pointer type for destructor functions.
Definition allocator.h:116
int cx_reallocatearray_(void **mem, size_t nmemb, size_t size)
Reallocate a previously allocated block.
struct cx_allocator_s CxAllocator
High-Level type alias for the allocator type.
Definition allocator.h:80
void cxFree(const CxAllocator *allocator, void *mem)
Free a block allocated by this allocator.
const CxAllocator * cxDefaultAllocator
The default allocator that is used by UCX.
void * cxZalloc(const CxAllocator *allocator, size_t n)
Allocate n bytes of memory and sets every byte to zero.
void(* cx_destructor_func)(void *memory)
Function pointer type for destructor functions.
Definition allocator.h:103
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.
int cx_reallocate_(void **mem, size_t n)
Reallocate a previously allocated block.
void * cxCalloc(const CxAllocator *allocator, size_t nmemb, size_t size)
Allocate nmemb elements of size 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.
void cxFreeDefault(void *mem)
Free a block of memory.
int cxReallocateArray_(const CxAllocator *allocator, void **mem, size_t nmemb, size_t size)
Reallocate a previously allocated block.
const CxAllocator *const cxStdlibAllocator
A pre-defined allocator using standard library malloc() etc.
Common definitions and feature checks.
#define CX_MALLOC
The attributed function always returns freshly allocated memory.
Definition common.h:156
#define CX_EXPORT
Only used for building Windows DLLs.
Definition common.h:289
#define CX_NONNULL
All pointer arguments must be non-NULL.
Definition common.h:141
#define CX_NODISCARD
Warn about discarded return value.
Definition common.h:256
#define CX_NONNULL_ARG(...)
The specified pointer arguments must be non-NULL.
Definition common.h:146
#define CX_EXTERN
Declares a function with external linkage.
Definition common.h:297
#define CX_DEALLOC_UCX
Shortcut to specify cxFree() as deallocator.
Definition common.h:178
#define CX_ALLOCSIZE(...)
Specifies the parameters from which the allocation size is calculated.
Definition common.h:183
The class definition for an allocator.
Definition allocator.h:41
void(* free)(void *data, void *mem)
The allocator's free() implementation.
Definition allocator.h:60
Structure holding the data for an allocator.
Definition allocator.h:66
void * data
A pointer to the data this allocator uses.
Definition allocator.h:74
cx_allocator_class * cl
A pointer to the instance of the allocator class.
Definition allocator.h:70