1 /***
2 * Copyright 2020 HAProxy Technologies
3 *
4 * This file is part of the HAProxy OpenTracing filter.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 #include "include.h"
21
22
23 /***
24 * NAME
25 * flt_ot_pool_alloc -
26 *
27 * ARGUMENTS
28 * pool -
29 * size -
30 * flag_clear -
31 * err -
32 *
33 * DESCRIPTION
34 * -
35 *
36 * RETURN VALUE
37 * -
38 */
flt_ot_pool_alloc(struct pool_head * pool,size_t size,bool flag_clear,char ** err)39 void *flt_ot_pool_alloc(struct pool_head *pool, size_t size, bool flag_clear, char **err)
40 {
41 void *retptr;
42
43 FLT_OT_FUNC("%p, %zu, %hhu, %p:%p", pool, size, flag_clear, FLT_OT_DPTR_ARGS(err));
44
45 if (pool != NULL) {
46 retptr = pool_alloc(pool);
47 if (retptr != NULL)
48 FLT_OT_DBG(2, "POOL_ALLOC: %s:%d(%p %zu)", __func__, __LINE__, retptr, FLT_OT_DEREF(pool, size, size));
49 } else {
50 retptr = FLT_OT_MALLOC(size);
51 }
52
53 if (retptr == NULL)
54 FLT_OT_ERR("out of memory");
55 else if (flag_clear)
56 (void)memset(retptr, 0, size);
57
58 FLT_OT_RETURN(retptr);
59 }
60
61
62 /***
63 * NAME
64 * flt_ot_pool_strndup -
65 *
66 * ARGUMENTS
67 * pool -
68 * s -
69 * size -
70 * err -
71 *
72 * DESCRIPTION
73 * -
74 *
75 * RETURN VALUE
76 * -
77 */
flt_ot_pool_strndup(struct pool_head * pool,const char * s,size_t size,char ** err)78 void *flt_ot_pool_strndup(struct pool_head *pool, const char *s, size_t size, char **err)
79 {
80 void *retptr;
81
82 FLT_OT_FUNC("%p, \"%.*s\", %zu, %p:%p", pool, (int)size, s, size, FLT_OT_DPTR_ARGS(err));
83
84 if (pool != NULL) {
85 retptr = pool_alloc(pool);
86 if (retptr != NULL) {
87 (void)memcpy(retptr, s, MIN(pool->size - 1, size));
88
89 ((uint8_t *)retptr)[MIN(pool->size - 1, size)] = '\0';
90 }
91 } else {
92 retptr = FLT_OT_STRNDUP(s, size);
93 }
94
95 if (retptr != NULL)
96 FLT_OT_DBG(2, "POOL_STRNDUP: %s:%d(%p %zu)", __func__, __LINE__, retptr, FLT_OT_DEREF(pool, size, size));
97 else
98 FLT_OT_ERR("out of memory");
99
100 FLT_OT_RETURN(retptr);
101 }
102
103
104 /***
105 * NAME
106 * flt_ot_pool_free -
107 *
108 * ARGUMENTS
109 * pool -
110 * ptr -
111 *
112 * DESCRIPTION
113 * -
114 *
115 * RETURN VALUE
116 * This function does not return a value.
117 */
flt_ot_pool_free(struct pool_head * pool,void ** ptr)118 void flt_ot_pool_free(struct pool_head *pool, void **ptr)
119 {
120 FLT_OT_FUNC("%p, %p:%p", pool, FLT_OT_DPTR_ARGS(ptr));
121
122 if ((ptr == NULL) || (*ptr == NULL))
123 FLT_OT_RETURN();
124
125 FLT_OT_DBG(2, "POOL_FREE: %s:%d(%p %u)", __func__, __LINE__, *ptr, FLT_OT_DEREF(pool, size, 0));
126
127 if (pool != NULL)
128 pool_free(pool, *ptr);
129 else
130 FLT_OT_FREE(*ptr);
131
132 *ptr = NULL;
133
134 FLT_OT_RETURN();
135 }
136
137
138 /***
139 * NAME
140 * flt_ot_trash_alloc -
141 *
142 * ARGUMENTS
143 * flag_clear -
144 * err -
145 *
146 * DESCRIPTION
147 * -
148 *
149 * RETURN VALUE
150 * This function does not return a value.
151 */
flt_ot_trash_alloc(bool flag_clear,char ** err)152 struct buffer *flt_ot_trash_alloc(bool flag_clear, char **err)
153 {
154 struct buffer *retptr;
155
156 FLT_OT_FUNC("%hhu, %p:%p", flag_clear, FLT_OT_DPTR_ARGS(err));
157
158 #ifdef USE_TRASH_CHUNK
159 retptr = alloc_trash_chunk();
160 if (retptr != NULL)
161 FLT_OT_DBG(2, "TRASH_ALLOC: %s:%d(%p %zu)", __func__, __LINE__, retptr, retptr->size);
162 #else
163 retptr = FLT_OT_MALLOC(sizeof(*retptr));
164 if (retptr != NULL) {
165 chunk_init(retptr, FLT_OT_MALLOC(global.tune.bufsize), global.tune.bufsize);
166 if (retptr->area == NULL)
167 FLT_OT_FREE_CLEAR(retptr);
168 else
169 *(retptr->area) = '\0';
170 }
171 #endif
172
173 if (retptr == NULL)
174 FLT_OT_ERR("out of memory");
175 else if (flag_clear)
176 (void)memset(retptr->area, 0, retptr->size);
177
178 FLT_OT_RETURN(retptr);
179 }
180
181
182 /***
183 * NAME
184 * flt_ot_trash_free -
185 *
186 * ARGUMENTS
187 * ptr -
188 *
189 * DESCRIPTION
190 * -
191 *
192 * RETURN VALUE
193 * This function does not return a value.
194 */
flt_ot_trash_free(struct buffer ** ptr)195 void flt_ot_trash_free(struct buffer **ptr)
196 {
197 FLT_OT_FUNC("%p:%p", FLT_OT_DPTR_ARGS(ptr));
198
199 if ((ptr == NULL) || (*ptr == NULL))
200 FLT_OT_RETURN();
201
202 FLT_OT_DBG(2, "TRASH_FREE: %s:%d(%p %zu)", __func__, __LINE__, *ptr, (*ptr)->size);
203
204 #ifdef USE_TRASH_CHUNK
205 free_trash_chunk(*ptr);
206 #else
207 FLT_OT_FREE((*ptr)->area);
208 FLT_OT_FREE(*ptr);
209 #endif
210
211 *ptr = NULL;
212
213 FLT_OT_RETURN();
214 }
215
216 /*
217 * Local variables:
218 * c-indent-level: 8
219 * c-basic-offset: 8
220 * End:
221 *
222 * vi: noexpandtab shiftwidth=8 tabstop=8
223 */
224