alloc.c revision 1.1.1.1 1 1.1 christos /* $NetBSD: alloc.c,v 1.1.1.1 2018/04/07 22:34:25 christos Exp $ */
2 1.1 christos
3 1.1 christos /* alloc.c
4 1.1 christos
5 1.1 christos Memory allocation... */
6 1.1 christos
7 1.1 christos /*
8 1.1 christos * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
9 1.1 christos * Copyright (c) 1996-2003 by Internet Software Consortium
10 1.1 christos *
11 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public
12 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
13 1.1 christos * file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 1.1 christos *
15 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
18 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 1.1 christos *
23 1.1 christos * Internet Systems Consortium, Inc.
24 1.1 christos * 950 Charter Street
25 1.1 christos * Redwood City, CA 94063
26 1.1 christos * <info (at) isc.org>
27 1.1 christos * https://www.isc.org/
28 1.1 christos *
29 1.1 christos */
30 1.1 christos
31 1.1 christos #include <sys/cdefs.h>
32 1.1 christos __RCSID("$NetBSD: alloc.c,v 1.1.1.1 2018/04/07 22:34:25 christos Exp $");
33 1.1 christos
34 1.1 christos #include "dhcpd.h"
35 1.1 christos #include <omapip/omapip_p.h>
36 1.1 christos
37 1.1 christos struct dhcp_packet *dhcp_free_list;
38 1.1 christos struct packet *packet_free_list;
39 1.1 christos
40 1.1 christos int option_chain_head_allocate (ptr, file, line)
41 1.1 christos struct option_chain_head **ptr;
42 1.1 christos const char *file;
43 1.1 christos int line;
44 1.1 christos {
45 1.1 christos struct option_chain_head *h;
46 1.1 christos
47 1.1 christos if (!ptr) {
48 1.1 christos log_error ("%s(%d): null pointer", file, line);
49 1.1 christos #if defined (POINTER_DEBUG)
50 1.1 christos abort ();
51 1.1 christos #else
52 1.1 christos return 0;
53 1.1 christos #endif
54 1.1 christos }
55 1.1 christos if (*ptr) {
56 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
57 1.1 christos #if defined (POINTER_DEBUG)
58 1.1 christos abort ();
59 1.1 christos #else
60 1.1 christos *ptr = (struct option_chain_head *)0;
61 1.1 christos #endif
62 1.1 christos }
63 1.1 christos
64 1.1 christos h = dmalloc (sizeof *h, file, line);
65 1.1 christos if (h) {
66 1.1 christos memset (h, 0, sizeof *h);
67 1.1 christos return option_chain_head_reference (ptr, h, file, line);
68 1.1 christos }
69 1.1 christos return 0;
70 1.1 christos }
71 1.1 christos
72 1.1 christos int option_chain_head_reference (ptr, bp, file, line)
73 1.1 christos struct option_chain_head **ptr;
74 1.1 christos struct option_chain_head *bp;
75 1.1 christos const char *file;
76 1.1 christos int line;
77 1.1 christos {
78 1.1 christos if (!ptr) {
79 1.1 christos log_error ("%s(%d): null pointer", file, line);
80 1.1 christos #if defined (POINTER_DEBUG)
81 1.1 christos abort ();
82 1.1 christos #else
83 1.1 christos return 0;
84 1.1 christos #endif
85 1.1 christos }
86 1.1 christos if (*ptr) {
87 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
88 1.1 christos #if defined (POINTER_DEBUG)
89 1.1 christos abort ();
90 1.1 christos #else
91 1.1 christos *ptr = (struct option_chain_head *)0;
92 1.1 christos #endif
93 1.1 christos }
94 1.1 christos *ptr = bp;
95 1.1 christos bp -> refcnt++;
96 1.1 christos rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
97 1.1 christos return 1;
98 1.1 christos }
99 1.1 christos
100 1.1 christos int option_chain_head_dereference (ptr, file, line)
101 1.1 christos struct option_chain_head **ptr;
102 1.1 christos const char *file;
103 1.1 christos int line;
104 1.1 christos {
105 1.1 christos struct option_chain_head *option_chain_head;
106 1.1 christos pair car, cdr;
107 1.1 christos
108 1.1 christos if (!ptr || !*ptr) {
109 1.1 christos log_error ("%s(%d): null pointer", file, line);
110 1.1 christos #if defined (POINTER_DEBUG)
111 1.1 christos abort ();
112 1.1 christos #else
113 1.1 christos return 0;
114 1.1 christos #endif
115 1.1 christos }
116 1.1 christos
117 1.1 christos option_chain_head = *ptr;
118 1.1 christos *ptr = (struct option_chain_head *)0;
119 1.1 christos --option_chain_head -> refcnt;
120 1.1 christos rc_register (file, line, ptr, option_chain_head,
121 1.1 christos option_chain_head -> refcnt, 1, RC_MISC);
122 1.1 christos if (option_chain_head -> refcnt > 0)
123 1.1 christos return 1;
124 1.1 christos
125 1.1 christos if (option_chain_head -> refcnt < 0) {
126 1.1 christos log_error ("%s(%d): negative refcnt!", file, line);
127 1.1 christos #if defined (DEBUG_RC_HISTORY)
128 1.1 christos dump_rc_history (option_chain_head);
129 1.1 christos #endif
130 1.1 christos #if defined (POINTER_DEBUG)
131 1.1 christos abort ();
132 1.1 christos #else
133 1.1 christos return 0;
134 1.1 christos #endif
135 1.1 christos }
136 1.1 christos
137 1.1 christos /* If there are any options on this head, free them. */
138 1.1 christos for (car = option_chain_head -> first; car; car = cdr) {
139 1.1 christos cdr = car -> cdr;
140 1.1 christos if (car -> car)
141 1.1 christos option_cache_dereference ((struct option_cache **)
142 1.1 christos (&car -> car), MDL);
143 1.1 christos dfree (car, MDL);
144 1.1 christos }
145 1.1 christos
146 1.1 christos dfree (option_chain_head, file, line);
147 1.1 christos return 1;
148 1.1 christos }
149 1.1 christos
150 1.1 christos int group_allocate (ptr, file, line)
151 1.1 christos struct group **ptr;
152 1.1 christos const char *file;
153 1.1 christos int line;
154 1.1 christos {
155 1.1 christos struct group *g;
156 1.1 christos
157 1.1 christos if (!ptr) {
158 1.1 christos log_error ("%s(%d): null pointer", file, line);
159 1.1 christos #if defined (POINTER_DEBUG)
160 1.1 christos abort ();
161 1.1 christos #else
162 1.1 christos return 0;
163 1.1 christos #endif
164 1.1 christos }
165 1.1 christos if (*ptr) {
166 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
167 1.1 christos #if defined (POINTER_DEBUG)
168 1.1 christos abort ();
169 1.1 christos #else
170 1.1 christos *ptr = (struct group *)0;
171 1.1 christos #endif
172 1.1 christos }
173 1.1 christos
174 1.1 christos g = dmalloc (sizeof *g, file, line);
175 1.1 christos if (g) {
176 1.1 christos memset (g, 0, sizeof *g);
177 1.1 christos return group_reference (ptr, g, file, line);
178 1.1 christos }
179 1.1 christos return 0;
180 1.1 christos }
181 1.1 christos
182 1.1 christos int group_reference (ptr, bp, file, line)
183 1.1 christos struct group **ptr;
184 1.1 christos struct group *bp;
185 1.1 christos const char *file;
186 1.1 christos int line;
187 1.1 christos {
188 1.1 christos if (!ptr) {
189 1.1 christos log_error ("%s(%d): null pointer", file, line);
190 1.1 christos #if defined (POINTER_DEBUG)
191 1.1 christos abort ();
192 1.1 christos #else
193 1.1 christos return 0;
194 1.1 christos #endif
195 1.1 christos }
196 1.1 christos if (*ptr) {
197 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
198 1.1 christos #if defined (POINTER_DEBUG)
199 1.1 christos abort ();
200 1.1 christos #else
201 1.1 christos *ptr = (struct group *)0;
202 1.1 christos #endif
203 1.1 christos }
204 1.1 christos *ptr = bp;
205 1.1 christos bp -> refcnt++;
206 1.1 christos rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
207 1.1 christos return 1;
208 1.1 christos }
209 1.1 christos
210 1.1 christos int group_dereference (ptr, file, line)
211 1.1 christos struct group **ptr;
212 1.1 christos const char *file;
213 1.1 christos int line;
214 1.1 christos {
215 1.1 christos struct group *group;
216 1.1 christos
217 1.1 christos if (!ptr || !*ptr) {
218 1.1 christos log_error ("%s(%d): null pointer", file, line);
219 1.1 christos #if defined (POINTER_DEBUG)
220 1.1 christos abort ();
221 1.1 christos #else
222 1.1 christos return 0;
223 1.1 christos #endif
224 1.1 christos }
225 1.1 christos
226 1.1 christos group = *ptr;
227 1.1 christos *ptr = (struct group *)0;
228 1.1 christos --group -> refcnt;
229 1.1 christos rc_register (file, line, ptr, group, group -> refcnt, 1, RC_MISC);
230 1.1 christos if (group -> refcnt > 0)
231 1.1 christos return 1;
232 1.1 christos
233 1.1 christos if (group -> refcnt < 0) {
234 1.1 christos log_error ("%s(%d): negative refcnt!", file, line);
235 1.1 christos #if defined (DEBUG_RC_HISTORY)
236 1.1 christos dump_rc_history (group);
237 1.1 christos #endif
238 1.1 christos #if defined (POINTER_DEBUG)
239 1.1 christos abort ();
240 1.1 christos #else
241 1.1 christos return 0;
242 1.1 christos #endif
243 1.1 christos }
244 1.1 christos
245 1.1 christos if (group -> object)
246 1.1 christos group_object_dereference (&group -> object, file, line);
247 1.1 christos if (group -> subnet)
248 1.1 christos subnet_dereference (&group -> subnet, file, line);
249 1.1 christos if (group -> shared_network)
250 1.1 christos shared_network_dereference (&group -> shared_network,
251 1.1 christos file, line);
252 1.1 christos if (group -> statements)
253 1.1 christos executable_statement_dereference (&group -> statements,
254 1.1 christos file, line);
255 1.1 christos if (group -> next)
256 1.1 christos group_dereference (&group -> next, file, line);
257 1.1 christos dfree (group, file, line);
258 1.1 christos return 1;
259 1.1 christos }
260 1.1 christos
261 1.1 christos struct dhcp_packet *new_dhcp_packet (file, line)
262 1.1 christos const char *file;
263 1.1 christos int line;
264 1.1 christos {
265 1.1 christos struct dhcp_packet *rval;
266 1.1 christos rval = (struct dhcp_packet *)dmalloc (sizeof (struct dhcp_packet),
267 1.1 christos file, line);
268 1.1 christos return rval;
269 1.1 christos }
270 1.1 christos
271 1.1 christos struct protocol *new_protocol (file, line)
272 1.1 christos const char *file;
273 1.1 christos int line;
274 1.1 christos {
275 1.1 christos struct protocol *rval = dmalloc (sizeof (struct protocol), file, line);
276 1.1 christos return rval;
277 1.1 christos }
278 1.1 christos
279 1.1 christos struct domain_search_list *new_domain_search_list (file, line)
280 1.1 christos const char *file;
281 1.1 christos int line;
282 1.1 christos {
283 1.1 christos struct domain_search_list *rval =
284 1.1 christos dmalloc (sizeof (struct domain_search_list), file, line);
285 1.1 christos return rval;
286 1.1 christos }
287 1.1 christos
288 1.1 christos struct name_server *new_name_server (file, line)
289 1.1 christos const char *file;
290 1.1 christos int line;
291 1.1 christos {
292 1.1 christos struct name_server *rval =
293 1.1 christos dmalloc (sizeof (struct name_server), file, line);
294 1.1 christos return rval;
295 1.1 christos }
296 1.1 christos
297 1.1 christos void free_name_server (ptr, file, line)
298 1.1 christos struct name_server *ptr;
299 1.1 christos const char *file;
300 1.1 christos int line;
301 1.1 christos {
302 1.1 christos dfree ((void *)ptr, file, line);
303 1.1 christos }
304 1.1 christos
305 1.1 christos struct option *new_option (name, file, line)
306 1.1 christos const char *name;
307 1.1 christos const char *file;
308 1.1 christos int line;
309 1.1 christos {
310 1.1 christos struct option *rval;
311 1.1 christos int len;
312 1.1 christos
313 1.1 christos len = strlen(name);
314 1.1 christos
315 1.1 christos rval = dmalloc(sizeof(struct option) + len + 1, file, line);
316 1.1 christos
317 1.1 christos if(rval) {
318 1.1 christos memcpy(rval + 1, name, len);
319 1.1 christos rval->name = (char *)(rval + 1);
320 1.1 christos }
321 1.1 christos
322 1.1 christos return rval;
323 1.1 christos }
324 1.1 christos
325 1.1 christos struct universe *new_universe (file, line)
326 1.1 christos const char *file;
327 1.1 christos int line;
328 1.1 christos {
329 1.1 christos struct universe *rval =
330 1.1 christos dmalloc (sizeof (struct universe), file, line);
331 1.1 christos return rval;
332 1.1 christos }
333 1.1 christos
334 1.1 christos void free_universe (ptr, file, line)
335 1.1 christos struct universe *ptr;
336 1.1 christos const char *file;
337 1.1 christos int line;
338 1.1 christos {
339 1.1 christos dfree ((void *)ptr, file, line);
340 1.1 christos }
341 1.1 christos
342 1.1 christos void free_domain_search_list (ptr, file, line)
343 1.1 christos struct domain_search_list *ptr;
344 1.1 christos const char *file;
345 1.1 christos int line;
346 1.1 christos {
347 1.1 christos dfree ((void *)ptr, file, line);
348 1.1 christos }
349 1.1 christos
350 1.1 christos void free_protocol (ptr, file, line)
351 1.1 christos struct protocol *ptr;
352 1.1 christos const char *file;
353 1.1 christos int line;
354 1.1 christos {
355 1.1 christos dfree ((void *)ptr, file, line);
356 1.1 christos }
357 1.1 christos
358 1.1 christos void free_dhcp_packet (ptr, file, line)
359 1.1 christos struct dhcp_packet *ptr;
360 1.1 christos const char *file;
361 1.1 christos int line;
362 1.1 christos {
363 1.1 christos dfree ((void *)ptr, file, line);
364 1.1 christos }
365 1.1 christos
366 1.1 christos struct client_lease *new_client_lease (file, line)
367 1.1 christos const char *file;
368 1.1 christos int line;
369 1.1 christos {
370 1.1 christos return (struct client_lease *)dmalloc (sizeof (struct client_lease),
371 1.1 christos file, line);
372 1.1 christos }
373 1.1 christos
374 1.1 christos void free_client_lease (lease, file, line)
375 1.1 christos struct client_lease *lease;
376 1.1 christos const char *file;
377 1.1 christos int line;
378 1.1 christos {
379 1.1 christos dfree (lease, file, line);
380 1.1 christos }
381 1.1 christos
382 1.1 christos pair free_pairs;
383 1.1 christos
384 1.1 christos pair new_pair (file, line)
385 1.1 christos const char *file;
386 1.1 christos int line;
387 1.1 christos {
388 1.1 christos pair foo;
389 1.1 christos
390 1.1 christos if (free_pairs) {
391 1.1 christos foo = free_pairs;
392 1.1 christos free_pairs = foo -> cdr;
393 1.1 christos memset (foo, 0, sizeof *foo);
394 1.1 christos dmalloc_reuse (foo, file, line, 0);
395 1.1 christos return foo;
396 1.1 christos }
397 1.1 christos
398 1.1 christos foo = dmalloc (sizeof *foo, file, line);
399 1.1 christos if (!foo)
400 1.1 christos return foo;
401 1.1 christos memset (foo, 0, sizeof *foo);
402 1.1 christos return foo;
403 1.1 christos }
404 1.1 christos
405 1.1 christos void free_pair (foo, file, line)
406 1.1 christos pair foo;
407 1.1 christos const char *file;
408 1.1 christos int line;
409 1.1 christos {
410 1.1 christos foo -> cdr = free_pairs;
411 1.1 christos free_pairs = foo;
412 1.1 christos dmalloc_reuse (free_pairs, __FILE__, __LINE__, 0);
413 1.1 christos }
414 1.1 christos
415 1.1 christos #if defined (DEBUG_MEMORY_LEAKAGE) || \
416 1.1 christos defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
417 1.1 christos void relinquish_free_pairs ()
418 1.1 christos {
419 1.1 christos pair pf, pc;
420 1.1 christos
421 1.1 christos for (pf = free_pairs; pf; pf = pc) {
422 1.1 christos pc = pf -> cdr;
423 1.1 christos dfree (pf, MDL);
424 1.1 christos }
425 1.1 christos free_pairs = (pair)0;
426 1.1 christos }
427 1.1 christos #endif
428 1.1 christos
429 1.1 christos struct expression *free_expressions;
430 1.1 christos
431 1.1 christos int expression_allocate (cptr, file, line)
432 1.1 christos struct expression **cptr;
433 1.1 christos const char *file;
434 1.1 christos int line;
435 1.1 christos {
436 1.1 christos struct expression *rval;
437 1.1 christos
438 1.1 christos if (free_expressions) {
439 1.1 christos rval = free_expressions;
440 1.1 christos free_expressions = rval -> data.not;
441 1.1 christos dmalloc_reuse (rval, file, line, 1);
442 1.1 christos } else {
443 1.1 christos rval = dmalloc (sizeof (struct expression), file, line);
444 1.1 christos if (!rval)
445 1.1 christos return 0;
446 1.1 christos }
447 1.1 christos memset (rval, 0, sizeof *rval);
448 1.1 christos return expression_reference (cptr, rval, file, line);
449 1.1 christos }
450 1.1 christos
451 1.1 christos int expression_reference (ptr, src, file, line)
452 1.1 christos struct expression **ptr;
453 1.1 christos struct expression *src;
454 1.1 christos const char *file;
455 1.1 christos int line;
456 1.1 christos {
457 1.1 christos if (!ptr) {
458 1.1 christos log_error ("%s(%d): null pointer", file, line);
459 1.1 christos #if defined (POINTER_DEBUG)
460 1.1 christos abort ();
461 1.1 christos #else
462 1.1 christos return 0;
463 1.1 christos #endif
464 1.1 christos }
465 1.1 christos if (*ptr) {
466 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
467 1.1 christos #if defined (POINTER_DEBUG)
468 1.1 christos abort ();
469 1.1 christos #else
470 1.1 christos *ptr = (struct expression *)0;
471 1.1 christos #endif
472 1.1 christos }
473 1.1 christos *ptr = src;
474 1.1 christos src -> refcnt++;
475 1.1 christos rc_register (file, line, ptr, src, src -> refcnt, 0, RC_MISC);
476 1.1 christos return 1;
477 1.1 christos }
478 1.1 christos
479 1.1 christos void free_expression (expr, file, line)
480 1.1 christos struct expression *expr;
481 1.1 christos const char *file;
482 1.1 christos int line;
483 1.1 christos {
484 1.1 christos expr -> data.not = free_expressions;
485 1.1 christos free_expressions = expr;
486 1.1 christos dmalloc_reuse (free_expressions, __FILE__, __LINE__, 0);
487 1.1 christos }
488 1.1 christos
489 1.1 christos #if defined (DEBUG_MEMORY_LEAKAGE) || \
490 1.1 christos defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
491 1.1 christos void relinquish_free_expressions ()
492 1.1 christos {
493 1.1 christos struct expression *e, *n;
494 1.1 christos
495 1.1 christos for (e = free_expressions; e; e = n) {
496 1.1 christos n = e -> data.not;
497 1.1 christos dfree (e, MDL);
498 1.1 christos }
499 1.1 christos free_expressions = (struct expression *)0;
500 1.1 christos }
501 1.1 christos #endif
502 1.1 christos
503 1.1 christos struct binding_value *free_binding_values;
504 1.1 christos
505 1.1 christos int binding_value_allocate (cptr, file, line)
506 1.1 christos struct binding_value **cptr;
507 1.1 christos const char *file;
508 1.1 christos int line;
509 1.1 christos {
510 1.1 christos struct binding_value *rval;
511 1.1 christos
512 1.1 christos if (free_binding_values) {
513 1.1 christos rval = free_binding_values;
514 1.1 christos free_binding_values = rval -> value.bv;
515 1.1 christos dmalloc_reuse (rval, file, line, 1);
516 1.1 christos } else {
517 1.1 christos rval = dmalloc (sizeof (struct binding_value), file, line);
518 1.1 christos if (!rval)
519 1.1 christos return 0;
520 1.1 christos }
521 1.1 christos memset (rval, 0, sizeof *rval);
522 1.1 christos return binding_value_reference (cptr, rval, file, line);
523 1.1 christos }
524 1.1 christos
525 1.1 christos int binding_value_reference (ptr, src, file, line)
526 1.1 christos struct binding_value **ptr;
527 1.1 christos struct binding_value *src;
528 1.1 christos const char *file;
529 1.1 christos int line;
530 1.1 christos {
531 1.1 christos if (!ptr) {
532 1.1 christos log_error ("%s(%d): null pointer", file, line);
533 1.1 christos #if defined (POINTER_DEBUG)
534 1.1 christos abort ();
535 1.1 christos #else
536 1.1 christos return 0;
537 1.1 christos #endif
538 1.1 christos }
539 1.1 christos if (*ptr) {
540 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
541 1.1 christos #if defined (POINTER_DEBUG)
542 1.1 christos abort ();
543 1.1 christos #else
544 1.1 christos *ptr = (struct binding_value *)0;
545 1.1 christos #endif
546 1.1 christos }
547 1.1 christos *ptr = src;
548 1.1 christos src -> refcnt++;
549 1.1 christos rc_register (file, line, ptr, src, src -> refcnt, 0, RC_MISC);
550 1.1 christos return 1;
551 1.1 christos }
552 1.1 christos
553 1.1 christos void free_binding_value (bv, file, line)
554 1.1 christos struct binding_value *bv;
555 1.1 christos const char *file;
556 1.1 christos int line;
557 1.1 christos {
558 1.1 christos bv -> value.bv = free_binding_values;
559 1.1 christos free_binding_values = bv;
560 1.1 christos dmalloc_reuse (free_binding_values, (char *)0, 0, 0);
561 1.1 christos }
562 1.1 christos
563 1.1 christos #if defined (DEBUG_MEMORY_LEAKAGE) || \
564 1.1 christos defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
565 1.1 christos void relinquish_free_binding_values ()
566 1.1 christos {
567 1.1 christos struct binding_value *b, *n;
568 1.1 christos
569 1.1 christos for (b = free_binding_values; b; b = n) {
570 1.1 christos n = b -> value.bv;
571 1.1 christos dfree (b, MDL);
572 1.1 christos }
573 1.1 christos free_binding_values = (struct binding_value *)0;
574 1.1 christos }
575 1.1 christos #endif
576 1.1 christos
577 1.1 christos int fundef_allocate (cptr, file, line)
578 1.1 christos struct fundef **cptr;
579 1.1 christos const char *file;
580 1.1 christos int line;
581 1.1 christos {
582 1.1 christos struct fundef *rval;
583 1.1 christos
584 1.1 christos rval = dmalloc (sizeof (struct fundef), file, line);
585 1.1 christos if (!rval)
586 1.1 christos return 0;
587 1.1 christos memset (rval, 0, sizeof *rval);
588 1.1 christos return fundef_reference (cptr, rval, file, line);
589 1.1 christos }
590 1.1 christos
591 1.1 christos int fundef_reference (ptr, src, file, line)
592 1.1 christos struct fundef **ptr;
593 1.1 christos struct fundef *src;
594 1.1 christos const char *file;
595 1.1 christos int line;
596 1.1 christos {
597 1.1 christos if (!ptr) {
598 1.1 christos log_error ("%s(%d): null pointer", file, line);
599 1.1 christos #if defined (POINTER_DEBUG)
600 1.1 christos abort ();
601 1.1 christos #else
602 1.1 christos return 0;
603 1.1 christos #endif
604 1.1 christos }
605 1.1 christos if (*ptr) {
606 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
607 1.1 christos #if defined (POINTER_DEBUG)
608 1.1 christos abort ();
609 1.1 christos #else
610 1.1 christos *ptr = (struct fundef *)0;
611 1.1 christos #endif
612 1.1 christos }
613 1.1 christos *ptr = src;
614 1.1 christos src -> refcnt++;
615 1.1 christos rc_register (file, line, ptr, src, src -> refcnt, 0, RC_MISC);
616 1.1 christos return 1;
617 1.1 christos }
618 1.1 christos
619 1.1 christos struct option_cache *free_option_caches;
620 1.1 christos
621 1.1 christos #if defined (DEBUG_MEMORY_LEAKAGE) || \
622 1.1 christos defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
623 1.1 christos void relinquish_free_option_caches ()
624 1.1 christos {
625 1.1 christos struct option_cache *o, *n;
626 1.1 christos
627 1.1 christos for (o = free_option_caches; o; o = n) {
628 1.1 christos n = (struct option_cache *)(o -> expression);
629 1.1 christos dfree (o, MDL);
630 1.1 christos }
631 1.1 christos free_option_caches = (struct option_cache *)0;
632 1.1 christos }
633 1.1 christos #endif
634 1.1 christos
635 1.1 christos int option_cache_allocate (cptr, file, line)
636 1.1 christos struct option_cache **cptr;
637 1.1 christos const char *file;
638 1.1 christos int line;
639 1.1 christos {
640 1.1 christos struct option_cache *rval;
641 1.1 christos
642 1.1 christos if (free_option_caches) {
643 1.1 christos rval = free_option_caches;
644 1.1 christos free_option_caches =
645 1.1 christos (struct option_cache *)(rval -> expression);
646 1.1 christos dmalloc_reuse (rval, file, line, 0);
647 1.1 christos } else {
648 1.1 christos rval = dmalloc (sizeof (struct option_cache), file, line);
649 1.1 christos if (!rval)
650 1.1 christos return 0;
651 1.1 christos }
652 1.1 christos memset (rval, 0, sizeof *rval);
653 1.1 christos return option_cache_reference (cptr, rval, file, line);
654 1.1 christos }
655 1.1 christos
656 1.1 christos int option_cache_reference (ptr, src, file, line)
657 1.1 christos struct option_cache **ptr;
658 1.1 christos struct option_cache *src;
659 1.1 christos const char *file;
660 1.1 christos int line;
661 1.1 christos {
662 1.1 christos if (!ptr) {
663 1.1 christos log_error ("%s(%d): null pointer", file, line);
664 1.1 christos #if defined (POINTER_DEBUG)
665 1.1 christos abort ();
666 1.1 christos #else
667 1.1 christos return 0;
668 1.1 christos #endif
669 1.1 christos }
670 1.1 christos if (*ptr) {
671 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
672 1.1 christos #if defined (POINTER_DEBUG)
673 1.1 christos abort ();
674 1.1 christos #else
675 1.1 christos *ptr = (struct option_cache *)0;
676 1.1 christos #endif
677 1.1 christos }
678 1.1 christos *ptr = src;
679 1.1 christos src -> refcnt++;
680 1.1 christos rc_register (file, line, ptr, src, src -> refcnt, 0, RC_MISC);
681 1.1 christos return 1;
682 1.1 christos }
683 1.1 christos
684 1.1 christos int buffer_allocate (ptr, len, file, line)
685 1.1 christos struct buffer **ptr;
686 1.1 christos unsigned len;
687 1.1 christos const char *file;
688 1.1 christos int line;
689 1.1 christos {
690 1.1 christos struct buffer *bp;
691 1.1 christos
692 1.1 christos /* XXXSK: should check for bad ptr values, otherwise we
693 1.1 christos leak memory if they are wrong */
694 1.1 christos bp = dmalloc (len + sizeof *bp, file, line);
695 1.1 christos if (!bp)
696 1.1 christos return 0;
697 1.1 christos /* XXXSK: both of these initializations are unnecessary */
698 1.1 christos memset (bp, 0, sizeof *bp);
699 1.1 christos bp -> refcnt = 0;
700 1.1 christos return buffer_reference (ptr, bp, file, line);
701 1.1 christos }
702 1.1 christos
703 1.1 christos int buffer_reference (ptr, bp, file, line)
704 1.1 christos struct buffer **ptr;
705 1.1 christos struct buffer *bp;
706 1.1 christos const char *file;
707 1.1 christos int line;
708 1.1 christos {
709 1.1 christos if (!ptr) {
710 1.1 christos log_error ("%s(%d): null pointer", file, line);
711 1.1 christos #if defined (POINTER_DEBUG)
712 1.1 christos abort ();
713 1.1 christos #else
714 1.1 christos return 0;
715 1.1 christos #endif
716 1.1 christos }
717 1.1 christos if (*ptr) {
718 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
719 1.1 christos #if defined (POINTER_DEBUG)
720 1.1 christos abort ();
721 1.1 christos #else
722 1.1 christos *ptr = (struct buffer *)0;
723 1.1 christos #endif
724 1.1 christos }
725 1.1 christos *ptr = bp;
726 1.1 christos bp -> refcnt++;
727 1.1 christos rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
728 1.1 christos return 1;
729 1.1 christos }
730 1.1 christos
731 1.1 christos int buffer_dereference (ptr, file, line)
732 1.1 christos struct buffer **ptr;
733 1.1 christos const char *file;
734 1.1 christos int line;
735 1.1 christos {
736 1.1 christos if (!ptr) {
737 1.1 christos log_error ("%s(%d): null pointer", file, line);
738 1.1 christos #if defined (POINTER_DEBUG)
739 1.1 christos abort ();
740 1.1 christos #else
741 1.1 christos return 0;
742 1.1 christos #endif
743 1.1 christos }
744 1.1 christos
745 1.1 christos if (!*ptr) {
746 1.1 christos log_error ("%s(%d): null pointer", file, line);
747 1.1 christos #if defined (POINTER_DEBUG)
748 1.1 christos abort ();
749 1.1 christos #else
750 1.1 christos return 0;
751 1.1 christos #endif
752 1.1 christos }
753 1.1 christos
754 1.1 christos (*ptr) -> refcnt--;
755 1.1 christos rc_register (file, line, ptr, *ptr, (*ptr) -> refcnt, 1, RC_MISC);
756 1.1 christos if (!(*ptr) -> refcnt) {
757 1.1 christos dfree ((*ptr), file, line);
758 1.1 christos } else if ((*ptr) -> refcnt < 0) {
759 1.1 christos log_error ("%s(%d): negative refcnt!", file, line);
760 1.1 christos #if defined (DEBUG_RC_HISTORY)
761 1.1 christos dump_rc_history (*ptr);
762 1.1 christos #endif
763 1.1 christos #if defined (POINTER_DEBUG)
764 1.1 christos abort ();
765 1.1 christos #else
766 1.1 christos return 0;
767 1.1 christos #endif
768 1.1 christos }
769 1.1 christos *ptr = (struct buffer *)0;
770 1.1 christos return 1;
771 1.1 christos }
772 1.1 christos
773 1.1 christos int dns_host_entry_allocate (ptr, hostname, file, line)
774 1.1 christos struct dns_host_entry **ptr;
775 1.1 christos const char *hostname;
776 1.1 christos const char *file;
777 1.1 christos int line;
778 1.1 christos {
779 1.1 christos struct dns_host_entry *bp;
780 1.1 christos
781 1.1 christos bp = dmalloc (strlen (hostname) + sizeof *bp, file, line);
782 1.1 christos if (!bp)
783 1.1 christos return 0;
784 1.1 christos memset (bp, 0, sizeof *bp);
785 1.1 christos bp -> refcnt = 0;
786 1.1 christos strcpy (bp -> hostname, hostname);
787 1.1 christos return dns_host_entry_reference (ptr, bp, file, line);
788 1.1 christos }
789 1.1 christos
790 1.1 christos int dns_host_entry_reference (ptr, bp, file, line)
791 1.1 christos struct dns_host_entry **ptr;
792 1.1 christos struct dns_host_entry *bp;
793 1.1 christos const char *file;
794 1.1 christos int line;
795 1.1 christos {
796 1.1 christos if (!ptr) {
797 1.1 christos log_error ("%s(%d): null pointer", file, line);
798 1.1 christos #if defined (POINTER_DEBUG)
799 1.1 christos abort ();
800 1.1 christos #else
801 1.1 christos return 0;
802 1.1 christos #endif
803 1.1 christos }
804 1.1 christos if (*ptr) {
805 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
806 1.1 christos #if defined (POINTER_DEBUG)
807 1.1 christos abort ();
808 1.1 christos #else
809 1.1 christos *ptr = (struct dns_host_entry *)0;
810 1.1 christos #endif
811 1.1 christos }
812 1.1 christos *ptr = bp;
813 1.1 christos bp -> refcnt++;
814 1.1 christos rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
815 1.1 christos return 1;
816 1.1 christos }
817 1.1 christos
818 1.1 christos int dns_host_entry_dereference (ptr, file, line)
819 1.1 christos struct dns_host_entry **ptr;
820 1.1 christos const char *file;
821 1.1 christos int line;
822 1.1 christos {
823 1.1 christos if (!ptr || !*ptr) {
824 1.1 christos log_error ("%s(%d): null pointer", file, line);
825 1.1 christos #if defined (POINTER_DEBUG)
826 1.1 christos abort ();
827 1.1 christos #else
828 1.1 christos return 0;
829 1.1 christos #endif
830 1.1 christos }
831 1.1 christos
832 1.1 christos (*ptr)->refcnt--;
833 1.1 christos rc_register (file, line, ptr, *ptr, (*ptr)->refcnt, 1, RC_MISC);
834 1.1 christos if ((*ptr)->refcnt == 0) {
835 1.1 christos dfree ((*ptr), file, line);
836 1.1 christos } else if ((*ptr)->refcnt < 0) {
837 1.1 christos log_error ("%s(%d): negative refcnt!", file, line);
838 1.1 christos #if defined (DEBUG_RC_HISTORY)
839 1.1 christos dump_rc_history (*ptr);
840 1.1 christos #endif
841 1.1 christos #if defined (POINTER_DEBUG)
842 1.1 christos abort ();
843 1.1 christos #else
844 1.1 christos return 0;
845 1.1 christos #endif
846 1.1 christos }
847 1.1 christos *ptr = (struct dns_host_entry *)0;
848 1.1 christos return 1;
849 1.1 christos }
850 1.1 christos
851 1.1 christos int option_state_allocate (ptr, file, line)
852 1.1 christos struct option_state **ptr;
853 1.1 christos const char *file;
854 1.1 christos int line;
855 1.1 christos {
856 1.1 christos unsigned size;
857 1.1 christos
858 1.1 christos if (!ptr) {
859 1.1 christos log_error ("%s(%d): null pointer", file, line);
860 1.1 christos #if defined (POINTER_DEBUG)
861 1.1 christos abort ();
862 1.1 christos #else
863 1.1 christos return 0;
864 1.1 christos #endif
865 1.1 christos }
866 1.1 christos if (*ptr) {
867 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
868 1.1 christos #if defined (POINTER_DEBUG)
869 1.1 christos abort ();
870 1.1 christos #else
871 1.1 christos *ptr = (struct option_state *)0;
872 1.1 christos #endif
873 1.1 christos }
874 1.1 christos
875 1.1 christos size = sizeof **ptr + (universe_count - 1) * sizeof (void *);
876 1.1 christos *ptr = dmalloc (size, file, line);
877 1.1 christos if (*ptr) {
878 1.1 christos memset (*ptr, 0, size);
879 1.1 christos (*ptr) -> universe_count = universe_count;
880 1.1 christos (*ptr) -> refcnt = 1;
881 1.1 christos rc_register (file, line,
882 1.1 christos ptr, *ptr, (*ptr) -> refcnt, 0, RC_MISC);
883 1.1 christos return 1;
884 1.1 christos }
885 1.1 christos return 0;
886 1.1 christos }
887 1.1 christos
888 1.1 christos int option_state_reference (ptr, bp, file, line)
889 1.1 christos struct option_state **ptr;
890 1.1 christos struct option_state *bp;
891 1.1 christos const char *file;
892 1.1 christos int line;
893 1.1 christos {
894 1.1 christos if (!ptr) {
895 1.1 christos log_error ("%s(%d): null pointer", file, line);
896 1.1 christos #if defined (POINTER_DEBUG)
897 1.1 christos abort ();
898 1.1 christos #else
899 1.1 christos return 0;
900 1.1 christos #endif
901 1.1 christos }
902 1.1 christos if (*ptr) {
903 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
904 1.1 christos #if defined (POINTER_DEBUG)
905 1.1 christos abort ();
906 1.1 christos #else
907 1.1 christos *ptr = (struct option_state *)0;
908 1.1 christos #endif
909 1.1 christos }
910 1.1 christos *ptr = bp;
911 1.1 christos bp -> refcnt++;
912 1.1 christos rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
913 1.1 christos return 1;
914 1.1 christos }
915 1.1 christos
916 1.1 christos int option_state_dereference (ptr, file, line)
917 1.1 christos struct option_state **ptr;
918 1.1 christos const char *file;
919 1.1 christos int line;
920 1.1 christos {
921 1.1 christos int i;
922 1.1 christos struct option_state *options;
923 1.1 christos
924 1.1 christos if (!ptr || !*ptr) {
925 1.1 christos log_error ("%s(%d): null pointer", file, line);
926 1.1 christos #if defined (POINTER_DEBUG)
927 1.1 christos abort ();
928 1.1 christos #else
929 1.1 christos return 0;
930 1.1 christos #endif
931 1.1 christos }
932 1.1 christos
933 1.1 christos options = *ptr;
934 1.1 christos *ptr = (struct option_state *)0;
935 1.1 christos --options -> refcnt;
936 1.1 christos rc_register (file, line, ptr, options, options -> refcnt, 1, RC_MISC);
937 1.1 christos if (options -> refcnt > 0)
938 1.1 christos return 1;
939 1.1 christos
940 1.1 christos if (options -> refcnt < 0) {
941 1.1 christos log_error ("%s(%d): negative refcnt!", file, line);
942 1.1 christos #if defined (DEBUG_RC_HISTORY)
943 1.1 christos dump_rc_history (options);
944 1.1 christos #endif
945 1.1 christos #if defined (POINTER_DEBUG)
946 1.1 christos abort ();
947 1.1 christos #else
948 1.1 christos return 0;
949 1.1 christos #endif
950 1.1 christos }
951 1.1 christos
952 1.1 christos /* Loop through the per-universe state. */
953 1.1 christos for (i = 0; i < options -> universe_count; i++)
954 1.1 christos if (options -> universes [i] &&
955 1.1 christos universes [i] -> option_state_dereference)
956 1.1 christos ((*(universes [i] -> option_state_dereference))
957 1.1 christos (universes [i], options, file, line));
958 1.1 christos
959 1.1 christos dfree (options, file, line);
960 1.1 christos return 1;
961 1.1 christos }
962 1.1 christos
963 1.1 christos int executable_statement_allocate (ptr, file, line)
964 1.1 christos struct executable_statement **ptr;
965 1.1 christos const char *file;
966 1.1 christos int line;
967 1.1 christos {
968 1.1 christos struct executable_statement *bp;
969 1.1 christos
970 1.1 christos bp = dmalloc (sizeof *bp, file, line);
971 1.1 christos if (!bp)
972 1.1 christos return 0;
973 1.1 christos memset (bp, 0, sizeof *bp);
974 1.1 christos return executable_statement_reference (ptr, bp, file, line);
975 1.1 christos }
976 1.1 christos
977 1.1 christos int executable_statement_reference (ptr, bp, file, line)
978 1.1 christos struct executable_statement **ptr;
979 1.1 christos struct executable_statement *bp;
980 1.1 christos const char *file;
981 1.1 christos int line;
982 1.1 christos {
983 1.1 christos if (!ptr) {
984 1.1 christos log_error ("%s(%d): null pointer", file, line);
985 1.1 christos #if defined (POINTER_DEBUG)
986 1.1 christos abort ();
987 1.1 christos #else
988 1.1 christos return 0;
989 1.1 christos #endif
990 1.1 christos }
991 1.1 christos if (*ptr) {
992 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
993 1.1 christos #if defined (POINTER_DEBUG)
994 1.1 christos abort ();
995 1.1 christos #else
996 1.1 christos *ptr = (struct executable_statement *)0;
997 1.1 christos #endif
998 1.1 christos }
999 1.1 christos *ptr = bp;
1000 1.1 christos bp -> refcnt++;
1001 1.1 christos rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
1002 1.1 christos return 1;
1003 1.1 christos }
1004 1.1 christos
1005 1.1 christos static struct packet *free_packets;
1006 1.1 christos
1007 1.1 christos #if defined (DEBUG_MEMORY_LEAKAGE) || \
1008 1.1 christos defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
1009 1.1 christos void relinquish_free_packets ()
1010 1.1 christos {
1011 1.1 christos struct packet *p, *n;
1012 1.1 christos for (p = free_packets; p; p = n) {
1013 1.1 christos n = (struct packet *)(p -> raw);
1014 1.1 christos dfree (p, MDL);
1015 1.1 christos }
1016 1.1 christos free_packets = (struct packet *)0;
1017 1.1 christos }
1018 1.1 christos #endif
1019 1.1 christos
1020 1.1 christos int packet_allocate (ptr, file, line)
1021 1.1 christos struct packet **ptr;
1022 1.1 christos const char *file;
1023 1.1 christos int line;
1024 1.1 christos {
1025 1.1 christos struct packet *p;
1026 1.1 christos
1027 1.1 christos if (!ptr) {
1028 1.1 christos log_error ("%s(%d): null pointer", file, line);
1029 1.1 christos #if defined (POINTER_DEBUG)
1030 1.1 christos abort ();
1031 1.1 christos #else
1032 1.1 christos return 0;
1033 1.1 christos #endif
1034 1.1 christos }
1035 1.1 christos if (*ptr) {
1036 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
1037 1.1 christos #if defined (POINTER_DEBUG)
1038 1.1 christos abort ();
1039 1.1 christos #else
1040 1.1 christos *ptr = (struct packet *)0;
1041 1.1 christos #endif
1042 1.1 christos }
1043 1.1 christos
1044 1.1 christos if (free_packets) {
1045 1.1 christos p = free_packets;
1046 1.1 christos free_packets = (struct packet *)(p -> raw);
1047 1.1 christos dmalloc_reuse (p, file, line, 1);
1048 1.1 christos } else {
1049 1.1 christos p = dmalloc (sizeof *p, file, line);
1050 1.1 christos }
1051 1.1 christos if (p) {
1052 1.1 christos memset (p, 0, sizeof *p);
1053 1.1 christos return packet_reference (ptr, p, file, line);
1054 1.1 christos }
1055 1.1 christos return 0;
1056 1.1 christos }
1057 1.1 christos
1058 1.1 christos int packet_reference (ptr, bp, file, line)
1059 1.1 christos struct packet **ptr;
1060 1.1 christos struct packet *bp;
1061 1.1 christos const char *file;
1062 1.1 christos int line;
1063 1.1 christos {
1064 1.1 christos if (!ptr) {
1065 1.1 christos log_error ("%s(%d): null pointer", file, line);
1066 1.1 christos #if defined (POINTER_DEBUG)
1067 1.1 christos abort ();
1068 1.1 christos #else
1069 1.1 christos return 0;
1070 1.1 christos #endif
1071 1.1 christos }
1072 1.1 christos if (*ptr) {
1073 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
1074 1.1 christos #if defined (POINTER_DEBUG)
1075 1.1 christos abort ();
1076 1.1 christos #else
1077 1.1 christos *ptr = (struct packet *)0;
1078 1.1 christos #endif
1079 1.1 christos }
1080 1.1 christos *ptr = bp;
1081 1.1 christos bp -> refcnt++;
1082 1.1 christos rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
1083 1.1 christos return 1;
1084 1.1 christos }
1085 1.1 christos
1086 1.1 christos int packet_dereference (ptr, file, line)
1087 1.1 christos struct packet **ptr;
1088 1.1 christos const char *file;
1089 1.1 christos int line;
1090 1.1 christos {
1091 1.1 christos int i;
1092 1.1 christos struct packet *packet;
1093 1.1 christos
1094 1.1 christos if (!ptr || !*ptr) {
1095 1.1 christos log_error ("%s(%d): null pointer", file, line);
1096 1.1 christos #if defined (POINTER_DEBUG)
1097 1.1 christos abort ();
1098 1.1 christos #else
1099 1.1 christos return 0;
1100 1.1 christos #endif
1101 1.1 christos }
1102 1.1 christos
1103 1.1 christos packet = *ptr;
1104 1.1 christos *ptr = (struct packet *)0;
1105 1.1 christos --packet -> refcnt;
1106 1.1 christos rc_register (file, line, ptr, packet, packet -> refcnt, 1, RC_MISC);
1107 1.1 christos if (packet -> refcnt > 0)
1108 1.1 christos return 1;
1109 1.1 christos
1110 1.1 christos if (packet -> refcnt < 0) {
1111 1.1 christos log_error ("%s(%d): negative refcnt!", file, line);
1112 1.1 christos #if defined (DEBUG_RC_HISTORY)
1113 1.1 christos dump_rc_history (packet);
1114 1.1 christos #endif
1115 1.1 christos #if defined (POINTER_DEBUG)
1116 1.1 christos abort ();
1117 1.1 christos #else
1118 1.1 christos return 0;
1119 1.1 christos #endif
1120 1.1 christos }
1121 1.1 christos
1122 1.1 christos if (packet -> options)
1123 1.1 christos option_state_dereference (&packet -> options, file, line);
1124 1.1 christos if (packet -> interface)
1125 1.1 christos interface_dereference (&packet -> interface, MDL);
1126 1.1 christos if (packet -> shared_network)
1127 1.1 christos shared_network_dereference (&packet -> shared_network, MDL);
1128 1.1 christos for (i = 0; i < packet -> class_count && i < PACKET_MAX_CLASSES; i++) {
1129 1.1 christos if (packet -> classes [i])
1130 1.1 christos omapi_object_dereference ((omapi_object_t **)
1131 1.1 christos &packet -> classes [i], MDL);
1132 1.1 christos }
1133 1.1 christos packet -> raw = (struct dhcp_packet *)free_packets;
1134 1.1 christos free_packets = packet;
1135 1.1 christos dmalloc_reuse (free_packets, __FILE__, __LINE__, 0);
1136 1.1 christos return 1;
1137 1.1 christos }
1138 1.1 christos
1139 1.1 christos int dns_zone_allocate (ptr, file, line)
1140 1.1 christos struct dns_zone **ptr;
1141 1.1 christos const char *file;
1142 1.1 christos int line;
1143 1.1 christos {
1144 1.1 christos struct dns_zone *d;
1145 1.1 christos
1146 1.1 christos if (!ptr) {
1147 1.1 christos log_error ("%s(%d): null pointer", file, line);
1148 1.1 christos #if defined (POINTER_DEBUG)
1149 1.1 christos abort ();
1150 1.1 christos #else
1151 1.1 christos return 0;
1152 1.1 christos #endif
1153 1.1 christos }
1154 1.1 christos if (*ptr) {
1155 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
1156 1.1 christos #if defined (POINTER_DEBUG)
1157 1.1 christos abort ();
1158 1.1 christos #else
1159 1.1 christos *ptr = (struct dns_zone *)0;
1160 1.1 christos #endif
1161 1.1 christos }
1162 1.1 christos
1163 1.1 christos d = dmalloc (sizeof *d, file, line);
1164 1.1 christos if (d) {
1165 1.1 christos memset (d, 0, sizeof *d);
1166 1.1 christos return dns_zone_reference (ptr, d, file, line);
1167 1.1 christos }
1168 1.1 christos return 0;
1169 1.1 christos }
1170 1.1 christos
1171 1.1 christos int dns_zone_reference (ptr, bp, file, line)
1172 1.1 christos struct dns_zone **ptr;
1173 1.1 christos struct dns_zone *bp;
1174 1.1 christos const char *file;
1175 1.1 christos int line;
1176 1.1 christos {
1177 1.1 christos if (!ptr) {
1178 1.1 christos log_error ("%s(%d): null pointer", file, line);
1179 1.1 christos #if defined (POINTER_DEBUG)
1180 1.1 christos abort ();
1181 1.1 christos #else
1182 1.1 christos return 0;
1183 1.1 christos #endif
1184 1.1 christos }
1185 1.1 christos if (*ptr) {
1186 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
1187 1.1 christos #if defined (POINTER_DEBUG)
1188 1.1 christos abort ();
1189 1.1 christos #else
1190 1.1 christos *ptr = (struct dns_zone *)0;
1191 1.1 christos #endif
1192 1.1 christos }
1193 1.1 christos *ptr = bp;
1194 1.1 christos bp -> refcnt++;
1195 1.1 christos rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
1196 1.1 christos return 1;
1197 1.1 christos }
1198 1.1 christos
1199 1.1 christos int binding_scope_allocate (ptr, file, line)
1200 1.1 christos struct binding_scope **ptr;
1201 1.1 christos const char *file;
1202 1.1 christos int line;
1203 1.1 christos {
1204 1.1 christos struct binding_scope *bp;
1205 1.1 christos
1206 1.1 christos if (!ptr) {
1207 1.1 christos log_error ("%s(%d): null pointer", file, line);
1208 1.1 christos #if defined (POINTER_DEBUG)
1209 1.1 christos abort ();
1210 1.1 christos #else
1211 1.1 christos return 0;
1212 1.1 christos #endif
1213 1.1 christos }
1214 1.1 christos
1215 1.1 christos if (*ptr) {
1216 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
1217 1.1 christos #if defined (POINTER_DEBUG)
1218 1.1 christos abort ();
1219 1.1 christos #else
1220 1.1 christos return 0;
1221 1.1 christos #endif
1222 1.1 christos }
1223 1.1 christos
1224 1.1 christos bp = dmalloc (sizeof *bp, file, line);
1225 1.1 christos if (!bp)
1226 1.1 christos return 0;
1227 1.1 christos memset (bp, 0, sizeof *bp);
1228 1.1 christos binding_scope_reference (ptr, bp, file, line);
1229 1.1 christos return 1;
1230 1.1 christos }
1231 1.1 christos
1232 1.1 christos int binding_scope_reference (ptr, bp, file, line)
1233 1.1 christos struct binding_scope **ptr;
1234 1.1 christos struct binding_scope *bp;
1235 1.1 christos const char *file;
1236 1.1 christos int line;
1237 1.1 christos {
1238 1.1 christos if (!ptr) {
1239 1.1 christos log_error ("%s(%d): null pointer", file, line);
1240 1.1 christos #if defined (POINTER_DEBUG)
1241 1.1 christos abort ();
1242 1.1 christos #else
1243 1.1 christos return 0;
1244 1.1 christos #endif
1245 1.1 christos }
1246 1.1 christos if (*ptr) {
1247 1.1 christos log_error ("%s(%d): non-null pointer", file, line);
1248 1.1 christos #if defined (POINTER_DEBUG)
1249 1.1 christos abort ();
1250 1.1 christos #else
1251 1.1 christos *ptr = (struct binding_scope *)0;
1252 1.1 christos #endif
1253 1.1 christos }
1254 1.1 christos *ptr = bp;
1255 1.1 christos bp -> refcnt++;
1256 1.1 christos rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
1257 1.1 christos return 1;
1258 1.1 christos }
1259 1.1 christos
1260 1.1 christos /*!
1261 1.1 christos * \brief Constructs a null-terminated data_string from a char* and length.
1262 1.1 christos *
1263 1.1 christos * Allocates a data_string and copies into it the given length of bytes
1264 1.1 christos * from the given source, adding a terminating null if not present in the source
1265 1.1 christos * at length-1.
1266 1.1 christos *
1267 1.1 christos * \param new_string pointer to the data_string to construct. Cannot be
1268 1.1 christos * NULL. Note that its contents will be overwritten. Passing in the address
1269 1.1 christos * of an allocated data_string will result in memory leaks.
1270 1.1 christos * \param src data to be copied. Cannot be NULL.
1271 1.1 christos * \param len length of the data to copied
1272 1.1 christos *
1273 1.1 christos * \return 1 - if the data_string is constructed successfully, 0 if
1274 1.1 christos * target data_struct is NULL or the buffer allocation fails.
1275 1.1 christos */
1276 1.1 christos int
1277 1.1 christos data_string_new(struct data_string *new_string,
1278 1.1 christos const char *src, unsigned int len,
1279 1.1 christos const char *file, int line)
1280 1.1 christos {
1281 1.1 christos unsigned int copy_len = 0;
1282 1.1 christos
1283 1.1 christos if (new_string == NULL) {
1284 1.1 christos log_error("data_string_new: new_string cannot be NULL %s(%d)",
1285 1.1 christos file, line);
1286 1.1 christos return (0);
1287 1.1 christos }
1288 1.1 christos
1289 1.1 christos if (src == NULL) {
1290 1.1 christos log_error("data_string_new: src cannot be NULL %s(%d)",
1291 1.1 christos file, line);
1292 1.1 christos return (0);
1293 1.1 christos }
1294 1.1 christos
1295 1.1 christos memset(new_string, 0, sizeof (struct data_string));
1296 1.1 christos
1297 1.1 christos /* If we already have a NULL back off length by one. This lets
1298 1.1 christos * us always just add a NULL at the end. */
1299 1.1 christos copy_len = (len > 0 && src[len - 1] == 0) ? len - 1 : len;
1300 1.1 christos
1301 1.1 christos /* Allocate the buffer, accounting for terminating null */
1302 1.1 christos if (!buffer_allocate(&(new_string->buffer), copy_len + 1, MDL)) {
1303 1.1 christos log_error("data_string_new: No memory %s(%d)", file, line);
1304 1.1 christos return (0);
1305 1.1 christos }
1306 1.1 christos
1307 1.1 christos /* Only copy if there's something to copy */
1308 1.1 christos if (copy_len > 0) {
1309 1.1 christos memcpy(new_string->buffer->data, src, copy_len);
1310 1.1 christos }
1311 1.1 christos
1312 1.1 christos /* Always tack on the null */
1313 1.1 christos new_string->buffer->data[copy_len] = 0;
1314 1.1 christos
1315 1.1 christos /* Update data_string accessor values. Note len does NOT include
1316 1.1 christos * the NULL. */
1317 1.1 christos new_string->data = new_string->buffer->data;
1318 1.1 christos new_string->len = copy_len;
1319 1.1 christos new_string->terminated = 1;
1320 1.1 christos
1321 1.1 christos return (1);
1322 1.1 christos }
1323 1.1 christos
1324 1.1 christos /* Make a copy of the data in data_string, upping the buffer reference
1325 1.1 christos count if there's a buffer. */
1326 1.1 christos
1327 1.1 christos void
1328 1.1 christos data_string_copy(struct data_string *dest, const struct data_string *src,
1329 1.1 christos const char *file, int line)
1330 1.1 christos {
1331 1.1 christos if (src -> buffer) {
1332 1.1 christos buffer_reference (&dest -> buffer, src -> buffer, file, line);
1333 1.1 christos } else {
1334 1.1 christos dest->buffer = NULL;
1335 1.1 christos }
1336 1.1 christos dest -> data = src -> data;
1337 1.1 christos dest -> terminated = src -> terminated;
1338 1.1 christos dest -> len = src -> len;
1339 1.1 christos }
1340 1.1 christos
1341 1.1 christos /* Release the reference count to a data string's buffer (if any) and
1342 1.1 christos zero out the other information, yielding the null data string. */
1343 1.1 christos
1344 1.1 christos void data_string_forget (data, file, line)
1345 1.1 christos struct data_string *data;
1346 1.1 christos const char *file;
1347 1.1 christos int line;
1348 1.1 christos {
1349 1.1 christos if (data -> buffer)
1350 1.1 christos buffer_dereference (&data -> buffer, file, line);
1351 1.1 christos memset (data, 0, sizeof *data);
1352 1.1 christos }
1353 1.1 christos
1354 1.1 christos /* If the data_string is larger than the specified length, reduce
1355 1.1 christos the data_string to the specified size. */
1356 1.1 christos
1357 1.1 christos void data_string_truncate (dp, len)
1358 1.1 christos struct data_string *dp;
1359 1.1 christos int len;
1360 1.1 christos {
1361 1.1 christos /* XXX: do we need to consider the "terminated" flag in the check? */
1362 1.1 christos if (len < dp -> len) {
1363 1.1 christos dp -> terminated = 0;
1364 1.1 christos dp -> len = len;
1365 1.1 christos }
1366 1.1 christos }
1367 1.1 christos
1368 1.1 christos /* \brief Converts a data_string to a null-terminated data string
1369 1.1 christos *
1370 1.1 christos * If the given string isn't null-terminated, replace it with a
1371 1.1 christos * null-terminated version and free the current string decrementing
1372 1.1 christos * the referecne count. If the string is null-terminated it is left
1373 1.1 christos * as is.
1374 1.1 christos *
1375 1.1 christos * Currently this routine doesn't check if the string is 0 length
1376 1.1 christos * that must be checked by the caller.
1377 1.1 christos *
1378 1.1 christos * \param [in/out] str the data_string to convert
1379 1.1 christos * \param file the file this routine was called from
1380 1.1 christos * \param line the line this routine was called from
1381 1.1 christos *
1382 1.1 christos * \return 1 if the string was converted successfully (or already terminated),
1383 1.1 christos * 0 if the conversion failed. Failure is only possible if memory for the new
1384 1.1 christos * string could not be allocated. If the conversion fails, the original
1385 1.1 christos * string's content is lost.
1386 1.1 christos */
1387 1.1 christos int data_string_terminate(str, file, line)
1388 1.1 christos struct data_string* str;
1389 1.1 christos const char *file;
1390 1.1 christos int line;
1391 1.1 christos {
1392 1.1 christos int ret_val = 1;
1393 1.1 christos
1394 1.1 christos if (str->terminated == 0) {
1395 1.1 christos struct data_string temp;
1396 1.1 christos memset(&temp, 0, sizeof(temp));
1397 1.1 christos
1398 1.1 christos data_string_copy(&temp, str, file, line);
1399 1.1 christos data_string_forget(str, file, line);
1400 1.1 christos if (data_string_new(str, (const char*)temp.data, temp.len,
1401 1.1 christos file, line) == 0) {
1402 1.1 christos /* couldn't create a copy, probably a memory issue,
1403 1.1 christos * an error message has already been logged. */
1404 1.1 christos ret_val = 0;
1405 1.1 christos }
1406 1.1 christos
1407 1.1 christos /* get rid of temp string */
1408 1.1 christos data_string_forget(&temp, file, line);
1409 1.1 christos }
1410 1.1 christos
1411 1.1 christos return (ret_val);
1412 1.1 christos }
1413