add meson support
[protos/libecoli.git] / src / ecoli_malloc.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <ecoli_init.h>
6 #include <ecoli_test.h>
7 #include <ecoli_malloc.h>
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12
13 EC_LOG_TYPE_REGISTER(malloc);
14
15 static int init_done = 0;
16
17 struct ec_malloc_handler ec_malloc_handler;
18
19 int ec_malloc_register(ec_malloc_t usr_malloc, ec_free_t usr_free,
20         ec_realloc_t usr_realloc)
21 {
22         if (usr_malloc == NULL || usr_free == NULL || usr_realloc == NULL) {
23                 errno = EINVAL;
24                 return -1;
25         }
26
27         if (init_done) {
28                 errno = EBUSY;
29                 return -1;
30         }
31
32         ec_malloc_handler.malloc = usr_malloc;
33         ec_malloc_handler.free = usr_free;
34         ec_malloc_handler.realloc = usr_realloc;
35
36         return 0;
37 }
38
39 void *__ec_malloc(size_t size, const char *file, unsigned int line)
40 {
41         return ec_malloc_handler.malloc(size, file, line);
42 }
43
44 void *ec_malloc_func(size_t size)
45 {
46         return ec_malloc(size);
47 }
48
49 void __ec_free(void *ptr, const char *file, unsigned int line)
50 {
51         ec_malloc_handler.free(ptr, file, line);
52 }
53
54 void ec_free_func(void *ptr)
55 {
56         ec_free(ptr);
57 }
58
59 void *__ec_calloc(size_t nmemb, size_t size, const char *file,
60         unsigned int line)
61 {
62         void *ptr;
63         size_t total;
64
65         /* check overflow */
66         total = size * nmemb;
67         if (nmemb != 0 && size != (total / nmemb)) {
68                 errno = ENOMEM;
69                 return NULL;
70         }
71
72         ptr = __ec_malloc(total, file, line);
73         if (ptr == NULL)
74                 return NULL;
75
76         memset(ptr, 0, total);
77         return ptr;
78 }
79
80 void *__ec_realloc(void *ptr, size_t size, const char *file, unsigned int line)
81 {
82         return ec_malloc_handler.realloc(ptr, size, file, line);
83 }
84
85 void ec_realloc_func(void *ptr, size_t size)
86 {
87         ec_realloc(ptr, size);
88 }
89
90 char *__ec_strdup(const char *s, const char *file, unsigned int line)
91 {
92         size_t sz = strlen(s) + 1;
93         char *s2;
94
95         s2 = __ec_malloc(sz, file, line);
96         if (s2 == NULL)
97                 return NULL;
98
99         memcpy(s2, s, sz);
100
101         return s2;
102 }
103
104 char *__ec_strndup(const char *s, size_t n, const char *file, unsigned int line)
105 {
106         size_t sz = strnlen(s, n);
107         char *s2;
108
109         s2 = __ec_malloc(sz + 1, file, line);
110         if (s2 == NULL)
111                 return NULL;
112
113         memcpy(s2, s, sz);
114         s2[sz] = '\0';
115
116         return s2;
117 }
118
119 static int ec_malloc_init_func(void)
120 {
121         init_done = 1;
122         return 0;
123 }
124
125 static struct ec_init ec_malloc_init = {
126         .init = ec_malloc_init_func,
127         .priority = 40,
128 };
129
130 EC_INIT_REGISTER(ec_malloc_init);
131
132 /* LCOV_EXCL_START */
133 static int ec_malloc_testcase(void)
134 {
135         int ret, testres = 0;
136         char *ptr, *ptr2;
137
138         ret = ec_malloc_register(NULL, NULL, NULL);
139         testres |= EC_TEST_CHECK(ret == -1,
140                 "should not be able to register NULL malloc handlers");
141         ret = ec_malloc_register(__ec_malloc, __ec_free, __ec_realloc);
142         testres |= EC_TEST_CHECK(ret == -1,
143                 "should not be able to register after init");
144
145         /* registration is tested in the test main.c */
146
147         ptr = ec_malloc(10);
148         if (ptr == NULL)
149                 return -1;
150         memset(ptr, 0, 10);
151         ptr2 = ec_realloc(ptr, 20);
152         EC_TEST_CHECK(ptr2 != NULL, "cannot realloc ptr\n");
153         if (ptr2 == NULL)
154                 ec_free(ptr);
155         else
156                 ec_free(ptr2);
157         ptr = NULL;
158         ptr2 = NULL;
159
160         ptr = ec_malloc_func(10);
161         if (ptr == NULL)
162                 return -1;
163         memset(ptr, 0, 10);
164         ec_free_func(ptr);
165         ptr = NULL;
166
167         ptr = ec_calloc(2, (size_t)-1);
168         EC_TEST_CHECK(ptr == NULL, "bad overflow check in ec_calloc\n");
169
170         return testres;
171 }
172 /* LCOV_EXCL_STOP */
173
174 static struct ec_test ec_malloc_test = {
175         .name = "malloc",
176         .test = ec_malloc_testcase,
177 };
178
179 EC_TEST_REGISTER(ec_malloc_test);