]>
Commit | Line | Data |
---|---|---|
7ebd047d TL |
1 | /* salloc.c |
2 | ||
3 | Memory allocation for the DHCP server... */ | |
4 | ||
5 | /* | |
d758ad8c | 6 | * Copyright (c) 1996-2001 Internet Software Consortium. |
7ebd047d TL |
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[] = | |
d758ad8c | 46 | "$Id: salloc.c,v 1.4 2001/06/27 00:31:16 mellon Exp $ Copyright (c) 1996-2001 The Internet Software Consortium. All rights reserved.\n"; |
7ebd047d TL |
47 | #endif /* not lint */ |
48 | ||
49 | #include "dhcpd.h" | |
50 | #include <omapip/omapip_p.h> | |
51 | ||
d758ad8c TL |
52 | #if defined (COMPACT_LEASES) |
53 | struct lease *free_leases; | |
54 | ||
55 | # if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT) | |
56 | struct lease *lease_hunks; | |
57 | ||
58 | void relinquish_lease_hunks () | |
59 | { | |
60 | struct lease *c, *n, **p, *f; | |
61 | int i; | |
62 | ||
63 | /* Account for all the leases on the free list. */ | |
64 | for (n = lease_hunks; n; n = n -> next) { | |
65 | for (i = 1; i < n -> starts + 1; i++) { | |
66 | p = &free_leases; | |
67 | for (c = free_leases; c; c = c -> next) { | |
68 | if (c == &n [i]) { | |
69 | *p = c -> next; | |
70 | n -> ends++; | |
71 | break; | |
72 | } | |
73 | p = &c -> next; | |
74 | } | |
75 | if (!c) { | |
76 | log_info ("lease %s refcnt %d", | |
77 | piaddr (n [i].ip_addr), n [i].refcnt); | |
78 | dump_rc_history (&n [i]); | |
79 | } | |
80 | } | |
81 | } | |
82 | ||
83 | for (c = lease_hunks; c; c = n) { | |
84 | n = c -> next; | |
85 | if (c -> ends != c -> starts) { | |
86 | log_info ("lease hunk %lx leases %ld free %ld", | |
87 | (unsigned long)c, (unsigned long)c -> starts, | |
88 | (unsigned long)c -> ends); | |
89 | } | |
90 | dfree (c, MDL); | |
91 | } | |
92 | ||
93 | /* Free all the rogue leases. */ | |
94 | for (c = free_leases; c; c = n) { | |
95 | n = c -> next; | |
96 | dfree (c, MDL); | |
97 | } | |
98 | } | |
99 | #endif | |
100 | ||
7ebd047d TL |
101 | struct lease *new_leases (n, file, line) |
102 | unsigned n; | |
103 | const char *file; | |
104 | int line; | |
105 | { | |
d758ad8c TL |
106 | struct lease *rval; |
107 | #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT) | |
108 | rval = dmalloc ((n + 1) * sizeof (struct lease), file, line); | |
109 | memset (rval, 0, sizeof (struct lease)); | |
110 | rval -> starts = n; | |
111 | rval -> next = lease_hunks; | |
112 | lease_hunks = rval; | |
113 | rval++; | |
114 | #else | |
115 | rval = dmalloc (n * sizeof (struct lease), file, line); | |
116 | #endif | |
7ebd047d TL |
117 | return rval; |
118 | } | |
119 | ||
d758ad8c TL |
120 | /* If we are allocating leases in aggregations, there's really no way |
121 | to free one, although perhaps we can maintain a free list. */ | |
122 | ||
123 | isc_result_t dhcp_lease_free (omapi_object_t *lo, | |
124 | const char *file, int line) | |
125 | { | |
126 | struct lease *lease; | |
127 | if (lo -> type != dhcp_type_lease) | |
128 | return ISC_R_INVALIDARG; | |
129 | lease = (struct lease *)lo; | |
130 | memset (lease, 0, sizeof (struct lease)); | |
131 | lease -> next = free_leases; | |
132 | free_leases = lease; | |
133 | return ISC_R_SUCCESS; | |
134 | } | |
135 | ||
136 | isc_result_t dhcp_lease_get (omapi_object_t **lp, | |
137 | const char *file, int line) | |
138 | { | |
139 | struct lease **lease = (struct lease **)lp; | |
140 | struct lease *lt; | |
141 | ||
142 | if (free_leases) { | |
143 | lt = free_leases; | |
144 | free_leases = lt -> next; | |
145 | *lease = lt; | |
146 | return ISC_R_SUCCESS; | |
147 | } | |
148 | return ISC_R_NOMEMORY; | |
149 | } | |
150 | #endif /* COMPACT_LEASES */ | |
151 | ||
7ebd047d TL |
152 | OMAPI_OBJECT_ALLOC (lease, struct lease, dhcp_type_lease) |
153 | OMAPI_OBJECT_ALLOC (class, struct class, dhcp_type_class) | |
899d754f | 154 | OMAPI_OBJECT_ALLOC (subclass, struct class, dhcp_type_subclass) |
7ebd047d | 155 | OMAPI_OBJECT_ALLOC (pool, struct pool, dhcp_type_pool) |
ae9c72b7 TL |
156 | |
157 | #if !defined (NO_HOST_FREES) /* Scary debugging mode - don't enable! */ | |
7ebd047d | 158 | OMAPI_OBJECT_ALLOC (host, struct host_decl, dhcp_type_host) |
ae9c72b7 TL |
159 | #else |
160 | isc_result_t host_allocate (struct host_decl **p, const char *file, int line) | |
161 | { | |
162 | return omapi_object_allocate ((omapi_object_t **)p, | |
163 | dhcp_type_host, 0, file, line); | |
164 | } | |
165 | ||
166 | isc_result_t host_reference (struct host_decl **pptr, struct host_decl *ptr, | |
167 | const char *file, int line) | |
168 | { | |
169 | return omapi_object_reference ((omapi_object_t **)pptr, | |
170 | (omapi_object_t *)ptr, file, line); | |
171 | } | |
172 | ||
173 | isc_result_t host_dereference (struct host_decl **ptr, | |
174 | const char *file, int line) | |
175 | { | |
176 | if ((*ptr) -> refcnt == 1) { | |
177 | log_error ("host dereferenced with refcnt == 1."); | |
178 | #if defined (DEBUG_RC_HISTORY) | |
179 | dump_rc_history (); | |
180 | #endif | |
181 | abort (); | |
182 | } | |
183 | return omapi_object_dereference ((omapi_object_t **)ptr, file, line); | |
184 | } | |
185 | #endif | |
7ebd047d TL |
186 | |
187 | struct lease_state *free_lease_states; | |
188 | ||
189 | struct lease_state *new_lease_state (file, line) | |
190 | const char *file; | |
191 | int line; | |
192 | { | |
193 | struct lease_state *rval; | |
194 | ||
195 | if (free_lease_states) { | |
196 | rval = free_lease_states; | |
197 | free_lease_states = | |
198 | (struct lease_state *)(free_lease_states -> next); | |
199 | dmalloc_reuse (rval, file, line, 0); | |
200 | } else { | |
201 | rval = dmalloc (sizeof (struct lease_state), file, line); | |
202 | if (!rval) | |
203 | return rval; | |
204 | } | |
205 | memset (rval, 0, sizeof *rval); | |
206 | if (!option_state_allocate (&rval -> options, file, line)) { | |
207 | free_lease_state (rval, file, line); | |
208 | return (struct lease_state *)0; | |
209 | } | |
210 | return rval; | |
211 | } | |
212 | ||
213 | void free_lease_state (ptr, file, line) | |
214 | struct lease_state *ptr; | |
215 | const char *file; | |
216 | int line; | |
217 | { | |
218 | if (ptr -> options) | |
219 | option_state_dereference (&ptr -> options, file, line); | |
220 | if (ptr -> packet) | |
221 | packet_dereference (&ptr -> packet, file, line); | |
222 | if (ptr -> shared_network) | |
223 | shared_network_dereference (&ptr -> shared_network, | |
224 | file, line); | |
225 | ||
226 | data_string_forget (&ptr -> parameter_request_list, file, line); | |
227 | data_string_forget (&ptr -> filename, file, line); | |
228 | data_string_forget (&ptr -> server_name, file, line); | |
229 | ptr -> next = free_lease_states; | |
230 | free_lease_states = ptr; | |
231 | dmalloc_reuse (free_lease_states, (char *)0, 0, 0); | |
232 | } | |
233 | ||
d758ad8c TL |
234 | #if defined (DEBUG_MEMORY_LEAKAGE) || \ |
235 | defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT) | |
236 | void relinquish_free_lease_states () | |
237 | { | |
238 | struct lease_state *cs, *ns; | |
239 | ||
240 | for (cs = free_lease_states; cs; cs = ns) { | |
241 | ns = cs -> next; | |
242 | dfree (cs, MDL); | |
243 | } | |
244 | free_lease_states = (struct lease_state *)0; | |
245 | } | |
246 | #endif | |
247 | ||
7ebd047d TL |
248 | struct permit *new_permit (file, line) |
249 | const char *file; | |
250 | int line; | |
251 | { | |
252 | struct permit *permit = ((struct permit *) | |
253 | dmalloc (sizeof (struct permit), file, line)); | |
254 | if (!permit) | |
255 | return permit; | |
256 | memset (permit, 0, sizeof *permit); | |
257 | return permit; | |
258 | } | |
259 | ||
260 | void free_permit (permit, file, line) | |
261 | struct permit *permit; | |
262 | const char *file; | |
263 | int line; | |
264 | { | |
d758ad8c TL |
265 | if (permit -> type == permit_class) |
266 | class_dereference (&permit -> class, MDL); | |
7ebd047d TL |
267 | dfree (permit, file, line); |
268 | } |