]> git.ipfire.org Git - thirdparty/dhcp.git/blob - common/alloc.c
- Fix all the OMAPI objects in the DHCP server and client that weren't
[thirdparty/dhcp.git] / common / alloc.c
1 /* alloc.c
2
3 Memory allocation... */
4
5 /*
6 * Copyright (c) 1996-2000 Internet Software Consortium.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
38 * To learn more about the Internet Software Consortium, see
39 * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
40 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
41 * ``http://www.nominum.com''.
42 */
43
44 #ifndef lint
45 static char copyright[] =
46 "$Id: alloc.c,v 1.47 2000/05/16 23:02:08 mellon Exp $ Copyright (c) 1996-2000 The Internet Software Consortium. All rights reserved.\n";
47 #endif /* not lint */
48
49 #include "dhcpd.h"
50 #include <omapip/omapip_p.h>
51
52 struct dhcp_packet *dhcp_free_list;
53 struct packet *packet_free_list;
54
55 OMAPI_OBJECT_ALLOC (subnet, struct subnet, dhcp_type_subnet)
56 OMAPI_OBJECT_ALLOC (shared_network, struct shared_network,
57 dhcp_type_shared_network)
58 OMAPI_OBJECT_ALLOC (group_object, struct group_object, dhcp_type_group)
59
60 int group_allocate (ptr, file, line)
61 struct group **ptr;
62 const char *file;
63 int line;
64 {
65 int size;
66
67 if (!ptr) {
68 log_error ("%s(%d): null pointer", file, line);
69 #if defined (POINTER_DEBUG)
70 abort ();
71 #else
72 return 0;
73 #endif
74 }
75 if (*ptr) {
76 log_error ("%s(%d): non-null pointer", file, line);
77 #if defined (POINTER_DEBUG)
78 abort ();
79 #else
80 *ptr = (struct group *)0;
81 #endif
82 }
83
84 *ptr = dmalloc (sizeof **ptr, file, line);
85 if (*ptr) {
86 memset (*ptr, 0, sizeof **ptr);
87 (*ptr) -> refcnt = 1;
88 return 1;
89 }
90 return 0;
91 }
92
93 int group_reference (ptr, bp, file, line)
94 struct group **ptr;
95 struct group *bp;
96 const char *file;
97 int line;
98 {
99 if (!ptr) {
100 log_error ("%s(%d): null pointer", file, line);
101 #if defined (POINTER_DEBUG)
102 abort ();
103 #else
104 return 0;
105 #endif
106 }
107 if (*ptr) {
108 log_error ("%s(%d): non-null pointer", file, line);
109 #if defined (POINTER_DEBUG)
110 abort ();
111 #else
112 *ptr = (struct group *)0;
113 #endif
114 }
115 *ptr = bp;
116 bp -> refcnt++;
117 rc_register (file, line, ptr, bp, bp -> refcnt);
118 dmalloc_reuse (bp, file, line, 1);
119 return 1;
120 }
121
122 int group_dereference (ptr, file, line)
123 struct group **ptr;
124 const char *file;
125 int line;
126 {
127 int i;
128 struct group *group;
129
130 if (!ptr || !*ptr) {
131 log_error ("%s(%d): null pointer", file, line);
132 #if defined (POINTER_DEBUG)
133 abort ();
134 #else
135 return 0;
136 #endif
137 }
138
139 group = *ptr;
140 *ptr = (struct group *)0;
141 --group -> refcnt;
142 rc_register (file, line, ptr, group, group -> refcnt);
143 if (group -> refcnt > 0)
144 return 1;
145
146 if (group -> refcnt < 0) {
147 log_error ("%s(%d): negative refcnt!", file, line);
148 #if defined (DEBUG_RC_HISTORY)
149 dump_rc_history ();
150 #endif
151 #if defined (POINTER_DEBUG)
152 abort ();
153 #else
154 return 0;
155 #endif
156 }
157
158 if (group -> object)
159 group_object_dereference (&group -> object, MDL);
160 if (group -> subnet)
161 subnet_dereference (&group -> subnet, MDL);
162 if (group -> shared_network)
163 shared_network_dereference (&group -> shared_network, MDL);
164 if (group -> statements)
165 executable_statement_dereference (&group -> statements, MDL);
166 dfree (group, file, line);
167 return 1;
168 }
169
170 struct dhcp_packet *new_dhcp_packet (file, line)
171 const char *file;
172 int line;
173 {
174 struct dhcp_packet *rval;
175 rval = (struct dhcp_packet *)dmalloc (sizeof (struct dhcp_packet),
176 file, line);
177 return rval;
178 }
179
180 struct hash_table *new_hash_table (count, file, line)
181 int count;
182 const char *file;
183 int line;
184 {
185 struct hash_table *rval = dmalloc (sizeof (struct hash_table)
186 - (DEFAULT_HASH_SIZE
187 * sizeof (struct hash_bucket *))
188 + (count
189 * sizeof (struct hash_bucket *)),
190 file, line);
191 rval -> hash_count = count;
192 return rval;
193 }
194
195 struct hash_bucket *new_hash_bucket (file, line)
196 const char *file;
197 int line;
198 {
199 struct hash_bucket *rval = dmalloc (sizeof (struct hash_bucket),
200 file, line);
201 return rval;
202 }
203
204 void free_hash_bucket (ptr, file, line)
205 struct hash_bucket *ptr;
206 const char *file;
207 int line;
208 {
209 dfree ((VOIDPTR)ptr, file, line);
210 }
211
212 struct protocol *new_protocol (file, line)
213 const char *file;
214 int line;
215 {
216 struct protocol *rval = dmalloc (sizeof (struct protocol), file, line);
217 return rval;
218 }
219
220 struct domain_search_list *new_domain_search_list (file, line)
221 const char *file;
222 int line;
223 {
224 struct domain_search_list *rval =
225 dmalloc (sizeof (struct domain_search_list), file, line);
226 return rval;
227 }
228
229 struct name_server *new_name_server (file, line)
230 const char *file;
231 int line;
232 {
233 struct name_server *rval =
234 dmalloc (sizeof (struct name_server), file, line);
235 return rval;
236 }
237
238 void free_name_server (ptr, file, line)
239 struct name_server *ptr;
240 const char *file;
241 int line;
242 {
243 dfree ((VOIDPTR)ptr, file, line);
244 }
245
246 struct option *new_option (file, line)
247 const char *file;
248 int line;
249 {
250 struct option *rval =
251 dmalloc (sizeof (struct option), file, line);
252 if (rval)
253 memset (rval, 0, sizeof *rval);
254 return rval;
255 }
256
257 void free_option (ptr, file, line)
258 struct option *ptr;
259 const char *file;
260 int line;
261 {
262 /* XXX have to put all options on heap before this is possible. */
263 #if 0
264 if (ptr -> name)
265 dfree ((VOIDPTR)option -> name, file, line);
266 dfree ((VOIDPTR)ptr, file, line);
267 #endif
268 }
269
270 struct universe *new_universe (file, line)
271 const char *file;
272 int line;
273 {
274 struct universe *rval =
275 dmalloc (sizeof (struct universe), file, line);
276 return rval;
277 }
278
279 void free_universe (ptr, file, line)
280 struct universe *ptr;
281 const char *file;
282 int line;
283 {
284 dfree ((VOIDPTR)ptr, file, line);
285 }
286
287 void free_domain_search_list (ptr, file, line)
288 struct domain_search_list *ptr;
289 const char *file;
290 int line;
291 {
292 dfree ((VOIDPTR)ptr, file, line);
293 }
294
295 void free_protocol (ptr, file, line)
296 struct protocol *ptr;
297 const char *file;
298 int line;
299 {
300 dfree ((VOIDPTR)ptr, file, line);
301 }
302
303 void free_hash_table (ptr, file, line)
304 struct hash_table *ptr;
305 const char *file;
306 int line;
307 {
308 dfree ((VOIDPTR)ptr, file, line);
309 }
310
311 void free_dhcp_packet (ptr, file, line)
312 struct dhcp_packet *ptr;
313 const char *file;
314 int line;
315 {
316 dfree ((VOIDPTR)ptr, file, line);
317 }
318
319 struct client_lease *new_client_lease (file, line)
320 const char *file;
321 int line;
322 {
323 return (struct client_lease *)dmalloc (sizeof (struct client_lease),
324 file, line);
325 }
326
327 void free_client_lease (lease, file, line)
328 struct client_lease *lease;
329 const char *file;
330 int line;
331 {
332 dfree (lease, file, line);
333 }
334
335 struct auth_key *new_auth_key (len, file, line)
336 unsigned len;
337 const char *file;
338 int line;
339 {
340 struct auth_key *peer;
341 unsigned size = len - 1 + sizeof (struct auth_key);
342
343 peer = (struct auth_key *)dmalloc (size, file, line);
344 if (!peer)
345 return peer;
346 memset (peer, 0, size);
347 return peer;
348 }
349
350 void free_auth_key (peer, file, line)
351 struct auth_key *peer;
352 const char *file;
353 int line;
354 {
355 dfree (peer, file, line);
356 }
357
358 pair free_pairs;
359
360 pair new_pair (file, line)
361 const char *file;
362 int line;
363 {
364 pair foo;
365
366 if (free_pairs) {
367 foo = free_pairs;
368 free_pairs = foo -> cdr;
369 memset (foo, 0, sizeof *foo);
370 dmalloc_reuse (foo, file, line, 0);
371 return foo;
372 }
373
374 foo = dmalloc (sizeof *foo, file, line);
375 if (!foo)
376 return foo;
377 memset (foo, 0, sizeof *foo);
378 return foo;
379 }
380
381 void free_pair (foo, file, line)
382 pair foo;
383 const char *file;
384 int line;
385 {
386 foo -> cdr = free_pairs;
387 free_pairs = foo;
388 dmalloc_reuse (free_pairs, (char *)0, 0, 0);
389 }
390
391 struct expression *free_expressions;
392
393 int expression_allocate (cptr, file, line)
394 struct expression **cptr;
395 const char *file;
396 int line;
397 {
398 struct expression *rval;
399
400 if (free_expressions) {
401 rval = free_expressions;
402 free_expressions = rval -> data.not;
403 } else {
404 rval = dmalloc (sizeof (struct expression), file, line);
405 if (!rval)
406 return 0;
407 }
408 memset (rval, 0, sizeof *rval);
409 return expression_reference (cptr, rval, file, line);
410 }
411
412 int expression_reference (ptr, src, file, line)
413 struct expression **ptr;
414 struct expression *src;
415 const char *file;
416 int line;
417 {
418 if (!ptr) {
419 log_error ("%s(%d): null pointer", file, line);
420 #if defined (POINTER_DEBUG)
421 abort ();
422 #else
423 return 0;
424 #endif
425 }
426 if (*ptr) {
427 log_error ("%s(%d): non-null pointer", file, line);
428 #if defined (POINTER_DEBUG)
429 abort ();
430 #else
431 *ptr = (struct expression *)0;
432 #endif
433 }
434 *ptr = src;
435 src -> refcnt++;
436 rc_register (file, line, ptr, src, src -> refcnt);
437 dmalloc_reuse (src, file, line, 1);
438 return 1;
439 }
440
441 void free_expression (expr, file, line)
442 struct expression *expr;
443 const char *file;
444 int line;
445 {
446 expr -> data.not = free_expressions;
447 free_expressions = expr;
448 dmalloc_reuse (free_expressions, (char *)0, 0, 0);
449 }
450
451 struct binding_value *free_binding_values;
452
453 int binding_value_allocate (cptr, file, line)
454 struct binding_value **cptr;
455 const char *file;
456 int line;
457 {
458 struct binding_value *rval;
459
460 if (free_binding_values) {
461 rval = free_binding_values;
462 free_binding_values = rval -> value.bv;
463 } else {
464 rval = dmalloc (sizeof (struct binding_value), file, line);
465 if (!rval)
466 return 0;
467 }
468 memset (rval, 0, sizeof *rval);
469 return binding_value_reference (cptr, rval, file, line);
470 }
471
472 int binding_value_reference (ptr, src, file, line)
473 struct binding_value **ptr;
474 struct binding_value *src;
475 const char *file;
476 int line;
477 {
478 if (!ptr) {
479 log_error ("%s(%d): null pointer", file, line);
480 #if defined (POINTER_DEBUG)
481 abort ();
482 #else
483 return 0;
484 #endif
485 }
486 if (*ptr) {
487 log_error ("%s(%d): non-null pointer", file, line);
488 #if defined (POINTER_DEBUG)
489 abort ();
490 #else
491 *ptr = (struct binding_value *)0;
492 #endif
493 }
494 *ptr = src;
495 src -> refcnt++;
496 rc_register (file, line, ptr, src, src -> refcnt);
497 dmalloc_reuse (src, file, line, 1);
498 return 1;
499 }
500
501 void free_binding_value (bv, file, line)
502 struct binding_value *bv;
503 const char *file;
504 int line;
505 {
506 bv -> value.bv = free_binding_values;
507 free_binding_values = bv;
508 dmalloc_reuse (free_binding_values, (char *)0, 0, 0);
509 }
510
511 int fundef_allocate (cptr, file, line)
512 struct fundef **cptr;
513 const char *file;
514 int line;
515 {
516 struct fundef *rval;
517
518 rval = dmalloc (sizeof (struct fundef), file, line);
519 if (!rval)
520 return 0;
521 memset (rval, 0, sizeof *rval);
522 return fundef_reference (cptr, rval, file, line);
523 }
524
525 int fundef_reference (ptr, src, file, line)
526 struct fundef **ptr;
527 struct fundef *src;
528 const char *file;
529 int line;
530 {
531 if (!ptr) {
532 log_error ("%s(%d): null pointer", file, line);
533 #if defined (POINTER_DEBUG)
534 abort ();
535 #else
536 return 0;
537 #endif
538 }
539 if (*ptr) {
540 log_error ("%s(%d): non-null pointer", file, line);
541 #if defined (POINTER_DEBUG)
542 abort ();
543 #else
544 *ptr = (struct fundef *)0;
545 #endif
546 }
547 *ptr = src;
548 src -> refcnt++;
549 rc_register (file, line, ptr, src, src -> refcnt);
550 dmalloc_reuse (src, file, line, 1);
551 return 1;
552 }
553
554 struct option_cache *free_option_caches;
555
556 int option_cache_allocate (cptr, file, line)
557 struct option_cache **cptr;
558 const char *file;
559 int line;
560 {
561 struct option_cache *rval;
562
563 if (free_option_caches) {
564 rval = free_option_caches;
565 free_option_caches =
566 (struct option_cache *)(rval -> expression);
567 dmalloc_reuse (rval, file, line, 0);
568 } else {
569 rval = dmalloc (sizeof (struct option_cache), file, line);
570 if (!rval)
571 return 0;
572 }
573 memset (rval, 0, sizeof *rval);
574 return option_cache_reference (cptr, rval, file, line);
575 }
576
577 int option_cache_reference (ptr, src, file, line)
578 struct option_cache **ptr;
579 struct option_cache *src;
580 const char *file;
581 int line;
582 {
583 if (!ptr) {
584 log_error ("%s(%d): null pointer", file, line);
585 #if defined (POINTER_DEBUG)
586 abort ();
587 #else
588 return 0;
589 #endif
590 }
591 if (*ptr) {
592 log_error ("%s(%d): non-null pointer", file, line);
593 #if defined (POINTER_DEBUG)
594 abort ();
595 #else
596 *ptr = (struct option_cache *)0;
597 #endif
598 }
599 *ptr = src;
600 src -> refcnt++;
601 rc_register (file, line, ptr, src, src -> refcnt);
602 dmalloc_reuse (src, file, line, 1);
603 return 1;
604 }
605
606 int buffer_allocate (ptr, len, file, line)
607 struct buffer **ptr;
608 unsigned len;
609 const char *file;
610 int line;
611 {
612 struct buffer *bp;
613
614 bp = dmalloc (len + sizeof *bp, file, line);
615 if (!bp)
616 return 0;
617 memset (bp, 0, sizeof *bp);
618 bp -> refcnt = 0;
619 return buffer_reference (ptr, bp, file, line);
620 }
621
622 int buffer_reference (ptr, bp, file, line)
623 struct buffer **ptr;
624 struct buffer *bp;
625 const char *file;
626 int line;
627 {
628 if (!ptr) {
629 log_error ("%s(%d): null pointer", file, line);
630 #if defined (POINTER_DEBUG)
631 abort ();
632 #else
633 return 0;
634 #endif
635 }
636 if (*ptr) {
637 log_error ("%s(%d): non-null pointer", file, line);
638 #if defined (POINTER_DEBUG)
639 abort ();
640 #else
641 *ptr = (struct buffer *)0;
642 #endif
643 }
644 *ptr = bp;
645 bp -> refcnt++;
646 rc_register (file, line, ptr, bp, bp -> refcnt);
647 dmalloc_reuse (bp, file, line, 1);
648 return 1;
649 }
650
651 int buffer_dereference (ptr, file, line)
652 struct buffer **ptr;
653 const char *file;
654 int line;
655 {
656 struct buffer *bp;
657
658 if (!ptr) {
659 log_error ("%s(%d): null pointer", file, line);
660 #if defined (POINTER_DEBUG)
661 abort ();
662 #else
663 return 0;
664 #endif
665 }
666
667 if (!*ptr) {
668 log_error ("%s(%d): null pointer", file, line);
669 #if defined (POINTER_DEBUG)
670 abort ();
671 #else
672 return 0;
673 #endif
674 }
675
676 (*ptr) -> refcnt--;
677 rc_register (file, line, ptr, *ptr, (*ptr) -> refcnt);
678 if (!(*ptr) -> refcnt)
679 dfree ((*ptr), file, line);
680 if ((*ptr) -> refcnt < 0) {
681 log_error ("%s(%d): negative refcnt!", file, line);
682 #if defined (DEBUG_RC_HISTORY)
683 dump_rc_history ();
684 #endif
685 #if defined (POINTER_DEBUG)
686 abort ();
687 #else
688 return 0;
689 #endif
690 }
691 *ptr = (struct buffer *)0;
692 return 1;
693 }
694
695 int dns_host_entry_allocate (ptr, hostname, file, line)
696 struct dns_host_entry **ptr;
697 const char *hostname;
698 const char *file;
699 int line;
700 {
701 struct dns_host_entry *bp;
702
703 bp = dmalloc (strlen (hostname) + sizeof *bp, file, line);
704 if (!bp)
705 return 0;
706 memset (bp, 0, sizeof *bp);
707 bp -> refcnt = 0;
708 strcpy (bp -> hostname, hostname);
709 return dns_host_entry_reference (ptr, bp, file, line);
710 }
711
712 int dns_host_entry_reference (ptr, bp, file, line)
713 struct dns_host_entry **ptr;
714 struct dns_host_entry *bp;
715 const char *file;
716 int line;
717 {
718 if (!ptr) {
719 log_error ("%s(%d): null pointer", file, line);
720 #if defined (POINTER_DEBUG)
721 abort ();
722 #else
723 return 0;
724 #endif
725 }
726 if (*ptr) {
727 log_error ("%s(%d): non-null pointer", file, line);
728 #if defined (POINTER_DEBUG)
729 abort ();
730 #else
731 *ptr = (struct dns_host_entry *)0;
732 #endif
733 }
734 *ptr = bp;
735 bp -> refcnt++;
736 rc_register (file, line, ptr, bp, bp -> refcnt);
737 dmalloc_reuse (bp, file, line, 1);
738 return 1;
739 }
740
741 int dns_host_entry_dereference (ptr, file, line)
742 struct dns_host_entry **ptr;
743 const char *file;
744 int line;
745 {
746 struct dns_host_entry *bp;
747
748 if (!ptr || !*ptr) {
749 log_error ("%s(%d): null pointer", file, line);
750 #if defined (POINTER_DEBUG)
751 abort ();
752 #else
753 return 0;
754 #endif
755 }
756
757 (*ptr) -> refcnt--;
758 rc_register (file, line, ptr, bp, bp -> refcnt);
759 if (!(*ptr) -> refcnt)
760 dfree ((*ptr), file, line);
761 if ((*ptr) -> refcnt < 0) {
762 log_error ("%s(%d): negative refcnt!", file, line);
763 #if defined (DEBUG_RC_HISTORY)
764 dump_rc_history ();
765 #endif
766 #if defined (POINTER_DEBUG)
767 abort ();
768 #else
769 return 0;
770 #endif
771 }
772 *ptr = (struct dns_host_entry *)0;
773 return 1;
774 }
775
776 int option_state_allocate (ptr, file, line)
777 struct option_state **ptr;
778 const char *file;
779 int line;
780 {
781 unsigned size;
782
783 if (!ptr) {
784 log_error ("%s(%d): null pointer", file, line);
785 #if defined (POINTER_DEBUG)
786 abort ();
787 #else
788 return 0;
789 #endif
790 }
791 if (*ptr) {
792 log_error ("%s(%d): non-null pointer", file, line);
793 #if defined (POINTER_DEBUG)
794 abort ();
795 #else
796 *ptr = (struct option_state *)0;
797 #endif
798 }
799
800 size = sizeof **ptr + (universe_count - 1) * sizeof (VOIDPTR);
801 *ptr = dmalloc (size, file, line);
802 if (*ptr) {
803 memset (*ptr, 0, size);
804 (*ptr) -> universe_count = universe_count;
805 (*ptr) -> refcnt = 1;
806 rc_register (file, line, ptr, *ptr, (*ptr) -> refcnt);
807 return 1;
808 }
809 return 0;
810 }
811
812 int option_state_reference (ptr, bp, file, line)
813 struct option_state **ptr;
814 struct option_state *bp;
815 const char *file;
816 int line;
817 {
818 if (!ptr) {
819 log_error ("%s(%d): null pointer", file, line);
820 #if defined (POINTER_DEBUG)
821 abort ();
822 #else
823 return 0;
824 #endif
825 }
826 if (*ptr) {
827 log_error ("%s(%d): non-null pointer", file, line);
828 #if defined (POINTER_DEBUG)
829 abort ();
830 #else
831 *ptr = (struct option_state *)0;
832 #endif
833 }
834 *ptr = bp;
835 bp -> refcnt++;
836 rc_register (file, line, ptr, bp, bp -> refcnt);
837 dmalloc_reuse (bp, file, line, 1);
838 return 1;
839 }
840
841 int option_state_dereference (ptr, file, line)
842 struct option_state **ptr;
843 const char *file;
844 int line;
845 {
846 int i;
847 struct option_state *options;
848
849 if (!ptr || !*ptr) {
850 log_error ("%s(%d): null pointer", file, line);
851 #if defined (POINTER_DEBUG)
852 abort ();
853 #else
854 return 0;
855 #endif
856 }
857
858 options = *ptr;
859 *ptr = (struct option_state *)0;
860 --options -> refcnt;
861 rc_register (file, line, ptr, options, options -> refcnt);
862 if (options -> refcnt > 0)
863 return 1;
864
865 if (options -> refcnt < 0) {
866 log_error ("%s(%d): negative refcnt!", file, line);
867 #if defined (DEBUG_RC_HISTORY)
868 dump_rc_history ();
869 #endif
870 #if defined (POINTER_DEBUG)
871 abort ();
872 #else
873 return 0;
874 #endif
875 }
876
877 /* Loop through the per-universe state. */
878 for (i = 0; i < options -> universe_count; i++)
879 if (options -> universes [i] &&
880 universes [i] -> option_state_dereference)
881 ((*(universes [i] -> option_state_dereference))
882 (universes [i], options, file, line));
883 dfree (options, file, line);
884 return 1;
885 }
886
887 int executable_statement_allocate (ptr, file, line)
888 struct executable_statement **ptr;
889 const char *file;
890 int line;
891 {
892 struct executable_statement *bp;
893
894 bp = dmalloc (sizeof *bp, file, line);
895 if (!bp)
896 return 0;
897 memset (bp, 0, sizeof *bp);
898 return executable_statement_reference (ptr, bp, file, line);
899 }
900
901 int executable_statement_reference (ptr, bp, file, line)
902 struct executable_statement **ptr;
903 struct executable_statement *bp;
904 const char *file;
905 int line;
906 {
907 if (!ptr) {
908 log_error ("%s(%d): null pointer", file, line);
909 #if defined (POINTER_DEBUG)
910 abort ();
911 #else
912 return 0;
913 #endif
914 }
915 if (*ptr) {
916 log_error ("%s(%d): non-null pointer", file, line);
917 #if defined (POINTER_DEBUG)
918 abort ();
919 #else
920 *ptr = (struct executable_statement *)0;
921 #endif
922 }
923 *ptr = bp;
924 bp -> refcnt++;
925 rc_register (file, line, ptr, bp, bp -> refcnt);
926 dmalloc_reuse (bp, file, line, 1);
927 return 1;
928 }
929
930 static struct packet *free_packets;
931
932 int packet_allocate (ptr, file, line)
933 struct packet **ptr;
934 const char *file;
935 int line;
936 {
937 int size;
938
939 if (!ptr) {
940 log_error ("%s(%d): null pointer", file, line);
941 #if defined (POINTER_DEBUG)
942 abort ();
943 #else
944 return 0;
945 #endif
946 }
947 if (*ptr) {
948 log_error ("%s(%d): non-null pointer", file, line);
949 #if defined (POINTER_DEBUG)
950 abort ();
951 #else
952 *ptr = (struct packet *)0;
953 #endif
954 }
955
956 if (free_packets) {
957 *ptr = free_packets;
958 free_packets = (struct packet *)((*ptr) -> raw);
959 } else {
960 *ptr = dmalloc (sizeof **ptr, file, line);
961 }
962 if (*ptr) {
963 memset (*ptr, 0, sizeof **ptr);
964 (*ptr) -> refcnt = 1;
965 return 1;
966 }
967 return 0;
968 }
969
970 int packet_reference (ptr, bp, file, line)
971 struct packet **ptr;
972 struct packet *bp;
973 const char *file;
974 int line;
975 {
976 if (!ptr) {
977 log_error ("%s(%d): null pointer", file, line);
978 #if defined (POINTER_DEBUG)
979 abort ();
980 #else
981 return 0;
982 #endif
983 }
984 if (*ptr) {
985 log_error ("%s(%d): non-null pointer", file, line);
986 #if defined (POINTER_DEBUG)
987 abort ();
988 #else
989 *ptr = (struct packet *)0;
990 #endif
991 }
992 *ptr = bp;
993 bp -> refcnt++;
994 rc_register (file, line, ptr, bp, bp -> refcnt);
995 dmalloc_reuse (bp, file, line, 1);
996 return 1;
997 }
998
999 int packet_dereference (ptr, file, line)
1000 struct packet **ptr;
1001 const char *file;
1002 int line;
1003 {
1004 int i;
1005 struct packet *packet;
1006
1007 if (!ptr || !*ptr) {
1008 log_error ("%s(%d): null pointer", file, line);
1009 #if defined (POINTER_DEBUG)
1010 abort ();
1011 #else
1012 return 0;
1013 #endif
1014 }
1015
1016 packet = *ptr;
1017 *ptr = (struct packet *)0;
1018 --packet -> refcnt;
1019 rc_register (file, line, ptr, packet, packet -> refcnt);
1020 if (packet -> refcnt > 0)
1021 return 1;
1022
1023 if (packet -> refcnt < 0) {
1024 log_error ("%s(%d): negative refcnt!", file, line);
1025 #if defined (DEBUG_RC_HISTORY)
1026 dump_rc_history ();
1027 #endif
1028 #if defined (POINTER_DEBUG)
1029 abort ();
1030 #else
1031 return 0;
1032 #endif
1033 }
1034
1035 if (packet -> options)
1036 option_state_dereference (&packet -> options, file, line);
1037 if (packet -> interface)
1038 interface_dereference (&packet -> interface, MDL);
1039 packet -> raw = (struct dhcp_packet *)free_packets;
1040 free_packets = packet;
1041 dmalloc_reuse (free_packets, (char *)0, 0, 0);
1042 return 1;
1043 }
1044
1045 int tsig_key_allocate (ptr, file, line)
1046 struct tsig_key **ptr;
1047 const char *file;
1048 int line;
1049 {
1050 int size;
1051
1052 if (!ptr) {
1053 log_error ("%s(%d): null pointer", file, line);
1054 #if defined (POINTER_DEBUG)
1055 abort ();
1056 #else
1057 return 0;
1058 #endif
1059 }
1060 if (*ptr) {
1061 log_error ("%s(%d): non-null pointer", file, line);
1062 #if defined (POINTER_DEBUG)
1063 abort ();
1064 #else
1065 *ptr = (struct tsig_key *)0;
1066 #endif
1067 }
1068
1069 *ptr = dmalloc (sizeof **ptr, file, line);
1070 if (*ptr) {
1071 memset (*ptr, 0, sizeof **ptr);
1072 (*ptr) -> refcnt = 1;
1073 return 1;
1074 }
1075 return 0;
1076 }
1077
1078 int tsig_key_reference (ptr, bp, file, line)
1079 struct tsig_key **ptr;
1080 struct tsig_key *bp;
1081 const char *file;
1082 int line;
1083 {
1084 if (!ptr) {
1085 log_error ("%s(%d): null pointer", file, line);
1086 #if defined (POINTER_DEBUG)
1087 abort ();
1088 #else
1089 return 0;
1090 #endif
1091 }
1092 if (*ptr) {
1093 log_error ("%s(%d): non-null pointer", file, line);
1094 #if defined (POINTER_DEBUG)
1095 abort ();
1096 #else
1097 *ptr = (struct tsig_key *)0;
1098 #endif
1099 }
1100 *ptr = bp;
1101 bp -> refcnt++;
1102 rc_register (file, line, ptr, bp, bp -> refcnt);
1103 dmalloc_reuse (bp, file, line, 1);
1104 return 1;
1105 }
1106
1107 int tsig_key_dereference (ptr, file, line)
1108 struct tsig_key **ptr;
1109 const char *file;
1110 int line;
1111 {
1112 int i;
1113 struct tsig_key *tsig_key;
1114
1115 if (!ptr || !*ptr) {
1116 log_error ("%s(%d): null pointer", file, line);
1117 #if defined (POINTER_DEBUG)
1118 abort ();
1119 #else
1120 return 0;
1121 #endif
1122 }
1123
1124 tsig_key = *ptr;
1125 *ptr = (struct tsig_key *)0;
1126 --tsig_key -> refcnt;
1127 rc_register (file, line, ptr, tsig_key, tsig_key -> refcnt);
1128 if (tsig_key -> refcnt > 0)
1129 return 1;
1130
1131 if (tsig_key -> refcnt < 0) {
1132 log_error ("%s(%d): negative refcnt!", file, line);
1133 #if defined (DEBUG_RC_HISTORY)
1134 dump_rc_history ();
1135 #endif
1136 #if defined (POINTER_DEBUG)
1137 abort ();
1138 #else
1139 return 0;
1140 #endif
1141 }
1142
1143 if (tsig_key -> name)
1144 dfree (tsig_key -> name, file, line);
1145 if (tsig_key -> algorithm)
1146 dfree (tsig_key -> algorithm, file, line);
1147 if (tsig_key -> key.buffer)
1148 data_string_forget (&tsig_key -> key, file, line);
1149 dfree (tsig_key, file, line);
1150 return 1;
1151 }
1152
1153 int dns_zone_allocate (ptr, file, line)
1154 struct dns_zone **ptr;
1155 const char *file;
1156 int line;
1157 {
1158 int size;
1159
1160 if (!ptr) {
1161 log_error ("%s(%d): null pointer", file, line);
1162 #if defined (POINTER_DEBUG)
1163 abort ();
1164 #else
1165 return 0;
1166 #endif
1167 }
1168 if (*ptr) {
1169 log_error ("%s(%d): non-null pointer", file, line);
1170 #if defined (POINTER_DEBUG)
1171 abort ();
1172 #else
1173 *ptr = (struct dns_zone *)0;
1174 #endif
1175 }
1176
1177 *ptr = dmalloc (sizeof **ptr, file, line);
1178 if (*ptr) {
1179 memset (*ptr, 0, sizeof **ptr);
1180 (*ptr) -> refcnt = 1;
1181 return 1;
1182 }
1183 return 0;
1184 }
1185
1186 int dns_zone_reference (ptr, bp, file, line)
1187 struct dns_zone **ptr;
1188 struct dns_zone *bp;
1189 const char *file;
1190 int line;
1191 {
1192 if (!ptr) {
1193 log_error ("%s(%d): null pointer", file, line);
1194 #if defined (POINTER_DEBUG)
1195 abort ();
1196 #else
1197 return 0;
1198 #endif
1199 }
1200 if (*ptr) {
1201 log_error ("%s(%d): non-null pointer", file, line);
1202 #if defined (POINTER_DEBUG)
1203 abort ();
1204 #else
1205 *ptr = (struct dns_zone *)0;
1206 #endif
1207 }
1208 *ptr = bp;
1209 bp -> refcnt++;
1210 rc_register (file, line, ptr, bp, bp -> refcnt);
1211 dmalloc_reuse (bp, file, line, 1);
1212 return 1;
1213 }
1214
1215 int binding_scope_allocate (ptr, file, line)
1216 struct binding_scope **ptr;
1217 const char *file;
1218 int line;
1219 {
1220 struct binding_scope *bp;
1221
1222 if (!ptr) {
1223 log_error ("%s(%d): null pointer", file, line);
1224 #if defined (POINTER_DEBUG)
1225 abort ();
1226 #else
1227 return 0;
1228 #endif
1229 }
1230
1231 if (*ptr) {
1232 log_error ("%s(%d): non-null pointer", file, line);
1233 #if defined (POINTER_DEBUG)
1234 abort ();
1235 #else
1236 return 0;
1237 #endif
1238 }
1239
1240 bp = dmalloc (sizeof *bp, file, line);
1241 if (!bp)
1242 return 0;
1243 memset (bp, 0, sizeof *bp);
1244 *ptr = bp;
1245 return 1;
1246 }
1247
1248 /* Make a copy of the data in data_string, upping the buffer reference
1249 count if there's a buffer. */
1250
1251 void data_string_copy (dest, src, file, line)
1252 struct data_string *dest;
1253 struct data_string *src;
1254 const char *file;
1255 int line;
1256 {
1257 if (src -> buffer)
1258 buffer_reference (&dest -> buffer, src -> buffer, file, line);
1259 dest -> data = src -> data;
1260 dest -> terminated = src -> terminated;
1261 dest -> len = src -> len;
1262 }
1263
1264 /* Release the reference count to a data string's buffer (if any) and
1265 zero out the other information, yielding the null data string. */
1266
1267 void data_string_forget (data, file, line)
1268 struct data_string *data;
1269 const char *file;
1270 int line;
1271 {
1272 if (data -> buffer)
1273 buffer_dereference (&data -> buffer, file, line);
1274 memset (data, 0, sizeof *data);
1275 }
1276
1277 /* Make a copy of the data in data_string, upping the buffer reference
1278 count if there's a buffer. */
1279
1280 void data_string_truncate (dp, len)
1281 struct data_string *dp;
1282 int len;
1283 {
1284 if (len < dp -> len) {
1285 dp -> terminated = 0;
1286 dp -> len = len;
1287 }
1288 }