blob: ccddc00205a248213ea63df9002db071be6bf018 [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
34#if !defined(POLARSSL_CONFIG_OPTIONS)
35#define POLARSSL_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */
36#endif /* POLARSSL_CONFIG_OPTIONS */
37
38#define MEMORY_VERIFY_NONE 0
39#define MEMORY_VERIFY_ALLOC (1 << 0)
40#define MEMORY_VERIFY_FREE (1 << 1)
41#define MEMORY_VERIFY_ALWAYS (MEMORY_VERIFY_ALLOC | MEMORY_VERIFY_FREE)
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47/**
48 * \brief Initialize use of stack-based memory allocator.
49 * The stack-based allocator does memory management inside the
50 * presented buffer and does not call malloc() and free().
51 * It sets the global polarssl_malloc() and polarssl_free() pointers
52 * to its own functions.
53 * (Provided polarssl_malloc() and polarssl_free() are thread-safe if
54 * POLARSSL_THREADING_C is defined)
55 *
56 * \note This code is not optimized and provides a straight-forward
57 * implementation of a stack-based memory allocator.
58 *
59 * \param buf buffer to use as heap
60 * \param len size of the buffer
61 *
62 * \return 0 if successful
63 */
64int memory_buffer_alloc_init( unsigned char *buf, size_t len );
65
66/**
67 * \brief Free the mutex for thread-safety and clear remaining memory
68 */
Paul Bakker71dfa862014-02-05 15:38:15 +010069void memory_buffer_alloc_free( void );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010070
71/**
72 * \brief Determine when the allocator should automatically verify the state
73 * of the entire chain of headers / meta-data.
74 * (Default: MEMORY_VERIFY_NONE)
75 *
76 * \param verify One of MEMORY_VERIFY_NONE, MEMORY_VERIFY_ALLOC,
77 * MEMORY_VERIFY_FREE or MEMORY_VERIFY_ALWAYS
78 */
79void memory_buffer_set_verify( int verify );
80
81#if defined(POLARSSL_MEMORY_DEBUG)
82/**
83 * \brief Print out the status of the allocated memory (primarily for use
84 * after a program should have de-allocated all memory)
85 * Prints out a list of 'still allocated' blocks and their stack
86 * trace if POLARSSL_MEMORY_BACKTRACE is defined.
87 */
Paul Bakker71dfa862014-02-05 15:38:15 +010088void memory_buffer_alloc_status( void );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010089#endif /* POLARSSL_MEMORY_DEBUG */
90
91/**
92 * \brief Verifies that all headers in the memory buffer are correct
93 * and contain sane values. Helps debug buffer-overflow errors.
94 *
95 * Prints out first failure if POLARSSL_MEMORY_DEBUG is defined.
96 * Prints out full header information if POLARSSL_MEMORY_DEBUG_HEADERS
97 * is defined. (Includes stack trace information for each block if
98 * POLARSSL_MEMORY_BACKTRACE is defined as well).
99 *
100 * \returns 0 if verified, 1 otherwise
101 */
Paul Bakker71dfa862014-02-05 15:38:15 +0100102int memory_buffer_alloc_verify( void );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100103
104#ifdef __cplusplus
105}
106#endif
107
108#endif /* memory_buffer_alloc.h */