1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/srtp/src/crypto/kernel/alloc.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,121 @@ 1.4 +/* 1.5 + * alloc.c 1.6 + * 1.7 + * memory allocation and deallocation 1.8 + * 1.9 + * David A. McGrew 1.10 + * Cisco Systems, Inc. 1.11 + */ 1.12 +/* 1.13 + * 1.14 + * Copyright (c) 2001-2006 Cisco Systems, Inc. 1.15 + * All rights reserved. 1.16 + * 1.17 + * Redistribution and use in source and binary forms, with or without 1.18 + * modification, are permitted provided that the following conditions 1.19 + * are met: 1.20 + * 1.21 + * Redistributions of source code must retain the above copyright 1.22 + * notice, this list of conditions and the following disclaimer. 1.23 + * 1.24 + * Redistributions in binary form must reproduce the above 1.25 + * copyright notice, this list of conditions and the following 1.26 + * disclaimer in the documentation and/or other materials provided 1.27 + * with the distribution. 1.28 + * 1.29 + * Neither the name of the Cisco Systems, Inc. nor the names of its 1.30 + * contributors may be used to endorse or promote products derived 1.31 + * from this software without specific prior written permission. 1.32 + * 1.33 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.34 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.35 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 1.36 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 1.37 + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 1.38 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.39 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 1.40 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1.41 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 1.42 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.43 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 1.44 + * OF THE POSSIBILITY OF SUCH DAMAGE. 1.45 + * 1.46 + */ 1.47 + 1.48 +#include "alloc.h" 1.49 +#include "crypto_kernel.h" 1.50 + 1.51 +/* the debug module for memory allocation */ 1.52 + 1.53 +debug_module_t mod_alloc = { 1.54 + 0, /* debugging is off by default */ 1.55 + "alloc" /* printable name for module */ 1.56 +}; 1.57 + 1.58 +/* 1.59 + * Nota bene: the debugging statements for crypto_alloc() and 1.60 + * crypto_free() have identical prefixes, which include the addresses 1.61 + * of the memory locations on which they are operating. This fact can 1.62 + * be used to locate memory leaks, by turning on memory debugging, 1.63 + * grepping for 'alloc', then matching alloc and free calls by 1.64 + * address. 1.65 + */ 1.66 + 1.67 +#ifdef SRTP_KERNEL_LINUX 1.68 + 1.69 +#include <linux/interrupt.h> 1.70 + 1.71 +void * 1.72 +crypto_alloc(size_t size) { 1.73 + void *ptr; 1.74 + 1.75 + ptr = kmalloc(size, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); 1.76 + 1.77 + if (ptr) { 1.78 + debug_print(mod_alloc, "(location: %p) allocated", ptr); 1.79 + } else { 1.80 + debug_print(mod_alloc, "allocation failed (asked for %d bytes)\n", size); 1.81 + } 1.82 + 1.83 + return ptr; 1.84 +} 1.85 + 1.86 +void 1.87 +crypto_free(void *ptr) { 1.88 + 1.89 + debug_print(mod_alloc, "(location: %p) freed", ptr); 1.90 + 1.91 + kfree(ptr); 1.92 +} 1.93 + 1.94 + 1.95 +#elif defined(HAVE_STDLIB_H) 1.96 + 1.97 +void * 1.98 +crypto_alloc(size_t size) { 1.99 + void *ptr; 1.100 + 1.101 + ptr = malloc(size); 1.102 + 1.103 + if (ptr) { 1.104 + debug_print(mod_alloc, "(location: %p) allocated", ptr); 1.105 + } else { 1.106 + debug_print(mod_alloc, "allocation failed (asked for %d bytes)\n", size); 1.107 + } 1.108 + 1.109 + return ptr; 1.110 +} 1.111 + 1.112 +void 1.113 +crypto_free(void *ptr) { 1.114 + 1.115 + debug_print(mod_alloc, "(location: %p) freed", ptr); 1.116 + 1.117 + free(ptr); 1.118 +} 1.119 + 1.120 +#else /* we need to define our own memory allocation routines */ 1.121 + 1.122 +#error no memory allocation defined yet 1.123 + 1.124 +#endif