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