xref: /openbsd-src/usr.sbin/dhcpd/alloc.c (revision c525a1850093eeda7e2f4793bb28f06faaca922a)
1*c525a185Skrw /*	$OpenBSD: alloc.c,v 1.15 2017/02/13 19:13:14 krw Exp $	*/
2e853bc5dShenning 
304c8330aShenning /* Memory allocation... */
4e853bc5dShenning 
5e853bc5dShenning /*
6e853bc5dShenning  * Copyright (c) 1995, 1996, 1998 The Internet Software Consortium.
7e853bc5dShenning  * All rights reserved.
8e853bc5dShenning  *
9e853bc5dShenning  * Redistribution and use in source and binary forms, with or without
10e853bc5dShenning  * modification, are permitted provided that the following conditions
11e853bc5dShenning  * are met:
12e853bc5dShenning  *
13e853bc5dShenning  * 1. Redistributions of source code must retain the above copyright
14e853bc5dShenning  *    notice, this list of conditions and the following disclaimer.
15e853bc5dShenning  * 2. Redistributions in binary form must reproduce the above copyright
16e853bc5dShenning  *    notice, this list of conditions and the following disclaimer in the
17e853bc5dShenning  *    documentation and/or other materials provided with the distribution.
18e853bc5dShenning  * 3. Neither the name of The Internet Software Consortium nor the names
19e853bc5dShenning  *    of its contributors may be used to endorse or promote products derived
20e853bc5dShenning  *    from this software without specific prior written permission.
21e853bc5dShenning  *
22e853bc5dShenning  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23e853bc5dShenning  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24e853bc5dShenning  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25e853bc5dShenning  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26e853bc5dShenning  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27e853bc5dShenning  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28e853bc5dShenning  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29e853bc5dShenning  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30e853bc5dShenning  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31e853bc5dShenning  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32e853bc5dShenning  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33e853bc5dShenning  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34e853bc5dShenning  * SUCH DAMAGE.
35e853bc5dShenning  *
36e853bc5dShenning  * This software has been written for the Internet Software Consortium
37e853bc5dShenning  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38e853bc5dShenning  * Enterprises.  To learn more about the Internet Software Consortium,
39e853bc5dShenning  * see ``http://www.vix.com/isc''.  To learn more about Vixie
40e853bc5dShenning  * Enterprises, see ``http://www.vix.com''.
41e853bc5dShenning  */
42e853bc5dShenning 
43837cddffSkrw #include <sys/types.h>
44837cddffSkrw #include <sys/socket.h>
45837cddffSkrw 
46837cddffSkrw #include <net/if.h>
47837cddffSkrw 
48837cddffSkrw #include <netinet/in.h>
49837cddffSkrw 
50837cddffSkrw #include <stdio.h>
51837cddffSkrw #include <stdlib.h>
52837cddffSkrw 
53837cddffSkrw #include "dhcp.h"
54837cddffSkrw #include "tree.h"
55e853bc5dShenning #include "dhcpd.h"
56*c525a185Skrw #include "log.h"
57e853bc5dShenning 
5804c8330aShenning struct lease_state *free_lease_states;
59e853bc5dShenning struct tree_cache *free_tree_caches;
60e853bc5dShenning 
6104c8330aShenning struct tree_cache *
new_tree_cache(char * name)6204c8330aShenning new_tree_cache(char *name)
63e853bc5dShenning {
64e853bc5dShenning 	struct tree_cache *rval;
65e853bc5dShenning 
66e853bc5dShenning 	if (free_tree_caches) {
67e853bc5dShenning 		rval = free_tree_caches;
6804c8330aShenning 		free_tree_caches = (struct tree_cache *)(rval->value);
69e853bc5dShenning 	} else {
70d60fc4a4Skrw 		rval = calloc(1, sizeof(struct tree_cache));
71e853bc5dShenning 		if (!rval)
72*c525a185Skrw 			fatalx("unable to allocate tree cache for %s.", name);
73e853bc5dShenning 	}
7404c8330aShenning 	return (rval);
75e853bc5dShenning }
76e853bc5dShenning 
7704c8330aShenning void
free_tree_cache(struct tree_cache * ptr)78285f06efSderaadt free_tree_cache(struct tree_cache *ptr)
79e853bc5dShenning {
8004c8330aShenning 	ptr->value = (unsigned char *)free_tree_caches;
8104c8330aShenning 	free_tree_caches = ptr;
82e853bc5dShenning }
83e853bc5dShenning 
8404c8330aShenning struct lease_state *
new_lease_state(char * name)8504c8330aShenning new_lease_state(char *name)
86e853bc5dShenning {
87e853bc5dShenning 	struct lease_state *rval;
88e853bc5dShenning 
89e853bc5dShenning 	if (free_lease_states) {
90e853bc5dShenning 		rval = free_lease_states;
91d60fc4a4Skrw 		free_lease_states = free_lease_states->next;
92d60fc4a4Skrw 	} else {
93d60fc4a4Skrw 		rval = calloc(1, sizeof(struct lease_state));
94d60fc4a4Skrw 		if (!rval)
95*c525a185Skrw 			fatalx("unable to allocate lease state for %s.", name);
96d60fc4a4Skrw 	}
97d60fc4a4Skrw 
9804c8330aShenning 	return (rval);
99e853bc5dShenning }
100e853bc5dShenning 
10104c8330aShenning void
free_lease_state(struct lease_state * ptr,char * name)10204c8330aShenning free_lease_state(struct lease_state *ptr, char *name)
103e853bc5dShenning {
10492b98df2Skrw 	free(ptr->prl);
105e853bc5dShenning 	ptr->next = free_lease_states;
106e853bc5dShenning 	free_lease_states = ptr;
107e853bc5dShenning }
108