blob: ab36b416c218a33a4f93d8aad4b225b0abff5d71 [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é-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.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 */
24#ifndef POLARSSL_MEMORY_BUFFER_ALLOC_H
25#define POLARSSL_MEMORY_BUFFER_ALLOC_H
26
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#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
43#if !defined(POLARSSL_MEMORY_ALIGN_MULTIPLE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010044#define POLARSSL_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
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
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
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 */
75int 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 Bakker71dfa862014-02-05 15:38:15 +010080void memory_buffer_alloc_free( void );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010081
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 */
90void 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 Bakker71dfa862014-02-05 15:38:15 +010099void memory_buffer_alloc_status( void );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100100#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 Bakker71dfa862014-02-05 15:38:15 +0100113int memory_buffer_alloc_verify( void );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100114
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100115#if defined(POLARSSL_SELF_TEST)
116/**
117 * \brief Checkup routine
118 *
119 * \return 0 if successful, or 1 if a test failed
120 */
121int memory_buffer_alloc_self_test( int verbose );
122#endif
123
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100124#ifdef __cplusplus
125}
126#endif
127
128#endif /* memory_buffer_alloc.h */