malloc.c revision 1.5 1 1.5 riastrad /* $NetBSD: malloc.c,v 1.5 2023/07/04 18:40:14 riastradh Exp $ */
2 1.1 elric
3 1.1 elric /*
4 1.1 elric * Copyright (c) 1983, 1993
5 1.1 elric * The Regents of the University of California. All rights reserved.
6 1.1 elric *
7 1.1 elric * Redistribution and use in source and binary forms, with or without
8 1.1 elric * modification, are permitted provided that the following conditions
9 1.1 elric * are met:
10 1.1 elric * 1. Redistributions of source code must retain the above copyright
11 1.1 elric * notice, this list of conditions and the following disclaimer.
12 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 elric * notice, this list of conditions and the following disclaimer in the
14 1.1 elric * documentation and/or other materials provided with the distribution.
15 1.2 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 elric * may be used to endorse or promote products derived from this software
17 1.1 elric * without specific prior written permission.
18 1.1 elric *
19 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 elric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 elric * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 elric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 elric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 elric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 elric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 elric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 elric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 elric * SUCH DAMAGE.
30 1.1 elric */
31 1.1 elric
32 1.1 elric #include <sys/cdefs.h>
33 1.1 elric #if defined(LIBC_SCCS) && !defined(lint)
34 1.1 elric #if 0
35 1.1 elric static char sccsid[] = "@(#)malloc.c 8.1 (Berkeley) 6/4/93";
36 1.1 elric #else
37 1.5 riastrad __RCSID("$NetBSD: malloc.c,v 1.5 2023/07/04 18:40:14 riastradh Exp $");
38 1.1 elric #endif
39 1.1 elric #endif /* LIBC_SCCS and not lint */
40 1.1 elric
41 1.1 elric /*
42 1.1 elric * malloc.c (Caltech) 2/21/82
43 1.1 elric * Chris Kingsley, kingsley@cit-20.
44 1.1 elric *
45 1.4 riastrad * This is a very fast storage allocator. It allocates blocks of a small
46 1.1 elric * number of different sizes, and keeps free lists of each size. Blocks that
47 1.4 riastrad * don't exactly fit are passed up to the next larger size. In this
48 1.1 elric * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long.
49 1.1 elric * This is designed for use in a virtual memory environment.
50 1.1 elric */
51 1.1 elric
52 1.1 elric #include <sys/types.h>
53 1.1 elric #if defined(DEBUG) || defined(RCHECK)
54 1.1 elric #include <sys/uio.h>
55 1.1 elric #endif
56 1.3 riastrad
57 1.3 riastrad #include <errno.h>
58 1.3 riastrad #include <limits.h>
59 1.3 riastrad #include <stddef.h>
60 1.3 riastrad #include <stdint.h>
61 1.1 elric #if defined(RCHECK) || defined(MSTATS)
62 1.1 elric #include <stdio.h>
63 1.1 elric #endif
64 1.1 elric #include <stdlib.h>
65 1.1 elric #include <string.h>
66 1.1 elric #include <unistd.h>
67 1.3 riastrad
68 1.1 elric #include "reentrant.h"
69 1.1 elric
70 1.1 elric
71 1.1 elric /*
72 1.1 elric * The overhead on a block is at least 4 bytes. When free, this space
73 1.1 elric * contains a pointer to the next free block, and the bottom two bits must
74 1.1 elric * be zero. When in use, the first byte is set to MAGIC, and the second
75 1.1 elric * byte is the size index. The remaining bytes are for alignment.
76 1.1 elric * If range checking is enabled then a second word holds the size of the
77 1.1 elric * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
78 1.1 elric * The order of elements is critical: ov_magic must overlay the low order
79 1.1 elric * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
80 1.1 elric */
81 1.1 elric union overhead {
82 1.1 elric union overhead *ov_next; /* when free */
83 1.1 elric struct {
84 1.1 elric u_char ovu_magic; /* magic number */
85 1.1 elric u_char ovu_index; /* bucket # */
86 1.1 elric #ifdef RCHECK
87 1.1 elric u_short ovu_rmagic; /* range magic number */
88 1.1 elric u_long ovu_size; /* actual block size */
89 1.1 elric #endif
90 1.1 elric } ovu;
91 1.1 elric #define ov_magic ovu.ovu_magic
92 1.1 elric #define ov_index ovu.ovu_index
93 1.1 elric #define ov_rmagic ovu.ovu_rmagic
94 1.1 elric #define ov_size ovu.ovu_size
95 1.1 elric };
96 1.1 elric
97 1.1 elric #define MAGIC 0xef /* magic # on accounting info */
98 1.1 elric #ifdef RCHECK
99 1.1 elric #define RMAGIC 0x5555 /* magic # on range info */
100 1.1 elric #endif
101 1.1 elric
102 1.1 elric #ifdef RCHECK
103 1.1 elric #define RSLOP sizeof (u_short)
104 1.1 elric #else
105 1.1 elric #define RSLOP 0
106 1.1 elric #endif
107 1.1 elric
108 1.1 elric /*
109 1.1 elric * nextf[i] is the pointer to the next free block of size 2^(i+3). The
110 1.1 elric * smallest allocatable block is 8 bytes. The overhead information
111 1.1 elric * precedes the data area returned to the user.
112 1.1 elric */
113 1.1 elric #define NBUCKETS 30
114 1.1 elric static union overhead *nextf[NBUCKETS];
115 1.1 elric
116 1.1 elric static long pagesz; /* page size */
117 1.1 elric static int pagebucket; /* page size bucket */
118 1.1 elric
119 1.1 elric #ifdef MSTATS
120 1.1 elric /*
121 1.1 elric * nmalloc[i] is the difference between the number of mallocs and frees
122 1.1 elric * for a given block size.
123 1.1 elric */
124 1.1 elric static u_int nmalloc[NBUCKETS];
125 1.1 elric #endif
126 1.1 elric
127 1.1 elric #ifdef _REENT
128 1.1 elric static mutex_t malloc_mutex = MUTEX_INITIALIZER;
129 1.1 elric #endif
130 1.1 elric
131 1.1 elric static void morecore __P((int));
132 1.1 elric static int findbucket __P((union overhead *, int));
133 1.1 elric #ifdef MSTATS
134 1.1 elric void mstats __P((const char *));
135 1.1 elric #endif
136 1.1 elric
137 1.1 elric #if defined(DEBUG) || defined(RCHECK)
138 1.1 elric #define ASSERT(p) if (!(p)) botch(__STRING(p))
139 1.1 elric
140 1.1 elric static void botch __P((const char *));
141 1.1 elric
142 1.1 elric /*
143 1.1 elric * NOTE: since this may be called while malloc_mutex is locked, stdio must not
144 1.1 elric * be used in this function.
145 1.1 elric */
146 1.1 elric static void
147 1.1 elric botch(s)
148 1.1 elric const char *s;
149 1.1 elric {
150 1.1 elric struct iovec iov[3];
151 1.1 elric
152 1.1 elric iov[0].iov_base = "\nassertion botched: ";
153 1.1 elric iov[0].iov_len = 20;
154 1.1 elric iov[1].iov_base = (void *)s;
155 1.1 elric iov[1].iov_len = strlen(s);
156 1.1 elric iov[2].iov_base = "\n";
157 1.1 elric iov[2].iov_len = 1;
158 1.1 elric
159 1.1 elric /*
160 1.1 elric * This place deserves a word of warning: a cancellation point will
161 1.1 elric * occur when executing writev(), and we might be still owning
162 1.1 elric * malloc_mutex. At this point we need to disable cancellation
163 1.1 elric * until `after' abort() because i) establishing a cancellation handler
164 1.1 elric * might, depending on the implementation, result in another malloc()
165 1.1 elric * to be executed, and ii) it is really not desirable to let execution
166 1.1 elric * continue. `Fix me.'
167 1.4 riastrad *
168 1.1 elric * Note that holding mutex_lock during abort() is safe.
169 1.1 elric */
170 1.1 elric
171 1.1 elric (void)writev(STDERR_FILENO, iov, 3);
172 1.1 elric abort();
173 1.1 elric }
174 1.1 elric #else
175 1.3 riastrad #define ASSERT(p) ((void)sizeof((long)(p)))
176 1.1 elric #endif
177 1.1 elric
178 1.1 elric void *
179 1.5 riastrad malloc(size_t nbytes)
180 1.1 elric {
181 1.1 elric union overhead *op;
182 1.1 elric int bucket;
183 1.1 elric long n;
184 1.1 elric unsigned amt;
185 1.1 elric
186 1.1 elric mutex_lock(&malloc_mutex);
187 1.1 elric
188 1.1 elric /*
189 1.1 elric * First time malloc is called, setup page size and
190 1.1 elric * align break pointer so all data will be page aligned.
191 1.1 elric */
192 1.1 elric if (pagesz == 0) {
193 1.1 elric pagesz = n = getpagesize();
194 1.1 elric ASSERT(pagesz > 0);
195 1.1 elric op = (union overhead *)(void *)sbrk(0);
196 1.1 elric n = n - sizeof (*op) - ((long)op & (n - 1));
197 1.1 elric if (n < 0)
198 1.1 elric n += pagesz;
199 1.1 elric if (n) {
200 1.1 elric if (sbrk((int)n) == (void *)-1) {
201 1.1 elric mutex_unlock(&malloc_mutex);
202 1.1 elric return (NULL);
203 1.1 elric }
204 1.1 elric }
205 1.1 elric bucket = 0;
206 1.1 elric amt = 8;
207 1.1 elric while (pagesz > amt) {
208 1.1 elric amt <<= 1;
209 1.1 elric bucket++;
210 1.1 elric }
211 1.1 elric pagebucket = bucket;
212 1.1 elric }
213 1.1 elric /*
214 1.1 elric * Convert amount of memory requested into closest block size
215 1.1 elric * stored in hash buckets which satisfies request.
216 1.1 elric * Account for space used per block for accounting.
217 1.1 elric */
218 1.1 elric if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) {
219 1.1 elric #ifndef RCHECK
220 1.1 elric amt = 8; /* size of first bucket */
221 1.1 elric bucket = 0;
222 1.1 elric #else
223 1.1 elric amt = 16; /* size of first bucket */
224 1.1 elric bucket = 1;
225 1.1 elric #endif
226 1.1 elric n = -((long)sizeof (*op) + RSLOP);
227 1.1 elric } else {
228 1.1 elric amt = (unsigned)pagesz;
229 1.1 elric bucket = pagebucket;
230 1.1 elric }
231 1.1 elric while (nbytes > amt + n) {
232 1.1 elric amt <<= 1;
233 1.1 elric if (amt == 0)
234 1.1 elric return (NULL);
235 1.1 elric bucket++;
236 1.1 elric }
237 1.1 elric /*
238 1.1 elric * If nothing in hash bucket right now,
239 1.1 elric * request more memory from the system.
240 1.1 elric */
241 1.1 elric if ((op = nextf[bucket]) == NULL) {
242 1.1 elric morecore(bucket);
243 1.1 elric if ((op = nextf[bucket]) == NULL) {
244 1.1 elric mutex_unlock(&malloc_mutex);
245 1.1 elric return (NULL);
246 1.1 elric }
247 1.1 elric }
248 1.1 elric /* remove from linked list */
249 1.1 elric nextf[bucket] = op->ov_next;
250 1.1 elric op->ov_magic = MAGIC;
251 1.1 elric op->ov_index = bucket;
252 1.1 elric #ifdef MSTATS
253 1.1 elric nmalloc[bucket]++;
254 1.1 elric #endif
255 1.1 elric mutex_unlock(&malloc_mutex);
256 1.1 elric #ifdef RCHECK
257 1.1 elric /*
258 1.1 elric * Record allocated size of block and
259 1.1 elric * bound space with magic numbers.
260 1.1 elric */
261 1.1 elric op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
262 1.1 elric op->ov_rmagic = RMAGIC;
263 1.1 elric *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
264 1.1 elric #endif
265 1.1 elric return ((void *)(op + 1));
266 1.1 elric }
267 1.1 elric
268 1.1 elric /*
269 1.1 elric * Allocate more memory to the indicated bucket.
270 1.1 elric */
271 1.1 elric static void
272 1.5 riastrad morecore(int bucket)
273 1.1 elric {
274 1.1 elric union overhead *op;
275 1.1 elric long sz; /* size of desired block */
276 1.1 elric long amt; /* amount to allocate */
277 1.1 elric long nblks; /* how many blocks we get */
278 1.1 elric
279 1.1 elric /*
280 1.1 elric * sbrk_size <= 0 only for big, FLUFFY, requests (about
281 1.1 elric * 2^30 bytes on a VAX, I think) or for a negative arg.
282 1.1 elric */
283 1.1 elric sz = 1 << (bucket + 3);
284 1.1 elric #ifdef DEBUG
285 1.1 elric ASSERT(sz > 0);
286 1.1 elric #else
287 1.1 elric if (sz <= 0)
288 1.1 elric return;
289 1.1 elric #endif
290 1.1 elric if (sz < pagesz) {
291 1.1 elric amt = pagesz;
292 1.1 elric nblks = amt / sz;
293 1.1 elric } else {
294 1.1 elric amt = sz + pagesz;
295 1.1 elric nblks = 1;
296 1.1 elric }
297 1.1 elric op = (union overhead *)(void *)sbrk((int)amt);
298 1.1 elric /* no more room! */
299 1.1 elric if ((long)op == -1)
300 1.1 elric return;
301 1.1 elric /*
302 1.1 elric * Add new memory allocated to that on
303 1.1 elric * free list for this hash bucket.
304 1.1 elric */
305 1.1 elric nextf[bucket] = op;
306 1.1 elric while (--nblks > 0) {
307 1.1 elric op->ov_next =
308 1.1 elric (union overhead *)(void *)((caddr_t)(void *)op+(size_t)sz);
309 1.1 elric op = op->ov_next;
310 1.1 elric }
311 1.1 elric }
312 1.1 elric
313 1.1 elric void
314 1.5 riastrad free(void *cp)
315 1.4 riastrad {
316 1.1 elric long size;
317 1.1 elric union overhead *op;
318 1.1 elric
319 1.1 elric if (cp == NULL)
320 1.1 elric return;
321 1.1 elric op = (union overhead *)(void *)((caddr_t)cp - sizeof (union overhead));
322 1.1 elric #ifdef DEBUG
323 1.1 elric ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
324 1.1 elric #else
325 1.1 elric if (op->ov_magic != MAGIC)
326 1.1 elric return; /* sanity */
327 1.1 elric #endif
328 1.1 elric #ifdef RCHECK
329 1.1 elric ASSERT(op->ov_rmagic == RMAGIC);
330 1.1 elric ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
331 1.1 elric #endif
332 1.1 elric size = op->ov_index;
333 1.1 elric ASSERT(size < NBUCKETS);
334 1.1 elric mutex_lock(&malloc_mutex);
335 1.1 elric op->ov_next = nextf[(unsigned int)size];/* also clobbers ov_magic */
336 1.1 elric nextf[(unsigned int)size] = op;
337 1.1 elric #ifdef MSTATS
338 1.1 elric nmalloc[(size_t)size]--;
339 1.1 elric #endif
340 1.1 elric mutex_unlock(&malloc_mutex);
341 1.1 elric }
342 1.1 elric
343 1.1 elric /*
344 1.1 elric * When a program attempts "storage compaction" as mentioned in the
345 1.1 elric * old malloc man page, it realloc's an already freed block. Usually
346 1.1 elric * this is the last block it freed; occasionally it might be farther
347 1.1 elric * back. We have to search all the free lists for the block in order
348 1.1 elric * to determine its bucket: 1st we make one pass thru the lists
349 1.1 elric * checking only the first block in each; if that fails we search
350 1.1 elric * ``__realloc_srchlen'' blocks in each list for a match (the variable
351 1.1 elric * is extern so the caller can modify it). If that fails we just copy
352 1.1 elric * however many bytes was given to realloc() and hope it's not huge.
353 1.1 elric */
354 1.1 elric int __realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
355 1.1 elric
356 1.1 elric void *
357 1.5 riastrad realloc(void *cp, size_t nbytes)
358 1.4 riastrad {
359 1.1 elric u_long onb;
360 1.1 elric long i;
361 1.1 elric union overhead *op;
362 1.1 elric char *res;
363 1.1 elric int was_alloced = 0;
364 1.1 elric
365 1.1 elric if (cp == NULL)
366 1.1 elric return (malloc(nbytes));
367 1.1 elric if (nbytes == 0) {
368 1.1 elric free (cp);
369 1.1 elric return (NULL);
370 1.1 elric }
371 1.1 elric op = (union overhead *)(void *)((caddr_t)cp - sizeof (union overhead));
372 1.1 elric mutex_lock(&malloc_mutex);
373 1.1 elric if (op->ov_magic == MAGIC) {
374 1.1 elric was_alloced++;
375 1.1 elric i = op->ov_index;
376 1.1 elric } else {
377 1.1 elric /*
378 1.1 elric * Already free, doing "compaction".
379 1.1 elric *
380 1.1 elric * Search for the old block of memory on the
381 1.1 elric * free list. First, check the most common
382 1.1 elric * case (last element free'd), then (this failing)
383 1.1 elric * the last ``__realloc_srchlen'' items free'd.
384 1.1 elric * If all lookups fail, then assume the size of
385 1.1 elric * the memory block being realloc'd is the
386 1.1 elric * largest possible (so that all "nbytes" of new
387 1.1 elric * memory are copied into). Note that this could cause
388 1.1 elric * a memory fault if the old area was tiny, and the moon
389 1.1 elric * is gibbous. However, that is very unlikely.
390 1.1 elric */
391 1.1 elric if ((i = findbucket(op, 1)) < 0 &&
392 1.1 elric (i = findbucket(op, __realloc_srchlen)) < 0)
393 1.1 elric i = NBUCKETS;
394 1.1 elric }
395 1.1 elric onb = (u_long)1 << (u_long)(i + 3);
396 1.1 elric if (onb < pagesz)
397 1.1 elric onb -= sizeof (*op) + RSLOP;
398 1.1 elric else
399 1.1 elric onb += pagesz - sizeof (*op) - RSLOP;
400 1.1 elric /* avoid the copy if same size block */
401 1.1 elric if (was_alloced) {
402 1.1 elric if (i) {
403 1.1 elric i = (long)1 << (long)(i + 2);
404 1.1 elric if (i < pagesz)
405 1.1 elric i -= sizeof (*op) + RSLOP;
406 1.1 elric else
407 1.1 elric i += pagesz - sizeof (*op) - RSLOP;
408 1.1 elric }
409 1.1 elric if (nbytes <= onb && nbytes > i) {
410 1.1 elric #ifdef RCHECK
411 1.1 elric op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
412 1.1 elric *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
413 1.1 elric #endif
414 1.1 elric mutex_unlock(&malloc_mutex);
415 1.1 elric return (cp);
416 1.4 riastrad
417 1.1 elric }
418 1.1 elric #ifndef _REENT
419 1.1 elric else
420 1.1 elric free(cp);
421 1.1 elric #endif
422 1.1 elric }
423 1.1 elric mutex_unlock(&malloc_mutex);
424 1.1 elric if ((res = malloc(nbytes)) == NULL) {
425 1.1 elric #ifdef _REENT
426 1.1 elric free(cp);
427 1.1 elric #endif
428 1.1 elric return (NULL);
429 1.1 elric }
430 1.1 elric #ifndef _REENT
431 1.1 elric if (cp != res) /* common optimization if "compacting" */
432 1.1 elric (void)memmove(res, cp, (size_t)((nbytes < onb) ? nbytes : onb));
433 1.1 elric #else
434 1.1 elric (void)memmove(res, cp, (size_t)((nbytes < onb) ? nbytes : onb));
435 1.1 elric free(cp);
436 1.1 elric #endif
437 1.1 elric return (res);
438 1.1 elric }
439 1.1 elric
440 1.1 elric /*
441 1.1 elric * Search ``srchlen'' elements of each free list for a block whose
442 1.1 elric * header starts at ``freep''. If srchlen is -1 search the whole list.
443 1.1 elric * Return bucket number, or -1 if not found.
444 1.1 elric */
445 1.1 elric static int
446 1.5 riastrad findbucket(union overhead *freep, int srchlen)
447 1.1 elric {
448 1.1 elric union overhead *p;
449 1.1 elric int i, j;
450 1.1 elric
451 1.1 elric for (i = 0; i < NBUCKETS; i++) {
452 1.1 elric j = 0;
453 1.1 elric for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
454 1.1 elric if (p == freep)
455 1.1 elric return (i);
456 1.1 elric j++;
457 1.1 elric }
458 1.1 elric }
459 1.1 elric return (-1);
460 1.1 elric }
461 1.1 elric
462 1.1 elric #ifdef MSTATS
463 1.1 elric /*
464 1.1 elric * mstats - print out statistics about malloc
465 1.4 riastrad *
466 1.1 elric * Prints two lines of numbers, one showing the length of the free list
467 1.1 elric * for each size category, the second showing the number of mallocs -
468 1.1 elric * frees for each size category.
469 1.1 elric */
470 1.1 elric void
471 1.5 riastrad mstats(char *s)
472 1.1 elric {
473 1.1 elric int i, j;
474 1.1 elric union overhead *p;
475 1.1 elric int totfree = 0,
476 1.1 elric totused = 0;
477 1.1 elric
478 1.1 elric fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
479 1.1 elric for (i = 0; i < NBUCKETS; i++) {
480 1.1 elric for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
481 1.1 elric ;
482 1.1 elric fprintf(stderr, " %d", j);
483 1.1 elric totfree += j * (1 << (i + 3));
484 1.1 elric }
485 1.1 elric fprintf(stderr, "\nused:\t");
486 1.1 elric for (i = 0; i < NBUCKETS; i++) {
487 1.1 elric fprintf(stderr, " %d", nmalloc[i]);
488 1.1 elric totused += nmalloc[i] * (1 << (i + 3));
489 1.1 elric }
490 1.1 elric fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",
491 1.1 elric totused, totfree);
492 1.1 elric }
493 1.1 elric #endif
494 1.3 riastrad
495 1.3 riastrad /*
496 1.3 riastrad * Additional front ends:
497 1.3 riastrad * - aligned_alloc (C11)
498 1.3 riastrad * - calloc(n,m) = malloc(n*m) without overflow
499 1.3 riastrad * - posix_memalign (POSIX)
500 1.3 riastrad *
501 1.3 riastrad * These must all be in the same compilation unit as malloc, realloc,
502 1.3 riastrad * and free (or -lbsdmalloc must be surrounded by -Wl,--whole-archive
503 1.3 riastrad * -lbsdmalloc -Wl,--no-whole-archive) in order to override the libc
504 1.3 riastrad * built-in malloc implementation.
505 1.3 riastrad *
506 1.3 riastrad * Allocations of size n, up to and including the page size, are
507 1.3 riastrad * already aligned by malloc on multiples of n. Larger alignment is
508 1.3 riastrad * not supported.
509 1.3 riastrad */
510 1.3 riastrad
511 1.3 riastrad static long __constfunc
512 1.3 riastrad cachedpagesize(void)
513 1.3 riastrad {
514 1.3 riastrad long n;
515 1.3 riastrad
516 1.3 riastrad /* XXX atomic_load_relaxed, but that's not defined in userland atm */
517 1.3 riastrad if (__predict_false((n = pagesz) == 0)) {
518 1.3 riastrad mutex_lock(&malloc_mutex);
519 1.3 riastrad if ((n = pagesz) == 0)
520 1.3 riastrad n = pagesz = getpagesize();
521 1.3 riastrad mutex_unlock(&malloc_mutex);
522 1.3 riastrad }
523 1.3 riastrad
524 1.3 riastrad return n;
525 1.3 riastrad }
526 1.3 riastrad
527 1.3 riastrad void *
528 1.3 riastrad aligned_alloc(size_t alignment, size_t size)
529 1.3 riastrad {
530 1.3 riastrad char *p;
531 1.3 riastrad
532 1.3 riastrad if (alignment == 0 ||
533 1.3 riastrad (alignment & (alignment - 1)) != 0 ||
534 1.3 riastrad alignment > cachedpagesize() ||
535 1.3 riastrad (size & (alignment - 1)) != 0) {
536 1.3 riastrad errno = EINVAL;
537 1.3 riastrad return NULL;
538 1.3 riastrad }
539 1.3 riastrad p = malloc(size);
540 1.3 riastrad if (__predict_false(p == NULL))
541 1.3 riastrad ASSERT((uintptr_t)p % alignment == 0);
542 1.3 riastrad return p;
543 1.3 riastrad }
544 1.3 riastrad
545 1.3 riastrad void *
546 1.3 riastrad calloc(size_t nmemb, size_t size)
547 1.3 riastrad {
548 1.3 riastrad void *p;
549 1.3 riastrad size_t n;
550 1.3 riastrad
551 1.3 riastrad if (__builtin_mul_overflow_p(nmemb, size, (size_t)0)) {
552 1.3 riastrad errno = ENOMEM;
553 1.3 riastrad return NULL;
554 1.3 riastrad }
555 1.3 riastrad n = nmemb * size;
556 1.3 riastrad p = malloc(n);
557 1.3 riastrad if (__predict_false(p == NULL))
558 1.3 riastrad return NULL;
559 1.3 riastrad memset(p, 0, n);
560 1.3 riastrad return p;
561 1.3 riastrad }
562 1.3 riastrad
563 1.3 riastrad int
564 1.3 riastrad posix_memalign(void **memptr, size_t alignment, size_t size)
565 1.3 riastrad {
566 1.3 riastrad char *p;
567 1.3 riastrad
568 1.3 riastrad if (alignment < sizeof(void *) ||
569 1.3 riastrad (alignment & (alignment - 1)) != 0 ||
570 1.3 riastrad alignment > cachedpagesize())
571 1.3 riastrad return EINVAL;
572 1.3 riastrad p = malloc(size < alignment ? alignment : size);
573 1.3 riastrad if (__predict_false(p == NULL))
574 1.3 riastrad return ENOMEM;
575 1.3 riastrad ASSERT((uintptr_t)p % alignment == 0);
576 1.3 riastrad *memptr = p;
577 1.3 riastrad return 0;
578 1.3 riastrad }
579 1.3 riastrad
580 1.3 riastrad /*
581 1.3 riastrad * libc hooks required by fork
582 1.3 riastrad */
583 1.3 riastrad
584 1.3 riastrad #include "../libc/include/extern.h"
585 1.3 riastrad
586 1.3 riastrad void
587 1.3 riastrad _malloc_prefork(void)
588 1.3 riastrad {
589 1.3 riastrad
590 1.3 riastrad mutex_lock(&malloc_mutex);
591 1.3 riastrad }
592 1.3 riastrad
593 1.3 riastrad void
594 1.3 riastrad _malloc_postfork(void)
595 1.3 riastrad {
596 1.3 riastrad
597 1.3 riastrad mutex_unlock(&malloc_mutex);
598 1.3 riastrad }
599 1.3 riastrad
600 1.3 riastrad void
601 1.3 riastrad _malloc_postfork_child(void)
602 1.3 riastrad {
603 1.3 riastrad
604 1.3 riastrad mutex_unlock(&malloc_mutex);
605 1.3 riastrad }
606