Contiki-NG
heapmem.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005, Nicolas Tsiftes
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of the contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/**
31 * \addtogroup mem
32 * @{
33 */
34
35/**
36 * \defgroup heapmem heapmem: Dynamic heap memory allocator
37 *
38 * The heapmem module is a dynamic heap memory allocator similar to
39 * malloc() in standard C. The heap memory is managed in a block of
40 * static memory, whose size is determined at compile-time by setting
41 * HEAPMEM_CONF_ARENA_SIZE parameter. By default, the heap memory is
42 * only 1 bytes, which entails that this parameter must be set
43 * explicitly in order to be possible to use this module.
44 *
45 * Each allocated memory object is referred to as a "chunk". The
46 * allocator manages free chunks in a double-linked list. While this
47 * adds some memory overhead compared to a single-linked list, it
48 * improves the performance of list management.
49 *
50 * Internally, allocated chunks can be retrieved using the pointer to
51 * the allocated memory returned by heapmem_alloc() and
52 * heapmem_realloc(), because the chunk structure immediately precedes
53 * the memory of the chunk.
54 *
55 * \note If the HEAPMEM_CONF_ARENA_SIZE parameter is not set, the
56 * heapmem implementation will not be compiled, which could lead to a
57 * linking error if other modules call heapmem functions.
58 *
59 * \note This module does not contain a corresponding function to the
60 * standard C function calloc().
61 *
62 * \note Dynamic memory should be used carefully on
63 * memory-constrained, embedded systems, because fragmentation
64 * may be induced through various allocation/deallocation
65 * patterns, and no guarantees are given regarding the
66 * availability of memory.
67 *
68 * @{
69 */
70
71/**
72 * \file
73 * Header file for the dynamic heap memory allocator.
74 * \author
75 * Nicolas Tsiftes <nvt@acm.org>
76 */
77
78#ifndef HEAPMEM_H
79#define HEAPMEM_H
80
81#include "contiki.h"
82
83#include <stdlib.h>
84/*****************************************************************************/
85#ifndef HEAPMEM_DEBUG
86#define HEAPMEM_DEBUG 0
87#endif
88/*****************************************************************************/
89typedef struct heapmem_stats {
90 size_t allocated;
91 size_t overhead;
92 size_t available;
93 size_t footprint;
94 size_t chunks;
95} heapmem_stats_t;
96/*****************************************************************************/
97typedef uint8_t heapmem_zone_t;
98
99#define HEAPMEM_ZONE_INVALID (heapmem_zone_t)-1
100#define HEAPMEM_ZONE_GENERAL 0
101/*****************************************************************************/
102
103/**
104 * \brief Register a zone with a reserved subdivision of the heap.
105 * \param name A pointer to a chunk that has been allocated using
106 * heapmem_alloc() or heapmem_realloc().
107 * \param zone_size The number of bytes to reserve for the zone.
108 * \return A zone ID if the allocation succeeds, or HEAPMEM_ZONE_INVALID if it fails.
109 */
110heapmem_zone_t heapmem_zone_register(const char *name, size_t zone_size);
111/*****************************************************************************/
112
113#if HEAPMEM_DEBUG
114
115#define heapmem_alloc(size) heapmem_zone_alloc_debug(HEAPMEM_ZONE_GENERAL, (size), __FILE__, __LINE__)
116#define heapmem_zone_alloc(size) heapmem_zone_alloc_debug((zone), (size), __FILE__, __LINE__)
117#define heapmem_realloc(ptr, size) heapmem_realloc_debug((ptr), (size), __FILE__, __LINE__)
118#define heapmem_free(ptr) heapmem_free_debug((ptr), __FILE__, __LINE__)
119
120void *heapmem_alloc_debug(size_t size,
121 const char *file, const unsigned line);
122void *heapmem_zone_alloc_debug(heapmem_zone_t, size_t size,
123 const char *file, const unsigned line);
124void *heapmem_realloc_debug(void *ptr, size_t size,
125 const char *file, const unsigned line);
126bool heapmem_free_debug(void *ptr,
127 const char *file, const unsigned line);
128
129#else
130
131/**
132 * \brief Allocate a chunk of memory in the general zone of the heap.
133 * \param size The number of bytes to allocate.
134 * \return A pointer to the allocated memory chunk,
135 * or NULL if the allocation failed.
136 *
137 * \sa heapmem_realloc
138 * \sa heapmem_free
139 */
140
141#define heapmem_alloc(size) heapmem_zone_alloc(HEAPMEM_ZONE_GENERAL, (size))
142
143/**
144 * \brief Allocate a chunk of memory in the heap.
145 * \param zone The zone in which to allocate the memory.
146 * \param size The number of bytes to allocate.
147 * \return A pointer to the allocated memory chunk,
148 * or NULL if the allocation failed.
149 *
150 * \sa heapmem_realloc
151 * \sa heapmem_free
152 */
153void *heapmem_zone_alloc(heapmem_zone_t zone, size_t size);
154
155/**
156 * \brief Reallocate a chunk of memory in the heap.
157 * \param ptr A pointer to a chunk that has been allocated using
158 * heapmem_alloc() or heapmem_realloc().
159 * \param size The number of bytes to allocate.
160 * \return A pointer to the allocated memory chunk,
161 * or NULL if the allocation failed.
162 *
163 * \note If ptr is NULL, this function behaves the same as heapmem_alloc.
164 * \note If ptr is not NULL and size is zero, the function deallocates
165 * the chunk and returns NULL.
166 *
167 * \sa heapmem_alloc
168 * \sa heapmem_free
169 */
170
171void *heapmem_realloc(void *ptr, size_t size);
172
173/**
174 * \brief Deallocate a chunk of memory.
175 * \param ptr A pointer to a chunk that has been allocated using
176 * heapmem_alloc() or heapmem_realloc().
177 * \return A boolean indicating whether the memory could be deallocated.
178 *
179 * \note If ptr is NULL, this function will return immediately without
180 * without performing any action.
181 *
182 * \sa heapmem_alloc
183 * \sa heapmem_realloc
184 */
185
186bool heapmem_free(void *ptr);
187
188#endif /* HEAPMEM_DEBUG */
189
190/**
191 * \brief Obtain internal heapmem statistics regarding the
192 * allocated chunks.
193 * \param stats A pointer to an object of type heapmem_stats_t, which
194 * will be filled when calling this function.
195 *
196 * This function makes it possible to gain visibility into the internal
197 * structure of the heap. One can thus obtain information regarding
198 * the amount of memory allocated, overhead used for memory management,
199 * and the number of chunks allocated. By using this information, developers
200 * can tune their software to use the heapmem allocator more efficiently.
201 *
202 */
203
204void heapmem_stats(heapmem_stats_t *stats);
205
206/**
207 * \brief Obtain the minimum alignment of allocated addresses.
208 * \return The alignment value, which is a power of two.
209 */
210
211size_t heapmem_alignment(void);
212
213#endif /* !HEAPMEM_H */
214
215/** @} */
216/** @} */
void * heapmem_zone_alloc(heapmem_zone_t zone, size_t size)
Allocate a chunk of memory in the heap.
void * heapmem_realloc(void *ptr, size_t size)
Reallocate a chunk of memory in the heap.
heapmem_zone_t heapmem_zone_register(const char *name, size_t zone_size)
Register a zone with a reserved subdivision of the heap.
void heapmem_stats(heapmem_stats_t *stats)
Obtain internal heapmem statistics regarding the allocated chunks.
size_t heapmem_alignment(void)
Obtain the minimum alignment of allocated addresses.
bool heapmem_free(void *ptr)
Deallocate a chunk of memory.