Skip to content

Commit 7071ff8

Browse files
committed
Add RBS_LOC_CHILDREN_SIZE macro
1 parent 64a96b1 commit 7071ff8

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

ext/rbs_extension/location.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#define RBS_LOC_REQUIRED_P(loc, i) ((loc)->children->required_p & (1 << (i)))
44
#define RBS_LOC_OPTIONAL_P(loc, i) (!RBS_LOC_REQUIRED_P((loc), (i)))
5+
#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * (cap))
56

67
VALUE RBS_Location;
78

@@ -25,7 +26,7 @@ static void check_children_max(unsigned short n) {
2526
void rbs_loc_alloc_children(rbs_loc *loc, unsigned short cap) {
2627
check_children_max(cap);
2728

28-
size_t s = sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * cap;
29+
size_t s = RBS_LOC_CHILDREN_SIZE(cap);
2930
loc->children = malloc(s);
3031

3132
loc->children->len = 0;
@@ -39,7 +40,7 @@ static void check_children_cap(rbs_loc *loc) {
3940
} else {
4041
if (loc->children->len == loc->children->cap) {
4142
check_children_max(loc->children->cap + 1);
42-
size_t s = sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * (++loc->children->cap);
43+
size_t s = RBS_LOC_CHILDREN_SIZE(++loc->children->cap);
4344
loc->children = realloc(loc->children, s);
4445
}
4546
}
@@ -85,7 +86,7 @@ static size_t rbs_loc_memsize(const void *ptr) {
8586
if (loc->children == NULL) {
8687
return sizeof(rbs_loc);
8788
} else {
88-
return sizeof(rbs_loc) + sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * loc->children->cap;
89+
return sizeof(rbs_loc) + RBS_LOC_CHILDREN_SIZE(loc->children->cap);
8990
}
9091
}
9192

@@ -129,7 +130,7 @@ static VALUE location_initialize_copy(VALUE self, VALUE other) {
129130
self_loc->rg = other_loc->rg;
130131
if (other_loc->children != NULL) {
131132
rbs_loc_alloc_children(self_loc, other_loc->children->cap);
132-
memcpy(self_loc->children, other_loc->children, sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * other_loc->children->cap);
133+
memcpy(self_loc->children, other_loc->children, RBS_LOC_CHILDREN_SIZE(other_loc->children->cap));
133134
}
134135

135136
return Qnil;

0 commit comments

Comments
 (0)