blob: 68477a477dbea848f552922166607932034f12bc [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
30#include "config.h"
31
32#include <stdlib.h>
33
Paul Bakker088c5c52014-04-25 11:11:10 +020034/**
35 * \name SECTION: Module settings
36 *
37 * The configuration options you can set for this module are in this section.
38 * Either change them in config.h or define them on the compiler command line.
39 * \{
40 */
41
42#if !defined(POLARSSL_MEMORY_ALIGN_MULTIPLE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010043#define POLARSSL_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */
Paul Bakker088c5c52014-04-25 11:11:10 +020044#endif
45
46/* \} name SECTION: Module settings */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010047
48#define MEMORY_VERIFY_NONE 0
49#define MEMORY_VERIFY_ALLOC (1 << 0)
50#define MEMORY_VERIFY_FREE (1 << 1)
51#define MEMORY_VERIFY_ALWAYS (MEMORY_VERIFY_ALLOC | MEMORY_VERIFY_FREE)
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57/**
58 * \brief Initialize use of stack-based memory allocator.
59 * The stack-based allocator does memory management inside the
60 * presented buffer and does not call malloc() and free().
61 * It sets the global polarssl_malloc() and polarssl_free() pointers
62 * to its own functions.
63 * (Provided polarssl_malloc() and polarssl_free() are thread-safe if
64 * POLARSSL_THREADING_C is defined)
65 *
66 * \note This code is not optimized and provides a straight-forward
67 * implementation of a stack-based memory allocator.
68 *
69 * \param buf buffer to use as heap
70 * \param len size of the buffer
71 *
72 * \return 0 if successful
73 */
74int memory_buffer_alloc_init( unsigned char *buf, size_t len );
75
76/**
77 * \brief Free the mutex for thread-safety and clear remaining memory
78 */
Paul Bakker71dfa862014-02-05 15:38:15 +010079void memory_buffer_alloc_free( void );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010080
81/**
82 * \brief Determine when the allocator should automatically verify the state
83 * of the entire chain of headers / meta-data.
84 * (Default: MEMORY_VERIFY_NONE)
85 *
86 * \param verify One of MEMORY_VERIFY_NONE, MEMORY_VERIFY_ALLOC,
87 * MEMORY_VERIFY_FREE or MEMORY_VERIFY_ALWAYS
88 */
89void memory_buffer_set_verify( int verify );
90
91#if defined(POLARSSL_MEMORY_DEBUG)
92/**
93 * \brief Print out the status of the allocated memory (primarily for use
94 * after a program should have de-allocated all memory)
95 * Prints out a list of 'still allocated' blocks and their stack
96 * trace if POLARSSL_MEMORY_BACKTRACE is defined.
97 */
Paul Bakker71dfa862014-02-05 15:38:15 +010098void memory_buffer_alloc_status( void );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010099#endif /* POLARSSL_MEMORY_DEBUG */
100
101/**
102 * \brief Verifies that all headers in the memory buffer are correct
103 * and contain sane values. Helps debug buffer-overflow errors.
104 *
105 * Prints out first failure if POLARSSL_MEMORY_DEBUG is defined.
106 * Prints out full header information if POLARSSL_MEMORY_DEBUG_HEADERS
107 * is defined. (Includes stack trace information for each block if
108 * POLARSSL_MEMORY_BACKTRACE is defined as well).
109 *
110 * \returns 0 if verified, 1 otherwise
111 */
Paul Bakker71dfa862014-02-05 15:38:15 +0100112int memory_buffer_alloc_verify( void );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100113
114#ifdef __cplusplus
115}
116#endif
117
118#endif /* memory_buffer_alloc.h */