Handle allocation failures when copying tree arrays

This commit is contained in:
Max Brunsfeld 2016-06-14 14:46:49 -07:00
parent f77c08eff5
commit 2109f0ed74
4 changed files with 22 additions and 18 deletions

View file

@ -40,11 +40,21 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size,
return result;
}
TreeArray ts_tree_array_copy(TreeArray *self) {
TreeArray result = array_copy(self);
for (size_t i = 0; i < result.size; i++)
ts_tree_retain(result.contents[i]);
return result;
bool ts_tree_array_copy(TreeArray self, TreeArray *dest) {
TSTree **contents = NULL;
if (self.capacity > 0) {
contents = ts_calloc(self.capacity, sizeof(TSTree *));
if (!contents)
return false;
memcpy(contents, self.contents, self.size * sizeof(TSTree *));
for (size_t i = 0; i < self.size; i++)
ts_tree_retain(contents[i]);
}
dest->size = self.size;
dest->capacity = self.capacity;
dest->contents = contents;
return true;
}
void ts_tree_array_delete(TreeArray *self) {