malloc.c revision 1.10 1 1.10 simonb /* $NetBSD: malloc.c,v 1.10 2023/07/08 04:09:26 simonb 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.10 simonb __RCSID("$NetBSD: malloc.c,v 1.10 2023/07/08 04:09:26 simonb 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.6 riastrad static void morecore(int);
132 1.6 riastrad static int findbucket(union overhead *, int);
133 1.1 elric #ifdef MSTATS
134 1.6 riastrad void mstats(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.6 riastrad static void botch(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.6 riastrad botch(const char *s)
148 1.1 elric {
149 1.1 elric struct iovec iov[3];
150 1.1 elric
151 1.6 riastrad iov[0].iov_base = __UNCONST("\nassertion botched: ");
152 1.1 elric iov[0].iov_len = 20;
153 1.6 riastrad iov[1].iov_base = __UNCONST(s);
154 1.1 elric iov[1].iov_len = strlen(s);
155 1.6 riastrad iov[2].iov_base = __UNCONST("\n");
156 1.1 elric iov[2].iov_len = 1;
157 1.1 elric
158 1.1 elric /*
159 1.1 elric * This place deserves a word of warning: a cancellation point will
160 1.1 elric * occur when executing writev(), and we might be still owning
161 1.1 elric * malloc_mutex. At this point we need to disable cancellation
162 1.1 elric * until `after' abort() because i) establishing a cancellation handler
163 1.1 elric * might, depending on the implementation, result in another malloc()
164 1.1 elric * to be executed, and ii) it is really not desirable to let execution
165 1.1 elric * continue. `Fix me.'
166 1.4 riastrad *
167 1.1 elric * Note that holding mutex_lock during abort() is safe.
168 1.1 elric */
169 1.1 elric
170 1.1 elric (void)writev(STDERR_FILENO, iov, 3);
171 1.1 elric abort();
172 1.1 elric }
173 1.1 elric #else
174 1.3 riastrad #define ASSERT(p) ((void)sizeof((long)(p)))
175 1.1 elric #endif
176 1.1 elric
177 1.1 elric void *
178 1.5 riastrad malloc(size_t nbytes)
179 1.1 elric {
180 1.1 elric union overhead *op;
181 1.1 elric int bucket;
182 1.1 elric long n;
183 1.1 elric unsigned amt;
184 1.1 elric
185 1.1 elric mutex_lock(&malloc_mutex);
186 1.1 elric
187 1.1 elric /*
188 1.1 elric * First time malloc is called, setup page size and
189 1.1 elric * align break pointer so all data will be page aligned.
190 1.1 elric */
191 1.1 elric if (pagesz == 0) {
192 1.1 elric pagesz = n = getpagesize();
193 1.1 elric ASSERT(pagesz > 0);
194 1.1 elric op = (union overhead *)(void *)sbrk(0);
195 1.1 elric n = n - sizeof (*op) - ((long)op & (n - 1));
196 1.1 elric if (n < 0)
197 1.1 elric n += pagesz;
198 1.1 elric if (n) {
199 1.1 elric if (sbrk((int)n) == (void *)-1) {
200 1.1 elric mutex_unlock(&malloc_mutex);
201 1.1 elric return (NULL);
202 1.1 elric }
203 1.1 elric }
204 1.1 elric bucket = 0;
205 1.1 elric amt = 8;
206 1.1 elric while (pagesz > amt) {
207 1.1 elric amt <<= 1;
208 1.1 elric bucket++;
209 1.1 elric }
210 1.1 elric pagebucket = bucket;
211 1.1 elric }
212 1.1 elric /*
213 1.1 elric * Convert amount of memory requested into closest block size
214 1.1 elric * stored in hash buckets which satisfies request.
215 1.1 elric * Account for space used per block for accounting.
216 1.1 elric */
217 1.1 elric if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) {
218 1.1 elric #ifndef RCHECK
219 1.1 elric amt = 8; /* size of first bucket */
220 1.1 elric bucket = 0;
221 1.1 elric #else
222 1.1 elric amt = 16; /* size of first bucket */
223 1.1 elric bucket = 1;
224 1.1 elric #endif
225 1.1 elric n = -((long)sizeof (*op) + RSLOP);
226 1.1 elric } else {
227 1.1 elric amt = (unsigned)pagesz;
228 1.1 elric bucket = pagebucket;
229 1.1 elric }
230 1.1 elric while (nbytes > amt + n) {
231 1.1 elric amt <<= 1;
232 1.1 elric if (amt == 0)
233 1.1 elric return (NULL);
234 1.1 elric bucket++;
235 1.1 elric }
236 1.1 elric /*
237 1.1 elric * If nothing in hash bucket right now,
238 1.1 elric * request more memory from the system.
239 1.1 elric */
240 1.1 elric if ((op = nextf[bucket]) == NULL) {
241 1.1 elric morecore(bucket);
242 1.1 elric if ((op = nextf[bucket]) == NULL) {
243 1.1 elric mutex_unlock(&malloc_mutex);
244 1.1 elric return (NULL);
245 1.1 elric }
246 1.1 elric }
247 1.1 elric /* remove from linked list */
248 1.1 elric nextf[bucket] = op->ov_next;
249 1.1 elric op->ov_magic = MAGIC;
250 1.1 elric op->ov_index = bucket;
251 1.1 elric #ifdef MSTATS
252 1.1 elric nmalloc[bucket]++;
253 1.1 elric #endif
254 1.1 elric mutex_unlock(&malloc_mutex);
255 1.1 elric #ifdef RCHECK
256 1.1 elric /*
257 1.1 elric * Record allocated size of block and
258 1.1 elric * bound space with magic numbers.
259 1.1 elric */
260 1.1 elric op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
261 1.1 elric op->ov_rmagic = RMAGIC;
262 1.1 elric *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
263 1.1 elric #endif
264 1.1 elric return ((void *)(op + 1));
265 1.1 elric }
266 1.1 elric
267 1.1 elric /*
268 1.1 elric * Allocate more memory to the indicated bucket.
269 1.1 elric */
270 1.1 elric static void
271 1.5 riastrad morecore(int bucket)
272 1.1 elric {
273 1.1 elric union overhead *op;
274 1.1 elric long sz; /* size of desired block */
275 1.1 elric long amt; /* amount to allocate */
276 1.1 elric long nblks; /* how many blocks we get */
277 1.1 elric
278 1.1 elric /*
279 1.1 elric * sbrk_size <= 0 only for big, FLUFFY, requests (about
280 1.1 elric * 2^30 bytes on a VAX, I think) or for a negative arg.
281 1.1 elric */
282 1.1 elric sz = 1 << (bucket + 3);
283 1.1 elric #ifdef DEBUG
284 1.1 elric ASSERT(sz > 0);
285 1.1 elric #else
286 1.1 elric if (sz <= 0)
287 1.1 elric return;
288 1.1 elric #endif
289 1.1 elric if (sz < pagesz) {
290 1.1 elric amt = pagesz;
291 1.1 elric nblks = amt / sz;
292 1.1 elric } else {
293 1.1 elric amt = sz + pagesz;
294 1.1 elric nblks = 1;
295 1.1 elric }
296 1.1 elric op = (union overhead *)(void *)sbrk((int)amt);
297 1.1 elric /* no more room! */
298 1.1 elric if ((long)op == -1)
299 1.1 elric return;
300 1.1 elric /*
301 1.1 elric * Add new memory allocated to that on
302 1.1 elric * free list for this hash bucket.
303 1.1 elric */
304 1.1 elric nextf[bucket] = op;
305 1.1 elric while (--nblks > 0) {
306 1.1 elric op->ov_next =
307 1.1 elric (union overhead *)(void *)((caddr_t)(void *)op+(size_t)sz);
308 1.1 elric op = op->ov_next;
309 1.1 elric }
310 1.1 elric }
311 1.1 elric
312 1.1 elric void
313 1.5 riastrad free(void *cp)
314 1.4 riastrad {
315 1.1 elric long size;
316 1.1 elric union overhead *op;
317 1.1 elric
318 1.1 elric if (cp == NULL)
319 1.1 elric return;
320 1.1 elric op = (union overhead *)(void *)((caddr_t)cp - sizeof (union overhead));
321 1.1 elric #ifdef DEBUG
322 1.1 elric ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
323 1.1 elric #else
324 1.1 elric if (op->ov_magic != MAGIC)
325 1.1 elric return; /* sanity */
326 1.1 elric #endif
327 1.1 elric #ifdef RCHECK
328 1.1 elric ASSERT(op->ov_rmagic == RMAGIC);
329 1.1 elric ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
330 1.1 elric #endif
331 1.1 elric size = op->ov_index;
332 1.1 elric ASSERT(size < NBUCKETS);
333 1.1 elric mutex_lock(&malloc_mutex);
334 1.1 elric op->ov_next = nextf[(unsigned int)size];/* also clobbers ov_magic */
335 1.1 elric nextf[(unsigned int)size] = op;
336 1.1 elric #ifdef MSTATS
337 1.1 elric nmalloc[(size_t)size]--;
338 1.1 elric #endif
339 1.1 elric mutex_unlock(&malloc_mutex);
340 1.1 elric }
341 1.1 elric
342 1.1 elric /*
343 1.1 elric * When a program attempts "storage compaction" as mentioned in the
344 1.1 elric * old malloc man page, it realloc's an already freed block. Usually
345 1.1 elric * this is the last block it freed; occasionally it might be farther
346 1.1 elric * back. We have to search all the free lists for the block in order
347 1.1 elric * to determine its bucket: 1st we make one pass thru the lists
348 1.1 elric * checking only the first block in each; if that fails we search
349 1.1 elric * ``__realloc_srchlen'' blocks in each list for a match (the variable
350 1.1 elric * is extern so the caller can modify it). If that fails we just copy
351 1.1 elric * however many bytes was given to realloc() and hope it's not huge.
352 1.1 elric */
353 1.1 elric int __realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
354 1.1 elric
355 1.1 elric void *
356 1.5 riastrad realloc(void *cp, size_t nbytes)
357 1.4 riastrad {
358 1.1 elric u_long onb;
359 1.1 elric long i;
360 1.1 elric union overhead *op;
361 1.1 elric char *res;
362 1.1 elric int was_alloced = 0;
363 1.1 elric
364 1.1 elric if (cp == NULL)
365 1.1 elric return (malloc(nbytes));
366 1.1 elric if (nbytes == 0) {
367 1.1 elric free (cp);
368 1.1 elric return (NULL);
369 1.1 elric }
370 1.1 elric op = (union overhead *)(void *)((caddr_t)cp - sizeof (union overhead));
371 1.1 elric mutex_lock(&malloc_mutex);
372 1.1 elric if (op->ov_magic == MAGIC) {
373 1.1 elric was_alloced++;
374 1.1 elric i = op->ov_index;
375 1.1 elric } else {
376 1.1 elric /*
377 1.1 elric * Already free, doing "compaction".
378 1.1 elric *
379 1.1 elric * Search for the old block of memory on the
380 1.1 elric * free list. First, check the most common
381 1.1 elric * case (last element free'd), then (this failing)
382 1.1 elric * the last ``__realloc_srchlen'' items free'd.
383 1.1 elric * If all lookups fail, then assume the size of
384 1.1 elric * the memory block being realloc'd is the
385 1.1 elric * largest possible (so that all "nbytes" of new
386 1.1 elric * memory are copied into). Note that this could cause
387 1.1 elric * a memory fault if the old area was tiny, and the moon
388 1.1 elric * is gibbous. However, that is very unlikely.
389 1.1 elric */
390 1.1 elric if ((i = findbucket(op, 1)) < 0 &&
391 1.1 elric (i = findbucket(op, __realloc_srchlen)) < 0)
392 1.1 elric i = NBUCKETS;
393 1.1 elric }
394 1.1 elric onb = (u_long)1 << (u_long)(i + 3);
395 1.1 elric if (onb < pagesz)
396 1.1 elric onb -= sizeof (*op) + RSLOP;
397 1.1 elric else
398 1.1 elric onb += pagesz - sizeof (*op) - RSLOP;
399 1.1 elric /* avoid the copy if same size block */
400 1.1 elric if (was_alloced) {
401 1.1 elric if (i) {
402 1.1 elric i = (long)1 << (long)(i + 2);
403 1.1 elric if (i < pagesz)
404 1.1 elric i -= sizeof (*op) + RSLOP;
405 1.1 elric else
406 1.1 elric i += pagesz - sizeof (*op) - RSLOP;
407 1.1 elric }
408 1.1 elric if (nbytes <= onb && nbytes > i) {
409 1.1 elric #ifdef RCHECK
410 1.1 elric op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
411 1.1 elric *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
412 1.1 elric #endif
413 1.1 elric mutex_unlock(&malloc_mutex);
414 1.1 elric return (cp);
415 1.4 riastrad
416 1.1 elric }
417 1.1 elric #ifndef _REENT
418 1.1 elric else
419 1.1 elric free(cp);
420 1.1 elric #endif
421 1.1 elric }
422 1.1 elric mutex_unlock(&malloc_mutex);
423 1.1 elric if ((res = malloc(nbytes)) == NULL) {
424 1.1 elric #ifdef _REENT
425 1.1 elric free(cp);
426 1.1 elric #endif
427 1.1 elric return (NULL);
428 1.1 elric }
429 1.1 elric #ifndef _REENT
430 1.1 elric if (cp != res) /* common optimization if "compacting" */
431 1.1 elric (void)memmove(res, cp, (size_t)((nbytes < onb) ? nbytes : onb));
432 1.1 elric #else
433 1.1 elric (void)memmove(res, cp, (size_t)((nbytes < onb) ? nbytes : onb));
434 1.1 elric free(cp);
435 1.1 elric #endif
436 1.1 elric return (res);
437 1.1 elric }
438 1.1 elric
439 1.1 elric /*
440 1.1 elric * Search ``srchlen'' elements of each free list for a block whose
441 1.1 elric * header starts at ``freep''. If srchlen is -1 search the whole list.
442 1.1 elric * Return bucket number, or -1 if not found.
443 1.1 elric */
444 1.1 elric static int
445 1.5 riastrad findbucket(union overhead *freep, int srchlen)
446 1.1 elric {
447 1.1 elric union overhead *p;
448 1.1 elric int i, j;
449 1.1 elric
450 1.1 elric for (i = 0; i < NBUCKETS; i++) {
451 1.1 elric j = 0;
452 1.1 elric for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
453 1.1 elric if (p == freep)
454 1.1 elric return (i);
455 1.1 elric j++;
456 1.1 elric }
457 1.1 elric }
458 1.1 elric return (-1);
459 1.1 elric }
460 1.1 elric
461 1.1 elric #ifdef MSTATS
462 1.1 elric /*
463 1.1 elric * mstats - print out statistics about malloc
464 1.4 riastrad *
465 1.1 elric * Prints two lines of numbers, one showing the length of the free list
466 1.1 elric * for each size category, the second showing the number of mallocs -
467 1.1 elric * frees for each size category.
468 1.1 elric */
469 1.1 elric void
470 1.10 simonb mstats(const char *s)
471 1.1 elric {
472 1.1 elric int i, j;
473 1.1 elric union overhead *p;
474 1.1 elric int totfree = 0,
475 1.1 elric totused = 0;
476 1.1 elric
477 1.1 elric fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
478 1.1 elric for (i = 0; i < NBUCKETS; i++) {
479 1.1 elric for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
480 1.1 elric ;
481 1.1 elric fprintf(stderr, " %d", j);
482 1.1 elric totfree += j * (1 << (i + 3));
483 1.1 elric }
484 1.1 elric fprintf(stderr, "\nused:\t");
485 1.1 elric for (i = 0; i < NBUCKETS; i++) {
486 1.1 elric fprintf(stderr, " %d", nmalloc[i]);
487 1.1 elric totused += nmalloc[i] * (1 << (i + 3));
488 1.1 elric }
489 1.1 elric fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",
490 1.1 elric totused, totfree);
491 1.1 elric }
492 1.1 elric #endif
493 1.3 riastrad
494 1.3 riastrad /*
495 1.3 riastrad * Additional front ends:
496 1.3 riastrad * - aligned_alloc (C11)
497 1.3 riastrad * - calloc(n,m) = malloc(n*m) without overflow
498 1.3 riastrad * - posix_memalign (POSIX)
499 1.3 riastrad *
500 1.3 riastrad * These must all be in the same compilation unit as malloc, realloc,
501 1.3 riastrad * and free (or -lbsdmalloc must be surrounded by -Wl,--whole-archive
502 1.3 riastrad * -lbsdmalloc -Wl,--no-whole-archive) in order to override the libc
503 1.3 riastrad * built-in malloc implementation.
504 1.3 riastrad *
505 1.3 riastrad * Allocations of size n, up to and including the page size, are
506 1.3 riastrad * already aligned by malloc on multiples of n. Larger alignment is
507 1.3 riastrad * not supported.
508 1.3 riastrad */
509 1.3 riastrad
510 1.3 riastrad static long __constfunc
511 1.3 riastrad cachedpagesize(void)
512 1.3 riastrad {
513 1.3 riastrad long n;
514 1.3 riastrad
515 1.3 riastrad /* XXX atomic_load_relaxed, but that's not defined in userland atm */
516 1.3 riastrad if (__predict_false((n = pagesz) == 0)) {
517 1.3 riastrad mutex_lock(&malloc_mutex);
518 1.3 riastrad if ((n = pagesz) == 0)
519 1.3 riastrad n = pagesz = getpagesize();
520 1.3 riastrad mutex_unlock(&malloc_mutex);
521 1.3 riastrad }
522 1.3 riastrad
523 1.3 riastrad return n;
524 1.3 riastrad }
525 1.3 riastrad
526 1.3 riastrad void *
527 1.3 riastrad aligned_alloc(size_t alignment, size_t size)
528 1.3 riastrad {
529 1.3 riastrad char *p;
530 1.3 riastrad
531 1.3 riastrad if (alignment == 0 ||
532 1.3 riastrad (alignment & (alignment - 1)) != 0 ||
533 1.7 riastrad alignment > cachedpagesize()) {
534 1.3 riastrad errno = EINVAL;
535 1.3 riastrad return NULL;
536 1.3 riastrad }
537 1.8 riastrad p = malloc(size < alignment ? alignment : size);
538 1.3 riastrad if (__predict_false(p == NULL))
539 1.3 riastrad ASSERT((uintptr_t)p % alignment == 0);
540 1.3 riastrad return p;
541 1.3 riastrad }
542 1.3 riastrad
543 1.3 riastrad void *
544 1.3 riastrad calloc(size_t nmemb, size_t size)
545 1.3 riastrad {
546 1.3 riastrad void *p;
547 1.3 riastrad size_t n;
548 1.3 riastrad
549 1.9 riastrad if (__builtin_mul_overflow(nmemb, size, &n)) {
550 1.3 riastrad errno = ENOMEM;
551 1.3 riastrad return NULL;
552 1.3 riastrad }
553 1.3 riastrad p = malloc(n);
554 1.3 riastrad if (__predict_false(p == NULL))
555 1.3 riastrad return NULL;
556 1.3 riastrad memset(p, 0, n);
557 1.3 riastrad return p;
558 1.3 riastrad }
559 1.3 riastrad
560 1.3 riastrad int
561 1.3 riastrad posix_memalign(void **memptr, size_t alignment, size_t size)
562 1.3 riastrad {
563 1.3 riastrad char *p;
564 1.3 riastrad
565 1.3 riastrad if (alignment < sizeof(void *) ||
566 1.3 riastrad (alignment & (alignment - 1)) != 0 ||
567 1.3 riastrad alignment > cachedpagesize())
568 1.3 riastrad return EINVAL;
569 1.3 riastrad p = malloc(size < alignment ? alignment : size);
570 1.3 riastrad if (__predict_false(p == NULL))
571 1.3 riastrad return ENOMEM;
572 1.3 riastrad ASSERT((uintptr_t)p % alignment == 0);
573 1.3 riastrad *memptr = p;
574 1.3 riastrad return 0;
575 1.3 riastrad }
576 1.3 riastrad
577 1.3 riastrad /*
578 1.3 riastrad * libc hooks required by fork
579 1.3 riastrad */
580 1.3 riastrad
581 1.3 riastrad #include "../libc/include/extern.h"
582 1.3 riastrad
583 1.3 riastrad void
584 1.3 riastrad _malloc_prefork(void)
585 1.3 riastrad {
586 1.3 riastrad
587 1.3 riastrad mutex_lock(&malloc_mutex);
588 1.3 riastrad }
589 1.3 riastrad
590 1.3 riastrad void
591 1.3 riastrad _malloc_postfork(void)
592 1.3 riastrad {
593 1.3 riastrad
594 1.3 riastrad mutex_unlock(&malloc_mutex);
595 1.3 riastrad }
596 1.3 riastrad
597 1.3 riastrad void
598 1.3 riastrad _malloc_postfork_child(void)
599 1.3 riastrad {
600 1.3 riastrad
601 1.3 riastrad mutex_unlock(&malloc_mutex);
602 1.3 riastrad }
603