Skip to content

Commit eb5c8c1

Browse files
committed
mem: stop HG_MALLOC clobbering ROUNDTO for the other allocators
hg_malloc.h did "#undef ROUNDTO" and redefined it as sizeof(void *). Since common.h includes hg_malloc.h last - after hp_malloc.h - that definition won in every translation unit including common.h, silently replacing HP_MALLOC's "ROUNDTO 8UL" inside hp_malloc.c itself. On 64-bit sizeof(void *) is also 8, so the value was unchanged and nothing showed. On 32-bit it becomes 4 and size_t is unsigned int, which broke the build at mem/hp_malloc.o under -Werror=format ("%lu"/"%ld" against ROUNDTO in hp_malloc.c and hp_malloc_dyn.h), and, more quietly, changed HP_MALLOC's own geometry there: HP_LINEAR_HASH_SIZE and UN_HASH() are derived from ROUNDTO, the block struct is aligned to it, and it is persisted in the shm pattern file. Give HG_MALLOC its own HG_ROUNDTO instead, the way q_malloc already uses QM_ROUNDTO, and drop the #undef of the shared macro. No functional change to HG_MALLOC: HG_ROUNDTO keeps the exact definition ROUNDTO had here.
1 parent 13fbfcc commit eb5c8c1

5 files changed

Lines changed: 38 additions & 38 deletions

File tree

‎mem/hg_arena.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "../dprint.h"
3232
#include "../globals.h"
3333

34-
/* ~x1.5 ladder, all multiples of 32 so cells stay ROUNDTO-aligned; ported
34+
/* ~x1.5 ladder, all multiples of 32 so cells stay HG_ROUNDTO-aligned; ported
3535
* unchanged from cachedb_perf's pcache_arena.c cell_sizes[] - these are
3636
* TOTAL slot sizes (header + payload), same convention as there */
3737
static const unsigned int cell_sizes[HG_NCLASSES] = {
@@ -227,8 +227,8 @@ int hg_arena_init(struct hg_block *hb, unsigned long hdr_size)
227227
hb->chunk_max = HG_CHUNK_MIN;
228228

229229
/* size -> class LUT: needed = requested payload + hidden header */
230-
for (idx = 0; idx <= HG_CELL_MAX / ROUNDTO; idx++) {
231-
needed = idx * ROUNDTO + HG_CELL_HDR;
230+
for (idx = 0; idx <= HG_CELL_MAX / HG_ROUNDTO; idx++) {
231+
needed = idx * HG_ROUNDTO + HG_CELL_HDR;
232232
for (c = 0; c < HG_NCLASSES; c++)
233233
if (cell_sizes[c] >= needed)
234234
break;
@@ -336,7 +336,7 @@ void *hg_cell_alloc(struct hg_block *hb, unsigned long size)
336336
return hg_large_alloc(hb, size);
337337
#endif
338338
}
339-
c = hb->size2class[(size + ROUNDTO - 1) / ROUNDTO];
339+
c = hb->size2class[(size + HG_ROUNDTO - 1) / HG_ROUNDTO];
340340
if (c >= HG_NCLASSES)
341341
return NULL;
342342

@@ -388,9 +388,9 @@ void *hg_cell_alloc(struct hg_block *hb, unsigned long size)
388388
payload = cell_start + HG_CELL_HDR;
389389
#ifdef DBG_MALLOC
390390
{
391-
const char **hfile = (const char **)(cell_start + ROUNDTO);
392-
const char **hfunc = (const char **)(cell_start + ROUNDTO * 2);
393-
unsigned long *hline = (unsigned long *)(cell_start + ROUNDTO * 3);
391+
const char **hfile = (const char **)(cell_start + HG_ROUNDTO);
392+
const char **hfunc = (const char **)(cell_start + HG_ROUNDTO * 2);
393+
unsigned long *hline = (unsigned long *)(cell_start + HG_ROUNDTO * 3);
394394
*hfile = file;
395395
*hfunc = func;
396396
*hline = line;

‎mem/hg_large.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "hg_arena.h"
2828
#include "../dprint.h"
2929

30-
#define HG_LARGE_MIN_FRAG ROUNDTO
30+
#define HG_LARGE_MIN_FRAG HG_ROUNDTO
3131
#define HG_LARGE_DEFAULT_CHUNK (1UL << 20) /* 1 MB - amortizes future churn */
3232

3333
struct hg_large_chunk {
@@ -75,7 +75,7 @@ void *hg_large_alloc(struct hg_block *hb, unsigned long size)
7575
struct hg_large_chunk *ch;
7676
char *base, *tag;
7777

78-
/* round to HG_PAYLOAD_ALIGN, not ROUNDTO: a frag's size is what
78+
/* round to HG_PAYLOAD_ALIGN, not HG_ROUNDTO: a frag's size is what
7979
* places the NEXT frag (HG_LFRAG_NEXT), so rounding to 4 on 32-bit
8080
* ARM would walk every subsequent frag - and its payload - off the
8181
* 8-byte boundary the first one started on */
@@ -178,9 +178,9 @@ void *hg_large_alloc(struct hg_block *hb, unsigned long size)
178178
*(unsigned char *)tag = HG_LARGE_MARKER;
179179
#ifdef DBG_MALLOC
180180
{
181-
const char **hfile = (const char **)(tag + ROUNDTO);
182-
const char **hfunc = (const char **)(tag + ROUNDTO * 2);
183-
unsigned long *hline = (unsigned long *)(tag + ROUNDTO * 3);
181+
const char **hfile = (const char **)(tag + HG_ROUNDTO);
182+
const char **hfunc = (const char **)(tag + HG_ROUNDTO * 2);
183+
unsigned long *hline = (unsigned long *)(tag + HG_ROUNDTO * 3);
184184
*hfile = file;
185185
*hfunc = func;
186186
*hline = line;

‎mem/hg_malloc.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ static unsigned long hg_hps(void)
118118
* all go through this one macro rather than repeating the expression. */
119119
#define HG_HPS_ROUND(s) (((s) + HG_HPS - 1) & ~(HG_HPS - 1))
120120

121-
/* ROUNDTO=2^k so the following works (same trick as f_malloc.c) */
122-
#define ROUNDTO_MASK (~((unsigned long)ROUNDTO-1))
123-
#define ROUNDUP_TO(s) (((s)+(ROUNDTO-1))&ROUNDTO_MASK)
121+
/* HG_ROUNDTO=2^k so the following works (same trick as f_malloc.c) */
122+
#define ROUNDTO_MASK (~((unsigned long)HG_ROUNDTO-1))
123+
#define ROUNDUP_TO(s) (((s)+(HG_ROUNDTO-1))&ROUNDTO_MASK)
124124

125125
/*
126126
* Tier ladder + verification, ported near-verbatim from cachedb_perf's
@@ -494,14 +494,14 @@ static void hg_dbg_dump_cb(void *payload, void *ctx)
494494
* is gated on DBG_MALLOC alone, so it must not depend on
495495
* SHM_EXTRA_STATS also being on.
496496
*/
497-
file = *(const char **)(tag + ROUNDTO);
497+
file = *(const char **)(tag + HG_ROUNDTO);
498498
if (!file)
499499
return; /* stamped before any hg_malloc_dbg() call ever ran
500500
* on this cell (e.g. still on its very first carve
501501
* without having been freed+realloc'd) - matches
502502
* fm_status_dbg's own "if (f->file)" guard */
503-
func = *(const char **)(tag + ROUNDTO * 2);
504-
line = *(unsigned long *)(tag + ROUNDTO * 3);
503+
func = *(const char **)(tag + HG_ROUNDTO * 2);
504+
line = *(unsigned long *)(tag + HG_ROUNDTO * 3);
505505

506506
if (dbg_ht_update(*c->allocd, file, func, line, hg_frag_size(payload)) < 0)
507507
LM_ERR("unable to update the %s allocation summary\n", "HG_MALLOC");

‎mem/hg_malloc.h‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
* cross-compiler. */
3333
#include "../globals.h"
3434

35-
#undef ROUNDTO
35+
#undef HG_ROUNDTO
3636

3737
#if defined(__CPU_sparc64) || defined(__CPU_sparc)
38-
#define ROUNDTO sizeof(long long)
38+
#define HG_ROUNDTO sizeof(long long)
3939
#else
40-
#define ROUNDTO sizeof(void *)
40+
#define HG_ROUNDTO sizeof(void *)
4141
#endif
4242

4343
#define HG_NCLASSES 21
@@ -68,34 +68,34 @@ const char *hg_mem_tier_str(enum hg_mem_tier tier);
6868
*
6969
* offset 0 : class id (immutable, stamped at
7070
* chunk-carve time)
71-
* offset ROUNDTO (DBG) : file
72-
* offset ROUNDTO*2 (DBG) : func
73-
* offset ROUNDTO*3 (DBG) : line
74-
* offset ROUNDTO+HG_CELL_HDR_DBG
71+
* offset HG_ROUNDTO (DBG) : file
72+
* offset HG_ROUNDTO*2 (DBG) : func
73+
* offset HG_ROUNDTO*3 (DBG) : line
74+
* offset HG_ROUNDTO+HG_CELL_HDR_DBG
7575
* (SHM_EXTRA_STATS) : statistic_index (mem-group index)
7676
* offset HG_CELL_HDR : payload starts here; while a cell
77-
* is FREE, the first ROUNDTO*2 bytes
77+
* is FREE, the first HG_ROUNDTO*2 bytes
7878
* of payload double as the free-list
7979
* link (cell_next()/cell_set_next()
8080
* in hg_arena.c) - safe, since
8181
* nobody reads payload of a free cell.
8282
*/
8383
#ifdef DBG_MALLOC
84-
#define HG_CELL_HDR_DBG (ROUNDTO * 3) /* file ptr + func ptr + line */
84+
#define HG_CELL_HDR_DBG (HG_ROUNDTO * 3) /* file ptr + func ptr + line */
8585
#else
8686
#define HG_CELL_HDR_DBG 0
8787
#endif
8888

8989
#ifdef SHM_EXTRA_STATS
90-
#define HG_CELL_HDR_STATS (ROUNDTO) /* statistic_index */
90+
#define HG_CELL_HDR_STATS (HG_ROUNDTO) /* statistic_index */
9191
#else
9292
#define HG_CELL_HDR_STATS 0
9393
#endif
9494

9595
/*
9696
* Payloads must be aligned for the widest scalar a caller may store in
9797
* them; 8 covers uint64_t/double everywhere we build. This is NOT implied
98-
* by ROUNDTO: on 32-bit ARM ROUNDTO is 4, so the raw header below would be
98+
* by HG_ROUNDTO: on 32-bit ARM HG_ROUNDTO is 4, so the raw header below would be
9999
* 4 bytes in a plain build, and since cells always start 32-byte aligned
100100
* EVERY payload would land at 4 mod 8 - misaligned for any 64-bit field,
101101
* and an outright fault for the LDREXD/STREXD that gen_lock_t and the
@@ -106,13 +106,13 @@ const char *hg_mem_tier_str(enum hg_mem_tier tier);
106106
* stays exactly where it was and only the header's total size grows.
107107
*/
108108
#define HG_PAYLOAD_ALIGN 8
109-
#define HG_CELL_HDR_RAW (ROUNDTO + HG_CELL_HDR_DBG + HG_CELL_HDR_STATS)
109+
#define HG_CELL_HDR_RAW (HG_ROUNDTO + HG_CELL_HDR_DBG + HG_CELL_HDR_STATS)
110110
#define HG_CELL_HDR \
111111
(((HG_CELL_HDR_RAW + HG_PAYLOAD_ALIGN - 1) / HG_PAYLOAD_ALIGN) \
112112
* HG_PAYLOAD_ALIGN)
113113

114114
/* offset of the statistic_index field, valid only when SHM_EXTRA_STATS */
115-
#define HG_CELL_STATS_OFF (ROUNDTO + HG_CELL_HDR_DBG)
115+
#define HG_CELL_STATS_OFF (HG_ROUNDTO + HG_CELL_HDR_DBG)
116116

117117
#define HG_HDR(p) ((char *)(p) - HG_CELL_HDR)
118118
#define HG_CLASS(p) (*(unsigned char *)HG_HDR(p))
@@ -138,7 +138,7 @@ struct hg_lfrag; /* opaque here, defined in hg_large.h */
138138
* sizeof() on it) to locate a large frag's header from a payload pointer.
139139
* hg_large.c static_asserts this matches the real struct, so any future
140140
* field change there fails the build here instead of drifting silently. */
141-
#define HG_LFRAG_HDR_SIZE (4 * ROUNDTO)
141+
#define HG_LFRAG_HDR_SIZE (4 * HG_ROUNDTO)
142142

143143
struct hg_chunk {
144144
struct hg_chunk *next; /* global registry, append-only */
@@ -236,8 +236,8 @@ struct hg_block {
236236
enum hg_mem_tier tier;
237237
unsigned long locked_mb;
238238

239-
unsigned char size2class[(HG_CELL_MAX / ROUNDTO) + 1];
240-
} __attribute__ ((aligned (ROUNDTO)));
239+
unsigned char size2class[(HG_CELL_MAX / HG_ROUNDTO) + 1];
240+
} __attribute__ ((aligned (HG_ROUNDTO)));
241241

242242
/*
243243
* Reserves its own huge-page-backed (or gracefully degraded) region of
@@ -334,15 +334,15 @@ void hg_stats_set_index(void *ptr, unsigned long idx);
334334
#ifdef DBG_MALLOC
335335
static inline const char *hg_frag_file(void *p)
336336
{
337-
return *(const char **)(HG_HDR(p) + ROUNDTO);
337+
return *(const char **)(HG_HDR(p) + HG_ROUNDTO);
338338
}
339339
static inline const char *hg_frag_func(void *p)
340340
{
341-
return *(const char **)(HG_HDR(p) + ROUNDTO * 2);
341+
return *(const char **)(HG_HDR(p) + HG_ROUNDTO * 2);
342342
}
343343
static inline unsigned long hg_frag_line(void *p)
344344
{
345-
return *(unsigned long *)(HG_HDR(p) + ROUNDTO * 3);
345+
return *(unsigned long *)(HG_HDR(p) + HG_ROUNDTO * 3);
346346
}
347347
#else
348348
static inline const char *hg_frag_file(void *p) { return NULL; }

‎mem/hg_malloc_dyn.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void *hg_realloc(struct hg_block *hb, void *p, unsigned long size,
139139
return NULL;
140140
} else {
141141
cur_total = hg_cell_total_size(cls);
142-
need_total = ((size + ROUNDTO - 1) / ROUNDTO) * ROUNDTO + HG_CELL_HDR;
142+
need_total = ((size + HG_ROUNDTO - 1) / HG_ROUNDTO) * HG_ROUNDTO + HG_CELL_HDR;
143143
if (need_total <= cur_total)
144144
return p;
145145
}

0 commit comments

Comments
 (0)