32
32
33
33
alignas (TINYMALLOC_MAX_ALIGN) static struct {
34
34
char memory[TINYMALLOC_MAX_BYTES];
35
- unsigned used, last, free;
35
+ size_t used, last, free;
36
36
} heap;
37
37
38
38
static inline bool isheap (char *mem) {
@@ -41,46 +41,47 @@ static inline bool isheap(char *mem) {
41
41
42
42
void free (void *ptr) {
43
43
char *mem;
44
- unsigned base;
44
+ size_t base;
45
45
if (ptr) {
46
46
mem = (char *)ptr;
47
47
unassert (isheap (mem));
48
48
base = mem - heap.memory ;
49
- *(unsigned *)mem = heap.free ;
49
+ *(size_t *)mem = heap.free ;
50
50
heap.free = base;
51
51
}
52
52
}
53
53
54
54
size_t malloc_usable_size (void *ptr) {
55
55
char *mem = (char *)ptr;
56
56
unassert (isheap (mem));
57
- return ((unsigned *)mem)[-1 ];
57
+ return ((size_t *)mem)[-1 ];
58
58
}
59
59
60
60
void *memalign (size_t align, size_t need) {
61
61
char *res;
62
- unsigned next, next2, base, toto, *link, *link2;
62
+ size_t next, next2, base, toto, *link, *link2;
63
63
64
64
// normalize arguments
65
65
while (align & (align - 1 ))
66
66
++align;
67
- if (need < sizeof (unsigned ))
68
- need = sizeof (unsigned );
69
- if (align < sizeof (unsigned ))
70
- align = sizeof (unsigned );
67
+ if (need < sizeof (size_t ))
68
+ need = sizeof (size_t );
69
+ if (align < sizeof (size_t ))
70
+ align = sizeof (size_t );
71
71
if (align > TINYMALLOC_MAX_ALIGN)
72
72
goto InvalidArgument;
73
- if (ckd_add (&need, need, sizeof (unsigned ) - 1 ))
73
+ // TODO(jart): refactor append*() to not need size_t*2 granularity
74
+ if (ckd_add (&need, need, sizeof (size_t ) * 2 - 1 ))
74
75
goto OutOfMemory;
75
- need &= -sizeof (unsigned );
76
+ need &= -sizeof (size_t );
76
77
77
78
// allocate from free list
78
79
next = heap.free ;
79
80
link = &heap.free ;
80
81
while (next) {
81
- next2 = *(unsigned *)(heap.memory + next);
82
- link2 = (unsigned *)(heap.memory + next);
83
- if (need <= ((unsigned *)(heap.memory + next))[-1 ]) {
82
+ next2 = *(size_t *)(heap.memory + next);
83
+ link2 = (size_t *)(heap.memory + next);
84
+ if (need <= ((size_t *)(heap.memory + next))[-1 ]) {
84
85
*link = next2;
85
86
return (void *)(heap.memory + next);
86
87
}
@@ -90,15 +91,15 @@ void *memalign(size_t align, size_t need) {
90
91
91
92
// allocate new static memory
92
93
base = heap.used ;
93
- base += sizeof (unsigned );
94
+ base += sizeof (size_t );
94
95
base += align - 1 ;
95
96
base &= -align;
96
97
if (ckd_add (&toto, base, need))
97
98
goto OutOfMemory;
98
99
if (toto > TINYMALLOC_MAX_BYTES)
99
100
goto OutOfMemory;
100
101
res = heap.memory + base;
101
- ((unsigned *)res)[-1 ] = need;
102
+ ((size_t *)res)[-1 ] = need;
102
103
heap.used = toto;
103
104
heap.last = base;
104
105
return res;
@@ -118,7 +119,7 @@ void *malloc(size_t need) {
118
119
119
120
void *calloc (size_t count, size_t size) {
120
121
char *res;
121
- unsigned need, used;
122
+ size_t need, used;
122
123
if (ckd_mul (&need, count, size))
123
124
need = -1 ;
124
125
used = heap.used ;
@@ -130,27 +131,27 @@ void *calloc(size_t count, size_t size) {
130
131
131
132
void *realloc (void *ptr, size_t need) {
132
133
char *res, *mem;
133
- unsigned base, have, toto;
134
+ size_t base, have, toto;
134
135
if (!ptr) {
135
136
res = (char *)malloc (need);
136
137
} else {
137
138
mem = (char *)ptr;
138
139
unassert (isheap (mem));
139
- have = ((unsigned *)mem)[-1 ];
140
+ have = ((size_t *)mem)[-1 ];
140
141
base = mem - heap.memory ;
141
142
if (need < have) {
142
143
res = mem;
143
144
} else if (base == heap.last ) {
144
- if (need < sizeof (unsigned ))
145
- need = sizeof (unsigned );
146
- if (ckd_add (&need, need, sizeof (unsigned ) - 1 ))
145
+ if (need < sizeof (size_t ))
146
+ need = sizeof (size_t );
147
+ if (ckd_add (&need, need, sizeof (size_t ) - 1 ))
147
148
goto OutOfMemory;
148
- need &= -sizeof (unsigned );
149
+ need &= -sizeof (size_t );
149
150
if (ckd_add (&toto, base, need))
150
151
goto OutOfMemory;
151
152
if (toto > TINYMALLOC_MAX_BYTES)
152
153
goto OutOfMemory;
153
- ((unsigned *)mem)[-1 ] = need;
154
+ ((size_t *)mem)[-1 ] = need;
154
155
heap.used = toto;
155
156
res = mem;
156
157
} else if ((res = (char *)malloc (need))) {
0 commit comments