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 |
| 5 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 9 | * |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | */ |
| 24 | #ifndef POLARSSL_MEMORY_BUFFER_ALLOC_H |
| 25 | #define POLARSSL_MEMORY_BUFFER_ALLOC_H |
| 26 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 28 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
| 30 | #include POLARSSL_CONFIG_FILE |
| 31 | #endif |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 32 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame^] | 33 | #include <stddef.h> |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 34 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 35 | /** |
| 36 | * \name SECTION: Module settings |
| 37 | * |
| 38 | * The configuration options you can set for this module are in this section. |
| 39 | * Either change them in config.h or define them on the compiler command line. |
| 40 | * \{ |
| 41 | */ |
| 42 | |
| 43 | #if !defined(POLARSSL_MEMORY_ALIGN_MULTIPLE) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 44 | #define POLARSSL_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */ |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 45 | #endif |
| 46 | |
| 47 | /* \} name SECTION: Module settings */ |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 48 | |
| 49 | #define MEMORY_VERIFY_NONE 0 |
| 50 | #define MEMORY_VERIFY_ALLOC (1 << 0) |
| 51 | #define MEMORY_VERIFY_FREE (1 << 1) |
| 52 | #define MEMORY_VERIFY_ALWAYS (MEMORY_VERIFY_ALLOC | MEMORY_VERIFY_FREE) |
| 53 | |
| 54 | #ifdef __cplusplus |
| 55 | extern "C" { |
| 56 | #endif |
| 57 | |
| 58 | /** |
| 59 | * \brief Initialize use of stack-based memory allocator. |
| 60 | * The stack-based allocator does memory management inside the |
| 61 | * presented buffer and does not call malloc() and free(). |
| 62 | * It sets the global polarssl_malloc() and polarssl_free() pointers |
| 63 | * to its own functions. |
| 64 | * (Provided polarssl_malloc() and polarssl_free() are thread-safe if |
| 65 | * POLARSSL_THREADING_C is defined) |
| 66 | * |
| 67 | * \note This code is not optimized and provides a straight-forward |
| 68 | * implementation of a stack-based memory allocator. |
| 69 | * |
| 70 | * \param buf buffer to use as heap |
| 71 | * \param len size of the buffer |
| 72 | * |
| 73 | * \return 0 if successful |
| 74 | */ |
| 75 | int memory_buffer_alloc_init( unsigned char *buf, size_t len ); |
| 76 | |
| 77 | /** |
| 78 | * \brief Free the mutex for thread-safety and clear remaining memory |
| 79 | */ |
Paul Bakker | 71dfa86 | 2014-02-05 15:38:15 +0100 | [diff] [blame] | 80 | void memory_buffer_alloc_free( void ); |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * \brief Determine when the allocator should automatically verify the state |
| 84 | * of the entire chain of headers / meta-data. |
| 85 | * (Default: MEMORY_VERIFY_NONE) |
| 86 | * |
| 87 | * \param verify One of MEMORY_VERIFY_NONE, MEMORY_VERIFY_ALLOC, |
| 88 | * MEMORY_VERIFY_FREE or MEMORY_VERIFY_ALWAYS |
| 89 | */ |
| 90 | void memory_buffer_set_verify( int verify ); |
| 91 | |
| 92 | #if defined(POLARSSL_MEMORY_DEBUG) |
| 93 | /** |
| 94 | * \brief Print out the status of the allocated memory (primarily for use |
| 95 | * after a program should have de-allocated all memory) |
| 96 | * Prints out a list of 'still allocated' blocks and their stack |
| 97 | * trace if POLARSSL_MEMORY_BACKTRACE is defined. |
| 98 | */ |
Paul Bakker | 71dfa86 | 2014-02-05 15:38:15 +0100 | [diff] [blame] | 99 | void memory_buffer_alloc_status( void ); |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 100 | #endif /* POLARSSL_MEMORY_DEBUG */ |
| 101 | |
| 102 | /** |
| 103 | * \brief Verifies that all headers in the memory buffer are correct |
| 104 | * and contain sane values. Helps debug buffer-overflow errors. |
| 105 | * |
| 106 | * Prints out first failure if POLARSSL_MEMORY_DEBUG is defined. |
| 107 | * Prints out full header information if POLARSSL_MEMORY_DEBUG_HEADERS |
| 108 | * is defined. (Includes stack trace information for each block if |
| 109 | * POLARSSL_MEMORY_BACKTRACE is defined as well). |
| 110 | * |
| 111 | * \returns 0 if verified, 1 otherwise |
| 112 | */ |
Paul Bakker | 71dfa86 | 2014-02-05 15:38:15 +0100 | [diff] [blame] | 113 | int memory_buffer_alloc_verify( void ); |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 114 | |
Manuel Pégourié-Gonnard | 5ba1d52 | 2014-11-27 11:33:55 +0100 | [diff] [blame] | 115 | #if defined(POLARSSL_SELF_TEST) |
| 116 | /** |
| 117 | * \brief Checkup routine |
| 118 | * |
| 119 | * \return 0 if successful, or 1 if a test failed |
| 120 | */ |
| 121 | int memory_buffer_alloc_self_test( int verbose ); |
| 122 | #endif |
| 123 | |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 124 | #ifdef __cplusplus |
| 125 | } |
| 126 | #endif |
| 127 | |
| 128 | #endif /* memory_buffer_alloc.h */ |