1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 #pragma once
13 
14 #if !defined(HAVE_JEMALLOC)
15 
16 #include <stddef.h>
17 
18 #include <isc/util.h>
19 
20 const char *malloc_conf = NULL;
21 
22 #if defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE)
23 
24 #include <stdlib.h>
25 
26 static inline void *
mallocx(size_t size,int flags)27 mallocx(size_t size, int flags) {
28 	UNUSED(flags);
29 
30 	return (malloc(size));
31 }
32 
33 static inline void
sdallocx(void * ptr,size_t size,int flags)34 sdallocx(void *ptr, size_t size, int flags) {
35 	UNUSED(size);
36 	UNUSED(flags);
37 
38 	free(ptr);
39 }
40 
41 static inline void *
rallocx(void * ptr,size_t size,int flags)42 rallocx(void *ptr, size_t size, int flags) {
43 	UNUSED(flags);
44 	REQUIRE(size != 0);
45 
46 	return (realloc(ptr, size));
47 }
48 
49 #ifdef HAVE_MALLOC_SIZE
50 
51 #include <malloc/malloc.h>
52 
53 static inline size_t
sallocx(void * ptr,int flags)54 sallocx(void *ptr, int flags) {
55 	UNUSED(flags);
56 
57 	return (malloc_size(ptr));
58 }
59 
60 #elif HAVE_MALLOC_USABLE_SIZE
61 
62 #include <malloc.h>
63 
64 static inline size_t
sallocx(void * ptr,int flags)65 sallocx(void *ptr, int flags) {
66 	UNUSED(flags);
67 
68 	return (malloc_usable_size(ptr));
69 }
70 
71 #endif /* HAVE_MALLOC_SIZE */
72 
73 #else /* defined(HAVE_MALLOC_SIZE) || defined (HAVE_MALLOC_USABLE_SIZE) */
74 
75 #include <stdlib.h>
76 
77 typedef union {
78 	size_t size;
79 	max_align_t __alignment;
80 } size_info;
81 
82 static inline void *
mallocx(size_t size,int flags)83 mallocx(size_t size, int flags) {
84 	void *ptr = NULL;
85 
86 	UNUSED(flags);
87 
88 	size_info *si = malloc(size + sizeof(*si));
89 	INSIST(si != NULL);
90 
91 	si->size = size;
92 	ptr = &si[1];
93 
94 	return (ptr);
95 }
96 
97 static inline void
sdallocx(void * ptr,size_t size,int flags)98 sdallocx(void *ptr, size_t size, int flags) {
99 	size_info *si = &(((size_info *)ptr)[-1]);
100 
101 	UNUSED(size);
102 	UNUSED(flags);
103 
104 	free(si);
105 }
106 
107 static inline size_t
sallocx(void * ptr,int flags)108 sallocx(void *ptr, int flags) {
109 	size_info *si = &(((size_info *)ptr)[-1]);
110 
111 	UNUSED(flags);
112 
113 	return (si[0].size);
114 }
115 
116 static inline void *
rallocx(void * ptr,size_t size,int flags)117 rallocx(void *ptr, size_t size, int flags) {
118 	size_info *si = &(((size_info *)ptr)[-1]);
119 
120 	UNUSED(flags);
121 
122 	si = realloc(si, size + sizeof(*si));
123 	INSIST(si != NULL);
124 
125 	si->size = size;
126 	ptr = &si[1];
127 
128 	return (ptr);
129 }
130 
131 #endif /* defined(HAVE_MALLOC_SIZE) || defined (HAVE_MALLOC_USABLE_SIZE) */
132 
133 #endif /* !defined(HAVE_JEMALLOC) */
134