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 This module does not contain a corresponding function to the
56 * standard C function calloc().
57 *
58 * \note Dynamic memory should be used carefully on
59 * memory-constrained, embedded systems, because fragmentation
60 * may be induced through various allocation/deallocation
61 * patterns, and no guarantees are given regarding the
62 * availability of memory.
63 *
64 * @{
65 */
66
67/**
68 * \file
69 * Header file for the dynamic heap memory allocator.
70 * \author
71 * Nicolas Tsiftes <nvt@acm.org>
72 */
73
74#ifndef HEAPMEM_H
75#define HEAPMEM_H
76
77#include <stdlib.h>
78
79#include "contiki.h"
80
81typedef struct heapmem_stats {
82 size_t allocated;
83 size_t overhead;
84 size_t available;
85 size_t footprint;
86 size_t chunks;
87} heapmem_stats_t;
88
89#if HEAPMEM_DEBUG
90
91#define heapmem_alloc(size) heapmem_alloc_debug((size), __FILE__, __LINE__)
92#define heapmem_realloc(ptr, size) heapmem_realloc_debug((ptr), (size), __FILE__, __LINE__)
93#define heapmem_free(ptr) heapmem_free_debug((ptr), __FILE__, __LINE__)
94
95void *heapmem_alloc_debug(size_t size,
96 const char *file, const unsigned line);
97void *heapmem_realloc_debug(void *ptr, size_t size,
98 const char *file, const unsigned line);
99void heapmem_free_debug(void *ptr,
100 const char *file, const unsigned line);
101
102#else
103
104/**
105 * \brief Allocate a chunk of memory in the heap.
106 * \param size The number of bytes to allocate.
107 * \return A pointer to the allocated memory chunk,
108 * or NULL if the allocation failed.
109 *
110 * \sa heapmem_realloc
111 * \sa heapmem_free
112 */
113
114void *heapmem_alloc(size_t size);
115
116/**
117 * \brief Reallocate a chunk of memory in the heap.
118 * \param ptr A pointer to a chunk that has been allocated using
119 * heapmem_alloc() or heapmem_realloc().
120 * \param size The number of bytes to allocate.
121 * \return A pointer to the allocated memory chunk,
122 * or NULL if the allocation failed.
123 *
124 * \note If ptr is NULL, this function behaves the same as heapmem_alloc.
125 * \note If ptr is not NULL and size is zero, the function deallocates
126 * the chunk and returns NULL.
127 *
128 * \sa heapmem_alloc
129 * \sa heapmem_free
130 */
131
132void *heapmem_realloc(void *ptr, size_t size);
133
134/**
135 * \brief Deallocate a chunk of memory.
136 * \param ptr A pointer to a chunk that has been allocated using
137 * heapmem_alloc() or heapmem_realloc().
138 * \return A boolean indicating whether the memory could be deallocated.
139 *
140 * \note If ptr is NULL, this function will return immediately without
141 * without performing any action.
142 *
143 * \sa heapmem_alloc
144 * \sa heapmem_realloc
145 */
146
147bool heapmem_free(void *ptr);
148
149#endif /* HEAMMEM_DEBUG */
150
151/**
152 * \brief Obtain internal heapmem statistics regarding the
153 * allocated chunks.
154 * \param stats A pointer to an object of type heapmem_stats_t, which
155 * will be filled when calling this function.
156 *
157 * This function makes it possible to gain visibility into the internal
158 * structure of the heap. One can thus obtain information regarding
159 * the amount of memory allocated, overhead used for memory management,
160 * and the number of chunks allocated. By using this information, developers
161 * can tune their software to use the heapmem allocator more efficiently.
162 *
163 */
164
165void heapmem_stats(heapmem_stats_t *stats);
166
167#endif /* !HEAPMEM_H */
168
169/** @} */
170/** @} */
void * heapmem_realloc(void *ptr, size_t size)
Reallocate a chunk of memory in the heap.
Definition: heapmem.c:402
void * heapmem_alloc(size_t size)
Allocate a chunk of memory in the heap.
Definition: heapmem.c:310
void heapmem_stats(heapmem_stats_t *stats)
Obtain internal heapmem statistics regarding the allocated chunks.
Definition: heapmem.c:495
bool heapmem_free(void *ptr)
Deallocate a chunk of memory.
Definition: heapmem.c:357