]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/uclibc/ARCH-all/libc/stdlib/malloc/malloc.h
d1c592455557ea68b863d09f26dba58aca48684b
[l4.git] / l4 / pkg / uclibc / lib / uclibc / ARCH-all / libc / stdlib / malloc / malloc.h
1 /*
2  * libc/stdlib/malloc/malloc.h -- small malloc implementation
3  *
4  *  Copyright (C) 2002  NEC Corporation
5  *  Copyright (C) 2002  Miles Bader <miles@gnu.org>
6  *
7  * This file is subject to the terms and conditions of the GNU Lesser
8  * General Public License.  See the file COPYING.LIB in the main
9  * directory of this archive for more details.
10  *
11  * Written by Miles Bader <miles@gnu.org>
12  */
13
14 /* The alignment we guarantee for malloc return values.  We prefer this
15    to be at least sizeof (size_t) bytes because (a) we have to allocate
16    that many bytes for the header anyway and (b) guaranteeing word
17    alignment can be a significant win on targets like m68k and Coldfire,
18    where __alignof__(double) == 2.  */
19 #define MALLOC_ALIGNMENT \
20   __alignof__ (double __attribute_aligned__ (sizeof (size_t)))
21
22 /* The system pagesize... */
23 extern size_t __pagesize;
24 #define MALLOC_PAGE_SIZE        __pagesize
25
26 /* The minimum size of block we request from the the system to extend the
27    heap for small allocations (we may request a bigger block if necessary to
28    satisfy a particularly big request).  */
29 #define MALLOC_HEAP_EXTEND_SIZE MALLOC_PAGE_SIZE
30
31 /* When a heap free-area grows above this size, try to unmap it, releasing
32    the memory back to the system.  */
33 #define MALLOC_UNMAP_THRESHOLD  (8*MALLOC_PAGE_SIZE)
34 /* When unmapping a free-area, retain this many bytes if it's the only one,
35    to avoid completely emptying the heap.  This is only a heuristic -- the
36    existance of another free area, even if it's smaller than
37    MALLOC_MIN_SIZE, will cause us not to reserve anything.  */
38 #define MALLOC_MIN_SIZE         (2*MALLOC_PAGE_SIZE)
39
40 /* When realloc shrinks an allocation, it only does so if more than this
41    many bytes will be freed; it must at at least HEAP_MIN_SIZE.  Larger
42    values increase speed (by reducing heap fragmentation) at the expense of
43    space.  */
44 #define MALLOC_REALLOC_MIN_FREE_SIZE  (HEAP_MIN_SIZE + 16)
45
46
47 /* For systems with an MMU, use sbrk to map/unmap memory for the malloc
48    heap, instead of mmap/munmap.  This is a tradeoff -- sbrk is faster than
49    mmap/munmap, and guarantees contiguous allocation, but is also less
50    flexible, and causes the heap to only be shrinkable from the end.  */
51 #ifdef __ARCH_USE_MMU__
52 # define MALLOC_USE_SBRK
53 #endif
54
55 /* FM3: sbrk not implemented */
56 #undef MALLOC_USE_SBRK
57 /* make sure that we munmap areas with the same size we mmapped */
58 #define __UCLIBC_UCLINUX_BROKEN_MUNMAP__
59
60
61 /* The current implementation of munmap in uClinux doesn't work correctly:
62    it requires that ever call to munmap exactly match a corresponding call
63    to mmap (that is, it doesn't allow you to unmap only part of a
64    previously allocated block, or to unmap two contiguous blocks with a
65    single call to munmap).  This behavior is broken, and uClinux should be
66    fixed; however, until it is, we add code to work around the problem in
67    malloc.  */
68 #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
69
70 /* A structure recording a block of memory mmapped by malloc.  */
71 struct malloc_mmb
72 {
73   void *mem;                    /* the mmapped block */
74   size_t size;                  /* its size */
75   struct malloc_mmb *next;
76 };
77
78 /* A list of all malloc_mmb structures describing blocks that malloc has
79    mmapped, ordered by the block address.  */
80 extern struct malloc_mmb *__malloc_mmapped_blocks;
81
82 /* A heap used for allocating malloc_mmb structures.  We could allocate
83    them from the main heap, but that tends to cause heap fragmentation in
84    annoying ways.  */
85 extern struct heap_free_area *__malloc_mmb_heap;
86
87 /* Define MALLOC_MMB_DEBUGGING to cause malloc to emit debugging info about
88    about mmap block allocation/freeing by the `uclinux broken munmap' code
89    to stderr, when the variable __malloc_mmb_debug is set to true. */
90 #ifdef MALLOC_MMB_DEBUGGING
91 # include <stdio.h>
92
93 extern int __malloc_mmb_debug;
94 # define MALLOC_MMB_DEBUG(indent, fmt, args...)                               \
95    (__malloc_mmb_debug ? __malloc_debug_printf (indent, fmt , ##args) : 0)
96 # define MALLOC_MMB_DEBUG_INDENT(indent)                                      \
97    (__malloc_mmb_debug ? __malloc_debug_indent (indent) : 0)
98 # ifndef MALLOC_DEBUGGING
99 #  define MALLOC_DEBUGGING
100 # endif
101 #else /* !MALLOC_MMB_DEBUGGING */
102 # define MALLOC_MMB_DEBUG(fmt, args...) (void)0
103 # define MALLOC_MMB_DEBUG_INDENT(indent) (void)0
104 #endif /* MALLOC_MMB_DEBUGGING */
105
106 #endif /* __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
107
108
109 /* The size of a malloc allocation is stored in a size_t word
110    MALLOC_HEADER_SIZE bytes prior to the start address of the allocation:
111
112      +--------+---------+-------------------+
113      | SIZE   |(unused) | allocation  ...   |
114      +--------+---------+-------------------+
115      ^ BASE             ^ ADDR
116      ^ ADDR - MALLOC_HEADER_SIZE
117 */
118
119 /* The amount of extra space used by the malloc header.  */
120 #define MALLOC_HEADER_SIZE                      \
121   (MALLOC_ALIGNMENT < sizeof (size_t)           \
122    ? sizeof (size_t)                            \
123    : MALLOC_ALIGNMENT)
124
125 /* Set up the malloc header, and return the user address of a malloc block. */
126 #define MALLOC_SETUP(base, size)  \
127   (MALLOC_SET_SIZE (base, size), (void *)((char *)base + MALLOC_HEADER_SIZE))
128 /* Set the size of a malloc allocation, given the base address.  */
129 #define MALLOC_SET_SIZE(base, size)     (*(size_t *)(base) = (size))
130
131 /* Return base-address of a malloc allocation, given the user address.  */
132 #define MALLOC_BASE(addr)       ((void *)((char *)addr - MALLOC_HEADER_SIZE))
133 /* Return the size of a malloc allocation, given the user address.  */
134 #define MALLOC_SIZE(addr)       (*(size_t *)MALLOC_BASE(addr))
135
136
137 /* Locking for multithreaded apps.  */
138 #ifdef __UCLIBC_HAS_THREADS__
139
140 # include <pthread.h>
141 # include <bits/uClibc_pthread.h>
142
143 # define MALLOC_USE_LOCKING
144
145 typedef pthread_mutex_t malloc_mutex_t;
146 # define MALLOC_MUTEX_INIT      PTHREAD_MUTEX_INITIALIZER
147
148 # ifdef MALLOC_USE_SBRK
149 /* This lock is used to serialize uses of the `sbrk' function (in both
150    malloc and free, sbrk may be used several times in succession, and
151    things will break if these multiple calls are interleaved with another
152    thread's use of sbrk!).  */
153 extern malloc_mutex_t __malloc_sbrk_lock;
154 #  define __malloc_lock_sbrk()  __pthread_mutex_lock (&__malloc_sbrk_lock)
155 #  define __malloc_unlock_sbrk() __pthread_mutex_unlock (&__malloc_sbrk_lock)
156 # endif /* MALLOC_USE_SBRK */
157
158 #else /* !__UCLIBC_HAS_THREADS__ */
159
160 /* Without threads, mutex operations are a nop.  */
161 # define __malloc_lock_sbrk()   (void)0
162 # define __malloc_unlock_sbrk() (void)0
163
164 #endif /* __UCLIBC_HAS_THREADS__ */
165
166
167 /* branch-prediction macros; they may already be defined by libc.  */
168 #ifndef likely
169 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
170 #define likely(cond)    __builtin_expect(!!(int)(cond), 1)
171 #define unlikely(cond)  __builtin_expect((int)(cond), 0)
172 #else
173 #define likely(cond)    (cond)
174 #define unlikely(cond)  (cond)
175 #endif
176 #endif /* !likely */
177
178
179 /* Define MALLOC_DEBUGGING to cause malloc to emit debugging info to stderr
180    when the variable __malloc_debug is set to true. */
181 #ifdef MALLOC_DEBUGGING
182
183 extern void __malloc_debug_init (void);
184
185 /* The number of spaces in a malloc debug indent level.  */
186 #define MALLOC_DEBUG_INDENT_SIZE 3
187
188 extern int __malloc_debug, __malloc_check;
189
190 # define MALLOC_DEBUG(indent, fmt, args...)                                   \
191    (__malloc_debug ? __malloc_debug_printf (indent, fmt , ##args) : 0)
192 # define MALLOC_DEBUG_INDENT(indent)                                          \
193    (__malloc_debug ? __malloc_debug_indent (indent) : 0)
194
195 extern int __malloc_debug_cur_indent;
196
197 /* Print FMT and args indented at the current debug print level, followed
198    by a newline, and change the level by INDENT.  */
199 extern void __malloc_debug_printf (int indent, const char *fmt, ...);
200
201 /* Change the current debug print level by INDENT, and return the value.  */
202 #define __malloc_debug_indent(indent) (__malloc_debug_cur_indent += indent)
203
204 /* Set the current debug print level to LEVEL.  */
205 #define __malloc_debug_set_indent(level) (__malloc_debug_cur_indent = level)
206
207 #else /* !MALLOC_DEBUGGING */
208 # define MALLOC_DEBUG(fmt, args...) (void)0
209 # define MALLOC_DEBUG_INDENT(indent) (void)0
210 #endif /* MALLOC_DEBUGGING */
211
212
213 /* Return SZ rounded down to POWER_OF_2_SIZE (which must be power of 2).  */
214 #define MALLOC_ROUND_DOWN(sz, power_of_2_size)  \
215   ((sz) & ~(power_of_2_size - 1))
216 /* Return SZ rounded to POWER_OF_2_SIZE (which must be power of 2).  */
217 #define MALLOC_ROUND_UP(sz, power_of_2_size)                            \
218   MALLOC_ROUND_DOWN ((sz) + (power_of_2_size - 1), (power_of_2_size))
219
220 /* Return SZ rounded down to a multiple MALLOC_PAGE_SIZE.  */
221 #define MALLOC_ROUND_DOWN_TO_PAGE_SIZE(sz)  \
222   MALLOC_ROUND_DOWN (sz, MALLOC_PAGE_SIZE)
223 /* Return SZ rounded up to a multiple MALLOC_PAGE_SIZE.  */
224 #define MALLOC_ROUND_UP_TO_PAGE_SIZE(sz)  \
225   MALLOC_ROUND_UP (sz, MALLOC_PAGE_SIZE)
226
227
228 /* The malloc heap.  */
229 extern struct heap_free_area *__malloc_heap;
230 #ifdef __UCLIBC_HAS_THREADS__
231 extern malloc_mutex_t __malloc_heap_lock;
232 #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
233 extern malloc_mutex_t __malloc_mmb_heap_lock;
234 #endif
235 #endif