OSDN Git Service

Use MAP_PRIVATE whenever __ARCH_HAS_MMU__ is set.
[uclinux-h8/uclibc-ng.git] / libc / stdlib / malloc-simple / alloc.c
1 /* alloc.c
2  *
3  * Written by Erik Andersen <andersee@debian.org>
4  * LGPLv2
5  *
6  * Parts of the memalign code were stolen from malloc-930716.
7  */
8
9 #define _GNU_SOURCE
10 #include <features.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <sys/mman.h>
18
19
20 #ifdef L_malloc
21 void *malloc(size_t size)
22 {
23     void *result;
24
25     if (unlikely(size == 0)) {
26 #if defined(__MALLOC_GLIBC_COMPAT__)
27         size++;
28 #else
29         /* Some programs will call malloc (0).  Lets be strict and return NULL */
30         return 0;
31 #endif
32     }
33
34 #ifdef __ARCH_HAS_MMU__
35     result = mmap((void *) 0, size + sizeof(size_t), PROT_READ | PROT_WRITE,
36             MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
37     if (result == MAP_FAILED)
38         return 0;
39     * (size_t *) result = size;
40     return(result + sizeof(size_t));
41 #else
42     result = mmap((void *) 0, size, PROT_READ | PROT_WRITE,
43             MAP_SHARED | MAP_ANONYMOUS, 0, 0);
44     if (result == MAP_FAILED)
45         return 0;
46     return(result);
47 #endif
48 }
49 #endif
50
51 #ifdef L_calloc
52 void * calloc(size_t nmemb, size_t lsize)
53 {
54         void *result;
55         size_t size=lsize * nmemb;
56
57         /* guard vs integer overflow, but allow nmemb
58          * to fall through and call malloc(0) */
59         if (nmemb && lsize != (size / nmemb)) {
60                 __set_errno(ENOMEM);
61                 return NULL;
62         }
63         result=malloc(size);
64 #if 0
65         /* Standard unix mmap using /dev/zero clears memory so calloc
66          * doesn't need to actually zero anything....
67          */
68         if (result != NULL) {
69                 memset(result, 0, size);
70         }
71 #endif
72         return result;
73 }
74 #endif
75
76 #ifdef L_realloc
77 void *realloc(void *ptr, size_t size)
78 {
79     void *newptr = NULL;
80
81     if (!ptr)
82         return malloc(size);
83     if (!size) {
84         free(ptr);
85         return malloc(0);
86     }
87
88     newptr = malloc(size);
89     if (newptr) {
90         memcpy(newptr, ptr,
91 #ifdef __ARCH_HAS_MMU__
92                 *((size_t *) (ptr - sizeof(size_t)))
93 #else
94                 size
95 #endif
96               );
97         free(ptr);
98     }
99     return newptr;
100 }
101 #endif
102
103 #ifdef L_free
104 extern int weak_function __libc_free_aligned(void *ptr);
105 void free(void *ptr)
106 {
107     if (ptr == NULL)
108         return;
109     if (unlikely(__libc_free_aligned!=NULL)) {
110         if (__libc_free_aligned(ptr)) {
111             return;
112         }
113     }
114 #ifdef __ARCH_HAS_MMU__
115     ptr -= sizeof(size_t);
116     munmap(ptr, * (size_t *) ptr + sizeof(size_t));
117 #else
118     munmap(ptr, 0);
119 #endif
120 }
121 #endif
122
123 #ifdef L_memalign
124 #ifdef __UCLIBC_HAS_THREADS__
125 #include <pthread.h>
126 pthread_mutex_t __malloc_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
127 # define LOCK   __pthread_mutex_lock(&__malloc_lock)
128 # define UNLOCK __pthread_mutex_unlock(&__malloc_lock);
129 #else
130 # define LOCK
131 # define UNLOCK
132 #endif
133
134 /* List of blocks allocated with memalign or valloc */
135 struct alignlist
136 {
137     struct alignlist *next;
138     __ptr_t aligned;    /* The address that memaligned returned.  */
139     __ptr_t exact;      /* The address that malloc returned.  */
140 };
141 struct alignlist *_aligned_blocks;
142
143 /* Return memory to the heap. */
144 int __libc_free_aligned(void *ptr)
145 {
146     struct alignlist *l;
147
148     if (ptr == NULL)
149         return 0;
150
151     LOCK;
152     for (l = _aligned_blocks; l != NULL; l = l->next) {
153         if (l->aligned == ptr) {
154             /* Mark the block as free */
155             l->aligned = NULL;
156             ptr = l->exact;
157 #ifdef __ARCH_HAS_MMU__
158             ptr -= sizeof(size_t);
159             munmap(ptr, * (size_t *) ptr + sizeof(size_t));
160 #else
161             munmap(ptr, 0);
162 #endif
163             return 1;
164         }
165     }
166     UNLOCK;
167     return 0;
168 }
169 void * memalign (size_t alignment, size_t size)
170 {
171     void * result;
172     unsigned long int adj;
173
174     result = malloc (size + alignment - 1);
175     if (result == NULL)
176         return NULL;
177     adj = (unsigned long int) ((unsigned long int) ((char *) result -
178                 (char *) NULL)) % alignment;
179     if (adj != 0)
180     {
181         struct alignlist *l;
182         LOCK;
183         for (l = _aligned_blocks; l != NULL; l = l->next)
184             if (l->aligned == NULL)
185                 /* This slot is free.  Use it.  */
186                 break;
187         if (l == NULL)
188         {
189             l = (struct alignlist *) malloc (sizeof (struct alignlist));
190             if (l == NULL) {
191                 free(result);
192                 UNLOCK;
193                 return NULL;
194             }
195             l->next = _aligned_blocks;
196             _aligned_blocks = l;
197         }
198         l->exact = result;
199         result = l->aligned = (char *) result + alignment - adj;
200         UNLOCK;
201     }
202
203     return result;
204 }
205 #endif
206