81 }; |
81 }; |
82 |
82 |
83 /** |
83 /** |
84 * High-Level type alias for the allocator type. |
84 * High-Level type alias for the allocator type. |
85 */ |
85 */ |
86 typedef struct cx_allocator_s *CxAllocator; |
86 typedef struct cx_allocator_s CxAllocator; |
87 |
87 |
88 /** |
88 /** |
89 * A default allocator using standard library malloc() etc. |
89 * A default allocator using standard library malloc() etc. |
90 */ |
90 */ |
91 extern CxAllocator cxDefaultAllocator; |
91 extern CxAllocator *cxDefaultAllocator; |
92 |
92 |
93 /** |
93 /** |
94 * Allocate \p n bytes of memory. |
94 * Allocate \p n bytes of memory. |
95 * |
95 * |
96 * @param allocator the allocator |
96 * @param allocator the allocator |
97 * @param n the number of bytes |
97 * @param n the number of bytes |
98 * @return a pointer to the allocated memory |
98 * @return a pointer to the allocated memory |
99 */ |
99 */ |
100 void *cxMalloc(CxAllocator allocator, size_t n) |
100 void *cxMalloc( |
|
101 CxAllocator *allocator, |
|
102 size_t n |
|
103 ) |
101 __attribute__((__malloc__)) |
104 __attribute__((__malloc__)) |
102 __attribute__((__alloc_size__(2))); |
105 __attribute__((__alloc_size__(2))); |
103 |
106 |
104 /** |
107 /** |
105 * Re-allocate the previously allocated block in \p mem, making the new block \p n bytes long. |
108 * Re-allocate the previously allocated block in \p mem, making the new block \p n bytes long. |
111 * @param allocator the allocator |
114 * @param allocator the allocator |
112 * @param mem pointer to the previously allocated block |
115 * @param mem pointer to the previously allocated block |
113 * @param n the new size in bytes |
116 * @param n the new size in bytes |
114 * @return a pointer to the re-allocated memory |
117 * @return a pointer to the re-allocated memory |
115 */ |
118 */ |
116 void *cxRealloc(CxAllocator allocator, void *mem, size_t n) |
119 void *cxRealloc( |
|
120 CxAllocator *allocator, |
|
121 void *mem, |
|
122 size_t n |
|
123 ) |
117 __attribute__((__warn_unused_result__)) |
124 __attribute__((__warn_unused_result__)) |
118 __attribute__((__alloc_size__(3))); |
125 __attribute__((__alloc_size__(3))); |
119 |
126 |
120 /** |
127 /** |
121 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. |
128 * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. |
130 * @param allocator the allocator |
137 * @param allocator the allocator |
131 * @param mem pointer to the pointer to allocated block |
138 * @param mem pointer to the pointer to allocated block |
132 * @param n the new size in bytes |
139 * @param n the new size in bytes |
133 * @return zero on success, non-zero on failure |
140 * @return zero on success, non-zero on failure |
134 */ |
141 */ |
135 int cxReallocate(CxAllocator allocator, void **mem, size_t n) |
142 int cxReallocate( |
|
143 CxAllocator *allocator, |
|
144 void **mem, |
|
145 size_t n |
|
146 ) |
136 __attribute__((__nonnull__)); |
147 __attribute__((__nonnull__)); |
137 |
148 |
138 /** |
149 /** |
139 * Allocate \p nelem elements of \p n bytes each, all initialized to zero. |
150 * Allocate \p nelem elements of \p n bytes each, all initialized to zero. |
140 * |
151 * |
141 * @param allocator the allocator |
152 * @param allocator the allocator |
142 * @param nelem the number of elements |
153 * @param nelem the number of elements |
143 * @param n the size of each element in bytes |
154 * @param n the size of each element in bytes |
144 * @return a pointer to the allocated memory |
155 * @return a pointer to the allocated memory |
145 */ |
156 */ |
146 void *cxCalloc(CxAllocator allocator, size_t nelem, size_t n) |
157 void *cxCalloc( |
|
158 CxAllocator *allocator, |
|
159 size_t nelem, |
|
160 size_t n |
|
161 ) |
147 __attribute__((__malloc__)) |
162 __attribute__((__malloc__)) |
148 __attribute__((__alloc_size__(2, 3))); |
163 __attribute__((__alloc_size__(2, 3))); |
149 |
164 |
150 /** |
165 /** |
151 * Free a block allocated by this allocator. |
166 * Free a block allocated by this allocator. |
153 * \note Freeing a block of a different allocator is undefined. |
168 * \note Freeing a block of a different allocator is undefined. |
154 * |
169 * |
155 * @param allocator the allocator |
170 * @param allocator the allocator |
156 * @param mem a pointer to the block to free |
171 * @param mem a pointer to the block to free |
157 */ |
172 */ |
158 void cxFree(CxAllocator allocator, void *mem) |
173 void cxFree( |
|
174 CxAllocator *allocator, |
|
175 void *mem |
|
176 ) |
159 __attribute__((__nonnull__)); |
177 __attribute__((__nonnull__)); |
160 |
178 |
161 #ifdef __cplusplus |
179 #ifdef __cplusplus |
162 } /* extern "C" */ |
180 } /* extern "C" */ |
163 #endif |
181 #endif |