ext/rbs_extension/location.c in rbs-3.5.0 vs ext/rbs_extension/location.c in rbs-3.5.1.pre.1
- old
+ new
@@ -1,9 +1,10 @@
#include "rbs_extension.h"
#define RBS_LOC_REQUIRED_P(loc, i) ((loc)->children->required_p & (1 << (i)))
#define RBS_LOC_OPTIONAL_P(loc, i) (!RBS_LOC_REQUIRED_P((loc), (i)))
+#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))
VALUE RBS_Location;
position rbs_loc_position(int char_pos) {
position pos = { 0, char_pos, -1, -1 };
@@ -23,11 +24,11 @@
}
void rbs_loc_alloc_children(rbs_loc *loc, unsigned short cap) {
check_children_max(cap);
- size_t s = sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * cap;
+ size_t s = RBS_LOC_CHILDREN_SIZE(cap);
loc->children = malloc(s);
loc->children->len = 0;
loc->children->required_p = 0;
loc->children->cap = cap;
@@ -37,11 +38,11 @@
if (loc->children == NULL) {
rbs_loc_alloc_children(loc, 1);
} else {
if (loc->children->len == loc->children->cap) {
check_children_max(loc->children->cap + 1);
- size_t s = sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * (++loc->children->cap);
+ size_t s = RBS_LOC_CHILDREN_SIZE(++loc->children->cap);
loc->children = realloc(loc->children, s);
}
}
}
@@ -83,11 +84,11 @@
static size_t rbs_loc_memsize(const void *ptr) {
const rbs_loc *loc = ptr;
if (loc->children == NULL) {
return sizeof(rbs_loc);
} else {
- return sizeof(rbs_loc) + sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * loc->children->cap;
+ return sizeof(rbs_loc) + RBS_LOC_CHILDREN_SIZE(loc->children->cap);
}
}
static rb_data_type_t location_type = {
"RBS::Location",
@@ -127,10 +128,10 @@
self_loc->buffer = other_loc->buffer;
self_loc->rg = other_loc->rg;
if (other_loc->children != NULL) {
rbs_loc_alloc_children(self_loc, other_loc->children->cap);
- memcpy(self_loc->children, other_loc->children, sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * other_loc->children->cap);
+ memcpy(self_loc->children, other_loc->children, RBS_LOC_CHILDREN_SIZE(other_loc->children->cap));
}
return Qnil;
}