]>
git.ipfire.org Git - thirdparty/dhcp.git/blob - server/salloc.c
3 Memory allocation for the DHCP server... */
6 * Copyright (c) 2004-2016 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
25 * https://www.isc.org/
30 #include <omapip/omapip_p.h>
32 #if defined (COMPACT_LEASES)
33 struct lease
*free_leases
;
35 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
36 struct lease
*lease_hunks
;
38 void relinquish_lease_hunks ()
40 struct lease
*c
, *n
, **p
;
43 /* Account for all the leases on the free list. */
44 for (n
= lease_hunks
; n
; n
= n
->next
) {
45 for (i
= 1; i
< n
->starts
+ 1; i
++) {
47 for (c
= free_leases
; c
; c
= c
->next
) {
56 log_info("lease %s refcnt %d",
57 piaddr (n
[i
].ip_addr
), n
[i
].refcnt
);
58 #if defined (DEBUG_RC_HISTORY)
59 dump_rc_history(&n
[i
]);
65 for (c
= lease_hunks
; c
; c
= n
) {
67 if (c
->ends
!= c
->starts
) {
68 log_info("lease hunk %lx leases %ld free %ld",
69 (unsigned long)c
, (unsigned long)(c
->starts
),
70 (unsigned long)(c
->ends
));
75 /* Free all the rogue leases. */
76 for (c
= free_leases
; c
; c
= n
) {
84 struct lease
*new_leases (n
, file
, line
)
90 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
91 rval
= dmalloc ((n
+ 1) * sizeof (struct lease
), file
, line
);
93 memset (rval
, 0, sizeof (struct lease
));
95 rval
-> next
= lease_hunks
;
100 rval
= dmalloc (n
* sizeof (struct lease
), file
, line
);
105 /* If we are allocating leases in aggregations, there's really no way
106 to free one, although perhaps we can maintain a free list. */
108 isc_result_t
dhcp_lease_free (omapi_object_t
*lo
,
109 const char *file
, int line
)
112 if (lo
-> type
!= dhcp_type_lease
)
113 return DHCP_R_INVALIDARG
;
114 lease
= (struct lease
*)lo
;
115 memset (lease
, 0, sizeof (struct lease
));
116 lease
-> next
= free_leases
;
118 return ISC_R_SUCCESS
;
121 isc_result_t
dhcp_lease_get (omapi_object_t
**lp
,
122 const char *file
, int line
)
124 struct lease
**lease
= (struct lease
**)lp
;
129 free_leases
= lt
-> next
;
131 return ISC_R_SUCCESS
;
133 return ISC_R_NOMEMORY
;
135 #endif /* COMPACT_LEASES */
137 OMAPI_OBJECT_ALLOC (lease
, struct lease
, dhcp_type_lease
)
138 OMAPI_OBJECT_ALLOC (class, struct class, dhcp_type_class
)
139 OMAPI_OBJECT_ALLOC (subclass
, struct class, dhcp_type_subclass
)
140 OMAPI_OBJECT_ALLOC (pool
, struct pool
, dhcp_type_pool
)
142 #if !defined (NO_HOST_FREES) /* Scary debugging mode - don't enable! */
143 OMAPI_OBJECT_ALLOC (host
, struct host_decl
, dhcp_type_host
)
145 isc_result_t
host_allocate (struct host_decl
**p
, const char *file
, int line
)
147 return omapi_object_allocate ((omapi_object_t
**)p
,
148 dhcp_type_host
, 0, file
, line
);
151 isc_result_t
host_reference (struct host_decl
**pptr
, struct host_decl
*ptr
,
152 const char *file
, int line
)
154 return omapi_object_reference ((omapi_object_t
**)pptr
,
155 (omapi_object_t
*)ptr
, file
, line
);
158 isc_result_t
host_dereference (struct host_decl
**ptr
,
159 const char *file
, int line
)
161 if ((*ptr
) -> refcnt
== 1) {
162 log_error ("host dereferenced with refcnt == 1.");
163 #if defined (DEBUG_RC_HISTORY)
168 return omapi_object_dereference ((omapi_object_t
**)ptr
, file
, line
);
172 struct lease_state
*free_lease_states
;
174 struct lease_state
*new_lease_state (file
, line
)
178 struct lease_state
*rval
;
180 if (free_lease_states
) {
181 rval
= free_lease_states
;
183 (struct lease_state
*)(free_lease_states
-> next
);
184 dmalloc_reuse (rval
, file
, line
, 0);
186 rval
= dmalloc (sizeof (struct lease_state
), file
, line
);
190 memset (rval
, 0, sizeof *rval
);
191 if (!option_state_allocate (&rval
-> options
, file
, line
)) {
192 free_lease_state (rval
, file
, line
);
193 return (struct lease_state
*)0;
198 void free_lease_state (ptr
, file
, line
)
199 struct lease_state
*ptr
;
204 option_state_dereference (&ptr
-> options
, file
, line
);
206 packet_dereference (&ptr
-> packet
, file
, line
);
207 if (ptr
-> shared_network
)
208 shared_network_dereference (&ptr
-> shared_network
,
211 data_string_forget (&ptr
-> parameter_request_list
, file
, line
);
212 data_string_forget (&ptr
-> filename
, file
, line
);
213 data_string_forget (&ptr
-> server_name
, file
, line
);
214 ptr
-> next
= free_lease_states
;
215 free_lease_states
= ptr
;
216 dmalloc_reuse (free_lease_states
, (char *)0, 0, 0);
219 #if defined (DEBUG_MEMORY_LEAKAGE) || \
220 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
221 void relinquish_free_lease_states ()
223 struct lease_state
*cs
, *ns
;
225 for (cs
= free_lease_states
; cs
; cs
= ns
) {
229 free_lease_states
= (struct lease_state
*)0;
233 struct permit
*new_permit (file
, line
)
237 struct permit
*permit
= ((struct permit
*)
238 dmalloc (sizeof (struct permit
), file
, line
));
241 memset (permit
, 0, sizeof *permit
);
245 void free_permit (permit
, file
, line
)
246 struct permit
*permit
;
250 if (permit
-> type
== permit_class
)
251 class_dereference (&permit
-> class, MDL
);
252 dfree (permit
, file
, line
);