server/salloc.c

Go to the documentation of this file.
00001 /* salloc.c
00002 
00003    Memory allocation for the DHCP server... */
00004 
00005 /*
00006  * Copyright (c) 2009,2012,2014 by Internet Systems Consortium, Inc. ("ISC")
00007  * Copyright (c) 2004-2007 by Internet Systems Consortium, Inc. ("ISC")
00008  * Copyright (c) 1996-2003 by Internet Software Consortium
00009  *
00010  * Permission to use, copy, modify, and distribute this software for any
00011  * purpose with or without fee is hereby granted, provided that the above
00012  * copyright notice and this permission notice appear in all copies.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
00015  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00016  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
00017  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00018  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00019  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
00020  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00021  *
00022  *   Internet Systems Consortium, Inc.
00023  *   950 Charter Street
00024  *   Redwood City, CA 94063
00025  *   <info@isc.org>
00026  *   https://www.isc.org/
00027  *
00028  */
00029 
00030 #include "dhcpd.h"
00031 #include <omapip/omapip_p.h>
00032 
00033 #if defined (COMPACT_LEASES)
00034 struct lease *free_leases;
00035 
00036 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
00037 struct lease *lease_hunks;
00038 
00039 void relinquish_lease_hunks ()
00040 {
00041         struct lease *c, *n, **p;
00042         int i;
00043 
00044         /* Account for all the leases on the free list. */
00045         for (n = lease_hunks; n; n = n->next) {
00046             for (i = 1; i < n->starts + 1; i++) {
00047                 p = &free_leases;
00048                 for (c = free_leases; c; c = c->next) {
00049                     if (c == &n[i]) {
00050                         *p = c->next;
00051                         n->ends++;
00052                         break;
00053                     }
00054                     p = &c->next;
00055                 }
00056                 if (!c) {
00057                     log_info("lease %s refcnt %d",
00058                              piaddr (n[i].ip_addr), n[i].refcnt);
00059 #if defined (DEBUG_RC_HISTORY)
00060                     dump_rc_history(&n[i]);
00061 #endif
00062                 }
00063             }
00064         }
00065 
00066         for (c = lease_hunks; c; c = n) {
00067                 n = c->next;
00068                 if (c->ends != c->starts) {
00069                         log_info("lease hunk %lx leases %ld free %ld",
00070                                  (unsigned long)c, (unsigned long)(c->starts),
00071                                  (unsigned long)(c->ends));
00072                 }
00073                 dfree(c, MDL);
00074         }
00075 
00076         /* Free all the rogue leases. */
00077         for (c = free_leases; c; c = n) {
00078                 n = c->next;
00079                 dfree(c, MDL);
00080         }
00081 }
00082 #endif
00083 
00084 struct lease *new_leases (n, file, line)
00085         unsigned n;
00086         const char *file;
00087         int line;
00088 {
00089         struct lease *rval;
00090 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
00091         rval = dmalloc ((n + 1) * sizeof (struct lease), file, line);
00092         memset (rval, 0, sizeof (struct lease));
00093         rval -> starts = n;
00094         rval -> next = lease_hunks;
00095         lease_hunks = rval;
00096         rval++;
00097 #else
00098         rval = dmalloc (n * sizeof (struct lease), file, line);
00099 #endif
00100         return rval;
00101 }
00102 
00103 /* If we are allocating leases in aggregations, there's really no way
00104    to free one, although perhaps we can maintain a free list. */
00105 
00106 isc_result_t dhcp_lease_free (omapi_object_t *lo,
00107                               const char *file, int line)
00108 {
00109         struct lease *lease;
00110         if (lo -> type != dhcp_type_lease)
00111                 return DHCP_R_INVALIDARG;
00112         lease = (struct lease *)lo;
00113         memset (lease, 0, sizeof (struct lease));
00114         lease -> next = free_leases;
00115         free_leases = lease;
00116         return ISC_R_SUCCESS;
00117 }
00118 
00119 isc_result_t dhcp_lease_get (omapi_object_t **lp,
00120                              const char *file, int line)
00121 {
00122         struct lease **lease = (struct lease **)lp;
00123         struct lease *lt;
00124 
00125         if (free_leases) {
00126                 lt = free_leases;
00127                 free_leases = lt -> next;
00128                 *lease = lt;
00129                 return ISC_R_SUCCESS;
00130         }
00131         return ISC_R_NOMEMORY;
00132 }
00133 #endif /* COMPACT_LEASES */
00134 
00135 OMAPI_OBJECT_ALLOC (lease, struct lease, dhcp_type_lease)
00136 OMAPI_OBJECT_ALLOC (class, struct class, dhcp_type_class)
00137 OMAPI_OBJECT_ALLOC (subclass, struct class, dhcp_type_subclass)
00138 OMAPI_OBJECT_ALLOC (pool, struct pool, dhcp_type_pool)
00139 
00140 #if !defined (NO_HOST_FREES)    /* Scary debugging mode - don't enable! */
00141 OMAPI_OBJECT_ALLOC (host, struct host_decl, dhcp_type_host)
00142 #else
00143 isc_result_t host_allocate (struct host_decl **p, const char *file, int line)
00144 {
00145         return omapi_object_allocate ((omapi_object_t **)p,
00146                                       dhcp_type_host, 0, file, line);
00147 }
00148 
00149 isc_result_t host_reference (struct host_decl **pptr, struct host_decl *ptr,
00150                                const char *file, int line)
00151 {
00152         return omapi_object_reference ((omapi_object_t **)pptr,
00153                                        (omapi_object_t *)ptr, file, line);
00154 }
00155 
00156 isc_result_t host_dereference (struct host_decl **ptr,
00157                                const char *file, int line)
00158 {
00159         if ((*ptr) -> refcnt == 1) {
00160                 log_error ("host dereferenced with refcnt == 1.");
00161 #if defined (DEBUG_RC_HISTORY)
00162                 dump_rc_history ();
00163 #endif
00164                 abort ();
00165         }
00166         return omapi_object_dereference ((omapi_object_t **)ptr, file, line);
00167 }
00168 #endif
00169 
00170 struct lease_state *free_lease_states;
00171 
00172 struct lease_state *new_lease_state (file, line)
00173         const char *file;
00174         int line;
00175 {
00176         struct lease_state *rval;
00177 
00178         if (free_lease_states) {
00179                 rval = free_lease_states;
00180                 free_lease_states =
00181                         (struct lease_state *)(free_lease_states -> next);
00182                 dmalloc_reuse (rval, file, line, 0);
00183         } else {
00184                 rval = dmalloc (sizeof (struct lease_state), file, line);
00185                 if (!rval)
00186                         return rval;
00187         }
00188         memset (rval, 0, sizeof *rval);
00189         if (!option_state_allocate (&rval -> options, file, line)) {
00190                 free_lease_state (rval, file, line);
00191                 return (struct lease_state *)0;
00192         }
00193         return rval;
00194 }
00195 
00196 void free_lease_state (ptr, file, line)
00197         struct lease_state *ptr;
00198         const char *file;
00199         int line;
00200 {
00201         if (ptr -> options)
00202                 option_state_dereference (&ptr -> options, file, line);
00203         if (ptr -> packet)
00204                 packet_dereference (&ptr -> packet, file, line);
00205         if (ptr -> shared_network)
00206                 shared_network_dereference (&ptr -> shared_network,
00207                                             file, line);
00208 
00209         data_string_forget (&ptr -> parameter_request_list, file, line);
00210         data_string_forget (&ptr -> filename, file, line);
00211         data_string_forget (&ptr -> server_name, file, line);
00212         ptr -> next = free_lease_states;
00213         free_lease_states = ptr;
00214         dmalloc_reuse (free_lease_states, (char *)0, 0, 0);
00215 }
00216 
00217 #if defined (DEBUG_MEMORY_LEAKAGE) || \
00218                 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
00219 void relinquish_free_lease_states ()
00220 {
00221         struct lease_state *cs, *ns;
00222 
00223         for (cs = free_lease_states; cs; cs = ns) {
00224                 ns = cs -> next;
00225                 dfree (cs, MDL);
00226         }
00227         free_lease_states = (struct lease_state *)0;
00228 }
00229 #endif
00230 
00231 struct permit *new_permit (file, line)
00232         const char *file;
00233         int line;
00234 {
00235         struct permit *permit = ((struct permit *)
00236                                  dmalloc (sizeof (struct permit), file, line));
00237         if (!permit)
00238                 return permit;
00239         memset (permit, 0, sizeof *permit);
00240         return permit;
00241 }
00242 
00243 void free_permit (permit, file, line)
00244         struct permit *permit;
00245         const char *file;
00246         int line;
00247 {
00248         if (permit -> type == permit_class)
00249                 class_dereference (&permit -> class, MDL);
00250         dfree (permit, file, line);
00251 }

Generated on 5 Apr 2014 for ISC DHCP by  doxygen 1.6.1