OSDN Git Service

Define MALLOC_SET_SIZE to take the user-address rather than the base-address.
[uclinux-h8/uClibc.git] / 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.  */
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
36 /* For systems with an MMU, use sbrk to map/unmap memory for the malloc
37    heap, instead of mmap/munmap.  This is a tradeoff -- sbrk is faster than
38    mmap/munmap, and guarantees contiguous allocation, but is also less
39    flexible, and causes the heap to only be shrinkable from the end.  */
40 #ifdef __UCLIBC_HAS_MMU__
41 # define MALLOC_USE_SBRK
42 #endif
43
44
45 /* The size of a malloc allocation is stored in a size_t word
46    MALLOC_ALIGNMENT bytes prior to the start address of the allocation:
47
48      +--------+---------+-------------------+
49      | SIZE   |(unused) | allocation  ...   |
50      +--------+---------+-------------------+
51      ^ BASE             ^ ADDR
52      ^ ADDR - MALLOC_ALIGN
53 */
54
55 /* Return base-address of a malloc allocation, given the user address.  */
56 #define MALLOC_BASE(addr)       ((void *)((char *)addr - MALLOC_ALIGNMENT))
57 /* Return the size of a malloc allocation, given the user address.  */
58 #define MALLOC_SIZE(addr)       (*(size_t *)MALLOC_BASE(addr))
59 /* Sets the size of a malloc allocation, given the user address.  */
60 #define MALLOC_SET_SIZE(addr, size)     (*(size_t *)MALLOC_BASE(addr) = (size))
61
62 /* Return the user address of a malloc allocation, given the base address.  */
63 #define MALLOC_ADDR(base)       ((void *)((char *)base + MALLOC_ALIGNMENT))
64
65
66 #ifdef __UCLIBC_HAS_THREADS__
67
68 # include <pthread.h>
69
70 # define MALLOC_USE_LOCKING
71
72 typedef pthread_mutex_t malloc_mutex_t;
73 # define MALLOC_MUTEX_INIT      PTHREAD_MUTEX_INITIALIZER
74
75 /* The main malloc lock.  This must be hold while accessing __malloc_heap,
76    and in order to gain __malloc_sbrk_lock.  */
77 extern malloc_mutex_t __malloc_lock;
78 # define __malloc_lock()        pthread_mutex_lock (&__malloc_lock)
79 # define __malloc_unlock()      pthread_mutex_unlock (&__malloc_lock)
80
81 # ifdef MALLOC_USE_SBRK
82 /* This lock is used to serialize uses of the `sbrk' function (in both
83    malloc and free, sbrk may be used several times in succession, and
84    things will break if these multiple calls are interleaved with another
85    thread's use of sbrk!).  */
86 extern malloc_mutex_t __malloc_sbrk_lock;
87 #  define __malloc_lock_sbrk()  pthread_mutex_lock (&__malloc_sbrk_lock)
88 #  define __malloc_unlock_sbrk() pthread_mutex_unlock (&__malloc_sbrk_lock)
89 # endif /* MALLOC_USE_SBRK */
90
91 #else /* !__UCLIBC_HAS_THREADS__ */
92
93 /* Without threads, mutex operations are a nop.  */
94 # define __malloc_lock()        (void)0
95 # define __malloc_unlock()      (void)0
96 # define __malloc_lock_sbrk()   (void)0
97 # define __malloc_unlock_sbrk() (void)0
98
99 #endif /* __UCLIBC_HAS_THREADS__ */
100
101
102 /* Change this to `#if 1' to cause malloc to emit debugging info to stderr.  */
103 #if 0
104 #include <stdio.h>
105 #define MALLOC_DEBUG(fmt, args...) fprintf (stderr, fmt , ##args)
106 #else
107 #define MALLOC_DEBUG(fmt, args...) (void)0
108 #endif
109
110
111 /* Return SZ rounded down to POWER_OF_2_SIZE (which must be power of 2).  */
112 #define MALLOC_ROUND_DOWN(sz, power_of_2_size)  \
113   ((sz) & ~(power_of_2_size - 1))
114 /* Return SZ rounded to POWER_OF_2_SIZE (which must be power of 2).  */
115 #define MALLOC_ROUND_UP(sz, power_of_2_size)                            \
116   MALLOC_ROUND_DOWN ((sz) + (power_of_2_size - 1), (power_of_2_size))
117
118 /* Return SZ rounded down to a multiple MALLOC_PAGE_SIZE.  */
119 #define MALLOC_ROUND_DOWN_TO_PAGE_SIZE(sz)  \
120   MALLOC_ROUND_DOWN (sz, MALLOC_PAGE_SIZE)
121 /* Return SZ rounded up to a multiple MALLOC_PAGE_SIZE.  */
122 #define MALLOC_ROUND_UP_TO_PAGE_SIZE(sz)  \
123   MALLOC_ROUND_UP (sz, MALLOC_PAGE_SIZE)
124
125
126 /* The malloc heap.  */
127 extern struct heap __malloc_heap;