alloc.c 23.2 KB
Newer Older
Ted Lemon's avatar
Ted Lemon committed
1 2
/* alloc.c

3
   Memory allocation... */
Ted Lemon's avatar
Ted Lemon committed
4 5

/*
Ted Lemon's avatar
Ted Lemon committed
6 7
 * Copyright (c) 1996-2000 Internet Software Consortium.
 * All rights reserved.
Ted Lemon's avatar
Ted Lemon committed
8
 *
Ted Lemon's avatar
Ted Lemon committed
9 10 11
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
Ted Lemon's avatar
Ted Lemon committed
12
 *
Ted Lemon's avatar
Ted Lemon committed
13 14 15 16 17 18 19 20
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of The Internet Software Consortium nor the names
 *    of its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
Ted Lemon's avatar
Ted Lemon committed
21
 *
Ted Lemon's avatar
Ted Lemon committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * This software has been written for the Internet Software Consortium
 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
 * To learn more about the Internet Software Consortium, see
 * ``http://www.isc.org/''.  To learn more about Vixie Enterprises,
 * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
 * ``http://www.nominum.com''.
Ted Lemon's avatar
Ted Lemon committed
42 43 44 45
 */

#ifndef lint
static char copyright[] =
46
"$Id: alloc.c,v 1.52 2000/08/03 20:59:33 neild Exp $ Copyright (c) 1996-2000 The Internet Software Consortium.  All rights reserved.\n";
Ted Lemon's avatar
Ted Lemon committed
47 48 49
#endif /* not lint */

#include "dhcpd.h"
50
#include <omapip/omapip_p.h>
Ted Lemon's avatar
Ted Lemon committed
51 52 53 54

struct dhcp_packet *dhcp_free_list;
struct packet *packet_free_list;

55 56 57 58
OMAPI_OBJECT_ALLOC (subnet, struct subnet, dhcp_type_subnet)
OMAPI_OBJECT_ALLOC (shared_network, struct shared_network,
		    dhcp_type_shared_network)
OMAPI_OBJECT_ALLOC (group_object, struct group_object, dhcp_type_group)
Ted Lemon's avatar
Ted Lemon committed
59

60 61
int group_allocate (ptr, file, line)
	struct group **ptr;
Ted Lemon's avatar
Ted Lemon committed
62 63
	const char *file;
	int line;
Ted Lemon's avatar
Ted Lemon committed
64
{
65
	int size;
Ted Lemon's avatar
Ted Lemon committed
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	if (!ptr) {
		log_error ("%s(%d): null pointer", file, line);
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}
	if (*ptr) {
		log_error ("%s(%d): non-null pointer", file, line);
#if defined (POINTER_DEBUG)
		abort ();
#else
		*ptr = (struct group *)0;
#endif
	}

	*ptr = dmalloc (sizeof **ptr, file, line);
	if (*ptr) {
		memset (*ptr, 0, sizeof **ptr);
		(*ptr) -> refcnt = 1;
		return 1;
	}
	return 0;
Ted Lemon's avatar
Ted Lemon committed
91 92
}

93 94 95
int group_reference (ptr, bp, file, line)
	struct group **ptr;
	struct group *bp;
Ted Lemon's avatar
Ted Lemon committed
96 97
	const char *file;
	int line;
Ted Lemon's avatar
Ted Lemon committed
98
{
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	if (!ptr) {
		log_error ("%s(%d): null pointer", file, line);
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}
	if (*ptr) {
		log_error ("%s(%d): non-null pointer", file, line);
#if defined (POINTER_DEBUG)
		abort ();
#else
		*ptr = (struct group *)0;
#endif
	}
	*ptr = bp;
	bp -> refcnt++;
	rc_register (file, line, ptr, bp, bp -> refcnt);
	dmalloc_reuse (bp, file, line, 1);
	return 1;
Ted Lemon's avatar
Ted Lemon committed
120 121
}

122 123
int group_dereference (ptr, file, line)
	struct group **ptr;
Ted Lemon's avatar
Ted Lemon committed
124 125
	const char *file;
	int line;
Ted Lemon's avatar
Ted Lemon committed
126
{
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	int i;
	struct group *group;

	if (!ptr || !*ptr) {
		log_error ("%s(%d): null pointer", file, line);
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}

	group = *ptr;
	*ptr = (struct group *)0;
	--group -> refcnt;
	rc_register (file, line, ptr, group, group -> refcnt);
	if (group -> refcnt > 0)
		return 1;

	if (group -> refcnt < 0) {
		log_error ("%s(%d): negative refcnt!", file, line);
#if defined (DEBUG_RC_HISTORY)
		dump_rc_history ();
#endif
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}

	if (group -> object)
		group_object_dereference (&group -> object, MDL);
	if (group -> subnet)	
		subnet_dereference (&group -> subnet, MDL);
	if (group -> shared_network)
		shared_network_dereference (&group -> shared_network, MDL);
	if (group -> statements)
		executable_statement_dereference (&group -> statements, MDL);
	dfree (group, file, line);
	return 1;
Ted Lemon's avatar
Ted Lemon committed
168 169
}

170
struct dhcp_packet *new_dhcp_packet (file, line)
Ted Lemon's avatar
Ted Lemon committed
171 172
	const char *file;
	int line;
Ted Lemon's avatar
Ted Lemon committed
173
{
174 175 176
	struct dhcp_packet *rval;
	rval = (struct dhcp_packet *)dmalloc (sizeof (struct dhcp_packet),
					      file, line);
Ted Lemon's avatar
Ted Lemon committed
177 178 179
	return rval;
}

Ted Lemon's avatar
Ted Lemon committed
180 181 182
struct protocol *new_protocol (file, line)
	const char *file;
	int line;
183
{
Ted Lemon's avatar
Ted Lemon committed
184
	struct protocol *rval = dmalloc (sizeof (struct protocol), file, line);
185 186 187
	return rval;
}

Ted Lemon's avatar
Ted Lemon committed
188 189 190
struct domain_search_list *new_domain_search_list (file, line)
	const char *file;
	int line;
191 192
{
	struct domain_search_list *rval =
Ted Lemon's avatar
Ted Lemon committed
193
		dmalloc (sizeof (struct domain_search_list), file, line);
194 195 196
	return rval;
}

Ted Lemon's avatar
Ted Lemon committed
197 198 199
struct name_server *new_name_server (file, line)
	const char *file;
	int line;
200 201
{
	struct name_server *rval =
Ted Lemon's avatar
Ted Lemon committed
202
		dmalloc (sizeof (struct name_server), file, line);
203 204 205
	return rval;
}

Ted Lemon's avatar
Ted Lemon committed
206
void free_name_server (ptr, file, line)
207
	struct name_server *ptr;
Ted Lemon's avatar
Ted Lemon committed
208 209
	const char *file;
	int line;
210
{
Ted Lemon's avatar
Ted Lemon committed
211
	dfree ((VOIDPTR)ptr, file, line);
212 213
}

Ted Lemon's avatar
Ted Lemon committed
214 215 216
struct option *new_option (file, line)
	const char *file;
	int line;
Ted Lemon's avatar
Ted Lemon committed
217 218
{
	struct option *rval =
Ted Lemon's avatar
Ted Lemon committed
219
		dmalloc (sizeof (struct option), file, line);
Ted Lemon's avatar
Ted Lemon committed
220 221 222 223 224
	if (rval)
		memset (rval, 0, sizeof *rval);
	return rval;
}

Ted Lemon's avatar
Ted Lemon committed
225
void free_option (ptr, file, line)
Ted Lemon's avatar
Ted Lemon committed
226
	struct option *ptr;
Ted Lemon's avatar
Ted Lemon committed
227 228
	const char *file;
	int line;
Ted Lemon's avatar
Ted Lemon committed
229 230 231 232
{
/* XXX have to put all options on heap before this is possible. */
#if 0
	if (ptr -> name)
Ted Lemon's avatar
Ted Lemon committed
233 234
		dfree ((VOIDPTR)option -> name, file, line);
	dfree ((VOIDPTR)ptr, file, line);
Ted Lemon's avatar
Ted Lemon committed
235 236 237
#endif
}

Ted Lemon's avatar
Ted Lemon committed
238 239 240
struct universe *new_universe (file, line)
	const char *file;
	int line;
241 242
{
	struct universe *rval =
Ted Lemon's avatar
Ted Lemon committed
243
		dmalloc (sizeof (struct universe), file, line);
244 245 246
	return rval;
}

Ted Lemon's avatar
Ted Lemon committed
247
void free_universe (ptr, file, line)
248
	struct universe *ptr;
Ted Lemon's avatar
Ted Lemon committed
249 250
	const char *file;
	int line;
251
{
Ted Lemon's avatar
Ted Lemon committed
252
	dfree ((VOIDPTR)ptr, file, line);
253 254
}

Ted Lemon's avatar
Ted Lemon committed
255
void free_domain_search_list (ptr, file, line)
256
	struct domain_search_list *ptr;
Ted Lemon's avatar
Ted Lemon committed
257 258
	const char *file;
	int line;
259
{
Ted Lemon's avatar
Ted Lemon committed
260
	dfree ((VOIDPTR)ptr, file, line);
261 262
}

Ted Lemon's avatar
Ted Lemon committed
263
void free_protocol (ptr, file, line)
264
	struct protocol *ptr;
Ted Lemon's avatar
Ted Lemon committed
265 266
	const char *file;
	int line;
267
{
Ted Lemon's avatar
Ted Lemon committed
268
	dfree ((VOIDPTR)ptr, file, line);
269 270
}

Ted Lemon's avatar
Ted Lemon committed
271
void free_dhcp_packet (ptr, file, line)
272
	struct dhcp_packet *ptr;
Ted Lemon's avatar
Ted Lemon committed
273 274
	const char *file;
	int line;
Ted Lemon's avatar
Ted Lemon committed
275
{
Ted Lemon's avatar
Ted Lemon committed
276
	dfree ((VOIDPTR)ptr, file, line);
Ted Lemon's avatar
Ted Lemon committed
277 278
}

Ted Lemon's avatar
Ted Lemon committed
279 280 281
struct client_lease *new_client_lease (file, line)
	const char *file;
	int line;
Ted Lemon's avatar
Ted Lemon committed
282
{
283
	return (struct client_lease *)dmalloc (sizeof (struct client_lease),
Ted Lemon's avatar
Ted Lemon committed
284
					       file, line);
Ted Lemon's avatar
Ted Lemon committed
285 286
}

Ted Lemon's avatar
Ted Lemon committed
287
void free_client_lease (lease, file, line)
288
	struct client_lease *lease;
Ted Lemon's avatar
Ted Lemon committed
289 290
	const char *file;
	int line;
Ted Lemon's avatar
Ted Lemon committed
291
{
Ted Lemon's avatar
Ted Lemon committed
292
	dfree (lease, file, line);
293 294 295 296
}

pair free_pairs;

Ted Lemon's avatar
Ted Lemon committed
297 298 299
pair new_pair (file, line)
	const char *file;
	int line;
300 301 302 303 304 305 306
{
	pair foo;

	if (free_pairs) {
		foo = free_pairs;
		free_pairs = foo -> cdr;
		memset (foo, 0, sizeof *foo);
Ted Lemon's avatar
Ted Lemon committed
307
		dmalloc_reuse (foo, file, line, 0);
308 309 310
		return foo;
	}

Ted Lemon's avatar
Ted Lemon committed
311
	foo = dmalloc (sizeof *foo, file, line);
312 313 314 315 316 317
	if (!foo)
		return foo;
	memset (foo, 0, sizeof *foo);
	return foo;
}

Ted Lemon's avatar
Ted Lemon committed
318
void free_pair (foo, file, line)
319
	pair foo;
Ted Lemon's avatar
Ted Lemon committed
320 321
	const char *file;
	int line;
322 323 324
{
	foo -> cdr = free_pairs;
	free_pairs = foo;
325
	dmalloc_reuse (free_pairs, (char *)0, 0, 0);
326 327 328 329
}

struct expression *free_expressions;

Ted Lemon's avatar
Ted Lemon committed
330
int expression_allocate (cptr, file, line)
331
	struct expression **cptr;
Ted Lemon's avatar
Ted Lemon committed
332 333
	const char *file;
	int line;
334 335 336 337 338 339 340
{
	struct expression *rval;

	if (free_expressions) {
		rval = free_expressions;
		free_expressions = rval -> data.not;
	} else {
Ted Lemon's avatar
Ted Lemon committed
341
		rval = dmalloc (sizeof (struct expression), file, line);
342 343 344 345
		if (!rval)
			return 0;
	}
	memset (rval, 0, sizeof *rval);
Ted Lemon's avatar
Ted Lemon committed
346
	return expression_reference (cptr, rval, file, line);
347 348
}

Ted Lemon's avatar
Ted Lemon committed
349
int expression_reference (ptr, src, file, line)
350 351
	struct expression **ptr;
	struct expression *src;
Ted Lemon's avatar
Ted Lemon committed
352 353
	const char *file;
	int line;
354 355
{
	if (!ptr) {
356
		log_error ("%s(%d): null pointer", file, line);
357
#if defined (POINTER_DEBUG)
358
		abort ();
359 360 361
#else
		return 0;
#endif
362 363
	}
	if (*ptr) {
364
		log_error ("%s(%d): non-null pointer", file, line);
365
#if defined (POINTER_DEBUG)
366
		abort ();
367
#else
368
		*ptr = (struct expression *)0;
369
#endif
370 371 372
	}
	*ptr = src;
	src -> refcnt++;
373
	rc_register (file, line, ptr, src, src -> refcnt);
Ted Lemon's avatar
Ted Lemon committed
374
	dmalloc_reuse (src, file, line, 1);
375 376 377
	return 1;
}

Ted Lemon's avatar
Ted Lemon committed
378 379 380 381 382 383 384 385 386 387
void free_expression (expr, file, line)
	struct expression *expr;
	const char *file;
	int line;
{
	expr -> data.not = free_expressions;
	free_expressions = expr;
	dmalloc_reuse (free_expressions, (char *)0, 0, 0);
}

Ted Lemon's avatar
Ted Lemon committed
388
struct binding_value *free_binding_values;
Ted Lemon's avatar
Ted Lemon committed
389
				
Ted Lemon's avatar
Ted Lemon committed
390 391 392 393 394 395 396 397 398
int binding_value_allocate (cptr, file, line)
	struct binding_value **cptr;
	const char *file;
	int line;
{
	struct binding_value *rval;

	if (free_binding_values) {
		rval = free_binding_values;
Ted Lemon's avatar
Ted Lemon committed
399
		free_binding_values = rval -> value.bv;
Ted Lemon's avatar
Ted Lemon committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	} else {
		rval = dmalloc (sizeof (struct binding_value), file, line);
		if (!rval)
			return 0;
	}
	memset (rval, 0, sizeof *rval);
	return binding_value_reference (cptr, rval, file, line);
}

int binding_value_reference (ptr, src, file, line)
	struct binding_value **ptr;
	struct binding_value *src;
	const char *file;
	int line;
{
	if (!ptr) {
		log_error ("%s(%d): null pointer", file, line);
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}
	if (*ptr) {
		log_error ("%s(%d): non-null pointer", file, line);
#if defined (POINTER_DEBUG)
		abort ();
#else
		*ptr = (struct binding_value *)0;
#endif
	}
	*ptr = src;
	src -> refcnt++;
	rc_register (file, line, ptr, src, src -> refcnt);
	dmalloc_reuse (src, file, line, 1);
	return 1;
}

void free_binding_value (bv, file, line)
	struct binding_value *bv;
	const char *file;
	int line;
{
Ted Lemon's avatar
Ted Lemon committed
443
	bv -> value.bv = free_binding_values;
Ted Lemon's avatar
Ted Lemon committed
444 445 446 447
	free_binding_values = bv;
	dmalloc_reuse (free_binding_values, (char *)0, 0, 0);
}

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
int fundef_allocate (cptr, file, line)
	struct fundef **cptr;
	const char *file;
	int line;
{
	struct fundef *rval;

	rval = dmalloc (sizeof (struct fundef), file, line);
	if (!rval)
		return 0;
	memset (rval, 0, sizeof *rval);
	return fundef_reference (cptr, rval, file, line);
}

int fundef_reference (ptr, src, file, line)
	struct fundef **ptr;
	struct fundef *src;
	const char *file;
	int line;
{
	if (!ptr) {
		log_error ("%s(%d): null pointer", file, line);
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}
	if (*ptr) {
		log_error ("%s(%d): non-null pointer", file, line);
#if defined (POINTER_DEBUG)
		abort ();
#else
		*ptr = (struct fundef *)0;
#endif
	}
	*ptr = src;
	src -> refcnt++;
	rc_register (file, line, ptr, src, src -> refcnt);
	dmalloc_reuse (src, file, line, 1);
	return 1;
}

491 492
struct option_cache *free_option_caches;

Ted Lemon's avatar
Ted Lemon committed
493
int option_cache_allocate (cptr, file, line)
494
	struct option_cache **cptr;
Ted Lemon's avatar
Ted Lemon committed
495 496
	const char *file;
	int line;
497 498 499 500 501 502 503
{
	struct option_cache *rval;

	if (free_option_caches) {
		rval = free_option_caches;
		free_option_caches =
			(struct option_cache *)(rval -> expression);
Ted Lemon's avatar
Ted Lemon committed
504
		dmalloc_reuse (rval, file, line, 0);
505
	} else {
Ted Lemon's avatar
Ted Lemon committed
506
		rval = dmalloc (sizeof (struct option_cache), file, line);
507 508 509 510
		if (!rval)
			return 0;
	}
	memset (rval, 0, sizeof *rval);
Ted Lemon's avatar
Ted Lemon committed
511
	return option_cache_reference (cptr, rval, file, line);
512 513
}

Ted Lemon's avatar
Ted Lemon committed
514
int option_cache_reference (ptr, src, file, line)
515 516
	struct option_cache **ptr;
	struct option_cache *src;
Ted Lemon's avatar
Ted Lemon committed
517 518
	const char *file;
	int line;
519 520
{
	if (!ptr) {
521
		log_error ("%s(%d): null pointer", file, line);
522
#if defined (POINTER_DEBUG)
523
		abort ();
524 525 526
#else
		return 0;
#endif
527 528
	}
	if (*ptr) {
529
		log_error ("%s(%d): non-null pointer", file, line);
530
#if defined (POINTER_DEBUG)
531
		abort ();
532
#else
533
		*ptr = (struct option_cache *)0;
534
#endif
535 536 537
	}
	*ptr = src;
	src -> refcnt++;
538
	rc_register (file, line, ptr, src, src -> refcnt);
Ted Lemon's avatar
Ted Lemon committed
539
	dmalloc_reuse (src, file, line, 1);
540 541 542
	return 1;
}

Ted Lemon's avatar
Ted Lemon committed
543
int buffer_allocate (ptr, len, file, line)
544
	struct buffer **ptr;
545
	unsigned len;
Ted Lemon's avatar
Ted Lemon committed
546 547
	const char *file;
	int line;
548 549 550
{
	struct buffer *bp;

Ted Lemon's avatar
Ted Lemon committed
551
	bp = dmalloc (len + sizeof *bp, file, line);
552 553 554 555
	if (!bp)
		return 0;
	memset (bp, 0, sizeof *bp);
	bp -> refcnt = 0;
Ted Lemon's avatar
Ted Lemon committed
556
	return buffer_reference (ptr, bp, file, line);
557 558
}

Ted Lemon's avatar
Ted Lemon committed
559
int buffer_reference (ptr, bp, file, line)
560 561
	struct buffer **ptr;
	struct buffer *bp;
Ted Lemon's avatar
Ted Lemon committed
562 563
	const char *file;
	int line;
564 565
{
	if (!ptr) {
566
		log_error ("%s(%d): null pointer", file, line);
567
#if defined (POINTER_DEBUG)
568
		abort ();
569 570 571
#else
		return 0;
#endif
572 573
	}
	if (*ptr) {
574
		log_error ("%s(%d): non-null pointer", file, line);
575
#if defined (POINTER_DEBUG)
576
		abort ();
577
#else
578
		*ptr = (struct buffer *)0;
579
#endif
580 581 582
	}
	*ptr = bp;
	bp -> refcnt++;
583
	rc_register (file, line, ptr, bp, bp -> refcnt);
Ted Lemon's avatar
Ted Lemon committed
584
	dmalloc_reuse (bp, file, line, 1);
585 586 587
	return 1;
}

Ted Lemon's avatar
Ted Lemon committed
588
int buffer_dereference (ptr, file, line)
589
	struct buffer **ptr;
Ted Lemon's avatar
Ted Lemon committed
590 591
	const char *file;
	int line;
592 593 594
{
	struct buffer *bp;

595
	if (!ptr) {
596
		log_error ("%s(%d): null pointer", file, line);
597
#if defined (POINTER_DEBUG)
598
		abort ();
599 600 601
#else
		return 0;
#endif
602 603
	}

604
	if (!*ptr) {
605
		log_error ("%s(%d): null pointer", file, line);
606 607 608 609 610 611 612
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}

613
	(*ptr) -> refcnt--;
614
	rc_register (file, line, ptr, *ptr, (*ptr) -> refcnt);
615
	if (!(*ptr) -> refcnt) {
Ted Lemon's avatar
Ted Lemon committed
616
		dfree ((*ptr), file, line);
617
	} else if ((*ptr) -> refcnt < 0) {
618
		log_error ("%s(%d): negative refcnt!", file, line);
Ted Lemon's avatar
Ted Lemon committed
619 620 621 622 623 624 625 626 627
#if defined (DEBUG_RC_HISTORY)
		dump_rc_history ();
#endif
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}
628 629 630 631
	*ptr = (struct buffer *)0;
	return 1;
}

Ted Lemon's avatar
Ted Lemon committed
632
int dns_host_entry_allocate (ptr, hostname, file, line)
633
	struct dns_host_entry **ptr;
634
	const char *hostname;
Ted Lemon's avatar
Ted Lemon committed
635 636
	const char *file;
	int line;
637 638 639
{
	struct dns_host_entry *bp;

Ted Lemon's avatar
Ted Lemon committed
640
	bp = dmalloc (strlen (hostname) + sizeof *bp, file, line);
641 642 643 644
	if (!bp)
		return 0;
	memset (bp, 0, sizeof *bp);
	bp -> refcnt = 0;
645
	strcpy (bp -> hostname, hostname);
Ted Lemon's avatar
Ted Lemon committed
646
	return dns_host_entry_reference (ptr, bp, file, line);
647 648
}

Ted Lemon's avatar
Ted Lemon committed
649
int dns_host_entry_reference (ptr, bp, file, line)
650 651
	struct dns_host_entry **ptr;
	struct dns_host_entry *bp;
Ted Lemon's avatar
Ted Lemon committed
652 653
	const char *file;
	int line;
654 655
{
	if (!ptr) {
656
		log_error ("%s(%d): null pointer", file, line);
657
#if defined (POINTER_DEBUG)
658
		abort ();
659 660 661
#else
		return 0;
#endif
662 663
	}
	if (*ptr) {
664
		log_error ("%s(%d): non-null pointer", file, line);
665
#if defined (POINTER_DEBUG)
666
		abort ();
667
#else
668
		*ptr = (struct dns_host_entry *)0;
669
#endif
670 671 672
	}
	*ptr = bp;
	bp -> refcnt++;
673
	rc_register (file, line, ptr, bp, bp -> refcnt);
Ted Lemon's avatar
Ted Lemon committed
674
	dmalloc_reuse (bp, file, line, 1);
675 676 677
	return 1;
}

Ted Lemon's avatar
Ted Lemon committed
678
int dns_host_entry_dereference (ptr, file, line)
679
	struct dns_host_entry **ptr;
Ted Lemon's avatar
Ted Lemon committed
680 681
	const char *file;
	int line;
682 683 684 685
{
	struct dns_host_entry *bp;

	if (!ptr || !*ptr) {
686
		log_error ("%s(%d): null pointer", file, line);
687
#if defined (POINTER_DEBUG)
688
		abort ();
689 690 691
#else
		return 0;
#endif
692 693 694
	}

	(*ptr) -> refcnt--;
695
	rc_register (file, line, ptr, *ptr, (*ptr) -> refcnt);
696
	if (!(*ptr) -> refcnt)
Ted Lemon's avatar
Ted Lemon committed
697 698
		dfree ((*ptr), file, line);
	if ((*ptr) -> refcnt < 0) {
699
		log_error ("%s(%d): negative refcnt!", file, line);
Ted Lemon's avatar
Ted Lemon committed
700 701 702 703 704 705 706 707 708
#if defined (DEBUG_RC_HISTORY)
		dump_rc_history ();
#endif
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}
709 710
	*ptr = (struct dns_host_entry *)0;
	return 1;
Ted Lemon's avatar
Ted Lemon committed
711
}
712

Ted Lemon's avatar
Ted Lemon committed
713
int option_state_allocate (ptr, file, line)
714
	struct option_state **ptr;
Ted Lemon's avatar
Ted Lemon committed
715 716
	const char *file;
	int line;
717
{
718
	unsigned size;
719 720

	if (!ptr) {
721
		log_error ("%s(%d): null pointer", file, line);
722
#if defined (POINTER_DEBUG)
723
		abort ();
724 725 726
#else
		return 0;
#endif
727 728
	}
	if (*ptr) {
729
		log_error ("%s(%d): non-null pointer", file, line);
730
#if defined (POINTER_DEBUG)
731
		abort ();
732
#else
733
		*ptr = (struct option_state *)0;
734
#endif
735 736 737
	}

	size = sizeof **ptr + (universe_count - 1) * sizeof (VOIDPTR);
Ted Lemon's avatar
Ted Lemon committed
738
	*ptr = dmalloc (size, file, line);
739 740 741
	if (*ptr) {
		memset (*ptr, 0, size);
		(*ptr) -> universe_count = universe_count;
742
		(*ptr) -> refcnt = 1;
743
		rc_register (file, line, ptr, *ptr, (*ptr) -> refcnt);
744 745 746 747 748
		return 1;
	}
	return 0;
}

Ted Lemon's avatar
Ted Lemon committed
749
int option_state_reference (ptr, bp, file, line)
750 751
	struct option_state **ptr;
	struct option_state *bp;
Ted Lemon's avatar
Ted Lemon committed
752 753
	const char *file;
	int line;
754 755
{
	if (!ptr) {
756
		log_error ("%s(%d): null pointer", file, line);
757
#if defined (POINTER_DEBUG)
758
		abort ();
759 760 761
#else
		return 0;
#endif
762 763
	}
	if (*ptr) {
764
		log_error ("%s(%d): non-null pointer", file, line);
765
#if defined (POINTER_DEBUG)
766
		abort ();
767
#else
768
		*ptr = (struct option_state *)0;
769
#endif
770 771 772
	}
	*ptr = bp;
	bp -> refcnt++;
773
	rc_register (file, line, ptr, bp, bp -> refcnt);
Ted Lemon's avatar
Ted Lemon committed
774
	dmalloc_reuse (bp, file, line, 1);
775 776 777
	return 1;
}

Ted Lemon's avatar
Ted Lemon committed
778
int option_state_dereference (ptr, file, line)
779
	struct option_state **ptr;
Ted Lemon's avatar
Ted Lemon committed
780 781
	const char *file;
	int line;
782 783
{
	int i;
784
	struct option_state *options;
785 786

	if (!ptr || !*ptr) {
787
		log_error ("%s(%d): null pointer", file, line);
788
#if defined (POINTER_DEBUG)
789
		abort ();
790 791 792
#else
		return 0;
#endif
793 794
	}

795 796 797
	options = *ptr;
	*ptr = (struct option_state *)0;
	--options -> refcnt;
798
	rc_register (file, line, ptr, options, options -> refcnt);
Ted Lemon's avatar
Ted Lemon committed
799
	if (options -> refcnt > 0)
800 801
		return 1;

Ted Lemon's avatar
Ted Lemon committed
802
	if (options -> refcnt < 0) {
803
		log_error ("%s(%d): negative refcnt!", file, line);
Ted Lemon's avatar
Ted Lemon committed
804 805 806 807 808 809 810 811 812 813
#if defined (DEBUG_RC_HISTORY)
		dump_rc_history ();
#endif
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}

814
	/* Loop through the per-universe state. */
815 816
	for (i = 0; i < options -> universe_count; i++)
		if (options -> universes [i] &&
817 818
		    universes [i] -> option_state_dereference)
			((*(universes [i] -> option_state_dereference))
819
			 (universes [i], options, file, line));
Ted Lemon's avatar
Ted Lemon committed
820
	dfree (options, file, line);
821 822
	return 1;
}
823

Ted Lemon's avatar
Ted Lemon committed
824
int executable_statement_allocate (ptr, file, line)
825
	struct executable_statement **ptr;
Ted Lemon's avatar
Ted Lemon committed
826 827
	const char *file;
	int line;
828 829 830
{
	struct executable_statement *bp;

Ted Lemon's avatar
Ted Lemon committed
831
	bp = dmalloc (sizeof *bp, file, line);
832 833 834
	if (!bp)
		return 0;
	memset (bp, 0, sizeof *bp);
Ted Lemon's avatar
Ted Lemon committed
835
	return executable_statement_reference (ptr, bp, file, line);
836 837
}

Ted Lemon's avatar
Ted Lemon committed
838
int executable_statement_reference (ptr, bp, file, line)
839 840
	struct executable_statement **ptr;
	struct executable_statement *bp;
Ted Lemon's avatar
Ted Lemon committed
841 842
	const char *file;
	int line;
843 844
{
	if (!ptr) {
845
		log_error ("%s(%d): null pointer", file, line);
846 847 848 849 850 851 852
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}
	if (*ptr) {
853
		log_error ("%s(%d): non-null pointer", file, line);
854 855 856 857 858 859 860 861
#if defined (POINTER_DEBUG)
		abort ();
#else
		*ptr = (struct executable_statement *)0;
#endif
	}
	*ptr = bp;
	bp -> refcnt++;
862
	rc_register (file, line, ptr, bp, bp -> refcnt);
Ted Lemon's avatar
Ted Lemon committed
863
	dmalloc_reuse (bp, file, line, 1);
864 865
	return 1;
}
866 867 868

static struct packet *free_packets;

Ted Lemon's avatar
Ted Lemon committed
869
int packet_allocate (ptr, file, line)
870
	struct packet **ptr;
Ted Lemon's avatar
Ted Lemon committed
871 872
	const char *file;
	int line;
873 874 875 876
{
	int size;

	if (!ptr) {
877
		log_error ("%s(%d): null pointer", file, line);
878 879 880 881 882 883 884
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}
	if (*ptr) {
885
		log_error ("%s(%d): non-null pointer", file, line);
886 887 888 889 890 891 892
#if defined (POINTER_DEBUG)
		abort ();
#else
		*ptr = (struct packet *)0;
#endif
	}

893 894 895 896 897 898
	if (free_packets) {
		*ptr = free_packets;
		free_packets = (struct packet *)((*ptr) -> raw);
	} else {
		*ptr = dmalloc (sizeof **ptr, file, line);
	}
899 900 901 902 903 904 905 906
	if (*ptr) {
		memset (*ptr, 0, sizeof **ptr);
		(*ptr) -> refcnt = 1;
		return 1;
	}
	return 0;
}

Ted Lemon's avatar
Ted Lemon committed
907
int packet_reference (ptr, bp, file, line)
908 909
	struct packet **ptr;
	struct packet *bp;
Ted Lemon's avatar
Ted Lemon committed
910 911
	const char *file;
	int line;
912 913
{
	if (!ptr) {
914
		log_error ("%s(%d): null pointer", file, line);
915 916 917 918 919 920 921
#if defined (POINTER_DEBUG)
		abort ();
#else
		return 0;
#endif
	}
	if (*ptr) {
922
		log_error ("%s(%d): non-null pointer", file, line);
923 924 925 926 927 928 929 930
#if defined (POINTER_DEBUG)
		abort ();
#else
		*ptr = (struct packet *)0;
#endif
	}
	*ptr = bp;
	bp -> refcnt++;
931
	rc_register (file, line, ptr, bp, bp -> refcnt);
Ted Lemon's avatar
Ted Lemon committed
932
	dmalloc_reuse (bp, file, line, 1);