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