Skip to content

Commit 75f60db

Browse files
committed
Improved memory handling for dbvalue_t
1 parent c71ae6e commit 75f60db

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/postgresql/database_postgresql.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,7 @@ const char *database_value_text (dbvalue_t *value) {
15821582
v->cstring = OidOutputFunctionCall(outfunc, v->datum);
15831583
}
15841584
v->owns_cstring = true;
1585+
v->owns_cstring_palloc = true;
15851586
}
15861587

15871588
return v->cstring;
@@ -1616,10 +1617,10 @@ void database_value_free (dbvalue_t *value) {
16161617
if (!v) return;
16171618

16181619
if (v->owned_detoast) {
1619-
cloudsync_memory_free(v->owned_detoast);
1620+
(v->owns_detoast_palloc) ? pfree(v->owned_detoast) : cloudsync_memory_free(v->owned_detoast);
16201621
}
16211622
if (v->owns_cstring && v->cstring) {
1622-
cloudsync_memory_free(v->cstring);
1623+
(v->owns_cstring_palloc) ? pfree(v->cstring) : cloudsync_memory_free(v->cstring);
16231624
}
16241625
cloudsync_memory_free(v);
16251626
}
@@ -1631,14 +1632,16 @@ void *database_value_dup (dbvalue_t *value) {
16311632
pgvalue_t *copy = pgvalue_create(v->datum, v->typeid, v->typmod, v->collation, v->isnull);
16321633
if (v->detoasted && v->owned_detoast) {
16331634
Size len = VARSIZE_ANY(v->owned_detoast);
1634-
copy->owned_detoast = cloudsync_memory_alloc(len);
1635+
copy->owned_detoast = (v->owns_detoast_palloc) ? palloc(len) : cloudsync_memory_alloc(len);
16351636
memcpy(copy->owned_detoast, v->owned_detoast, len);
16361637
copy->datum = PointerGetDatum(copy->owned_detoast);
16371638
copy->detoasted = true;
1639+
copy->owns_detoast_palloc = v->owns_detoast_palloc;
16381640
}
16391641
if (v->cstring) {
1640-
copy->cstring = cloudsync_string_dup(v->cstring);
1642+
copy->cstring = (v->owns_cstring_palloc) ? pstrdup(v->cstring) : cloudsync_string_dup(v->cstring);
16411643
copy->owns_cstring = true;
1644+
copy->owns_cstring_palloc = v->owns_cstring_palloc;
16421645
}
16431646
return (void*)copy;
16441647
}

src/postgresql/pgvalue.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ void pgvalue_ensure_detoast(pgvalue_t *v) {
4848
v->owned_detoast = (void *)PG_DETOAST_DATUM_COPY(v->datum);
4949
v->datum = PointerGetDatum(v->owned_detoast);
5050
v->detoasted = true;
51+
v->owns_detoast_palloc = true;
5152
}
5253

5354
int pgvalue_dbtype(pgvalue_t *v) {

src/postgresql/pgvalue.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ typedef struct pgvalue_t {
3030
void *owned_detoast;
3131
char *cstring;
3232
bool owns_cstring;
33+
bool owns_cstring_palloc;
34+
bool owns_detoast_palloc;
3335
} pgvalue_t;
3436

3537
pgvalue_t *pgvalue_create(Datum datum, Oid typeid, int32 typmod, Oid collation, bool isnull);

0 commit comments

Comments
 (0)