Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers memory_buffer_alloc.h Source File

memory_buffer_alloc.h

Go to the documentation of this file.
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 in use or committed. This
00102  *                      includes bytes in allocated blocks too small to split
00103  *                      into smaller blocks but larger than the requested size.
00104  * \param max_blocks    Peak number of blocks in use, including free and used
00105  */
00106 void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks );
00107 
00108 /**
00109  * \brief   Reset peak statistics
00110  */
00111 void mbedtls_memory_buffer_alloc_max_reset( void );
00112 
00113 /**
00114  * \brief   Get the current heap usage
00115  *
00116  * \param cur_used      Current number of bytes in use or committed. This
00117  *                      includes bytes in allocated blocks too small to split
00118  *                      into smaller blocks but larger than the requested size.
00119  * \param cur_blocks    Current number of blocks in use, including free and used
00120  */
00121 void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks );
00122 #endif /* MBEDTLS_MEMORY_DEBUG */
00123 
00124 /**
00125  * \brief   Verifies that all headers in the memory buffer are correct
00126  *          and contain sane values. Helps debug buffer-overflow errors.
00127  *
00128  *          Prints out first failure if MBEDTLS_MEMORY_DEBUG is defined.
00129  *          Prints out full header information if MBEDTLS_MEMORY_DEBUG
00130  *          is defined. (Includes stack trace information for each block if
00131  *          MBEDTLS_MEMORY_BACKTRACE is defined as well).
00132  *
00133  * \return             0 if verified, 1 otherwise
00134  */
00135 int mbedtls_memory_buffer_alloc_verify( void );
00136 
00137 #if defined(MBEDTLS_SELF_TEST)
00138 /**
00139  * \brief          Checkup routine
00140  *
00141  * \return         0 if successful, or 1 if a test failed
00142  */
00143 int mbedtls_memory_buffer_alloc_self_test( int verbose );
00144 #endif
00145 
00146 #ifdef __cplusplus
00147 }
00148 #endif
00149 
00150 #endif /* memory_buffer_alloc.h */