1 /*
2 * This file is part of RTRlib.
3 *
4 * This file is subject to the terms and conditions of the MIT license.
5 * See the file LICENSE in the top level directory for more details.
6 *
7 * Website: http://rtrlib.realmv6.org/
8 */
9
10 #include <assert.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "alloc_utils_private.h"
15 #include "rtrlib/rtrlib_export_private.h"
16
17 static void *(*MALLOC_PTR)(size_t size) = malloc;
18 static void *(*REALLOC_PTR)(void *ptr, size_t size) = realloc;
19 static void (*FREE_PTR)(void *ptr) = free;
20
21 /* cppcheck-suppress unusedFunction */
lrtr_set_alloc_functions(void * (* malloc_function)(size_t size),void * (* realloc_function)(void * ptr,size_t size),void (free_function)(void * ptr))22 RTRLIB_EXPORT void lrtr_set_alloc_functions(
23 void *(*malloc_function)(size_t size),
24 void *(*realloc_function)(void *ptr, size_t size),
25 void (free_function)(void *ptr))
26 {
27 MALLOC_PTR = malloc_function;
28 REALLOC_PTR = realloc_function;
29 FREE_PTR = free_function;
30 }
31
lrtr_malloc(size_t size)32 inline void *lrtr_malloc(size_t size)
33 {
34 return MALLOC_PTR(size);
35 }
36
lrtr_free(void * ptr)37 inline void lrtr_free(void *ptr)
38 {
39 return FREE_PTR(ptr);
40 }
41
lrtr_realloc(void * ptr,size_t size)42 inline void *lrtr_realloc(void *ptr, size_t size)
43 {
44 return REALLOC_PTR(ptr, size);
45 }
46
lrtr_strdup(const char * string)47 char *lrtr_strdup(const char *string)
48 {
49 assert(string);
50
51 size_t length = strlen(string) + 1;
52 char *new_string = lrtr_malloc(length);
53
54 return new_string ? memcpy(new_string, string, length) : NULL;
55 }
56