1 /* alloc.c 2 3 Memory allocation... */ 4 5 /* 6 * Copyright (c) 1995, 1996, 1998 The Internet Software Consortium. 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 <mellon@fugue.com> in cooperation with Vixie 38 * Enterprises. To learn more about the Internet Software Consortium, 39 * see ``http://www.vix.com/isc''. To learn more about Vixie 40 * Enterprises, see ``http://www.vix.com''. 41 */ 42 43 #include "dhcpd.h" 44 45 struct dhcp_packet *dhcp_free_list; 46 struct packet *packet_free_list; 47 48 void * dmalloc(size, name) 49 int size; 50 char *name; 51 { 52 void *foo = calloc(size, sizeof(char)); 53 if (!foo) 54 warn ("No memory for %s.", name); 55 return foo; 56 } 57 58 void dfree(ptr, name) 59 void *ptr; 60 char *name; 61 { 62 if (!ptr) { 63 warn ("dfree %s: free on null pointer.", name); 64 return; 65 } 66 free (ptr); 67 } 68 69 struct packet *new_packet(name) 70 char *name; 71 { 72 struct packet *rval; 73 rval = (struct packet *)dmalloc(sizeof(struct packet), name); 74 return rval; 75 } 76 77 struct dhcp_packet *new_dhcp_packet(name) 78 char *name; 79 { 80 struct dhcp_packet *rval; 81 rval = (struct dhcp_packet *)dmalloc(sizeof(struct dhcp_packet), 82 name); 83 return rval; 84 } 85 86 struct tree *new_tree(name) 87 char *name; 88 { 89 struct tree *rval = dmalloc(sizeof(struct tree), name); 90 return rval; 91 } 92 93 struct string_list *new_string_list(size, name) 94 size_t size; 95 char * name; 96 { 97 struct string_list *rval; 98 99 rval =dmalloc(sizeof(struct string_list) + size, name); 100 if (rval != NULL) 101 rval->string = ((char *)rval) + sizeof(struct string_list); 102 return rval; 103 } 104 105 struct tree_cache *free_tree_caches; 106 107 struct tree_cache *new_tree_cache(name) 108 char *name; 109 { 110 struct tree_cache *rval; 111 112 if (free_tree_caches) { 113 rval = free_tree_caches; 114 free_tree_caches = 115 (struct tree_cache *)(rval->value); 116 } else { 117 rval = dmalloc(sizeof(struct tree_cache), name); 118 if (!rval) 119 error("unable to allocate tree cache for %s.", name); 120 } 121 return rval; 122 } 123 124 struct hash_table *new_hash_table(count, name) 125 int count; 126 char *name; 127 { 128 struct hash_table *rval; 129 rval = dmalloc(sizeof (struct hash_table) 130 - (DEFAULT_HASH_SIZE * sizeof(struct hash_bucket *)) 131 + (count * sizeof(struct hash_bucket *)), name); 132 if (rval == NULL) 133 return NULL; 134 rval->hash_count = count; 135 return rval; 136 } 137 138 struct hash_bucket *new_hash_bucket(name) 139 char *name; 140 { 141 struct hash_bucket *rval = dmalloc(sizeof(struct hash_bucket), name); 142 return rval; 143 } 144 145 struct lease *new_leases(n, name) 146 int n; 147 char *name; 148 { 149 struct lease *rval = dmalloc(n * sizeof(struct lease), name); 150 return rval; 151 } 152 153 struct lease *new_lease(name) 154 char *name; 155 { 156 struct lease *rval = dmalloc(sizeof(struct lease), name); 157 return rval; 158 } 159 160 struct subnet *new_subnet(name) 161 char *name; 162 { 163 struct subnet *rval = dmalloc(sizeof(struct subnet), name); 164 return rval; 165 } 166 167 struct class *new_class(name) 168 char *name; 169 { 170 struct class *rval = dmalloc(sizeof(struct class), name); 171 return rval; 172 } 173 174 struct shared_network *new_shared_network(name) 175 char *name; 176 { 177 struct shared_network *rval = 178 dmalloc (sizeof(struct shared_network), name); 179 return rval; 180 } 181 182 struct group *new_group(name) 183 char *name; 184 { 185 struct group *rval = 186 dmalloc(sizeof(struct group), name); 187 return rval; 188 } 189 190 struct protocol *new_protocol(name) 191 char *name; 192 { 193 struct protocol *rval = dmalloc(sizeof(struct protocol), name); 194 return rval; 195 } 196 197 struct lease_state *free_lease_states; 198 199 struct lease_state *new_lease_state (name) 200 char *name; 201 { 202 struct lease_state *rval; 203 204 if (free_lease_states) { 205 rval = free_lease_states; 206 free_lease_states = 207 (struct lease_state *)(free_lease_states->next); 208 } else { 209 rval = dmalloc (sizeof (struct lease_state), name); 210 } 211 return rval; 212 } 213 214 struct domain_search_list *new_domain_search_list (name) 215 char *name; 216 { 217 struct domain_search_list *rval = 218 dmalloc (sizeof (struct domain_search_list), name); 219 return rval; 220 } 221 222 struct name_server *new_name_server (name) 223 char *name; 224 { 225 struct name_server *rval = 226 dmalloc (sizeof (struct name_server), name); 227 return rval; 228 } 229 230 void free_name_server (ptr, name) 231 struct name_server *ptr; 232 char *name; 233 { 234 dfree (ptr, name); 235 } 236 237 void free_domain_search_list (ptr, name) 238 struct domain_search_list *ptr; 239 char *name; 240 { 241 dfree (ptr, name); 242 } 243 244 void free_lease_state (ptr, name) 245 struct lease_state *ptr; 246 char *name; 247 { 248 if (ptr->prl) 249 dfree (ptr->prl, name); 250 ptr->next = free_lease_states; 251 free_lease_states = ptr; 252 } 253 254 void free_protocol (ptr, name) 255 struct protocol *ptr; 256 char *name; 257 { 258 dfree (ptr, name); 259 } 260 261 void free_group (ptr, name) 262 struct group *ptr; 263 char *name; 264 { 265 dfree (ptr, name); 266 } 267 268 void free_shared_network (ptr, name) 269 struct shared_network *ptr; 270 char *name; 271 { 272 dfree (ptr, name); 273 } 274 275 void free_class (ptr, name) 276 struct class *ptr; 277 char *name; 278 { 279 dfree (ptr, name); 280 } 281 282 void free_subnet (ptr, name) 283 struct subnet *ptr; 284 char *name; 285 { 286 dfree (ptr, name); 287 } 288 289 void free_lease (ptr, name) 290 struct lease *ptr; 291 char *name; 292 { 293 dfree (ptr, name); 294 } 295 296 void free_hash_bucket (ptr, name) 297 struct hash_bucket *ptr; 298 char *name; 299 { 300 dfree (ptr, name); 301 } 302 303 void free_hash_table (ptr, name) 304 struct hash_table *ptr; 305 char *name; 306 { 307 dfree (ptr, name); 308 } 309 310 void free_tree_cache (ptr, name) 311 struct tree_cache *ptr; 312 char *name; 313 { 314 ptr->value = (unsigned char *)free_tree_caches; 315 free_tree_caches = ptr; 316 } 317 318 void free_packet (ptr, name) 319 struct packet *ptr; 320 char *name; 321 { 322 dfree (ptr, name); 323 } 324 325 void free_dhcp_packet (ptr, name) 326 struct dhcp_packet *ptr; 327 char *name; 328 { 329 dfree (ptr, name); 330 } 331 332 void free_tree (ptr, name) 333 struct tree *ptr; 334 char *name; 335 { 336 dfree (ptr, name); 337 } 338 339 void free_string_list (ptr, name) 340 struct string_list *ptr; 341 char *name; 342 { 343 dfree (ptr, name); 344 } 345