memory_buffer_alloc.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * \file memory_buffer_alloc.h
  3. *
  4. * \brief Buffer-based memory allocator
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_MEMORY_BUFFER_ALLOC_H
  26. #define MBEDTLS_MEMORY_BUFFER_ALLOC_H
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #include <stddef.h>
  33. /**
  34. * \name SECTION: Module settings
  35. *
  36. * The configuration options you can set for this module are in this section.
  37. * Either change them in config.h or define them on the compiler command line.
  38. * \{
  39. */
  40. #if !defined(MBEDTLS_MEMORY_ALIGN_MULTIPLE)
  41. #define MBEDTLS_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */
  42. #endif
  43. /* \} name SECTION: Module settings */
  44. #define MBEDTLS_MEMORY_VERIFY_NONE 0
  45. #define MBEDTLS_MEMORY_VERIFY_ALLOC (1 << 0)
  46. #define MBEDTLS_MEMORY_VERIFY_FREE (1 << 1)
  47. #define MBEDTLS_MEMORY_VERIFY_ALWAYS (MBEDTLS_MEMORY_VERIFY_ALLOC | MBEDTLS_MEMORY_VERIFY_FREE)
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. /**
  52. * \brief Initialize use of stack-based memory allocator.
  53. * The stack-based allocator does memory management inside the
  54. * presented buffer and does not call calloc() and free().
  55. * It sets the global mbedtls_calloc() and mbedtls_free() pointers
  56. * to its own functions.
  57. * (Provided mbedtls_calloc() and mbedtls_free() are thread-safe if
  58. * MBEDTLS_THREADING_C is defined)
  59. *
  60. * \note This code is not optimized and provides a straight-forward
  61. * implementation of a stack-based memory allocator.
  62. *
  63. * \param buf buffer to use as heap
  64. * \param len size of the buffer
  65. */
  66. void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len );
  67. /**
  68. * \brief Free the mutex for thread-safety and clear remaining memory
  69. */
  70. void mbedtls_memory_buffer_alloc_free( void );
  71. /**
  72. * \brief Determine when the allocator should automatically verify the state
  73. * of the entire chain of headers / meta-data.
  74. * (Default: MBEDTLS_MEMORY_VERIFY_NONE)
  75. *
  76. * \param verify One of MBEDTLS_MEMORY_VERIFY_NONE, MBEDTLS_MEMORY_VERIFY_ALLOC,
  77. * MBEDTLS_MEMORY_VERIFY_FREE or MBEDTLS_MEMORY_VERIFY_ALWAYS
  78. */
  79. void mbedtls_memory_buffer_set_verify( int verify );
  80. #if defined(MBEDTLS_MEMORY_DEBUG)
  81. /**
  82. * \brief Print out the status of the allocated memory (primarily for use
  83. * after a program should have de-allocated all memory)
  84. * Prints out a list of 'still allocated' blocks and their stack
  85. * trace if MBEDTLS_MEMORY_BACKTRACE is defined.
  86. */
  87. void mbedtls_memory_buffer_alloc_status( void );
  88. /**
  89. * \brief Get the peak heap usage so far
  90. *
  91. * \param max_used Peak number of bytes in use or committed. This
  92. * includes bytes in allocated blocks too small to split
  93. * into smaller blocks but larger than the requested size.
  94. * \param max_blocks Peak number of blocks in use, including free and used
  95. */
  96. void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks );
  97. /**
  98. * \brief Reset peak statistics
  99. */
  100. void mbedtls_memory_buffer_alloc_max_reset( void );
  101. /**
  102. * \brief Get the current heap usage
  103. *
  104. * \param cur_used Current number of bytes in use or committed. This
  105. * includes bytes in allocated blocks too small to split
  106. * into smaller blocks but larger than the requested size.
  107. * \param cur_blocks Current number of blocks in use, including free and used
  108. */
  109. void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks );
  110. #endif /* MBEDTLS_MEMORY_DEBUG */
  111. /**
  112. * \brief Verifies that all headers in the memory buffer are correct
  113. * and contain sane values. Helps debug buffer-overflow errors.
  114. *
  115. * Prints out first failure if MBEDTLS_MEMORY_DEBUG is defined.
  116. * Prints out full header information if MBEDTLS_MEMORY_DEBUG
  117. * is defined. (Includes stack trace information for each block if
  118. * MBEDTLS_MEMORY_BACKTRACE is defined as well).
  119. *
  120. * \return 0 if verified, 1 otherwise
  121. */
  122. int mbedtls_memory_buffer_alloc_verify( void );
  123. #if defined(MBEDTLS_SELF_TEST)
  124. /**
  125. * \brief Checkup routine
  126. *
  127. * \return 0 if successful, or 1 if a test failed
  128. */
  129. int mbedtls_memory_buffer_alloc_self_test( int verbose );
  130. #endif
  131. #ifdef __cplusplus
  132. }
  133. #endif
  134. #endif /* memory_buffer_alloc.h */