Skip to content

Commit

Permalink
Uniform usage of IS_FORWARDING_ADDR_OR_NIL() and FORWARDED_ADDR()
Browse files Browse the repository at this point in the history
(refactoring)

* allchblk.c (get_block_ending_at, GC_free_block_ending_at): Rename
phdr local variable to hhdr.
* alloc.c (GC_add_to_heap): Likewise.
* allchblk.c (get_block_ending_at): Change
while(IS_FORWARDING_ADDR_OR_NIL(hhdr)) to for() statement.
* checksums.c (GC_update_check_page): Likewise.
* ptr_chck.c (GC_same_obj, GC_is_valid_displacement): Likewise.
* checksums.c (GC_update_check_page): Use FORWARDED_ADDR().
* headers.c (GC_header_cache_miss): Likewise.
  • Loading branch information
ivmai committed Jan 24, 2024
1 parent 0038ded commit 3abb17f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 41 deletions.
23 changes: 11 additions & 12 deletions allchblk.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ STATIC void GC_remove_from_fl_at(hdr *hhdr, int index)
GC_ASSERT(GC_free_bytes[index] >= hhdr -> hb_sz);
GC_free_bytes[index] -= hhdr -> hb_sz;
if (0 != hhdr -> hb_next) {
hdr * nhdr;
hdr *nhdr;
GC_ASSERT(!IS_FORWARDING_ADDR_OR_NIL(NHDR(hhdr)));
GET_HDR(hhdr -> hb_next, nhdr);
nhdr -> hb_prev = hhdr -> hb_prev;
Expand All @@ -352,20 +352,19 @@ GC_INLINE void GC_remove_from_fl(hdr *hhdr)
static struct hblk * get_block_ending_at(struct hblk *h)
{
struct hblk * p = h - 1;
hdr * phdr;
hdr *hhdr;

GET_HDR(p, phdr);
while (0 != phdr && IS_FORWARDING_ADDR_OR_NIL(phdr)) {
p = FORWARDED_ADDR(p,phdr);
phdr = HDR(p);
GET_HDR(p, hhdr);
for (; IS_FORWARDING_ADDR_OR_NIL(hhdr) && hhdr != NULL; hhdr = HDR(p)) {
p = FORWARDED_ADDR(p, hhdr);
}
if (0 != phdr) {
if (hhdr != NULL) {
return p;
}
p = GC_prev_block(h - 1);
if (p) {
phdr = HDR(p);
if ((ptr_t)p + phdr -> hb_sz == (ptr_t)h) {
if (p != NULL) {
hhdr = HDR(p);
if ((ptr_t)p + hhdr -> hb_sz == (ptr_t)h) {
return p;
}
}
Expand All @@ -378,9 +377,9 @@ STATIC struct hblk * GC_free_block_ending_at(struct hblk *h)
struct hblk * p = get_block_ending_at(h);

if (p /* != NULL */) { /* CPPCHECK */
hdr * phdr = HDR(p);
hdr *hhdr = HDR(p);

if (HBLK_IS_FREE(phdr)) {
if (HBLK_IS_FREE(hhdr)) {
return p;
}
}
Expand Down
10 changes: 5 additions & 5 deletions alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ GC_INNER ptr_t GC_os_get_mem(size_t bytes)
/* Assumes p is HBLKSIZE aligned, bytes argument is a multiple of HBLKSIZE. */
STATIC void GC_add_to_heap(struct hblk *p, size_t bytes)
{
hdr * phdr;
hdr *hhdr;
word endp;
size_t old_capacity = 0;
void *old_heap_sects = NULL;
Expand Down Expand Up @@ -1424,8 +1424,8 @@ STATIC void GC_add_to_heap(struct hblk *p, size_t bytes)
if (0 == bytes) return;
endp -= HBLKSIZE;
}
phdr = GC_install_header(p);
if (EXPECT(NULL == phdr, FALSE)) {
hhdr = GC_install_header(p);
if (EXPECT(NULL == hhdr, FALSE)) {
/* This is extremely unlikely. Can't add it. This will */
/* almost certainly result in a 0 return from the allocator, */
/* which is entirely appropriate. */
Expand All @@ -1446,8 +1446,8 @@ STATIC void GC_add_to_heap(struct hblk *p, size_t bytes)
GC_heap_sects[GC_n_heap_sects].hs_start = (ptr_t)p;
GC_heap_sects[GC_n_heap_sects].hs_bytes = bytes;
GC_n_heap_sects++;
phdr -> hb_sz = bytes;
phdr -> hb_flags = 0;
hhdr -> hb_sz = bytes;
hhdr -> hb_flags = 0;
GC_freehblk(p);
GC_heapsize += bytes;

Expand Down
7 changes: 3 additions & 4 deletions checksums.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ STATIC void GC_update_check_page(struct hblk *h, int index)
} else {
GC_n_clean++;
}
b = h;
while (IS_FORWARDING_ADDR_OR_NIL(hhdr) && hhdr != 0) {
b -= (word)hhdr;
hhdr = HDR(b);
for (b = h; IS_FORWARDING_ADDR_OR_NIL(hhdr) && hhdr != NULL;
hhdr = HDR(b)) {
b = FORWARDED_ADDR(b, hhdr);
}
if (pe -> new_valid && hhdr != NULL && !IS_PTRFREE(hhdr)
&& pe -> old_sum != pe -> new_sum) {
Expand Down
18 changes: 9 additions & 9 deletions headers.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ GC_INNER hdr *
#endif
{
hdr *hhdr;

HC_MISS();
GET_HDR(p, hhdr);
if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
if (GC_all_interior_pointers) {
if (hhdr != 0) {
ptr_t current = p;
if (hhdr != NULL) {
ptr_t current = (ptr_t)HBLKPTR(p);

current = (ptr_t)HBLKPTR(current);
do {
current = current - HBLKSIZE * (word)hhdr;
current = (ptr_t)FORWARDED_ADDR(current, hhdr);
hhdr = HDR(current);
} while(IS_FORWARDING_ADDR_OR_NIL(hhdr));
} while (IS_FORWARDING_ADDR_OR_NIL(hhdr));
/* current points to near the start of the large object */
if (hhdr -> hb_flags & IGNORE_OFF_PAGE)
return 0;
if (HBLK_IS_FREE(hhdr)
|| p - current >= (signed_word)(hhdr -> hb_sz)) {
|| p - current >= (signed_word)(hhdr -> hb_sz)) {
GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
/* Pointer past the end of the block */
return 0;
Expand All @@ -80,13 +80,13 @@ GC_INNER hdr *
GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
/* And return zero: */
}
GC_ASSERT(hhdr == 0 || !HBLK_IS_FREE(hhdr));
GC_ASSERT(NULL == hhdr || !HBLK_IS_FREE(hhdr));
return hhdr;
/* Pointers past the first page are probably too rare */
/* to add them to the cache. We don't. */
/* And correctness relies on the fact that we don't. */
} else {
if (hhdr == 0) {
if (NULL == hhdr) {
GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
}
return 0;
Expand Down Expand Up @@ -338,7 +338,7 @@ GC_API void GC_CALL GC_apply_to_all_blocks(GC_walk_hblk_fn fn,
signed_word j;
bottom_index * index_p;

for (index_p = GC_all_bottom_indices; index_p != 0;
for (index_p = GC_all_bottom_indices; index_p != NULL;
index_p = index_p -> asc_link) {
for (j = BOTTOM_SZ-1; j >= 0;) {
if (!IS_FORWARDING_ADDR_OR_NIL(index_p->index[j])) {
Expand Down
10 changes: 5 additions & 5 deletions misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,11 @@ GC_API void * GC_CALL GC_base(void * p)
if (NULL == candidate_hdr) return NULL;
/* If it's a pointer to the middle of a large object, move it */
/* to the beginning. */
while (IS_FORWARDING_ADDR_OR_NIL(candidate_hdr)) {
h = FORWARDED_ADDR(h,candidate_hdr);
r = (ptr_t)h;
candidate_hdr = HDR(h);
}
while (IS_FORWARDING_ADDR_OR_NIL(candidate_hdr)) {
h = FORWARDED_ADDR(h, candidate_hdr);
r = (ptr_t)h;
candidate_hdr = HDR(h);
}
if (HBLK_IS_FREE(candidate_hdr)) return NULL;
/* Make sure r points to the beginning of the object */
r = (ptr_t)((word)r & ~(word)(WORDS_TO_BYTES(1)-1));
Expand Down
9 changes: 3 additions & 6 deletions ptr_chck.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ GC_API void * GC_CALL GC_same_obj(void *p, void *q)
/* to the beginning. */
if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
h = HBLKPTR(p) - (word)hhdr;
hhdr = HDR(h);
while (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
for (hhdr = HDR(h); IS_FORWARDING_ADDR_OR_NIL(hhdr); hhdr = HDR(h)) {
h = FORWARDED_ADDR(h, hhdr);
hhdr = HDR(h);
}
limit = (ptr_t)h + hhdr -> hb_sz;
if ((word)p >= (word)limit || (word)q >= (word)limit
Expand Down Expand Up @@ -113,9 +111,8 @@ GC_API void * GC_CALL GC_is_valid_displacement(void *p)
if (NULL == hhdr) return p;
h = HBLKPTR(p);
if (GC_all_interior_pointers) {
while (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
h = FORWARDED_ADDR(h, hhdr);
hhdr = HDR(h);
for (; IS_FORWARDING_ADDR_OR_NIL(hhdr); hhdr = HDR(h)) {
h = FORWARDED_ADDR(h, hhdr);
}
} else if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
goto fail;
Expand Down

0 comments on commit 3abb17f

Please sign in to comment.