5 void *aligned_alloc(size_t align, size_t len)
7 if ((align & -align) != align) {
12 if (len > SIZE_MAX - align || align >= (1ULL<<31)*UNIT) {
17 if (DISABLE_ALIGNED_ALLOC) {
22 if (align <= UNIT) align = UNIT;
24 unsigned char *p = malloc(len + align - UNIT);
25 struct meta *g = get_meta(p);
26 int idx = get_slot_index(p);
27 size_t stride = get_stride(g);
28 unsigned char *start = g->mem->storage + stride*idx;
29 unsigned char *end = g->mem->storage + stride*(idx+1) - IB;
30 size_t adj = -(uintptr_t)p & (align-1);
33 set_size(p, end, len);
37 uint32_t offset = (size_t)(p-g->mem->storage)/UNIT;
38 if (offset <= 0xffff) {
39 *(uint16_t *)(p-2) = offset;
42 // use a 32-bit offset if 16-bit doesn't fit. for this,
43 // 16-bit field must be zero, [-4] byte nonzero.
44 *(uint16_t *)(p-2) = 0;
45 *(uint32_t *)(p-8) = offset;
49 set_size(p, end, len);
50 // store offset to aligned enframing. this facilitates cycling
51 // offset and also iteration of heap for debugging/measurement.
52 // for extreme overalignment it won't fit but these are classless
53 // allocations anyway.
54 *(uint16_t *)(start - 2) = (size_t)(p-start)/UNIT;