From 4085c94aa71fa257612f6d80d0d8bc3eedc7f184 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 19 Apr 2013 18:11:28 -0700 Subject: [PATCH] made a version of firstVertexForCode() that doesn't allocate memory, for improved performance and use - created new copyFirstVertexForCode(code,output) which copies to the output buffer - converted firstVertexForCode() to behave same way as before, but use copy version --- libraries/shared/src/OctalCode.cpp | 14 +++++++++----- libraries/shared/src/OctalCode.h | 5 +++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/libraries/shared/src/OctalCode.cpp b/libraries/shared/src/OctalCode.cpp index 2a687f1cd4..898eaed377 100644 --- a/libraries/shared/src/OctalCode.cpp +++ b/libraries/shared/src/OctalCode.cpp @@ -104,9 +104,8 @@ unsigned char * childOctalCode(unsigned char * parentOctalCode, char childNumber return newCode; } -float * firstVertexForCode(unsigned char * octalCode) { - float * firstVertex = new float[3]; - memset(firstVertex, 0, 3 * sizeof(float)); +void copyFirstVertexForCode(unsigned char * octalCode, float* output) { + memset(output, 0, 3 * sizeof(float)); float currentScale = 0.5; @@ -114,11 +113,16 @@ float * firstVertexForCode(unsigned char * octalCode) { int sectionIndex = sectionValue(octalCode + 1 + (3 * i / 8), (3 * i) % 8); for (int j = 0; j < 3; j++) { - firstVertex[j] += currentScale * (int)oneAtBit(sectionIndex, 5 + j); + output[j] += currentScale * (int)oneAtBit(sectionIndex, 5 + j); } currentScale *= 0.5; } - +} + +float * firstVertexForCode(unsigned char * octalCode) { + float * firstVertex = new float[3]; + copyFirstVertexForCode(octalCode, firstVertex); return firstVertex; } + diff --git a/libraries/shared/src/OctalCode.h b/libraries/shared/src/OctalCode.h index d5367fbddf..7569e99868 100644 --- a/libraries/shared/src/OctalCode.h +++ b/libraries/shared/src/OctalCode.h @@ -16,6 +16,11 @@ int bytesRequiredForCodeLength(unsigned char threeBitCodes); bool isDirectParentOfChild(unsigned char *parentOctalCode, unsigned char * childOctalCode); int branchIndexWithDescendant(unsigned char * ancestorOctalCode, unsigned char * descendantOctalCode); unsigned char * childOctalCode(unsigned char * parentOctalCode, char childNumber); + + +// Note: copyFirstVertexForCode() is preferred because it doesn't allocate memory for the return +// but other than that these do the same thing. float * firstVertexForCode(unsigned char * octalCode); +void copyFirstVertexForCode(unsigned char * octalCode, float* output); #endif /* defined(__hifi__OctalCode__) */