6
6
//
7
7
// #define TRACK_ALLOCATIONS
8
8
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))
9
12
#define MAGIC 0xbadc0ffe
10
13
11
14
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);
13
21
}
14
22
15
23
memory_region::memory_region (rust_srv *srv, bool synchronized) :
@@ -54,7 +62,7 @@ memory_region::realloc(void *mem, size_t size) {
54
62
add_alloc ();
55
63
}
56
64
size_t old_size = size;
57
- size += sizeof (alloc_header) ;
65
+ size += HEADER_SIZE ;
58
66
alloc_header *alloc = get_header (mem);
59
67
assert (alloc->magic == MAGIC);
60
68
alloc->size = old_size;
@@ -64,7 +72,7 @@ memory_region::realloc(void *mem, size_t size) {
64
72
printf (" at index %d, found %p, expected %p\n " ,
65
73
alloc->index , _allocation_list[alloc->index ], alloc);
66
74
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 );
68
76
_srv->fatal (" not in allocation_list" , __FILE__, __LINE__, " " );
69
77
}
70
78
else {
@@ -74,25 +82,27 @@ memory_region::realloc(void *mem, size_t size) {
74
82
}
75
83
#endif
76
84
if (_synchronized) { _lock.unlock (); }
77
- return newMem-> data ;
85
+ return get_data ( newMem) ;
78
86
}
79
87
80
88
void *
81
89
memory_region::malloc (size_t size, const char *tag, bool zero) {
82
90
size_t old_size = size;
83
- size += sizeof (alloc_header) ;
91
+ size += HEADER_SIZE ;
84
92
alloc_header *mem = (alloc_header *)_srv->malloc (size);
85
93
mem->magic = MAGIC;
86
94
mem->tag = tag;
87
95
mem->index = -1 ;
88
96
mem->size = old_size;
89
- claim_alloc (mem->data );
97
+
98
+ void *data = get_data (mem);
99
+ claim_alloc (data);
90
100
91
101
if (zero) {
92
- memset (mem-> data , 0 , old_size);
102
+ memset (data, 0 , old_size);
93
103
}
94
104
95
- return mem-> data ;
105
+ return data;
96
106
}
97
107
98
108
void *
0 commit comments