1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-07-01 00:25:06 +02:00

Use designated initializers to initialize structs.

Previously memNew() used memset() to initialize all struct members to 0, NULL, false, etc.  While this appears to work in practice, it is a violation of the C specification.  For instance, NULL == 0 must be true but neither NULL nor 0 must be represented with all zero bits.

Instead use designated initializers to initialize structs.  These guarantee that struct members will be properly initialized even if they are not specified in the initializer.  Note that due to a quirk in the C99 specification at least one member must be explicitly initialized even if it needs to be the default value.

Since pre-zeroed memory is no longer required, adjust memAllocInternal()/memReallocInternal() to return raw memory and update dependent functions accordingly.  All instances of memset() have been removed except in debug/test code where needed.

Add memMewPtrArray() to allocate an array of pointers and automatically set all pointers to NULL.

Rename memGrowRaw() to the more logical memResize().
This commit is contained in:
David Steele
2020-01-23 14:15:58 -07:00
parent cf2024beaf
commit b134175fc7
77 changed files with 1049 additions and 716 deletions

View File

@ -62,13 +62,16 @@ pgClientNew(const String *host, const unsigned int port, const String *database,
MEM_CONTEXT_NEW_BEGIN("PgClient")
{
this = memNew(sizeof(PgClient));
this->memContext = memContextCurrent();
this->host = strDup(host);
this->port = port;
this->database = strDup(database);
this->user = strDup(user);
this->queryTimeout = queryTimeout;
*this = (PgClient)
{
.memContext = MEM_CONTEXT_NEW(),
.host = strDup(host),
.port = port,
.database = strDup(database),
.user = strDup(user),
.queryTimeout = queryTimeout,
};
}
MEM_CONTEXT_NEW_END();

View File

@ -124,12 +124,15 @@ Create a pg_control file for testing
{ \
ASSERT(buffer != NULL); \
\
((ControlFileData *)buffer)->system_identifier = pgControl.systemId; \
((ControlFileData *)buffer)->pg_control_version = PG_CONTROL_VERSION; \
((ControlFileData *)buffer)->catalog_version_no = CATALOG_VERSION_NO; \
((ControlFileData *)buffer)->blcksz = pgControl.pageSize; \
((ControlFileData *)buffer)->xlog_seg_size = pgControl.walSegmentSize; \
((ControlFileData *)buffer)->data_checksum_version = pgControl.pageChecksum; \
*(ControlFileData *)buffer = (ControlFileData) \
{ \
.system_identifier = pgControl.systemId, \
.pg_control_version = PG_CONTROL_VERSION, \
.catalog_version_no = CATALOG_VERSION_NO, \
.blcksz = pgControl.pageSize, \
.xlog_seg_size = pgControl.walSegmentSize, \
.data_checksum_version = pgControl.pageChecksum, \
}; \
}
#elif PG_VERSION >= PG_VERSION_83
@ -141,11 +144,14 @@ Create a pg_control file for testing
ASSERT(buffer != NULL); \
ASSERT(!pgControl.pageChecksum); \
\
((ControlFileData *)buffer)->system_identifier = pgControl.systemId; \
((ControlFileData *)buffer)->pg_control_version = PG_CONTROL_VERSION; \
((ControlFileData *)buffer)->catalog_version_no = CATALOG_VERSION_NO; \
((ControlFileData *)buffer)->blcksz = pgControl.pageSize; \
((ControlFileData *)buffer)->xlog_seg_size = pgControl.walSegmentSize; \
*(ControlFileData *)buffer = (ControlFileData) \
{ \
.system_identifier = pgControl.systemId, \
.pg_control_version = PG_CONTROL_VERSION, \
.catalog_version_no = CATALOG_VERSION_NO, \
.blcksz = pgControl.pageSize, \
.xlog_seg_size = pgControl.walSegmentSize, \
}; \
}
#endif