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