18
18
#endif
19
19
20
20
#undef DPRINT
21
- #define DPRINT (fmt, ...)
21
+ #define DPRINT (fmt, ...)
22
22
23
23
const size_t DEFAULT_CHUNK_SIZE = 128 ;
24
24
const size_t MAX_CHUNK_SIZE = (1024 *64 );
@@ -38,16 +38,20 @@ struct rust_obstack_alloc {
38
38
39
39
void *
40
40
rust_obstack_chunk::alloc (size_t len, type_desc *tydesc) {
41
- alen = align_to ( alen, DEFAULT_ALIGNMENT );
41
+ DPRINT ( " alloc(%lu) alen=%lu size=%lu \n " , len, alen, size );
42
42
43
- if (sizeof (rust_obstack_alloc) + len > size - alen) {
44
- DPRINT (" Not enough space, len=%lu!\n " , len);
43
+ size_t aligned_alen = align_to (alen, DEFAULT_ALIGNMENT);
44
+ size_t end_alen = aligned_alen + sizeof (rust_obstack_alloc) + len;
45
+ if (end_alen > size) {
46
+ DPRINT (" Not enough space, len=%lu alen=%lu aligned=%lu end=%lu!\n " ,
47
+ len, alen, aligned_alen, end_alen);
45
48
return NULL ; // Not enough space.
46
49
}
47
50
48
- rust_obstack_alloc *a = new (data + alen) rust_obstack_alloc (len, tydesc);
49
- alen += sizeof (*a) + len;
51
+ rust_obstack_alloc *a =
52
+ new (data + aligned_alen) rust_obstack_alloc ( len, tydesc) ;
50
53
memset (a->data , ' \0 ' , len); // FIXME: For GC.
54
+ alen = end_alen;
51
55
return &a->data ;
52
56
}
53
57
@@ -63,7 +67,9 @@ rust_obstack_chunk::free(void *ptr) {
63
67
64
68
void *
65
69
rust_obstack_chunk::mark () {
66
- return data + alen;
70
+ uint8_t *m = data + alen;
71
+ assert (m >= data && m <= data+size);
72
+ return m;
67
73
}
68
74
69
75
// Allocates the given number of bytes in a new chunk.
@@ -109,7 +115,9 @@ rust_obstack::free(void *ptr) {
109
115
110
116
assert (chunk);
111
117
while (!chunk->free (ptr)) {
112
- DPRINT (" deleting chunk at %p\n " , chunk);
118
+ DPRINT (" deleting chunk at %p (ptr=%p, data=%p-%p)\n " ,
119
+ chunk, ptr,
120
+ chunk->data , chunk->data + chunk->size );
113
121
rust_obstack_chunk *prev = chunk->prev ;
114
122
task->free (chunk);
115
123
chunk = prev;
@@ -119,7 +127,11 @@ rust_obstack::free(void *ptr) {
119
127
120
128
void *
121
129
rust_obstack::mark () {
122
- return chunk ? chunk->mark () : NULL ;
130
+ void *m = chunk ? chunk->mark () : NULL ;
131
+ DPRINT (" mark == %p, chunk == %p, data == %p-%p\n " , m, chunk,
132
+ (chunk ? chunk->data : NULL ),
133
+ (chunk ? chunk->data + chunk->size : NULL ));
134
+ return m;
123
135
}
124
136
125
137
0 commit comments