sw_engine mempool: ++optimization

These memory pools requires very simple mechanism.

Remove stl vector but use direct memory for less binary size.(2127696 -> 2094094)

@Issues: 75
This commit is contained in:
Hermet Park 2020-11-06 13:38:00 +09:00 committed by Hermet Park
parent c6013536ec
commit 5751fd13cc

View file

@ -19,7 +19,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
#include <vector>
#include "tvgSwCommon.h" #include "tvgSwCommon.h"
@ -27,8 +26,9 @@
/* Internal Class Implementation */ /* Internal Class Implementation */
/************************************************************************/ /************************************************************************/
static vector<SwOutline> outline; static SwOutline* outline = nullptr;
static vector<SwOutline> strokeOutline; static SwOutline* strokeOutline = nullptr;
static unsigned allocSize = 0;
/************************************************************************/ /************************************************************************/
@ -63,68 +63,72 @@ void mpoolRetStrokeOutline(unsigned idx)
bool mpoolInit(unsigned threads) bool mpoolInit(unsigned threads)
{ {
if (outline || strokeOutline) return false;
if (threads == 0) threads = 1; if (threads == 0) threads = 1;
outline.reserve(threads); outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline) * threads));
outline.resize(threads); if (!outline) goto err;
for (auto& outline : outline) { strokeOutline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline) * threads));
outline.cntrs = nullptr; if (!strokeOutline) goto err;
outline.pts = nullptr;
outline.types = nullptr;
outline.cntrsCnt = outline.reservedCntrsCnt = 0;
outline.ptsCnt = outline.reservedPtsCnt = 0;
}
strokeOutline.reserve(threads); allocSize = threads;
strokeOutline.resize(threads);
for (auto& outline : strokeOutline) {
outline.cntrs = nullptr;
outline.pts = nullptr;
outline.types = nullptr;
outline.cntrsCnt = outline.reservedCntrsCnt = 0;
outline.ptsCnt = outline.reservedPtsCnt = 0;
}
return true; return true;
err:
if (outline) {
free(outline);
outline = nullptr;
}
if (strokeOutline) {
free(strokeOutline);
strokeOutline = nullptr;
}
return false;
} }
bool mpoolClear() bool mpoolClear()
{ {
for (auto& outline : outline) { SwOutline* p;
if (outline.cntrs) {
free(outline.cntrs);
outline.cntrs = nullptr;
}
if (outline.pts) {
free(outline.pts);
outline.pts = nullptr;
}
if (outline.types) {
free(outline.types);
outline.types = nullptr;
}
outline.cntrsCnt = outline.reservedCntrsCnt = 0;
outline.ptsCnt = outline.reservedPtsCnt = 0;
}
for (auto& outline : strokeOutline) { for (unsigned i = 0; i < allocSize; ++i) {
if (outline.cntrs) {
free(outline.cntrs); p = &outline[i];
outline.cntrs = nullptr;
if (p->cntrs) {
free(p->cntrs);
p->cntrs = nullptr;
} }
if (outline.pts) { if (p->pts) {
free(outline.pts); free(p->pts);
outline.pts = nullptr; p->pts = nullptr;
} }
if (outline.types) { if (p->types) {
free(outline.types); free(p->types);
outline.types = nullptr; p->types = nullptr;
} }
outline.cntrsCnt = outline.reservedCntrsCnt = 0; p->cntrsCnt = p->reservedCntrsCnt = 0;
outline.ptsCnt = outline.reservedPtsCnt = 0; p->ptsCnt = p->reservedPtsCnt = 0;
p = &strokeOutline[i];
if (p->cntrs) {
free(p->cntrs);
p->cntrs = nullptr;
}
if (p->pts) {
free(p->pts);
p->pts = nullptr;
}
if (p->types) {
free(p->types);
p->types = nullptr;
}
p->cntrsCnt = p->reservedCntrsCnt = 0;
p->ptsCnt = p->reservedPtsCnt = 0;
} }
return true; return true;
@ -133,5 +137,19 @@ bool mpoolClear()
bool mpoolTerm() bool mpoolTerm()
{ {
return mpoolClear(); mpoolClear();
if (outline) {
free(outline);
outline = nullptr;
}
if (strokeOutline) {
free(strokeOutline);
strokeOutline = nullptr;
}
allocSize = 0;
return true;
} }