Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / dhcp / common / alloc.c
blob34df90d17ccf34b645992e13bb82efeb87746f81
1 /* alloc.c
3 Memory allocation... */
5 /*
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1996-2003 by Internet Software Consortium
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
22 * 950 Charter Street
23 * Redwood City, CA 94063
24 * <info@isc.org>
25 * http://www.isc.org/
27 * This software has been written for Internet Systems Consortium
28 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
29 * To learn more about Internet Systems Consortium, see
30 * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
31 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
32 * ``http://www.nominum.com''.
35 #ifndef lint
36 static char copyright[] =
37 "$Id: alloc.c,v 1.5 2005/08/11 17:13:21 drochner Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
38 #endif /* not lint */
40 #include "dhcpd.h"
41 #include <omapip/omapip_p.h>
43 struct dhcp_packet *dhcp_free_list;
44 struct packet *packet_free_list;
46 int option_chain_head_allocate (ptr, file, line)
47 struct option_chain_head **ptr;
48 const char *file;
49 int line;
51 struct option_chain_head *h;
53 if (!ptr) {
54 log_error ("%s(%d): null pointer", file, line);
55 #if defined (POINTER_DEBUG)
56 abort ();
57 #else
58 return 0;
59 #endif
61 if (*ptr) {
62 log_error ("%s(%d): non-null pointer", file, line);
63 #if defined (POINTER_DEBUG)
64 abort ();
65 #else
66 *ptr = (struct option_chain_head *)0;
67 #endif
70 h = dmalloc (sizeof *h, file, line);
71 if (h) {
72 memset (h, 0, sizeof *h);
73 return option_chain_head_reference (ptr, h, file, line);
75 return 0;
78 int option_chain_head_reference (ptr, bp, file, line)
79 struct option_chain_head **ptr;
80 struct option_chain_head *bp;
81 const char *file;
82 int line;
84 if (!ptr) {
85 log_error ("%s(%d): null pointer", file, line);
86 #if defined (POINTER_DEBUG)
87 abort ();
88 #else
89 return 0;
90 #endif
92 if (*ptr) {
93 log_error ("%s(%d): non-null pointer", file, line);
94 #if defined (POINTER_DEBUG)
95 abort ();
96 #else
97 *ptr = (struct option_chain_head *)0;
98 #endif
100 *ptr = bp;
101 bp -> refcnt++;
102 rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
103 return 1;
106 int option_chain_head_dereference (ptr, file, line)
107 struct option_chain_head **ptr;
108 const char *file;
109 int line;
111 struct option_chain_head *option_chain_head;
112 pair car, cdr;
114 if (!ptr || !*ptr) {
115 log_error ("%s(%d): null pointer", file, line);
116 #if defined (POINTER_DEBUG)
117 abort ();
118 #else
119 return 0;
120 #endif
123 option_chain_head = *ptr;
124 *ptr = (struct option_chain_head *)0;
125 --option_chain_head -> refcnt;
126 rc_register (file, line, ptr, option_chain_head,
127 option_chain_head -> refcnt, 1, RC_MISC);
128 if (option_chain_head -> refcnt > 0)
129 return 1;
131 if (option_chain_head -> refcnt < 0) {
132 log_error ("%s(%d): negative refcnt!", file, line);
133 #if defined (DEBUG_RC_HISTORY)
134 dump_rc_history (option_chain_head);
135 #endif
136 #if defined (POINTER_DEBUG)
137 abort ();
138 #else
139 return 0;
140 #endif
143 /* If there are any options on this head, free them. */
144 for (car = option_chain_head -> first; car; car = cdr) {
145 cdr = car -> cdr;
146 if (car -> car)
147 option_cache_dereference ((struct option_cache **)
148 (&car -> car), MDL);
149 dfree (car, MDL);
150 car = cdr;
153 dfree (option_chain_head, file, line);
154 return 1;
157 int group_allocate (ptr, file, line)
158 struct group **ptr;
159 const char *file;
160 int line;
162 struct group *g;
164 if (!ptr) {
165 log_error ("%s(%d): null pointer", file, line);
166 #if defined (POINTER_DEBUG)
167 abort ();
168 #else
169 return 0;
170 #endif
172 if (*ptr) {
173 log_error ("%s(%d): non-null pointer", file, line);
174 #if defined (POINTER_DEBUG)
175 abort ();
176 #else
177 *ptr = (struct group *)0;
178 #endif
181 g = dmalloc (sizeof *g, file, line);
182 if (g) {
183 memset (g, 0, sizeof *g);
184 return group_reference (ptr, g, file, line);
186 return 0;
189 int group_reference (ptr, bp, file, line)
190 struct group **ptr;
191 struct group *bp;
192 const char *file;
193 int line;
195 if (!ptr) {
196 log_error ("%s(%d): null pointer", file, line);
197 #if defined (POINTER_DEBUG)
198 abort ();
199 #else
200 return 0;
201 #endif
203 if (*ptr) {
204 log_error ("%s(%d): non-null pointer", file, line);
205 #if defined (POINTER_DEBUG)
206 abort ();
207 #else
208 *ptr = (struct group *)0;
209 #endif
211 *ptr = bp;
212 bp -> refcnt++;
213 rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
214 return 1;
217 int group_dereference (ptr, file, line)
218 struct group **ptr;
219 const char *file;
220 int line;
222 struct group *group;
224 if (!ptr || !*ptr) {
225 log_error ("%s(%d): null pointer", file, line);
226 #if defined (POINTER_DEBUG)
227 abort ();
228 #else
229 return 0;
230 #endif
233 group = *ptr;
234 *ptr = (struct group *)0;
235 --group -> refcnt;
236 rc_register (file, line, ptr, group, group -> refcnt, 1, RC_MISC);
237 if (group -> refcnt > 0)
238 return 1;
240 if (group -> refcnt < 0) {
241 log_error ("%s(%d): negative refcnt!", file, line);
242 #if defined (DEBUG_RC_HISTORY)
243 dump_rc_history (group);
244 #endif
245 #if defined (POINTER_DEBUG)
246 abort ();
247 #else
248 return 0;
249 #endif
252 if (group -> object)
253 group_object_dereference (&group -> object, file, line);
254 if (group -> subnet)
255 subnet_dereference (&group -> subnet, file, line);
256 if (group -> shared_network)
257 shared_network_dereference (&group -> shared_network,
258 file, line);
259 if (group -> statements)
260 executable_statement_dereference (&group -> statements,
261 file, line);
262 if (group -> next)
263 group_dereference (&group -> next, file, line);
264 dfree (group, file, line);
265 return 1;
268 struct dhcp_packet *new_dhcp_packet (file, line)
269 const char *file;
270 int line;
272 struct dhcp_packet *rval;
273 rval = (struct dhcp_packet *)dmalloc (sizeof (struct dhcp_packet),
274 file, line);
275 return rval;
278 struct protocol *new_protocol (file, line)
279 const char *file;
280 int line;
282 struct protocol *rval = dmalloc (sizeof (struct protocol), file, line);
283 return rval;
286 struct domain_search_list *new_domain_search_list (file, line)
287 const char *file;
288 int line;
290 struct domain_search_list *rval =
291 dmalloc (sizeof (struct domain_search_list), file, line);
292 return rval;
295 struct name_server *new_name_server (file, line)
296 const char *file;
297 int line;
299 struct name_server *rval =
300 dmalloc (sizeof (struct name_server), file, line);
301 return rval;
304 void free_name_server (ptr, file, line)
305 struct name_server *ptr;
306 const char *file;
307 int line;
309 dfree ((VOIDPTR)ptr, file, line);
312 struct option *new_option (file, line)
313 const char *file;
314 int line;
316 struct option *rval =
317 dmalloc (sizeof (struct option), file, line);
318 if (rval)
319 memset (rval, 0, sizeof *rval);
320 return rval;
323 void free_option (ptr, file, line)
324 struct option *ptr;
325 const char *file;
326 int line;
328 /* XXX have to put all options on heap before this is possible. */
329 #if 0
330 if (ptr -> name)
331 dfree ((VOIDPTR)option -> name, file, line);
332 dfree ((VOIDPTR)ptr, file, line);
333 #endif
336 struct universe *new_universe (file, line)
337 const char *file;
338 int line;
340 struct universe *rval =
341 dmalloc (sizeof (struct universe), file, line);
342 return rval;
345 void free_universe (ptr, file, line)
346 struct universe *ptr;
347 const char *file;
348 int line;
350 dfree ((VOIDPTR)ptr, file, line);
353 void free_domain_search_list (ptr, file, line)
354 struct domain_search_list *ptr;
355 const char *file;
356 int line;
358 dfree ((VOIDPTR)ptr, file, line);
361 void free_protocol (ptr, file, line)
362 struct protocol *ptr;
363 const char *file;
364 int line;
366 dfree ((VOIDPTR)ptr, file, line);
369 void free_dhcp_packet (ptr, file, line)
370 struct dhcp_packet *ptr;
371 const char *file;
372 int line;
374 dfree ((VOIDPTR)ptr, file, line);
377 struct client_lease *new_client_lease (file, line)
378 const char *file;
379 int line;
381 return (struct client_lease *)dmalloc (sizeof (struct client_lease),
382 file, line);
385 void free_client_lease (lease, file, line)
386 struct client_lease *lease;
387 const char *file;
388 int line;
390 dfree (lease, file, line);
393 pair free_pairs;
395 pair new_pair (file, line)
396 const char *file;
397 int line;
399 pair foo;
401 if (free_pairs) {
402 foo = free_pairs;
403 free_pairs = foo -> cdr;
404 memset (foo, 0, sizeof *foo);
405 dmalloc_reuse (foo, file, line, 0);
406 return foo;
409 foo = dmalloc (sizeof *foo, file, line);
410 if (!foo)
411 return foo;
412 memset (foo, 0, sizeof *foo);
413 return foo;
416 void free_pair (foo, file, line)
417 pair foo;
418 const char *file;
419 int line;
421 foo -> cdr = free_pairs;
422 free_pairs = foo;
423 dmalloc_reuse (free_pairs, (char *)0, 0, 0);
426 #if defined (DEBUG_MEMORY_LEAKAGE) || \
427 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
428 void relinquish_free_pairs ()
430 pair pf, pc;
432 for (pf = free_pairs; pf; pf = pc) {
433 pc = pf -> cdr;
434 dfree (pf, MDL);
436 free_pairs = (pair)0;
438 #endif
440 struct expression *free_expressions;
442 int expression_allocate (cptr, file, line)
443 struct expression **cptr;
444 const char *file;
445 int line;
447 struct expression *rval;
449 if (free_expressions) {
450 rval = free_expressions;
451 free_expressions = rval -> data.not;
452 dmalloc_reuse (rval, file, line, 1);
453 } else {
454 rval = dmalloc (sizeof (struct expression), file, line);
455 if (!rval)
456 return 0;
458 memset (rval, 0, sizeof *rval);
459 return expression_reference (cptr, rval, file, line);
462 int expression_reference (ptr, src, file, line)
463 struct expression **ptr;
464 struct expression *src;
465 const char *file;
466 int line;
468 if (!ptr) {
469 log_error ("%s(%d): null pointer", file, line);
470 #if defined (POINTER_DEBUG)
471 abort ();
472 #else
473 return 0;
474 #endif
476 if (*ptr) {
477 log_error ("%s(%d): non-null pointer", file, line);
478 #if defined (POINTER_DEBUG)
479 abort ();
480 #else
481 *ptr = (struct expression *)0;
482 #endif
484 *ptr = src;
485 src -> refcnt++;
486 rc_register (file, line, ptr, src, src -> refcnt, 0, RC_MISC);
487 return 1;
490 void free_expression (expr, file, line)
491 struct expression *expr;
492 const char *file;
493 int line;
495 expr -> data.not = free_expressions;
496 free_expressions = expr;
497 dmalloc_reuse (free_expressions, (char *)0, 0, 0);
500 #if defined (DEBUG_MEMORY_LEAKAGE) || \
501 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
502 void relinquish_free_expressions ()
504 struct expression *e, *n;
506 for (e = free_expressions; e; e = n) {
507 n = e -> data.not;
508 dfree (e, MDL);
510 free_expressions = (struct expression *)0;
512 #endif
514 struct binding_value *free_binding_values;
516 int binding_value_allocate (cptr, file, line)
517 struct binding_value **cptr;
518 const char *file;
519 int line;
521 struct binding_value *rval;
523 if (free_binding_values) {
524 rval = free_binding_values;
525 free_binding_values = rval -> value.bv;
526 dmalloc_reuse (rval, file, line, 1);
527 } else {
528 rval = dmalloc (sizeof (struct binding_value), file, line);
529 if (!rval)
530 return 0;
532 memset (rval, 0, sizeof *rval);
533 return binding_value_reference (cptr, rval, file, line);
536 int binding_value_reference (ptr, src, file, line)
537 struct binding_value **ptr;
538 struct binding_value *src;
539 const char *file;
540 int line;
542 if (!ptr) {
543 log_error ("%s(%d): null pointer", file, line);
544 #if defined (POINTER_DEBUG)
545 abort ();
546 #else
547 return 0;
548 #endif
550 if (*ptr) {
551 log_error ("%s(%d): non-null pointer", file, line);
552 #if defined (POINTER_DEBUG)
553 abort ();
554 #else
555 *ptr = (struct binding_value *)0;
556 #endif
558 *ptr = src;
559 src -> refcnt++;
560 rc_register (file, line, ptr, src, src -> refcnt, 0, RC_MISC);
561 return 1;
564 void free_binding_value (bv, file, line)
565 struct binding_value *bv;
566 const char *file;
567 int line;
569 bv -> value.bv = free_binding_values;
570 free_binding_values = bv;
571 dmalloc_reuse (free_binding_values, (char *)0, 0, 0);
574 #if defined (DEBUG_MEMORY_LEAKAGE) || \
575 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
576 void relinquish_free_binding_values ()
578 struct binding_value *b, *n;
580 for (b = free_binding_values; b; b = n) {
581 n = b -> value.bv;
582 dfree (b, MDL);
584 free_binding_values = (struct binding_value *)0;
586 #endif
588 int fundef_allocate (cptr, file, line)
589 struct fundef **cptr;
590 const char *file;
591 int line;
593 struct fundef *rval;
595 rval = dmalloc (sizeof (struct fundef), file, line);
596 if (!rval)
597 return 0;
598 memset (rval, 0, sizeof *rval);
599 return fundef_reference (cptr, rval, file, line);
602 int fundef_reference (ptr, src, file, line)
603 struct fundef **ptr;
604 struct fundef *src;
605 const char *file;
606 int line;
608 if (!ptr) {
609 log_error ("%s(%d): null pointer", file, line);
610 #if defined (POINTER_DEBUG)
611 abort ();
612 #else
613 return 0;
614 #endif
616 if (*ptr) {
617 log_error ("%s(%d): non-null pointer", file, line);
618 #if defined (POINTER_DEBUG)
619 abort ();
620 #else
621 *ptr = (struct fundef *)0;
622 #endif
624 *ptr = src;
625 src -> refcnt++;
626 rc_register (file, line, ptr, src, src -> refcnt, 0, RC_MISC);
627 return 1;
630 struct option_cache *free_option_caches;
632 #if defined (DEBUG_MEMORY_LEAKAGE) || \
633 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
634 void relinquish_free_option_caches ()
636 struct option_cache *o, *n;
638 for (o = free_option_caches; o; o = n) {
639 n = (struct option_cache *)(o -> expression);
640 dfree (o, MDL);
642 free_option_caches = (struct option_cache *)0;
644 #endif
646 int option_cache_allocate (cptr, file, line)
647 struct option_cache **cptr;
648 const char *file;
649 int line;
651 struct option_cache *rval;
653 if (free_option_caches) {
654 rval = free_option_caches;
655 free_option_caches =
656 (struct option_cache *)(rval -> expression);
657 dmalloc_reuse (rval, file, line, 0);
658 } else {
659 rval = dmalloc (sizeof (struct option_cache), file, line);
660 if (!rval)
661 return 0;
663 memset (rval, 0, sizeof *rval);
664 return option_cache_reference (cptr, rval, file, line);
667 int option_cache_reference (ptr, src, file, line)
668 struct option_cache **ptr;
669 struct option_cache *src;
670 const char *file;
671 int line;
673 if (!ptr) {
674 log_error ("%s(%d): null pointer", file, line);
675 #if defined (POINTER_DEBUG)
676 abort ();
677 #else
678 return 0;
679 #endif
681 if (*ptr) {
682 log_error ("%s(%d): non-null pointer", file, line);
683 #if defined (POINTER_DEBUG)
684 abort ();
685 #else
686 *ptr = (struct option_cache *)0;
687 #endif
689 *ptr = src;
690 src -> refcnt++;
691 rc_register (file, line, ptr, src, src -> refcnt, 0, RC_MISC);
692 return 1;
695 int buffer_allocate (ptr, len, file, line)
696 struct buffer **ptr;
697 unsigned len;
698 const char *file;
699 int line;
701 struct buffer *bp;
703 bp = dmalloc (len + sizeof *bp, file, line);
704 if (!bp)
705 return 0;
706 memset (bp, 0, sizeof *bp);
707 bp -> refcnt = 0;
708 return buffer_reference (ptr, bp, file, line);
711 int buffer_reference (ptr, bp, file, line)
712 struct buffer **ptr;
713 struct buffer *bp;
714 const char *file;
715 int line;
717 if (!ptr) {
718 log_error ("%s(%d): null pointer", file, line);
719 #if defined (POINTER_DEBUG)
720 abort ();
721 #else
722 return 0;
723 #endif
725 if (*ptr) {
726 log_error ("%s(%d): non-null pointer", file, line);
727 #if defined (POINTER_DEBUG)
728 abort ();
729 #else
730 *ptr = (struct buffer *)0;
731 #endif
733 *ptr = bp;
734 bp -> refcnt++;
735 rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
736 return 1;
739 int buffer_dereference (ptr, file, line)
740 struct buffer **ptr;
741 const char *file;
742 int line;
745 if (!ptr) {
746 log_error ("%s(%d): null pointer", file, line);
747 #if defined (POINTER_DEBUG)
748 abort ();
749 #else
750 return 0;
751 #endif
754 if (!*ptr) {
755 log_error ("%s(%d): null pointer", file, line);
756 #if defined (POINTER_DEBUG)
757 abort ();
758 #else
759 return 0;
760 #endif
763 (*ptr) -> refcnt--;
764 rc_register (file, line, ptr, *ptr, (*ptr) -> refcnt, 1, RC_MISC);
765 if (!(*ptr) -> refcnt) {
766 dfree ((*ptr), file, line);
767 } else if ((*ptr) -> refcnt < 0) {
768 log_error ("%s(%d): negative refcnt!", file, line);
769 #if defined (DEBUG_RC_HISTORY)
770 dump_rc_history (*ptr);
771 #endif
772 #if defined (POINTER_DEBUG)
773 abort ();
774 #else
775 return 0;
776 #endif
778 *ptr = (struct buffer *)0;
779 return 1;
782 int dns_host_entry_allocate (ptr, hostname, file, line)
783 struct dns_host_entry **ptr;
784 const char *hostname;
785 const char *file;
786 int line;
788 struct dns_host_entry *bp;
790 bp = dmalloc (strlen (hostname) + sizeof *bp, file, line);
791 if (!bp)
792 return 0;
793 memset (bp, 0, sizeof *bp);
794 bp -> refcnt = 0;
795 strcpy (bp -> hostname, hostname);
796 return dns_host_entry_reference (ptr, bp, file, line);
799 int dns_host_entry_reference (ptr, bp, file, line)
800 struct dns_host_entry **ptr;
801 struct dns_host_entry *bp;
802 const char *file;
803 int line;
805 if (!ptr) {
806 log_error ("%s(%d): null pointer", file, line);
807 #if defined (POINTER_DEBUG)
808 abort ();
809 #else
810 return 0;
811 #endif
813 if (*ptr) {
814 log_error ("%s(%d): non-null pointer", file, line);
815 #if defined (POINTER_DEBUG)
816 abort ();
817 #else
818 *ptr = (struct dns_host_entry *)0;
819 #endif
821 *ptr = bp;
822 bp -> refcnt++;
823 rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
824 return 1;
827 int dns_host_entry_dereference (ptr, file, line)
828 struct dns_host_entry **ptr;
829 const char *file;
830 int line;
833 if (!ptr || !*ptr) {
834 log_error ("%s(%d): null pointer", file, line);
835 #if defined (POINTER_DEBUG)
836 abort ();
837 #else
838 return 0;
839 #endif
842 (*ptr) -> refcnt--;
843 rc_register (file, line, ptr, *ptr, (*ptr) -> refcnt, 1, RC_MISC);
844 if (!(*ptr) -> refcnt)
845 dfree ((*ptr), file, line);
846 if ((*ptr) -> refcnt < 0) {
847 log_error ("%s(%d): negative refcnt!", file, line);
848 #if defined (DEBUG_RC_HISTORY)
849 dump_rc_history (*ptr);
850 #endif
851 #if defined (POINTER_DEBUG)
852 abort ();
853 #else
854 return 0;
855 #endif
857 *ptr = (struct dns_host_entry *)0;
858 return 1;
861 int option_state_allocate (ptr, file, line)
862 struct option_state **ptr;
863 const char *file;
864 int line;
866 unsigned size;
868 if (!ptr) {
869 log_error ("%s(%d): null pointer", file, line);
870 #if defined (POINTER_DEBUG)
871 abort ();
872 #else
873 return 0;
874 #endif
876 if (*ptr) {
877 log_error ("%s(%d): non-null pointer", file, line);
878 #if defined (POINTER_DEBUG)
879 abort ();
880 #else
881 *ptr = (struct option_state *)0;
882 #endif
885 size = sizeof **ptr + (universe_count - 1) * sizeof (VOIDPTR);
886 *ptr = dmalloc (size, file, line);
887 if (*ptr) {
888 memset (*ptr, 0, size);
889 (*ptr) -> universe_count = universe_count;
890 (*ptr) -> refcnt = 1;
891 rc_register (file, line,
892 ptr, *ptr, (*ptr) -> refcnt, 0, RC_MISC);
893 return 1;
895 return 0;
898 int option_state_reference (ptr, bp, file, line)
899 struct option_state **ptr;
900 struct option_state *bp;
901 const char *file;
902 int line;
904 if (!ptr) {
905 log_error ("%s(%d): null pointer", file, line);
906 #if defined (POINTER_DEBUG)
907 abort ();
908 #else
909 return 0;
910 #endif
912 if (*ptr) {
913 log_error ("%s(%d): non-null pointer", file, line);
914 #if defined (POINTER_DEBUG)
915 abort ();
916 #else
917 *ptr = (struct option_state *)0;
918 #endif
920 *ptr = bp;
921 bp -> refcnt++;
922 rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
923 return 1;
926 int option_state_dereference (ptr, file, line)
927 struct option_state **ptr;
928 const char *file;
929 int line;
931 int i;
932 struct option_state *options;
934 if (!ptr || !*ptr) {
935 log_error ("%s(%d): null pointer", file, line);
936 #if defined (POINTER_DEBUG)
937 abort ();
938 #else
939 return 0;
940 #endif
943 options = *ptr;
944 *ptr = (struct option_state *)0;
945 --options -> refcnt;
946 rc_register (file, line, ptr, options, options -> refcnt, 1, RC_MISC);
947 if (options -> refcnt > 0)
948 return 1;
950 if (options -> refcnt < 0) {
951 log_error ("%s(%d): negative refcnt!", file, line);
952 #if defined (DEBUG_RC_HISTORY)
953 dump_rc_history (options);
954 #endif
955 #if defined (POINTER_DEBUG)
956 abort ();
957 #else
958 return 0;
959 #endif
962 /* Loop through the per-universe state. */
963 for (i = 0; i < options -> universe_count; i++)
964 if (options -> universes [i] &&
965 universes [i] -> option_state_dereference)
966 ((*(universes [i] -> option_state_dereference))
967 (universes [i], options, file, line));
968 dfree (options, file, line);
969 return 1;
972 int executable_statement_allocate (ptr, file, line)
973 struct executable_statement **ptr;
974 const char *file;
975 int line;
977 struct executable_statement *bp;
979 bp = dmalloc (sizeof *bp, file, line);
980 if (!bp)
981 return 0;
982 memset (bp, 0, sizeof *bp);
983 return executable_statement_reference (ptr, bp, file, line);
986 int executable_statement_reference (ptr, bp, file, line)
987 struct executable_statement **ptr;
988 struct executable_statement *bp;
989 const char *file;
990 int line;
992 if (!ptr) {
993 log_error ("%s(%d): null pointer", file, line);
994 #if defined (POINTER_DEBUG)
995 abort ();
996 #else
997 return 0;
998 #endif
1000 if (*ptr) {
1001 log_error ("%s(%d): non-null pointer", file, line);
1002 #if defined (POINTER_DEBUG)
1003 abort ();
1004 #else
1005 *ptr = (struct executable_statement *)0;
1006 #endif
1008 *ptr = bp;
1009 bp -> refcnt++;
1010 rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
1011 return 1;
1014 static struct packet *free_packets;
1016 #if defined (DEBUG_MEMORY_LEAKAGE) || \
1017 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
1018 void relinquish_free_packets ()
1020 struct packet *p, *n;
1021 for (p = free_packets; p; p = n) {
1022 n = (struct packet *)(p -> raw);
1023 dfree (p, MDL);
1025 free_packets = (struct packet *)0;
1027 #endif
1029 int packet_allocate (ptr, file, line)
1030 struct packet **ptr;
1031 const char *file;
1032 int line;
1034 struct packet *p;
1036 if (!ptr) {
1037 log_error ("%s(%d): null pointer", file, line);
1038 #if defined (POINTER_DEBUG)
1039 abort ();
1040 #else
1041 return 0;
1042 #endif
1044 if (*ptr) {
1045 log_error ("%s(%d): non-null pointer", file, line);
1046 #if defined (POINTER_DEBUG)
1047 abort ();
1048 #else
1049 *ptr = (struct packet *)0;
1050 #endif
1053 if (free_packets) {
1054 p = free_packets;
1055 free_packets = (struct packet *)(p -> raw);
1056 dmalloc_reuse (p, file, line, 1);
1057 } else {
1058 p = dmalloc (sizeof *p, file, line);
1060 if (p) {
1061 memset (p, 0, sizeof *p);
1062 return packet_reference (ptr, p, file, line);
1064 return 0;
1067 int packet_reference (ptr, bp, file, line)
1068 struct packet **ptr;
1069 struct packet *bp;
1070 const char *file;
1071 int line;
1073 if (!ptr) {
1074 log_error ("%s(%d): null pointer", file, line);
1075 #if defined (POINTER_DEBUG)
1076 abort ();
1077 #else
1078 return 0;
1079 #endif
1081 if (*ptr) {
1082 log_error ("%s(%d): non-null pointer", file, line);
1083 #if defined (POINTER_DEBUG)
1084 abort ();
1085 #else
1086 *ptr = (struct packet *)0;
1087 #endif
1089 *ptr = bp;
1090 bp -> refcnt++;
1091 rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
1092 return 1;
1095 int packet_dereference (ptr, file, line)
1096 struct packet **ptr;
1097 const char *file;
1098 int line;
1100 int i;
1101 struct packet *packet;
1103 if (!ptr || !*ptr) {
1104 log_error ("%s(%d): null pointer", file, line);
1105 #if defined (POINTER_DEBUG)
1106 abort ();
1107 #else
1108 return 0;
1109 #endif
1112 packet = *ptr;
1113 *ptr = (struct packet *)0;
1114 --packet -> refcnt;
1115 rc_register (file, line, ptr, packet, packet -> refcnt, 1, RC_MISC);
1116 if (packet -> refcnt > 0)
1117 return 1;
1119 if (packet -> refcnt < 0) {
1120 log_error ("%s(%d): negative refcnt!", file, line);
1121 #if defined (DEBUG_RC_HISTORY)
1122 dump_rc_history (packet);
1123 #endif
1124 #if defined (POINTER_DEBUG)
1125 abort ();
1126 #else
1127 return 0;
1128 #endif
1131 if (packet -> options)
1132 option_state_dereference (&packet -> options, file, line);
1133 if (packet -> interface)
1134 interface_dereference (&packet -> interface, MDL);
1135 if (packet -> shared_network)
1136 shared_network_dereference (&packet -> shared_network, MDL);
1137 for (i = 0; i < packet -> class_count && i < PACKET_MAX_CLASSES; i++) {
1138 if (packet -> classes [i])
1139 omapi_object_dereference ((omapi_object_t **)
1140 &packet -> classes [i], MDL);
1142 packet -> raw = (struct dhcp_packet *)free_packets;
1143 free_packets = packet;
1144 dmalloc_reuse (free_packets, (char *)0, 0, 0);
1145 return 1;
1148 int dns_zone_allocate (ptr, file, line)
1149 struct dns_zone **ptr;
1150 const char *file;
1151 int line;
1153 struct dns_zone *d;
1155 if (!ptr) {
1156 log_error ("%s(%d): null pointer", file, line);
1157 #if defined (POINTER_DEBUG)
1158 abort ();
1159 #else
1160 return 0;
1161 #endif
1163 if (*ptr) {
1164 log_error ("%s(%d): non-null pointer", file, line);
1165 #if defined (POINTER_DEBUG)
1166 abort ();
1167 #else
1168 *ptr = (struct dns_zone *)0;
1169 #endif
1172 d = dmalloc (sizeof *d, file, line);
1173 if (d) {
1174 memset (d, 0, sizeof *d);
1175 return dns_zone_reference (ptr, d, file, line);
1177 return 0;
1180 int dns_zone_reference (ptr, bp, file, line)
1181 struct dns_zone **ptr;
1182 struct dns_zone *bp;
1183 const char *file;
1184 int line;
1186 if (!ptr) {
1187 log_error ("%s(%d): null pointer", file, line);
1188 #if defined (POINTER_DEBUG)
1189 abort ();
1190 #else
1191 return 0;
1192 #endif
1194 if (*ptr) {
1195 log_error ("%s(%d): non-null pointer", file, line);
1196 #if defined (POINTER_DEBUG)
1197 abort ();
1198 #else
1199 *ptr = (struct dns_zone *)0;
1200 #endif
1202 *ptr = bp;
1203 bp -> refcnt++;
1204 rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
1205 return 1;
1208 int binding_scope_allocate (ptr, file, line)
1209 struct binding_scope **ptr;
1210 const char *file;
1211 int line;
1213 struct binding_scope *bp;
1215 if (!ptr) {
1216 log_error ("%s(%d): null pointer", file, line);
1217 #if defined (POINTER_DEBUG)
1218 abort ();
1219 #else
1220 return 0;
1221 #endif
1224 if (*ptr) {
1225 log_error ("%s(%d): non-null pointer", file, line);
1226 #if defined (POINTER_DEBUG)
1227 abort ();
1228 #else
1229 return 0;
1230 #endif
1233 bp = dmalloc (sizeof *bp, file, line);
1234 if (!bp)
1235 return 0;
1236 memset (bp, 0, sizeof *bp);
1237 binding_scope_reference (ptr, bp, file, line);
1238 return 1;
1241 int binding_scope_reference (ptr, bp, file, line)
1242 struct binding_scope **ptr;
1243 struct binding_scope *bp;
1244 const char *file;
1245 int line;
1247 if (!ptr) {
1248 log_error ("%s(%d): null pointer", file, line);
1249 #if defined (POINTER_DEBUG)
1250 abort ();
1251 #else
1252 return 0;
1253 #endif
1255 if (*ptr) {
1256 log_error ("%s(%d): non-null pointer", file, line);
1257 #if defined (POINTER_DEBUG)
1258 abort ();
1259 #else
1260 *ptr = (struct binding_scope *)0;
1261 #endif
1263 *ptr = bp;
1264 bp -> refcnt++;
1265 rc_register (file, line, ptr, bp, bp -> refcnt, 0, RC_MISC);
1266 return 1;
1269 /* Make a copy of the data in data_string, upping the buffer reference
1270 count if there's a buffer. */
1272 void data_string_copy (dest, src, file, line)
1273 struct data_string *dest;
1274 struct data_string *src;
1275 const char *file;
1276 int line;
1278 if (src -> buffer)
1279 buffer_reference (&dest -> buffer, src -> buffer, file, line);
1280 dest -> data = src -> data;
1281 dest -> terminated = src -> terminated;
1282 dest -> len = src -> len;
1285 /* Release the reference count to a data string's buffer (if any) and
1286 zero out the other information, yielding the null data string. */
1288 void data_string_forget (data, file, line)
1289 struct data_string *data;
1290 const char *file;
1291 int line;
1293 if (data -> buffer)
1294 buffer_dereference (&data -> buffer, file, line);
1295 memset (data, 0, sizeof *data);
1298 /* Make a copy of the data in data_string, upping the buffer reference
1299 count if there's a buffer. */
1301 void data_string_truncate (dp, len)
1302 struct data_string *dp;
1303 int len;
1305 if (len < dp -> len) {
1306 dp -> terminated = 0;
1307 dp -> len = len;