From 102bcf08a7753e89ddc07c078af2042eb8ca917c Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Fri, 23 Oct 2020 14:52:36 +0300 Subject: [PATCH 1/2] pacify warning about _BSD_SOURCE --- free.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/free.c b/free.c index 40745f9..18e99ea 100644 --- a/free.c +++ b/free.c @@ -1,4 +1,4 @@ -#define _BSD_SOURCE +#define _DEFAULT_SOURCE #include #include From ed1e7dee94ec90938e020a645c74f87cd8512cb1 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Fri, 23 Oct 2020 14:41:14 +0300 Subject: [PATCH 2/2] pacify warnings about different signedness Depending on compiler options, -Wsign-compare option can be active; if it is active together with -Werror, the build fails. --- free.c | 2 +- malloc.c | 10 +++++----- meta.h | 17 +++++++++-------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/free.c b/free.c index 18e99ea..8eb844e 100644 --- a/free.c +++ b/free.c @@ -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; } diff --git a/malloc.c b/malloc.c index d695ab8..8fc6289 100644 --- a/malloc.c +++ b/malloc.c @@ -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) { @@ -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++; @@ -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; diff --git a/meta.h b/meta.h index 61ec53f..7f25e20 100644 --- a/meta.h +++ b/meta.h @@ -117,20 +117,20 @@ static inline uint32_t activate_group(struct meta *m) assert(!m->avail_mask); uint32_t mask, act = (2u<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); @@ -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); } @@ -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); @@ -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;