1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-07-05 00:28:52 +02:00

Allow additional memory to be allocated with a mem context.

The primary benefit is that objects can allocate memory for their struct with the context, which saves an additional allocation and makes it easier to read context/allocation dumps. Also, the memory context does not need to be stored with the object since it can be determined using the object pointer.

Object pointers cannot be moved, so this means whatever additional memory is allocated cannot be resized. That makes the additional memory ideal for object structs, but not so much for allocating a list that might change size.

Mem contexts can no longer be reused since they will probably be the wrong size so their memory is freed on memContextFree(). This still means fewer allocations and frees overall.

Interfaces still need to be freed by mem context so the old objMove() and objFree() have been preserved as objMoveContext() and objFreeContext(). This will be addressed in a future commit.
This commit is contained in:
David Steele
2021-09-01 11:10:35 -04:00
parent 02b06aa495
commit 475b57c89b
117 changed files with 754 additions and 769 deletions

View File

@ -7,7 +7,6 @@ Postgres Client
#include "common/debug.h"
#include "common/log.h"
#include "common/memContext.h"
#include "common/type/list.h"
#include "common/wait.h"
#include "postgres/client.h"
@ -17,7 +16,6 @@ Object type
***********************************************************************************************************************************/
struct PgClient
{
MemContext *memContext;
const String *host;
unsigned int port;
const String *database;
@ -63,13 +61,12 @@ pgClientNew(const String *host, const unsigned int port, const String *database,
PgClient *this = NULL;
MEM_CONTEXT_NEW_BEGIN("PgClient")
OBJ_NEW_BEGIN(PgClient)
{
this = memNew(sizeof(PgClient));
this = OBJ_NEW_ALLOC();
*this = (PgClient)
{
.memContext = MEM_CONTEXT_NEW(),
.host = strDup(host),
.port = port,
.database = strDup(database),
@ -77,7 +74,7 @@ pgClientNew(const String *host, const unsigned int port, const String *database,
.queryTimeout = queryTimeout,
};
}
MEM_CONTEXT_NEW_END();
OBJ_NEW_END();
FUNCTION_LOG_RETURN(PG_CLIENT, this);
}
@ -151,7 +148,7 @@ pgClientOpen(PgClient *this)
this->connection = PQconnectdb(strZ(connInfo));
// Set a callback to shutdown the connection
memContextCallbackSet(this->memContext, pgClientFreeResource, this);
memContextCallbackSet(objMemContext(this), pgClientFreeResource, this);
// Handle errors
if (PQstatus(this->connection) != CONNECTION_OK)
@ -347,7 +344,7 @@ pgClientClose(PgClient *this)
if (this->connection != NULL)
{
memContextCallbackClear(this->memContext);
memContextCallbackClear(objMemContext(this));
PQfinish(this->connection);
this->connection = NULL;
}