3 Memory allocation for the DHCP server... */
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1996-2003 by Internet Software Consortium
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
23 * Redwood City, CA 94063
27 * This software has been written for Internet Systems Consortium
28 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
29 * To learn more about Internet Systems Consortium, see
30 * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
31 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
32 * ``http://www.nominum.com''.
36 static char copyright
[] =
37 "$Id$ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
41 #include <omapip/omapip_p.h>
43 #if defined (COMPACT_LEASES)
44 struct lease
*free_leases
;
46 # if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
47 struct lease
*lease_hunks
;
49 void relinquish_lease_hunks ()
51 struct lease
*c
, *n
, **p
, *f
;
54 /* Account for all the leases on the free list. */
55 for (n
= lease_hunks
; n
; n
= n
-> next
) {
56 for (i
= 1; i
< n
-> starts
+ 1; i
++) {
58 for (c
= free_leases
; c
; c
= c
-> next
) {
67 log_info ("lease %s refcnt %d",
68 piaddr (n
[i
].ip_addr
), n
[i
].refcnt
);
69 dump_rc_history (&n
[i
]);
74 for (c
= lease_hunks
; c
; c
= n
) {
76 if (c
-> ends
!= c
-> starts
) {
77 log_info ("lease hunk %lx leases %ld free %ld",
78 (unsigned long)c
, (unsigned long)c
-> starts
,
79 (unsigned long)c
-> ends
);
84 /* Free all the rogue leases. */
85 for (c
= free_leases
; c
; c
= n
) {
92 struct lease
*new_leases (n
, file
, line
)
98 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
99 rval
= dmalloc ((n
+ 1) * sizeof (struct lease
), file
, line
);
100 memset (rval
, 0, sizeof (struct lease
));
102 rval
-> next
= lease_hunks
;
106 rval
= dmalloc (n
* sizeof (struct lease
), file
, line
);
111 /* If we are allocating leases in aggregations, there's really no way
112 to free one, although perhaps we can maintain a free list. */
114 isc_result_t
dhcp_lease_free (omapi_object_t
*lo
,
115 const char *file
, int line
)
118 if (lo
-> type
!= dhcp_type_lease
)
119 return ISC_R_INVALIDARG
;
120 lease
= (struct lease
*)lo
;
121 memset (lease
, 0, sizeof (struct lease
));
122 lease
-> next
= free_leases
;
124 return ISC_R_SUCCESS
;
127 isc_result_t
dhcp_lease_get (omapi_object_t
**lp
,
128 const char *file
, int line
)
130 struct lease
**lease
= (struct lease
**)lp
;
135 free_leases
= lt
-> next
;
137 return ISC_R_SUCCESS
;
139 return ISC_R_NOMEMORY
;
141 #endif /* COMPACT_LEASES */
143 OMAPI_OBJECT_ALLOC (lease
, struct lease
, dhcp_type_lease
)
144 OMAPI_OBJECT_ALLOC (class, struct class, dhcp_type_class
)
145 OMAPI_OBJECT_ALLOC (pool
, struct pool
, dhcp_type_pool
)
147 #if !defined (NO_HOST_FREES) /* Scary debugging mode - don't enable! */
148 OMAPI_OBJECT_ALLOC (host
, struct host_decl
, dhcp_type_host
)
150 isc_result_t
host_allocate (struct host_decl
**p
, const char *file
, int line
)
152 return omapi_object_allocate ((omapi_object_t
**)p
,
153 dhcp_type_host
, 0, file
, line
);
156 isc_result_t
host_reference (struct host_decl
**pptr
, struct host_decl
*ptr
,
157 const char *file
, int line
)
159 return omapi_object_reference ((omapi_object_t
**)pptr
,
160 (omapi_object_t
*)ptr
, file
, line
);
163 isc_result_t
host_dereference (struct host_decl
**ptr
,
164 const char *file
, int line
)
166 if ((*ptr
) -> refcnt
== 1) {
167 log_error ("host dereferenced with refcnt == 1.");
168 #if defined (DEBUG_RC_HISTORY)
173 return omapi_object_dereference ((omapi_object_t
**)ptr
, file
, line
);
177 struct lease_state
*free_lease_states
;
179 struct lease_state
*new_lease_state (file
, line
)
183 struct lease_state
*rval
;
185 if (free_lease_states
) {
186 rval
= free_lease_states
;
188 (struct lease_state
*)(free_lease_states
-> next
);
189 dmalloc_reuse (rval
, file
, line
, 0);
191 rval
= dmalloc (sizeof (struct lease_state
), file
, line
);
195 memset (rval
, 0, sizeof *rval
);
196 if (!option_state_allocate (&rval
-> options
, file
, line
)) {
197 free_lease_state (rval
, file
, line
);
198 return (struct lease_state
*)0;
203 void free_lease_state (ptr
, file
, line
)
204 struct lease_state
*ptr
;
209 option_state_dereference (&ptr
-> options
, file
, line
);
211 packet_dereference (&ptr
-> packet
, file
, line
);
212 if (ptr
-> shared_network
)
213 shared_network_dereference (&ptr
-> shared_network
,
216 data_string_forget (&ptr
-> parameter_request_list
, file
, line
);
217 data_string_forget (&ptr
-> filename
, file
, line
);
218 data_string_forget (&ptr
-> server_name
, file
, line
);
219 ptr
-> next
= free_lease_states
;
220 free_lease_states
= ptr
;
221 dmalloc_reuse (free_lease_states
, (char *)0, 0, 0);
224 #if defined (DEBUG_MEMORY_LEAKAGE) || \
225 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
226 void relinquish_free_lease_states ()
228 struct lease_state
*cs
, *ns
;
230 for (cs
= free_lease_states
; cs
; cs
= ns
) {
234 free_lease_states
= (struct lease_state
*)0;
238 struct permit
*new_permit (file
, line
)
242 struct permit
*permit
= ((struct permit
*)
243 dmalloc (sizeof (struct permit
), file
, line
));
246 memset (permit
, 0, sizeof *permit
);
250 void free_permit (permit
, file
, line
)
251 struct permit
*permit
;
255 if (permit
-> type
== permit_class
)
256 class_dereference (&permit
-> class, MDL
);
257 dfree (permit
, file
, line
);