alloc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * alloc.c
  3. *
  4. * memory allocation and deallocation
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2006 Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #include "alloc.h"
  45. #include "crypto_kernel.h"
  46. /* the debug module for memory allocation */
  47. debug_module_t mod_alloc = {
  48. 0, /* debugging is off by default */
  49. "alloc" /* printable name for module */
  50. };
  51. /*
  52. * Nota bene: the debugging statements for crypto_alloc() and
  53. * crypto_free() have identical prefixes, which include the addresses
  54. * of the memory locations on which they are operating. This fact can
  55. * be used to locate memory leaks, by turning on memory debugging,
  56. * grepping for 'alloc', then matching alloc and free calls by
  57. * address.
  58. */
  59. #ifdef SRTP_KERNEL_LINUX
  60. #include <linux/interrupt.h>
  61. void *
  62. crypto_alloc(size_t size) {
  63. void *ptr;
  64. ptr = kmalloc(size, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
  65. if (ptr) {
  66. debug_print(mod_alloc, "(location: %p) allocated", ptr);
  67. } else {
  68. debug_print(mod_alloc, "allocation failed (asked for %d bytes)\n", size);
  69. }
  70. return ptr;
  71. }
  72. void
  73. crypto_free(void *ptr) {
  74. debug_print(mod_alloc, "(location: %p) freed", ptr);
  75. kfree(ptr);
  76. }
  77. #elif defined(HAVE_STDLIB_H)
  78. void *
  79. crypto_alloc(size_t size) {
  80. void *ptr;
  81. ptr = malloc(size);
  82. if (ptr) {
  83. debug_print(mod_alloc, "(location: %p) allocated", ptr);
  84. } else
  85. debug_print(mod_alloc, "allocation failed (asked for %d bytes)\n", size);
  86. return ptr;
  87. }
  88. void
  89. crypto_free(void *ptr) {
  90. debug_print(mod_alloc, "(location: %p) freed", ptr);
  91. free(ptr);
  92. }
  93. #else /* we need to define our own memory allocation routines */
  94. #error no memory allocation defined yet
  95. #endif