OLD | NEW |
1 /* | 1 /* |
2 * alloc.c | 2 * alloc.c |
3 * | 3 * |
4 * memory allocation and deallocation | 4 * memory allocation and deallocation |
5 * | 5 * |
6 * David A. McGrew | 6 * David A. McGrew |
7 * Cisco Systems, Inc. | 7 * Cisco Systems, Inc. |
8 */ | 8 */ |
9 /* | 9 /* |
10 * | 10 * |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 | 44 |
45 #ifdef HAVE_CONFIG_H | 45 #ifdef HAVE_CONFIG_H |
46 #include <config.h> | 46 #include <config.h> |
47 #endif | 47 #endif |
48 | 48 |
49 #include "alloc.h" | 49 #include "alloc.h" |
50 #include "crypto_kernel.h" | 50 #include "crypto_kernel.h" |
51 | 51 |
52 /* the debug module for memory allocation */ | 52 /* the debug module for memory allocation */ |
53 | 53 |
54 debug_module_t mod_alloc = { | 54 srtp_debug_module_t mod_alloc = { |
55 0, /* debugging is off by default */ | 55 0, /* debugging is off by default */ |
56 "alloc" /* printable name for module */ | 56 "alloc" /* printable name for module */ |
57 }; | 57 }; |
58 | 58 |
59 /* | 59 /* |
60 * Nota bene: the debugging statements for crypto_alloc() and | 60 * Nota bene: the debugging statements for srtp_crypto_alloc() and |
61 * crypto_free() have identical prefixes, which include the addresses | 61 * srtp_crypto_free() have identical prefixes, which include the addresses |
62 * of the memory locations on which they are operating. This fact can | 62 * of the memory locations on which they are operating. This fact can |
63 * be used to locate memory leaks, by turning on memory debugging, | 63 * be used to locate memory leaks, by turning on memory debugging, |
64 * grepping for 'alloc', then matching alloc and free calls by | 64 * grepping for 'alloc', then matching alloc and free calls by |
65 * address. | 65 * address. |
66 */ | 66 */ |
67 | 67 |
68 #ifdef SRTP_KERNEL_LINUX | 68 #if defined(HAVE_STDLIB_H) |
69 | 69 |
70 #include <linux/interrupt.h> | 70 void * srtp_crypto_alloc(size_t size) { |
71 | |
72 void * | |
73 crypto_alloc(size_t size) { | |
74 void *ptr; | |
75 | |
76 ptr = kmalloc(size, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL); | |
77 | |
78 if (ptr) { | |
79 debug_print(mod_alloc, "(location: %p) allocated", ptr); | |
80 } else { | |
81 debug_print(mod_alloc, "allocation failed (asked for %d bytes)\n", size); | |
82 } | |
83 | |
84 return ptr; | |
85 } | |
86 | |
87 void | |
88 crypto_free(void *ptr) { | |
89 | |
90 debug_print(mod_alloc, "(location: %p) freed", ptr); | |
91 | |
92 kfree(ptr); | |
93 } | |
94 | |
95 | |
96 #elif defined(HAVE_STDLIB_H) | |
97 | |
98 void * | |
99 crypto_alloc(size_t size) { | |
100 void *ptr; | 71 void *ptr; |
101 | 72 |
102 ptr = malloc(size); | 73 ptr = malloc(size); |
103 | 74 |
104 if (ptr) { | 75 if (ptr) { |
105 debug_print(mod_alloc, "(location: %p) allocated", ptr); | 76 debug_print(mod_alloc, "(location: %p) allocated", ptr); |
106 } else { | 77 } else { |
107 debug_print(mod_alloc, "allocation failed (asked for %d bytes)\n", size); | 78 debug_print(mod_alloc, "allocation failed (asked for %d bytes)\n", size); |
108 } | 79 } |
109 | 80 |
110 return ptr; | 81 return ptr; |
111 } | 82 } |
112 | 83 |
113 void | 84 void srtp_crypto_free(void *ptr) { |
114 crypto_free(void *ptr) { | |
115 | 85 |
116 debug_print(mod_alloc, "(location: %p) freed", ptr); | 86 debug_print(mod_alloc, "(location: %p) freed", ptr); |
117 | 87 |
118 free(ptr); | 88 free(ptr); |
119 } | 89 } |
120 | 90 |
121 #else /* we need to define our own memory allocation routines */ | 91 #else /* we need to define our own memory allocation routines */ |
122 | 92 |
123 #error no memory allocation defined yet | 93 #error no memory allocation defined yet |
124 | 94 |
125 #endif | 95 #endif |
OLD | NEW |