[common] vector: eliminate double allocation when possible

This commit creates two constructor/destructor pairs for vector:
* vector_alloc/vector_free dynamically allocates the vector itself
* vector_create/vector_destroy uses existing Vector objects
This commit is contained in:
Quantum
2021-08-27 00:27:16 -04:00
committed by Geoffrey McRae
parent ba527761ef
commit e6df0acad9
5 changed files with 61 additions and 44 deletions

View File

@@ -38,7 +38,7 @@ static const EGL_FilterOps * EGL_Filters[] =
struct EGL_PostProcess
{
Vector * filters;
Vector filters;
GLuint output;
unsigned int outputX, outputY;
_Atomic(bool) modified;
@@ -74,8 +74,8 @@ static void configUI(void * opaque, int * id)
igGetWindowPos(&window);
igGetMousePos(&pos);
EGL_Filter ** filters = vector_data(this->filters);
size_t count = vector_size(this->filters);
EGL_Filter ** filters = vector_data(&this->filters);
size_t count = vector_size(&this->filters);
for (size_t i = 0; i < count; ++i)
{
EGL_Filter * filter = filters[i];
@@ -144,8 +144,7 @@ bool egl_postProcessInit(EGL_PostProcess ** pp)
return false;
}
this->filters = vector_create(sizeof(EGL_Filter *), ARRAY_LENGTH(EGL_Filters));
if (!this->filters)
if (!vector_create(&this->filters, sizeof(EGL_Filter *), ARRAY_LENGTH(EGL_Filters)))
{
DEBUG_ERROR("Failed to allocate the filter list");
goto error_this;
@@ -164,7 +163,7 @@ bool egl_postProcessInit(EGL_PostProcess ** pp)
return true;
error_filters:
vector_free(this->filters);
vector_destroy(&this->filters);
error_this:
free(this);
@@ -178,13 +177,10 @@ void egl_postProcessFree(EGL_PostProcess ** pp)
EGL_PostProcess * this = *pp;
if (this->filters)
{
EGL_Filter ** filter;
vector_forEachRef(filter, this->filters)
egl_filterFree(filter);
vector_free(this->filters);
}
EGL_Filter ** filter;
vector_forEachRef(filter, &this->filters)
egl_filterFree(filter);
vector_destroy(&this->filters);
egl_modelFree(&this->model);
free(this);
@@ -197,7 +193,7 @@ bool egl_postProcessAdd(EGL_PostProcess * this, const EGL_FilterOps * ops)
if (!egl_filterInit(ops, &filter))
return false;
vector_push(this->filters, &filter);
vector_push(&this->filters, &filter);
return true;
}
@@ -219,7 +215,7 @@ bool egl_postProcessRun(EGL_PostProcess * this, EGL_Texture * tex,
atomic_store(&this->modified, false);
EGL_Filter * filter;
vector_forEach(filter, this->filters)
vector_forEach(filter, &this->filters)
{
egl_filterSetOutputResHint(filter, targetX, targetY);
egl_filterSetup(filter, tex->format.pixFmt, sizeX, sizeY);

View File

@@ -36,7 +36,7 @@ typedef struct TexDMABUF
TextureBuffer base;
EGLDisplay display;
Vector * images;
Vector images;
}
TexDMABUF;
@@ -47,9 +47,9 @@ EGL_TextureOps EGL_TextureDMABUF;
static void egl_texDMABUFCleanup(TexDMABUF * this)
{
struct FdImage * image;
vector_forEachRef(image, this->images)
vector_forEachRef(image, &this->images)
eglDestroyImage(this->display, image->image);
vector_clear(this->images);
vector_clear(&this->images);
}
// dmabuf functions
@@ -59,8 +59,7 @@ static bool egl_texDMABUFInit(EGL_Texture ** texture, EGLDisplay * display)
TexDMABUF * this = calloc(1, sizeof(*this));
*texture = &this->base.base;
this->images = vector_create(sizeof(struct FdImage), 2);
if (!this->images)
if (!vector_create(&this->images, sizeof(struct FdImage), 2))
{
free(this);
*texture = NULL;
@@ -70,7 +69,7 @@ static bool egl_texDMABUFInit(EGL_Texture ** texture, EGLDisplay * display)
EGL_Texture * parent = &this->base.base;
if (!egl_texBufferStreamInit(&parent, display))
{
vector_free(this->images);
vector_destroy(&this->images);
free(this);
*texture = NULL;
return false;
@@ -86,7 +85,7 @@ static void egl_texDMABUFFree(EGL_Texture * texture)
TexDMABUF * this = UPCAST(TexDMABUF , parent);
egl_texDMABUFCleanup(this);
free(this->images);
vector_destroy(&this->images);
egl_texBufferFree(&parent->base);
free(this);
@@ -113,7 +112,7 @@ static bool egl_texDMABUFUpdate(EGL_Texture * texture,
EGLImage image = EGL_NO_IMAGE;
struct FdImage * fdImage;
vector_forEachRef(fdImage, this->images)
vector_forEachRef(fdImage, &this->images)
if (fdImage->fd == update->dmaFD)
{
image = fdImage->image;
@@ -146,7 +145,7 @@ static bool egl_texDMABUFUpdate(EGL_Texture * texture,
return false;
}
if (!vector_push(this->images, &(struct FdImage) {
if (!vector_push(&this->images, &(struct FdImage) {
.fd = update->dmaFD,
.image = image,
}))