OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [stdlib/] [malloc/] [malloc.h] - Blame information for rev 1771

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.