blob: 3408f343bff1167a32b57612cf940b6819a921fb [file] [log] [blame]
Paul Bakkerdefc0ca2014-02-04 17:30:24 +01001/**
2 * \file memory_buffer_alloc.h
3 *
4 * \brief Buffer-based memory allocator
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerdefc0ca2014-02-04 17:30:24 +01007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +01009 *
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010010 * 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_MEMORY_BUFFER_ALLOC_H
25#define MBEDTLS_MEMORY_BUFFER_ALLOC_H
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010034
Paul Bakker088c5c52014-04-25 11:11:10 +020035/**
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_MEMORY_ALIGN_MULTIPLE)
44#define MBEDTLS_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */
Paul Bakker088c5c52014-04-25 11:11:10 +020045#endif
46
47/* \} name SECTION: Module settings */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define MBEDTLS_MEMORY_VERIFY_NONE 0
50#define MBEDTLS_MEMORY_VERIFY_ALLOC (1 << 0)
51#define MBEDTLS_MEMORY_VERIFY_FREE (1 << 1)
52#define MBEDTLS_MEMORY_VERIFY_ALWAYS (MBEDTLS_MEMORY_VERIFY_ALLOC | MBEDTLS_MEMORY_VERIFY_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010053
54#ifdef __cplusplus
55extern "C" {
56#endif
57
58/**
59 * \brief Initialize use of stack-based memory allocator.
60 * The stack-based allocator does memory management inside the
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +020061 * presented buffer and does not call calloc() and free().
62 * It sets the global mbedtls_calloc() and mbedtls_free() pointers
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010063 * to its own functions.
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +020064 * (Provided mbedtls_calloc() and mbedtls_free() are thread-safe if
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 * MBEDTLS_THREADING_C is defined)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010066 *
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
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010072 */
Manuel Pégourié-Gonnard69a69cc2015-04-29 01:05:19 +020073void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010074
75/**
76 * \brief Free the mutex for thread-safety and clear remaining memory
77 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078void mbedtls_memory_buffer_alloc_free( void );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010079
80/**
81 * \brief Determine when the allocator should automatically verify the state
82 * of the entire chain of headers / meta-data.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 * (Default: MBEDTLS_MEMORY_VERIFY_NONE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010084 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 * \param verify One of MBEDTLS_MEMORY_VERIFY_NONE, MBEDTLS_MEMORY_VERIFY_ALLOC,
86 * MBEDTLS_MEMORY_VERIFY_FREE or MBEDTLS_MEMORY_VERIFY_ALWAYS
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088void mbedtls_memory_buffer_set_verify( int verify );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010091/**
92 * \brief Print out the status of the allocated memory (primarily for use
93 * after a program should have de-allocated all memory)
94 * Prints out a list of 'still allocated' blocks and their stack
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 * trace if MBEDTLS_MEMORY_BACKTRACE is defined.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097void mbedtls_memory_buffer_alloc_status( void );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010098
99/**
100 * \brief Get the peak heap usage so far
101 *
102 * \param max_used Peak number of bytes reauested by the application
103 * \param max_blocks Peak number of blocks reauested by the application
104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100106
107/**
108 * \brief Reset peak statistics
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110void mbedtls_memory_buffer_alloc_max_reset( void );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100111
112/**
113 * \brief Get the current heap usage
114 *
115 * \param cur_used Number of bytes reauested by the application
116 * \param cur_blocks Number of blocks reauested by the application
117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks );
119#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100120
121/**
122 * \brief Verifies that all headers in the memory buffer are correct
123 * and contain sane values. Helps debug buffer-overflow errors.
124 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 * Prints out first failure if MBEDTLS_MEMORY_DEBUG is defined.
126 * Prints out full header information if MBEDTLS_MEMORY_DEBUG
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100127 * is defined. (Includes stack trace information for each block if
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 * MBEDTLS_MEMORY_BACKTRACE is defined as well).
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100129 *
130 * \returns 0 if verified, 1 otherwise
131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132int mbedtls_memory_buffer_alloc_verify( void );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100135/**
136 * \brief Checkup routine
137 *
138 * \return 0 if successful, or 1 if a test failed
139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140int mbedtls_memory_buffer_alloc_self_test( int verbose );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100141#endif
142
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100143#ifdef __cplusplus
144}
145#endif
146
147#endif /* memory_buffer_alloc.h */