Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file memory_buffer_alloc.h |
| 3 | * |
| 4 | * \brief Buffer-based memory allocator |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | * |
| 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | * See the License for the specific language governing permissions and |
| 20 | * limitations under the License. |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 21 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #ifndef MBEDTLS_MEMORY_BUFFER_ALLOC_H |
| 23 | #define MBEDTLS_MEMORY_BUFFER_ALLOC_H |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 24 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 25 | #include "mbedtls/build_info.h" |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 26 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 27 | #include <stddef.h> |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 28 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 29 | /** |
| 30 | * \name SECTION: Module settings |
| 31 | * |
| 32 | * The configuration options you can set for this module are in this section. |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 33 | * Either change them in mbedtls_config.h or define them on the compiler command |
| 34 | * line. |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 35 | * \{ |
| 36 | */ |
| 37 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #if !defined(MBEDTLS_MEMORY_ALIGN_MULTIPLE) |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 39 | /** Align on multiples of this value */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 40 | # define MBEDTLS_MEMORY_ALIGN_MULTIPLE 4 |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | /* \} name SECTION: Module settings */ |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 44 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 45 | #define MBEDTLS_MEMORY_VERIFY_NONE 0 |
| 46 | #define MBEDTLS_MEMORY_VERIFY_ALLOC (1 << 0) |
| 47 | #define MBEDTLS_MEMORY_VERIFY_FREE (1 << 1) |
| 48 | #define MBEDTLS_MEMORY_VERIFY_ALWAYS \ |
| 49 | (MBEDTLS_MEMORY_VERIFY_ALLOC | MBEDTLS_MEMORY_VERIFY_FREE) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 50 | |
| 51 | #ifdef __cplusplus |
| 52 | extern "C" { |
| 53 | #endif |
| 54 | |
| 55 | /** |
| 56 | * \brief Initialize use of stack-based memory allocator. |
| 57 | * The stack-based allocator does memory management inside the |
Manuel Pégourié-Gonnard | 200e731 | 2015-05-26 17:42:13 +0200 | [diff] [blame] | 58 | * presented buffer and does not call calloc() and free(). |
| 59 | * It sets the global mbedtls_calloc() and mbedtls_free() pointers |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 60 | * to its own functions. |
Manuel Pégourié-Gonnard | 200e731 | 2015-05-26 17:42:13 +0200 | [diff] [blame] | 61 | * (Provided mbedtls_calloc() and mbedtls_free() are thread-safe if |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | * MBEDTLS_THREADING_C is defined) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 63 | * |
| 64 | * \note This code is not optimized and provides a straight-forward |
| 65 | * implementation of a stack-based memory allocator. |
| 66 | * |
| 67 | * \param buf buffer to use as heap |
| 68 | * \param len size of the buffer |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 69 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 70 | void mbedtls_memory_buffer_alloc_init(unsigned char *buf, size_t len); |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * \brief Free the mutex for thread-safety and clear remaining memory |
| 74 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 75 | void mbedtls_memory_buffer_alloc_free(void); |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * \brief Determine when the allocator should automatically verify the state |
| 79 | * of the entire chain of headers / meta-data. |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | * (Default: MBEDTLS_MEMORY_VERIFY_NONE) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 81 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | * \param verify One of MBEDTLS_MEMORY_VERIFY_NONE, MBEDTLS_MEMORY_VERIFY_ALLOC, |
| 83 | * MBEDTLS_MEMORY_VERIFY_FREE or MBEDTLS_MEMORY_VERIFY_ALWAYS |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 84 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 85 | void mbedtls_memory_buffer_set_verify(int verify); |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 86 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | #if defined(MBEDTLS_MEMORY_DEBUG) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 88 | /** |
| 89 | * \brief Print out the status of the allocated memory (primarily for use |
| 90 | * after a program should have de-allocated all memory) |
| 91 | * Prints out a list of 'still allocated' blocks and their stack |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | * trace if MBEDTLS_MEMORY_BACKTRACE is defined. |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 93 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 94 | void mbedtls_memory_buffer_alloc_status(void); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * \brief Get the peak heap usage so far |
| 98 | * |
SimonB | 5be3a25 | 2016-05-02 22:15:42 +0100 | [diff] [blame] | 99 | * \param max_used Peak number of bytes in use or committed. This |
| 100 | * includes bytes in allocated blocks too small to split |
| 101 | * into smaller blocks but larger than the requested size. |
| 102 | * \param max_blocks Peak number of blocks in use, including free and used |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 103 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 104 | void mbedtls_memory_buffer_alloc_max_get(size_t *max_used, size_t *max_blocks); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * \brief Reset peak statistics |
| 108 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 109 | void mbedtls_memory_buffer_alloc_max_reset(void); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * \brief Get the current heap usage |
| 113 | * |
SimonB | 5be3a25 | 2016-05-02 22:15:42 +0100 | [diff] [blame] | 114 | * \param cur_used Current number of bytes in use or committed. This |
| 115 | * includes bytes in allocated blocks too small to split |
| 116 | * into smaller blocks but larger than the requested size. |
| 117 | * \param cur_blocks Current number of blocks in use, including free and used |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 118 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 119 | void mbedtls_memory_buffer_alloc_cur_get(size_t *cur_used, size_t *cur_blocks); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | #endif /* MBEDTLS_MEMORY_DEBUG */ |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * \brief Verifies that all headers in the memory buffer are correct |
| 124 | * and contain sane values. Helps debug buffer-overflow errors. |
| 125 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | * Prints out first failure if MBEDTLS_MEMORY_DEBUG is defined. |
| 127 | * Prints out full header information if MBEDTLS_MEMORY_DEBUG |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 128 | * is defined. (Includes stack trace information for each block if |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | * MBEDTLS_MEMORY_BACKTRACE is defined as well). |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 130 | * |
Manuel Pégourié-Gonnard | 81abefd | 2015-05-29 12:53:47 +0200 | [diff] [blame] | 131 | * \return 0 if verified, 1 otherwise |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 132 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 133 | int mbedtls_memory_buffer_alloc_verify(void); |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 134 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | 5ba1d52 | 2014-11-27 11:33:55 +0100 | [diff] [blame] | 136 | /** |
| 137 | * \brief Checkup routine |
| 138 | * |
| 139 | * \return 0 if successful, or 1 if a test failed |
| 140 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 141 | int mbedtls_memory_buffer_alloc_self_test(int verbose); |
Manuel Pégourié-Gonnard | 5ba1d52 | 2014-11-27 11:33:55 +0100 | [diff] [blame] | 142 | #endif |
| 143 | |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 144 | #ifdef __cplusplus |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | #endif /* memory_buffer_alloc.h */ |