src/ucx/allocator.c

changeset 415
d938228c382e
parent 254
4784c14aa639
child 438
22eca559aded
equal deleted inserted replaced
414:99a34860c105 415:d938228c382e
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 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 "ucx/allocator.h" 29 #include "cx/allocator.h"
30 30
31 #include <stdlib.h> 31 #include <stdlib.h>
32 32
33 static UcxAllocator default_allocator = { 33 __attribute__((__malloc__, __alloc_size__(2)))
34 NULL, 34 static void *cx_malloc_stdlib(
35 ucx_default_malloc, 35 __attribute__((__unused__)) void *d,
36 ucx_default_calloc, 36 size_t n
37 ucx_default_realloc, 37 ) {
38 ucx_default_free
39 };
40
41 UcxAllocator *ucx_default_allocator() {
42 UcxAllocator *allocator = &default_allocator;
43 return allocator;
44 }
45
46 void *ucx_default_malloc(void *ignore, size_t n) {
47 return malloc(n); 38 return malloc(n);
48 } 39 }
49 40
50 void *ucx_default_calloc(void *ignore, size_t n, size_t size) { 41 __attribute__((__warn_unused_result__, __alloc_size__(3)))
51 return calloc(n, size); 42 static void *cx_realloc_stdlib(
43 __attribute__((__unused__)) void *d,
44 void *mem,
45 size_t n
46 ) {
47 return realloc(mem, n);
52 } 48 }
53 49
54 void *ucx_default_realloc(void *ignore, void *data, size_t n) { 50 __attribute__((__malloc__, __alloc_size__(2, 3)))
55 return realloc(data, n); 51 static void *cx_calloc_stdlib(
52 __attribute__((__unused__)) void *d,
53 size_t nelem,
54 size_t n
55 ) {
56 return calloc(nelem, n);
56 } 57 }
57 58
58 void ucx_default_free(void *ignore, void *data) { 59 __attribute__((__nonnull__))
59 free(data); 60 static void cx_free_stdlib(
61 __attribute__((__unused__)) void *d,
62 void *mem
63 ) {
64 free(mem);
60 } 65 }
66
67 static cx_allocator_class cx_default_allocator_class = {
68 cx_malloc_stdlib,
69 cx_realloc_stdlib,
70 cx_calloc_stdlib,
71 cx_free_stdlib
72 };
73
74 struct cx_allocator_s cx_default_allocator = {
75 &cx_default_allocator_class,
76 NULL
77 };
78 CxAllocator *cxDefaultAllocator = &cx_default_allocator;
79
80 /* IMPLEMENTATION OF HIGH LEVEL API */
81
82 void *cxMalloc(
83 CxAllocator const *allocator,
84 size_t n
85 ) {
86 return allocator->cl->malloc(allocator->data, n);
87 }
88
89 void *cxRealloc(
90 CxAllocator const *allocator,
91 void *mem,
92 size_t n
93 ) {
94 return allocator->cl->realloc(allocator->data, mem, n);
95 }
96
97 int cxReallocate(
98 CxAllocator const *allocator,
99 void **mem,
100 size_t n
101 ) {
102 void *nmem = allocator->cl->realloc(allocator->data, *mem, n);
103 if (nmem == NULL) {
104 return 1;
105 } else {
106 *mem = nmem;
107 return 0;
108 }
109 }
110
111 void *cxCalloc(
112 CxAllocator const *allocator,
113 size_t nelem,
114 size_t n
115 ) {
116 return allocator->cl->calloc(allocator->data, nelem, n);
117 }
118
119 void cxFree(
120 CxAllocator const *allocator,
121 void *mem
122 ) {
123 allocator->cl->free(allocator->data, mem);
124 }

mercurial