Contiki-NG
Loading...
Searching...
No Matches
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 * the HEAPMEM_CONF_ARENA_SIZE parameter.
42 *
43 * Each allocated memory object is referred to as a "chunk". The
44 * allocator manages free chunks in a double-linked list. While this
45 * adds some memory overhead compared to a single-linked list, it
46 * improves the performance of list management.
47 *
48 * Internally, allocated chunks can be retrieved using the pointer to
49 * the allocated memory returned by heapmem_alloc() and
50 * heapmem_realloc(), because the chunk structure immediately precedes
51 * the memory of the chunk.
52 *
53 * \note If the HEAPMEM_CONF_ARENA_SIZE parameter is not set, the
54 * heapmem implementation will not be compiled, which could lead to a
55 * linking error if other modules call heapmem functions.
56 *
57 * \note This module does not contain a corresponding function to the
58 * standard C function calloc().
59 *
60 * \note Dynamic memory should be used carefully on
61 * memory-constrained embedded systems, because fragmentation
62 * may be induced through various allocation/deallocation
63 * patterns, and no guarantees are given regarding the
64 * availability of memory.
65 *
66 * @{
67 */
68
69/**
70 * \file
71 * Header file for the dynamic heap memory allocator.
72 * \author
73 * Nicolas Tsiftes <nvt@acm.org>
74 */
75
76#ifndef HEAPMEM_H
77#define HEAPMEM_H
78
79#include "contiki.h"
80
81#include <stdlib.h>
82/*****************************************************************************/
83#ifndef HEAPMEM_DEBUG
84#define HEAPMEM_DEBUG 0
85#endif
86/*****************************************************************************/
87typedef struct heapmem_stats {
88 size_t allocated;
89 size_t overhead;
90 size_t available;
91 size_t footprint;
92 size_t max_footprint;
93 size_t chunks;
94} heapmem_stats_t;
95/*****************************************************************************/
96typedef uint8_t heapmem_zone_t;
97
98#define HEAPMEM_ZONE_INVALID (heapmem_zone_t)-1
99#define HEAPMEM_ZONE_GENERAL 0
100/*****************************************************************************/
101
102/**
103 * \brief Register a zone with a reserved subdivision of the heap.
104 * \param name A string containing the name of the zone.
105 * \param zone_size The number of bytes to reserve for the zone.
106 * \return A zone ID if the allocation succeeds, or
107 * HEAPMEM_ZONE_INVALID if it fails.
108 */
109heapmem_zone_t heapmem_zone_register(const char *name, size_t zone_size);
110/*****************************************************************************/
111
112#if HEAPMEM_DEBUG
113
114#define heapmem_alloc(size) \
115 heapmem_zone_alloc_debug(HEAPMEM_ZONE_GENERAL, (size), __FILE__, __LINE__)
116#define heapmem_zone_alloc(zone, size) \
117 heapmem_zone_alloc_debug((zone), (size), __FILE__, __LINE__)
118#define heapmem_realloc(ptr, size) \
119 heapmem_realloc_debug((ptr), (size), __FILE__, __LINE__)
120#define heapmem_calloc(nmemb, size) \
121 heapmem_calloc_debug((nmemb), (size), __FILE__, __LINE__)
122#define heapmem_free(ptr) \
123 heapmem_free_debug((ptr), __FILE__, __LINE__)
124
125void *heapmem_alloc_debug(size_t size,
126 const char *file, const unsigned line);
127void *heapmem_zone_alloc_debug(heapmem_zone_t zone, size_t size,
128 const char *file, const unsigned line);
129void *heapmem_realloc_debug(void *ptr, size_t size,
130 const char *file, const unsigned line);
131void *heapmem_calloc_debug(size_t nmemb, size_t size,
132 const char *file, const unsigned line);
133bool heapmem_free_debug(void *ptr,
134 const char *file, const unsigned line);
135
136#else
137
138/**
139 * \brief Allocate a chunk of memory in the general zone of the heap.
140 * \param size The number of bytes to allocate.
141 * \return A pointer to the allocated memory chunk,
142 * or NULL if the allocation failed.
143 *
144 * \sa heapmem_realloc
145 * \sa heapmem_free
146 */
147#define heapmem_alloc(size) heapmem_zone_alloc(HEAPMEM_ZONE_GENERAL, (size))
148
149/**
150 * \brief Allocate a chunk of memory in the heap.
151 * \param zone The zone in which to allocate the memory.
152 * \param size The number of bytes to allocate.
153 * \return A pointer to the allocated memory chunk,
154 * or NULL if the allocation failed.
155 *
156 * \sa heapmem_realloc
157 * \sa heapmem_free
158 */
159void *heapmem_zone_alloc(heapmem_zone_t zone, size_t size);
160
161/**
162 * \brief Reallocate a chunk of memory in the heap.
163 * \param ptr A pointer to a chunk that has been allocated using
164 * heapmem_alloc(), heapmem_calloc(), or heapmem_realloc().
165 * \param size The number of bytes to allocate.
166 * \return A pointer to the allocated memory chunk,
167 * or NULL if the allocation failed.
168 *
169 * \note If ptr is NULL, this function behaves the same as heapmem_alloc.
170 * \note If ptr is not NULL and size is zero, the function deallocates
171 * the chunk and returns NULL.
172 *
173 * \sa heapmem_alloc
174 * \sa heapmem_calloc
175 * \sa heapmem_free
176 */
177void *heapmem_realloc(void *ptr, size_t size);
178
179/**
180 * \brief Allocate memory for a zero-initialized array.
181 * \param nmemb The number of elements to allocate.
182 * \param size The size of each element.
183 * \return A pointer to the allocated memory,
184 * or NULL if the allocation failed.
185 *
186 * \sa heapmem_alloc
187 * \sa heapmem_free
188 */
189void *heapmem_calloc(size_t nmemb, size_t size);
190
191/**
192 * \brief Deallocate a chunk of memory.
193 * \param ptr A pointer to a chunk that has been allocated using
194 * heapmem_alloc(), heapmem_calloc(), or heapmem_realloc().
195 * \return A boolean indicating whether the memory could be deallocated.
196 *
197 * \note If ptr is NULL, this function will return immediately without
198 * performing any action.
199 *
200 * \sa heapmem_alloc
201 * \sa heapmem_calloc
202 * \sa heapmem_realloc
203 */
204bool heapmem_free(void *ptr);
205
206#endif /* HEAPMEM_DEBUG */
207
208/**
209 * \brief Obtain internal heapmem statistics regarding the
210 * allocated chunks.
211 * \param stats A pointer to an object of type heapmem_stats_t, which
212 * will be filled when calling this function.
213 *
214 * This function makes it possible to gain visibility into the internal
215 * structure of the heap. One can thus obtain information regarding
216 * the amount of memory allocated, overhead used for memory management,
217 * and the number of chunks allocated. By using this information, developers
218 * can tune their software to use the heapmem allocator more efficiently.
219 */
220void heapmem_stats(heapmem_stats_t *stats);
221
222/**
223 * \brief Print debugging information for the heap memory
224 * management.
225 * \param print_chunks Determines whether to print information about
226 * all allocated chunks.
227 */
228void heapmem_print_debug_info(bool print_chunks);
229
230/**
231 * \brief Obtain the minimum alignment of allocated addresses.
232 * \return The alignment value, which is a power of two.
233 */
234size_t heapmem_alignment(void);
235
236#endif /* !HEAPMEM_H */
237
238/** @} */
239/** @} */
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.
void heapmem_print_debug_info(bool print_chunks)
Print debugging information for the heap memory management.
void * heapmem_calloc(size_t nmemb, size_t size)
Allocate memory for a zero-initialized array.