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