Skip to content

Commit 75b98a9

Browse files
committed
Correct the arithmetic on 64-bit builds.
1 parent 34a5074 commit 75b98a9

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/rt/memory_region.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66
//
77
// #define TRACK_ALLOCATIONS
88

9+
#define PTR_SIZE (sizeof(void*))
10+
#define ALIGN_PTR(x) (((x)+PTR_SIZE-1)/PTR_SIZE*PTR_SIZE)
11+
#define HEADER_SIZE ALIGN_PTR(sizeof(alloc_header))
912
#define MAGIC 0xbadc0ffe
1013

1114
memory_region::alloc_header *memory_region::get_header(void *mem) {
12-
return (alloc_header *)((char *)mem - sizeof(alloc_header));
15+
return (alloc_header *)((char *)mem - HEADER_SIZE);
16+
}
17+
18+
void *memory_region::get_data(alloc_header *ptr) {
19+
assert(ptr->magic == MAGIC);
20+
return (void*)((char *)ptr + HEADER_SIZE);
1321
}
1422

1523
memory_region::memory_region(rust_srv *srv, bool synchronized) :
@@ -54,7 +62,7 @@ memory_region::realloc(void *mem, size_t size) {
5462
add_alloc();
5563
}
5664
size_t old_size = size;
57-
size += sizeof(alloc_header);
65+
size += HEADER_SIZE;
5866
alloc_header *alloc = get_header(mem);
5967
assert(alloc->magic == MAGIC);
6068
alloc->size = old_size;
@@ -64,7 +72,7 @@ memory_region::realloc(void *mem, size_t size) {
6472
printf("at index %d, found %p, expected %p\n",
6573
alloc->index, _allocation_list[alloc->index], alloc);
6674
printf("realloc: ptr 0x%" PRIxPTR " (%s) is not in allocation_list\n",
67-
(uintptr_t) &alloc->data, alloc->tag);
75+
(uintptr_t) get_data(alloc), alloc->tag);
6876
_srv->fatal("not in allocation_list", __FILE__, __LINE__, "");
6977
}
7078
else {
@@ -74,25 +82,27 @@ memory_region::realloc(void *mem, size_t size) {
7482
}
7583
#endif
7684
if (_synchronized) { _lock.unlock(); }
77-
return newMem->data;
85+
return get_data(newMem);
7886
}
7987

8088
void *
8189
memory_region::malloc(size_t size, const char *tag, bool zero) {
8290
size_t old_size = size;
83-
size += sizeof(alloc_header);
91+
size += HEADER_SIZE;
8492
alloc_header *mem = (alloc_header *)_srv->malloc(size);
8593
mem->magic = MAGIC;
8694
mem->tag = tag;
8795
mem->index = -1;
8896
mem->size = old_size;
89-
claim_alloc(mem->data);
97+
98+
void *data = get_data(mem);
99+
claim_alloc(data);
90100

91101
if(zero) {
92-
memset(mem->data, 0, old_size);
102+
memset(data, 0, old_size);
93103
}
94104

95-
return mem->data;
105+
return data;
96106
}
97107

98108
void *

src/rt/memory_region.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class memory_region {
2020
int index;
2121
const char *tag;
2222
uint32_t size;
23-
char data[];
2423
};
2524

2625
alloc_header *get_header(void *mem);
26+
void *get_data(alloc_header *);
2727

2828
rust_srv *_srv;
2929
memory_region *_parent;

0 commit comments

Comments
 (0)