Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a macro causing double allocations #76

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions xp/common/gg_strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const char* const GG_String_EmptyString = "";
+---------------------------------------------------------------------*/
#define GG_UPPERCASE(x) (((x) >= 'a' && (x) <= 'z') ? (x)&0xdf : (x))
#define GG_LOWERCASE(x) (((x) >= 'A' && (x) <= 'Z') ? (x)^32 : (x))
#define GG_STRING_BUFFER_CHARS(b) (b == NULL ? NULL : ((char*)((b)+1)))
#define GG_STRING_BUFFER_CHARS(b) ((char*)((b)+1))

/*--------------------------------------------------------------------*/
static GG_StringBuffer*
Expand Down Expand Up @@ -69,6 +69,9 @@ GG_StringBuffer_Create(size_t length)
{
/* allocate a buffer of the requested size */
GG_StringBuffer* buffer = GG_StringBuffer_Allocate(length, length);
if (buffer == NULL){
return NULL;
}
return GG_STRING_BUFFER_CHARS(buffer);
}

Expand Down Expand Up @@ -192,7 +195,12 @@ GG_String_PrepareToWrite(GG_String* self, size_t length)
if (grow > length) needed = grow;
GG_FreeMemory((void*)GG_String_GetBuffer(self));
}
self->chars = GG_STRING_BUFFER_CHARS(GG_StringBuffer_Allocate(needed, length));
GG_StringBuffer* temp = GG_StringBuffer_Allocate(needed, length);
if (temp != NULL) {
self->chars = GG_STRING_BUFFER_CHARS(temp);
} else {
self->chars = NULL;
}
} else {
GG_String_GetBuffer(self)->length = length;
}
Expand All @@ -212,10 +220,12 @@ GG_String_Reserve(GG_String* self, size_t allocate)
}

size_t length = GG_String_GetLength(self);
char* copy = GG_STRING_BUFFER_CHARS(GG_StringBuffer_Allocate(needed, length));
if (copy == NULL) {
GG_StringBuffer* temp = GG_StringBuffer_Allocate(needed, length);
if (temp == NULL) {
return GG_ERROR_OUT_OF_MEMORY;
}
char* copy = GG_STRING_BUFFER_CHARS(temp);

if (self->chars != NULL) {
memcpy(copy, self->chars, length + 1);
GG_FreeMemory(GG_String_GetBuffer(self));
Expand Down