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