505f49f753254d1827af5635ed821b27cb960369
[protos/libecoli.git] / libecoli / 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, __FILE__, __LINE__);
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, __FILE__, __LINE__);
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 char *__ec_strdup(const char *s, const char *file, unsigned int line)
86 {
87         size_t sz = strlen(s) + 1;
88         char *s2;
89
90         s2 = __ec_malloc(sz, file, line);
91         if (s2 == NULL)
92                 return NULL;
93
94         memcpy(s2, s, sz);
95
96         return s2;
97 }
98
99 char *__ec_strndup(const char *s, size_t n, const char *file, unsigned int line)
100 {
101         size_t sz = strnlen(s, n);
102         char *s2;
103
104         s2 = __ec_malloc(sz + 1, file, line);
105         if (s2 == NULL)
106                 return NULL;
107
108         memcpy(s2, s, sz);
109         s2[sz] = '\0';
110
111         return s2;
112 }
113
114 static int ec_malloc_init_func(void)
115 {
116         init_done = 1;
117         return 0;
118 }
119
120 static struct ec_init ec_malloc_init = {
121         .init = ec_malloc_init_func,
122         .priority = 40,
123 };
124
125 EC_INIT_REGISTER(ec_malloc_init);
126
127 /* LCOV_EXCL_START */
128 static int ec_malloc_testcase(void)
129 {
130         int ret, testres = 0;
131         char *ptr, *ptr2;
132
133         ret = ec_malloc_register(NULL, NULL, NULL);
134         testres |= EC_TEST_CHECK(ret == -1,
135                 "should not be able to register NULL malloc handlers");
136         ret = ec_malloc_register(__ec_malloc, __ec_free, __ec_realloc);
137         testres |= EC_TEST_CHECK(ret == -1,
138                 "should not be able to register after init");
139
140         /* registration is tested in the test main.c */
141
142         ptr = ec_malloc(10);
143         if (ptr == NULL)
144                 return -1;
145         memset(ptr, 0, 10);
146         ptr2 = ec_realloc(ptr, 20);
147         EC_TEST_CHECK(ptr2 != NULL, "cannot realloc ptr\n");
148         if (ptr2 == NULL)
149                 ec_free(ptr);
150         else
151                 ec_free(ptr2);
152         ptr = NULL;
153         ptr2 = NULL;
154
155         ptr = ec_malloc_func(10);
156         if (ptr == NULL)
157                 return -1;
158         memset(ptr, 0, 10);
159         ec_free_func(ptr);
160         ptr = NULL;
161
162         ptr = ec_calloc(2, (size_t)-1);
163         EC_TEST_CHECK(ptr == NULL, "bad overflow check in ec_calloc\n");
164
165         return testres;
166 }
167 /* LCOV_EXCL_STOP */
168
169 static struct ec_test ec_malloc_test = {
170         .name = "malloc",
171         .test = ec_malloc_testcase,
172 };
173
174 EC_TEST_REGISTER(ec_malloc_test);