mallocng/aligned_alloc: check for malloc failure
[musl] / src / malloc / mallocng / aligned_alloc.c
1 #include <stdlib.h>
2 #include <errno.h>
3 #include "meta.h"
4
5 void *aligned_alloc(size_t align, size_t len)
6 {
7         if ((align & -align) != align) {
8                 errno = EINVAL;
9                 return 0;
10         }
11
12         if (len > SIZE_MAX - align || align >= (1ULL<<31)*UNIT) {
13                 errno = ENOMEM;
14                 return 0;
15         }
16
17         if (DISABLE_ALIGNED_ALLOC) {
18                 errno = ENOMEM;
19                 return 0;
20         }
21
22         if (align <= UNIT) align = UNIT;
23
24         unsigned char *p = malloc(len + align - UNIT);
25         if (!p)
26                 return 0;
27
28         struct meta *g = get_meta(p);
29         int idx = get_slot_index(p);
30         size_t stride = get_stride(g);
31         unsigned char *start = g->mem->storage + stride*idx;
32         unsigned char *end = g->mem->storage + stride*(idx+1) - IB;
33         size_t adj = -(uintptr_t)p & (align-1);
34
35         if (!adj) {
36                 set_size(p, end, len);
37                 return p;
38         }
39         p += adj;
40         uint32_t offset = (size_t)(p-g->mem->storage)/UNIT;
41         if (offset <= 0xffff) {
42                 *(uint16_t *)(p-2) = offset;
43                 p[-4] = 0;
44         } else {
45                 // use a 32-bit offset if 16-bit doesn't fit. for this,
46                 // 16-bit field must be zero, [-4] byte nonzero.
47                 *(uint16_t *)(p-2) = 0;
48                 *(uint32_t *)(p-8) = offset;
49                 p[-4] = 1;
50         }
51         p[-3] = idx;
52         set_size(p, end, len);
53         // store offset to aligned enframing. this facilitates cycling
54         // offset and also iteration of heap for debugging/measurement.
55         // for extreme overalignment it won't fit but these are classless
56         // allocations anyway.
57         *(uint16_t *)(start - 2) = (size_t)(p-start)/UNIT;
58         start[-3] = 7<<5;
59         return p;
60 }