blob: 567a64a16456dab7d68b24759d679a2823cb06ff [file] [log] [blame]
Paul Bakker6e339b52013-07-03 13:37:05 +02001/**
2 * \file memory.h
3 *
4 * \brief Memory allocation layer
5 *
6 * Copyright (C) 2006-2013, 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_H
28#define POLARSSL_MEMORY_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
37#define POLARSSL_MEMORY_STDMALLOC malloc /**< Default allocator to use, can be undefined */
38#define POLARSSL_MEMORY_STDFREE free /**< Default free to use, can be undefined */
39#endif /* POLARSSL_CONFIG_OPTIONS */
40
41#define MEMORY_VERIFY_NONE 0
42#define MEMORY_VERIFY_ALLOC (1 << 0)
43#define MEMORY_VERIFY_FREE (1 << 1)
44#define MEMORY_VERIFY_ALWAYS (MEMORY_VERIFY_ALLOC | MEMORY_VERIFY_FREE)
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50/*
51 * The function pointers for malloc and free
52 */
53extern void * (*polarssl_malloc)( size_t len );
54extern void (*polarssl_free)( void *ptr );
55
56/**
57 * \brief Set your own memory implementation function pointers
58 *
59 * \param malloc_func the malloc function implementation
60 * \param free_func the free function implementation
61 *
62 * \return 0 if successful
63 */
64int memory_set_own( void * (*malloc_func)( size_t ),
65 void (*free_func)( void * ) );
66
67#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
68/**
69 * \brief Initialize use of stack-based memory allocator.
70 * The stack-based allocator does memory management inside the
71 * presented buffer and does not call malloc() and free().
72 * It sets the global polarssl_malloc() and polarssl_free() pointers
73 * to its own functions.
74 *
75 * \note This code is not optimized and provides a straight-forward
76 * implementation of a stack-based memory allocator.
77 *
78 * \param buf buffer to use as heap
79 * \param len size of the buffer
80 *
81 * \return 0 if successful
82 */
83int memory_buffer_alloc_init( unsigned char *buf, size_t len );
84
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 */
102void memory_buffer_alloc_status();
103#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 */
116int memory_buffer_alloc_verify();
117
118#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */
119
120#ifdef __cplusplus
121}
122#endif
123
124#endif /* memory.h */