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