Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce realloc() count in 4.2 beta #2325

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions spine-c/spine-c/src/spine/Animation.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ _SP_ARRAY_IMPLEMENT_TYPE(spPropertyIdArray, spPropertyId)
_SP_ARRAY_IMPLEMENT_TYPE(spTimelineArray, spTimeline *)

spAnimation *spAnimation_create(const char *name, spTimelineArray *timelines, float duration) {
int i, n;
int i, n, totalCount = 0;
spAnimation *self = NEW(spAnimation);
MALLOC_STR(self->name, name);
self->timelines = timelines != NULL ? timelines : spTimelineArray_create(1);
timelines = self->timelines;
self->timelineIds = spPropertyIdArray_create(16);

for (i = 0, n = timelines->size; i < n; i++)
totalCount += timelines->items[i]->propertyIdsCount;
self->timelineIds = spPropertyIdArray_create(totalCount);

for (i = 0, n = timelines->size; i < n; i++) {
spPropertyIdArray_addAllValues(self->timelineIds, timelines->items[i]->propertyIds, 0,
timelines->items[i]->propertyIdsCount);
Expand Down
2 changes: 1 addition & 1 deletion spine-c/spine-c/src/spine/BoneData.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
spBoneData *spBoneData_create(int index, const char *name, spBoneData *parent) {
spBoneData *self = NEW(spBoneData);
CONST_CAST(int, self->index) = index;
MALLOC_STR(self->name, name);
CONST_CAST(const char *, self->name) = name;
CONST_CAST(spBoneData *, self->parent) = parent;
self->scaleX = 1;
self->scaleY = 1;
Expand Down
2 changes: 1 addition & 1 deletion spine-c/spine-c/src/spine/IkConstraintData.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

spIkConstraintData *spIkConstraintData_create(const char *name) {
spIkConstraintData *self = NEW(spIkConstraintData);
MALLOC_STR(self->name, name);
CONST_CAST(const char *, self->name) = name;
self->bendDirection = 1;
self->compress = 0;
self->stretch = 0;
Expand Down
2 changes: 1 addition & 1 deletion spine-c/spine-c/src/spine/PathConstraintData.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

spPathConstraintData *spPathConstraintData_create(const char *name) {
spPathConstraintData *self = NEW(spPathConstraintData);
MALLOC_STR(self->name, name);
CONST_CAST(const char *, self->name) = name;
return self;
}

Expand Down
20 changes: 4 additions & 16 deletions spine-c/spine-c/src/spine/SkeletonBinary.c
Original file line number Diff line number Diff line change
Expand Up @@ -957,8 +957,8 @@ static void _readVertices(spSkeletonBinary *self, _dataInput *input, int *bonesC
float **vertices, int *worldVerticesLength, int vertexCount) {
int i, ii;
int verticesLength = vertexCount << 1;
spFloatArray *weights = spFloatArray_create(8);
spIntArray *bones = spIntArray_create(8);
spFloatArray *weights;
spIntArray *bones;

*worldVerticesLength = verticesLength;

Expand All @@ -967,13 +967,11 @@ static void _readVertices(spSkeletonBinary *self, _dataInput *input, int *bonesC
*vertices = _readFloatArray(input, verticesLength, self->scale);
*bonesCount = 0;
*bones2 = NULL;
spFloatArray_dispose(weights);
spIntArray_dispose(bones);
return;
}

spFloatArray_ensureCapacity(weights, verticesLength * 3 * 3);
spIntArray_ensureCapacity(bones, verticesLength * 3);
weights = spFloatArray_create(verticesLength * 3 * 3);
bones = spIntArray_create(verticesLength * 3);

for (i = 0; i < vertexCount; ++i) {
int boneCount = readVarint(input, 1);
Expand Down Expand Up @@ -1379,9 +1377,7 @@ spSkeletonData *spSkeletonBinary_readSkeletonData(spSkeletonBinary *self, const
int mode;
const char *name = readString(input);
spBoneData *parent = i == 0 ? 0 : skeletonData->bones[readVarint(input, 1)];
/* TODO Avoid copying of name */
data = spBoneData_create(i, name, parent);
FREE(name);
data->rotation = readFloat(input);
data->x = readFloat(input) * self->scale;
data->y = readFloat(input) * self->scale;
Expand Down Expand Up @@ -1423,9 +1419,7 @@ spSkeletonData *spSkeletonBinary_readSkeletonData(spSkeletonBinary *self, const
const char *attachmentName;
const char *slotName = readString(input);
spBoneData *boneData = skeletonData->bones[readVarint(input, 1)];
/* TODO Avoid copying of slotName */
spSlotData *slotData = spSlotData_create(i, slotName, boneData);
FREE(slotName);
readColor(input, &slotData->color.r, &slotData->color.g, &slotData->color.b, &slotData->color.a);
a = readByte(input);
r = readByte(input);
Expand All @@ -1448,11 +1442,9 @@ spSkeletonData *spSkeletonBinary_readSkeletonData(spSkeletonBinary *self, const
skeletonData->ikConstraints = MALLOC(spIkConstraintData *, skeletonData->ikConstraintsCount);
for (i = 0; i < skeletonData->ikConstraintsCount; ++i) {
const char *name = readString(input);
/* TODO Avoid copying of name */
spIkConstraintData *data = spIkConstraintData_create(name);
data->order = readVarint(input, 1);
data->skinRequired = readBoolean(input);
FREE(name);
data->bonesCount = readVarint(input, 1);
data->bones = MALLOC(spBoneData *, data->bonesCount);
for (ii = 0; ii < data->bonesCount; ++ii)
Expand All @@ -1473,11 +1465,9 @@ spSkeletonData *spSkeletonBinary_readSkeletonData(spSkeletonBinary *self, const
spTransformConstraintData *, skeletonData->transformConstraintsCount);
for (i = 0; i < skeletonData->transformConstraintsCount; ++i) {
const char *name = readString(input);
/* TODO Avoid copying of name */
spTransformConstraintData *data = spTransformConstraintData_create(name);
data->order = readVarint(input, 1);
data->skinRequired = readBoolean(input);
FREE(name);
data->bonesCount = readVarint(input, 1);
CONST_CAST(spBoneData **, data->bones) = MALLOC(spBoneData *, data->bonesCount);
for (ii = 0; ii < data->bonesCount; ++ii)
Expand Down Expand Up @@ -1505,11 +1495,9 @@ spSkeletonData *spSkeletonBinary_readSkeletonData(spSkeletonBinary *self, const
skeletonData->pathConstraints = MALLOC(spPathConstraintData *, skeletonData->pathConstraintsCount);
for (i = 0; i < skeletonData->pathConstraintsCount; ++i) {
const char *name = readString(input);
/* TODO Avoid copying of name */
spPathConstraintData *data = spPathConstraintData_create(name);
data->order = readVarint(input, 1);
data->skinRequired = readBoolean(input);
FREE(name);
data->bonesCount = readVarint(input, 1);
CONST_CAST(spBoneData **, data->bones) = MALLOC(spBoneData *, data->bonesCount);
for (ii = 0; ii < data->bonesCount; ++ii)
Expand Down
36 changes: 21 additions & 15 deletions spine-c/spine-c/src/spine/SkeletonJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ spSkeletonData *spSkeletonJson_readSkeletonData(spSkeletonJson *self, const char
skeletonData->bones = MALLOC(spBoneData *, bones->size);
for (boneMap = bones->child, i = 0; boneMap; boneMap = boneMap->next, ++i) {
spBoneData *data;
const char *name;
const char *transformMode;
const char *color;

Expand All @@ -1030,7 +1031,8 @@ spSkeletonData *spSkeletonJson_readSkeletonData(spSkeletonJson *self, const char
}
}

data = spBoneData_create(skeletonData->bonesCount, Json_getString(boneMap, "name", 0), parent);
MALLOC_STR(name, Json_getString(boneMap, "name", 0));
data = spBoneData_create(skeletonData->bonesCount, name, parent);
data->length = Json_getFloat(boneMap, "length", 0) * self->scale;
data->x = Json_getFloat(boneMap, "x", 0) * self->scale;
data->y = Json_getFloat(boneMap, "y", 0) * self->scale;
Expand Down Expand Up @@ -1067,6 +1069,7 @@ spSkeletonData *spSkeletonJson_readSkeletonData(spSkeletonJson *self, const char
skeletonData->slots = MALLOC(spSlotData *, slots->size);
for (slotMap = slots->child, i = 0; slotMap; slotMap = slotMap->next, ++i) {
spSlotData *data;
const char *name;
const char *color;
const char *dark;
Json *item;
Expand All @@ -1079,7 +1082,8 @@ spSkeletonData *spSkeletonJson_readSkeletonData(spSkeletonJson *self, const char
return NULL;
}

data = spSlotData_create(i, Json_getString(slotMap, "name", 0), boneData);
MALLOC_STR(name, Json_getString(slotMap, "name", 0));
data = spSlotData_create(i, name, boneData);

color = Json_getString(slotMap, "color", 0);
if (color) {
Expand Down Expand Up @@ -1124,9 +1128,10 @@ spSkeletonData *spSkeletonJson_readSkeletonData(spSkeletonJson *self, const char
skeletonData->ikConstraintsCount = ik->size;
skeletonData->ikConstraints = MALLOC(spIkConstraintData *, ik->size);
for (constraintMap = ik->child, i = 0; constraintMap; constraintMap = constraintMap->next, ++i) {
const char *targetName;
const char *name, *targetName;

spIkConstraintData *data = spIkConstraintData_create(Json_getString(constraintMap, "name", 0));
MALLOC_STR(name, Json_getString(constraintMap, "name", 0));
spIkConstraintData *data = spIkConstraintData_create(name);
data->order = Json_getInt(constraintMap, "order", 0);
data->skinRequired = Json_getInt(constraintMap, "skin", 0) ? 1 : 0;

Expand Down Expand Up @@ -1168,10 +1173,10 @@ spSkeletonData *spSkeletonJson_readSkeletonData(spSkeletonJson *self, const char
skeletonData->transformConstraintsCount = transform->size;
skeletonData->transformConstraints = MALLOC(spTransformConstraintData *, transform->size);
for (constraintMap = transform->child, i = 0; constraintMap; constraintMap = constraintMap->next, ++i) {
const char *name;
const char *name, *targetName;

spTransformConstraintData *data = spTransformConstraintData_create(
Json_getString(constraintMap, "name", 0));
MALLOC_STR(name, Json_getString(constraintMap, "name", 0));
spTransformConstraintData *data = spTransformConstraintData_create(name);
data->order = Json_getInt(constraintMap, "order", 0);
data->skinRequired = Json_getInt(constraintMap, "skin", 0) ? 1 : 0;

Expand All @@ -1187,11 +1192,11 @@ spSkeletonData *spSkeletonJson_readSkeletonData(spSkeletonJson *self, const char
}
}

name = Json_getString(constraintMap, "target", 0);
data->target = spSkeletonData_findBone(skeletonData, name);
targetName = Json_getString(constraintMap, "target", 0);
data->target = spSkeletonData_findBone(skeletonData, targetName);
if (!data->target) {
spSkeletonData_dispose(skeletonData);
_spSkeletonJson_setError(self, root, "Target bone not found: ", name);
_spSkeletonJson_setError(self, root, "Target bone not found: ", targetName);
return NULL;
}

Expand Down Expand Up @@ -1222,10 +1227,11 @@ spSkeletonData *spSkeletonJson_readSkeletonData(spSkeletonJson *self, const char
skeletonData->pathConstraintsCount = pathJson->size;
skeletonData->pathConstraints = MALLOC(spPathConstraintData *, pathJson->size);
for (constraintMap = pathJson->child, i = 0; constraintMap; constraintMap = constraintMap->next, ++i) {
const char *name;
const char *name, *targetName;
const char *item;

spPathConstraintData *data = spPathConstraintData_create(Json_getString(constraintMap, "name", 0));
MALLOC_STR(name, Json_getString(constraintMap, "name", 0));
spPathConstraintData *data = spPathConstraintData_create(name);
data->order = Json_getInt(constraintMap, "order", 0);
data->skinRequired = Json_getInt(constraintMap, "skin", 0) ? 1 : 0;

Expand All @@ -1241,11 +1247,11 @@ spSkeletonData *spSkeletonJson_readSkeletonData(spSkeletonJson *self, const char
}
}

name = Json_getString(constraintMap, "target", 0);
data->target = spSkeletonData_findSlot(skeletonData, name);
targetName = Json_getString(constraintMap, "target", 0);
data->target = spSkeletonData_findSlot(skeletonData, targetName);
if (!data->target) {
spSkeletonData_dispose(skeletonData);
_spSkeletonJson_setError(self, root, "Target slot not found: ", name);
_spSkeletonJson_setError(self, root, "Target slot not found: ", targetName);
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion spine-c/spine-c/src/spine/SlotData.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
spSlotData *spSlotData_create(const int index, const char *name, spBoneData *boneData) {
spSlotData *self = NEW(spSlotData);
CONST_CAST(int, self->index) = index;
MALLOC_STR(self->name, name);
CONST_CAST(const char *, self->name) = name;
CONST_CAST(spBoneData *, self->boneData) = boneData;
spColor_setFromFloats(&self->color, 1, 1, 1, 1);
return self;
Expand Down
2 changes: 1 addition & 1 deletion spine-c/spine-c/src/spine/TransformConstraintData.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

spTransformConstraintData *spTransformConstraintData_create(const char *name) {
spTransformConstraintData *self = NEW(spTransformConstraintData);
MALLOC_STR(self->name, name);
CONST_CAST(const char *, self->name) = name;
return self;
}

Expand Down