Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
memory_buffer_alloc.h
00001 /** 00002 * \file memory_buffer_alloc.h 00003 * 00004 * \brief Buffer-based memory allocator 00005 */ 00006 /* 00007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 00008 * SPDX-License-Identifier: Apache-2.0 00009 * 00010 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00011 * not use this file except in compliance with the License. 00012 * You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, software 00017 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00018 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 * See the License for the specific language governing permissions and 00020 * limitations under the License. 00021 * 00022 * This file is part of mbed TLS (https://tls.mbed.org) 00023 */ 00024 #ifndef MBEDTLS_MEMORY_BUFFER_ALLOC_H 00025 #define MBEDTLS_MEMORY_BUFFER_ALLOC_H 00026 00027 #if !defined(MBEDTLS_CONFIG_FILE) 00028 #include "config.h" 00029 #else 00030 #include MBEDTLS_CONFIG_FILE 00031 #endif 00032 00033 #include <stddef.h> 00034 00035 /** 00036 * \name SECTION: Module settings 00037 * 00038 * The configuration options you can set for this module are in this section. 00039 * Either change them in config.h or define them on the compiler command line. 00040 * \{ 00041 */ 00042 00043 #if !defined(MBEDTLS_MEMORY_ALIGN_MULTIPLE) 00044 #define MBEDTLS_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */ 00045 #endif 00046 00047 /* \} name SECTION: Module settings */ 00048 00049 #define MBEDTLS_MEMORY_VERIFY_NONE 0 00050 #define MBEDTLS_MEMORY_VERIFY_ALLOC (1 << 0) 00051 #define MBEDTLS_MEMORY_VERIFY_FREE (1 << 1) 00052 #define MBEDTLS_MEMORY_VERIFY_ALWAYS (MBEDTLS_MEMORY_VERIFY_ALLOC | MBEDTLS_MEMORY_VERIFY_FREE) 00053 00054 #ifdef __cplusplus 00055 extern "C" { 00056 #endif 00057 00058 /** 00059 * \brief Initialize use of stack-based memory allocator. 00060 * The stack-based allocator does memory management inside the 00061 * presented buffer and does not call calloc() and free(). 00062 * It sets the global mbedtls_calloc() and mbedtls_free() pointers 00063 * to its own functions. 00064 * (Provided mbedtls_calloc() and mbedtls_free() are thread-safe if 00065 * MBEDTLS_THREADING_C is defined) 00066 * 00067 * \note This code is not optimized and provides a straight-forward 00068 * implementation of a stack-based memory allocator. 00069 * 00070 * \param buf buffer to use as heap 00071 * \param len size of the buffer 00072 */ 00073 void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len ); 00074 00075 /** 00076 * \brief Free the mutex for thread-safety and clear remaining memory 00077 */ 00078 void mbedtls_memory_buffer_alloc_free( void ); 00079 00080 /** 00081 * \brief Determine when the allocator should automatically verify the state 00082 * of the entire chain of headers / meta-data. 00083 * (Default: MBEDTLS_MEMORY_VERIFY_NONE) 00084 * 00085 * \param verify One of MBEDTLS_MEMORY_VERIFY_NONE, MBEDTLS_MEMORY_VERIFY_ALLOC, 00086 * MBEDTLS_MEMORY_VERIFY_FREE or MBEDTLS_MEMORY_VERIFY_ALWAYS 00087 */ 00088 void mbedtls_memory_buffer_set_verify( int verify ); 00089 00090 #if defined(MBEDTLS_MEMORY_DEBUG) 00091 /** 00092 * \brief Print out the status of the allocated memory (primarily for use 00093 * after a program should have de-allocated all memory) 00094 * Prints out a list of 'still allocated' blocks and their stack 00095 * trace if MBEDTLS_MEMORY_BACKTRACE is defined. 00096 */ 00097 void mbedtls_memory_buffer_alloc_status( void ); 00098 00099 /** 00100 * \brief Get the peak heap usage so far 00101 * 00102 * \param max_used Peak number of bytes in use or committed. This 00103 * includes bytes in allocated blocks too small to split 00104 * into smaller blocks but larger than the requested size. 00105 * \param max_blocks Peak number of blocks in use, including free and used 00106 */ 00107 void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks ); 00108 00109 /** 00110 * \brief Reset peak statistics 00111 */ 00112 void mbedtls_memory_buffer_alloc_max_reset( void ); 00113 00114 /** 00115 * \brief Get the current heap usage 00116 * 00117 * \param cur_used Current number of bytes in use or committed. This 00118 * includes bytes in allocated blocks too small to split 00119 * into smaller blocks but larger than the requested size. 00120 * \param cur_blocks Current number of blocks in use, including free and used 00121 */ 00122 void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks ); 00123 #endif /* MBEDTLS_MEMORY_DEBUG */ 00124 00125 /** 00126 * \brief Verifies that all headers in the memory buffer are correct 00127 * and contain sane values. Helps debug buffer-overflow errors. 00128 * 00129 * Prints out first failure if MBEDTLS_MEMORY_DEBUG is defined. 00130 * Prints out full header information if MBEDTLS_MEMORY_DEBUG 00131 * is defined. (Includes stack trace information for each block if 00132 * MBEDTLS_MEMORY_BACKTRACE is defined as well). 00133 * 00134 * \return 0 if verified, 1 otherwise 00135 */ 00136 int mbedtls_memory_buffer_alloc_verify( void ); 00137 00138 #if defined(MBEDTLS_SELF_TEST) 00139 /** 00140 * \brief Checkup routine 00141 * 00142 * \return 0 if successful, or 1 if a test failed 00143 */ 00144 int mbedtls_memory_buffer_alloc_self_test( int verbose ); 00145 #endif 00146 00147 #ifdef __cplusplus 00148 } 00149 #endif 00150 00151 #endif /* memory_buffer_alloc.h */
Generated on Tue Jul 12 2022 14:24:27 by
