src/allocator.c

changeset 963
2f601274bbac
parent 935
312fb24c14de
equal deleted inserted replaced
962:cd418898af5c 963:2f601274bbac
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 "cx/allocator.h" 29 #include "cx/allocator.h"
30 #include "cx/utils.h"
31
32 #include <errno.h>
30 33
31 __attribute__((__malloc__, __alloc_size__(2))) 34 __attribute__((__malloc__, __alloc_size__(2)))
32 static void *cx_malloc_stdlib( 35 static void *cx_malloc_stdlib(
33 __attribute__((__unused__)) void *d, 36 __attribute__((__unused__)) void *d,
34 size_t n 37 size_t n
87 *mem = nmem; 90 *mem = nmem;
88 return 0; 91 return 0;
89 } 92 }
90 } 93 }
91 94
95 #undef cx_reallocatearray
96 int cx_reallocatearray(
97 void **mem,
98 size_t nmemb,
99 size_t size
100 ) {
101 size_t n;
102 if (cx_szmul(nmemb, size, &n)) {
103 errno = ENOMEM;
104 return 1;
105 } else {
106 void *nmem = realloc(*mem, n);
107 if (nmem == NULL) {
108 return 1;
109 } else {
110 *mem = nmem;
111 return 0;
112 }
113 }
114 }
115
92 // IMPLEMENTATION OF HIGH LEVEL API 116 // IMPLEMENTATION OF HIGH LEVEL API
93 117
94 void *cxMalloc( 118 void *cxMalloc(
95 const CxAllocator *allocator, 119 const CxAllocator *allocator,
96 size_t n 120 size_t n
104 size_t n 128 size_t n
105 ) { 129 ) {
106 return allocator->cl->realloc(allocator->data, mem, n); 130 return allocator->cl->realloc(allocator->data, mem, n);
107 } 131 }
108 132
133 void *cxReallocArray(
134 const CxAllocator *allocator,
135 void *mem,
136 size_t nmemb,
137 size_t size
138 ) {
139 size_t n;
140 if (cx_szmul(nmemb, size, &n)) {
141 errno = ENOMEM;
142 return NULL;
143 } else {
144 return allocator->cl->realloc(allocator->data, mem, n);
145 }
146 }
147
148 #undef cxReallocate
109 int cxReallocate( 149 int cxReallocate(
110 const CxAllocator *allocator, 150 const CxAllocator *allocator,
111 void **mem, 151 void **mem,
112 size_t n 152 size_t n
113 ) { 153 ) {
114 void *nmem = allocator->cl->realloc(allocator->data, *mem, n); 154 void *nmem = allocator->cl->realloc(allocator->data, *mem, n);
155 if (nmem == NULL) {
156 return 1;
157 } else {
158 *mem = nmem;
159 return 0;
160 }
161 }
162
163 #undef cxReallocateArray
164 int cxReallocateArray(
165 const CxAllocator *allocator,
166 void **mem,
167 size_t nmemb,
168 size_t size
169 ) {
170 void *nmem = cxReallocArray(allocator, *mem, nmemb, size);
115 if (nmem == NULL) { 171 if (nmem == NULL) {
116 return 1; 172 return 1;
117 } else { 173 } else {
118 *mem = nmem; 174 *mem = nmem;
119 return 0; 175 return 0;

mercurial