File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / dhcp / server / salloc.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Oct 9 09:06:55 2012 UTC (11 years, 8 months ago) by misho
Branches: dhcp, MAIN
CVS tags: v4_1_R7p0, v4_1_R7, v4_1_R4, HEAD
dhcp 4.1 r7

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>