Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions free.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#include <stdlib.h>
#include <sys/mman.h>

Expand Down Expand Up @@ -131,7 +131,7 @@ void free(void *p)
if (!freed || mask+self==all) break;
if (!MT)
g->freed_mask = freed+self;
else if (a_cas(&g->freed_mask, freed, freed+self)!=freed)
else if ((uint32_t)a_cas(&g->freed_mask, freed, freed+self)!=freed)
continue;
return;
}
Expand Down
10 changes: 5 additions & 5 deletions malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct meta *alloc_meta(void)
if ((m = dequeue_head(&ctx.free_meta_head))) return m;
if (!ctx.avail_meta_count) {
int need_unprotect = 1;
if (!ctx.avail_meta_area_count && ctx.brk!=-1) {
if (!ctx.avail_meta_area_count && ctx.brk!=(uintptr_t)-1) {
uintptr_t new = ctx.brk + pagesize;
int need_guard = 0;
if (!ctx.brk) {
Expand Down Expand Up @@ -174,13 +174,13 @@ static int alloc_slot(int, size_t);
static struct meta *alloc_group(int sc, size_t req)
{
size_t size = UNIT*size_classes[sc];
int i = 0, cnt;
ssize_t i = 0, cnt;
unsigned char *p;
struct meta *m = alloc_meta();
if (!m) return 0;
size_t usage = ctx.usage_by_class[sc];
ssize_t usage = ctx.usage_by_class[sc];
size_t pagesize = PGSZ;
int active_idx;
ssize_t active_idx;
if (sc < 9) {
while (i<2 && 4*small_cnt_tab[sc][i] > usage)
i++;
Expand Down Expand Up @@ -359,7 +359,7 @@ void *malloc(size_t n)
if (!first) break;
if (RDLOCK_IS_EXCLUSIVE || !MT)
g->avail_mask = mask-first;
else if (a_cas(&g->avail_mask, mask, mask-first)!=mask)
else if ((uint32_t)a_cas(&g->avail_mask, mask, mask-first)!=mask)
continue;
idx = a_ctz_32(first);
goto success;
Expand Down
17 changes: 9 additions & 8 deletions meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,20 @@ static inline uint32_t activate_group(struct meta *m)
assert(!m->avail_mask);
uint32_t mask, act = (2u<<m->mem->active_idx)-1;
do mask = m->freed_mask;
while (a_cas(&m->freed_mask, mask, mask&~act)!=mask);
while ((uint32_t)a_cas(&m->freed_mask, mask, mask&~act)!=mask);
return m->avail_mask = mask & act;
}

static inline int get_slot_index(const unsigned char *p)
static inline size_t get_slot_index(const unsigned char *p)
{
return p[-3] & 31;
return (size_t)p[-3] & 31;
}

static inline struct meta *get_meta(const unsigned char *p)
{
assert(!((uintptr_t)p & 15));
int offset = *(const uint16_t *)(p - 2);
int index = get_slot_index(p);
uintptr_t offset = *(const uint16_t *)(p - 2);
size_t index = get_slot_index(p);
if (p[-4]) {
assert(!offset);
offset = *(uint32_t *)(p - 8);
Expand All @@ -146,7 +146,7 @@ static inline struct meta *get_meta(const unsigned char *p)
assert(area->check == ctx.secret);
if (meta->sizeclass < 48) {
assert(offset >= size_classes[meta->sizeclass]*index);
assert(offset < size_classes[meta->sizeclass]*(index+1));
assert(offset < size_classes[meta->sizeclass]*(index+1u));
} else {
assert(meta->sizeclass == 63);
}
Expand All @@ -165,7 +165,8 @@ static inline size_t get_nominal_size(const unsigned char *p, const unsigned cha
assert(reserved >= 5);
assert(!end[-5]);
}
assert(reserved <= end-p);
assert(p <= end);
assert(reserved <= (size_t)(end-p));
assert(!*(end-reserved));
// also check the slot's overflow byte
assert(!*end);
Expand Down Expand Up @@ -201,7 +202,7 @@ static inline void *enframe(struct meta *g, int idx, size_t n, int ctr)
unsigned char *end = p+stride-IB;
// cycle offset within slot to increase interval to address
// reuse, facilitate trapping double-free.
int off = (p[-3] ? *(uint16_t *)(p-2) + 1 : ctr) & 255;
uintptr_t off = (p[-3] ? (*(uint16_t *)(p-2u) + 1) : ctr) & 255;
assert(!p[-4]);
if (off > slack) {
size_t m = slack;
Expand Down