1386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari/*
2386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * alloc.c
3386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
4386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * memory allocation and deallocation
5386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
6386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * David A. McGrew
7386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * Cisco Systems, Inc.
8386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari */
9386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari/*
10386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
11386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * Copyright (c) 2001-2006 Cisco Systems, Inc.
12386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * All rights reserved.
13386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
14386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * Redistribution and use in source and binary forms, with or without
15386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * modification, are permitted provided that the following conditions
16386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * are met:
17386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
18386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   Redistributions of source code must retain the above copyright
19386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   notice, this list of conditions and the following disclaimer.
20386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
21386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   Redistributions in binary form must reproduce the above
22386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   copyright notice, this list of conditions and the following
23386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   disclaimer in the documentation and/or other materials provided
24386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   with the distribution.
25386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
26386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   Neither the name of the Cisco Systems, Inc. nor the names of its
27386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   contributors may be used to endorse or promote products derived
28386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *   from this software without specific prior written permission.
29386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
30386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * OF THE POSSIBILITY OF SUCH DAMAGE.
42386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari *
43386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari */
44386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
45386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#include "alloc.h"
46386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#include "crypto_kernel.h"
47386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
48386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari/* the debug module for memory allocation */
49386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
50386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagaridebug_module_t mod_alloc = {
51386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  0,                  /* debugging is off by default */
52386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  "alloc"             /* printable name for module   */
53386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari};
54386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
55386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari/*
56386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * Nota bene: the debugging statements for crypto_alloc() and
57386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * crypto_free() have identical prefixes, which include the addresses
58386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * of the memory locations on which they are operating.  This fact can
59386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * be used to locate memory leaks, by turning on memory debugging,
60386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * grepping for 'alloc', then matching alloc and free calls by
61386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari * address.
62386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari */
63386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
64386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#ifdef SRTP_KERNEL_LINUX
65386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
66386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#include <linux/interrupt.h>
67386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
68386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagarivoid *
69386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagaricrypto_alloc(size_t size) {
70386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  void *ptr;
71386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
72386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  ptr = kmalloc(size, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
73386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
74386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  if (ptr) {
75386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    debug_print(mod_alloc, "(location: %p) allocated", ptr);
76386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  } else
77386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    debug_print(mod_alloc, "allocation failed (asked for %d bytes)\n", size);
78386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
79386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  return ptr;
80386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari}
81386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
82386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagarivoid
83386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagaricrypto_free(void *ptr) {
84386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
85386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  debug_print(mod_alloc, "(location: %p) freed", ptr);
86386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
87386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  kfree(ptr);
88386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari}
89386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
90386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
91386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#elif defined(HAVE_STDLIB_H)
92386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
93386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagarivoid *
94386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagaricrypto_alloc(size_t size) {
95386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  void *ptr;
96386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
97386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  ptr = malloc(size);
98386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
99386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  if (ptr) {
100386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    debug_print(mod_alloc, "(location: %p) allocated", ptr);
101386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  } else
102386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari    debug_print(mod_alloc, "allocation failed (asked for %d bytes)\n", size);
103386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
104386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  return ptr;
105386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari}
106386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
107386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagarivoid
108386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagaricrypto_free(void *ptr) {
109386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
110386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  debug_print(mod_alloc, "(location: %p) freed", ptr);
111386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
112386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari  free(ptr);
113386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari}
114386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
115386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#else  /* we need to define our own memory allocation routines */
116386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
117386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#error no memory allocation defined yet
118386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari
119386ce4d9144fc190797f4e43a31aeaf76ca2e373Param Reddappagari#endif
120