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