Skip to content

Commit d8b062b

Browse files
committed
refactor(postgres): use palloc in TopMemoryContext for memory allocation
Replace malloc/free with palloc/pfree in the PostgreSQL dbmem_* abstraction layer. This integrates cloudsync memory management with PostgreSQL's memory context system. Benefits: - Memory tracking via MemoryContextStats() for debugging - Automatic cleanup when PostgreSQL connection terminates - Consistent memory management across the extension - Respects PostgreSQL memory configuration and limits
1 parent 288db90 commit d8b062b

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

src/postgresql/database_postgresql.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,21 +2693,30 @@ int database_rollback_savepoint (cloudsync_context *data, const char *savepoint_
26932693
}
26942694

26952695
// MARK: - MEMORY -
2696+
// Use palloc in TopMemoryContext for PostgreSQL memory management integration.
2697+
// This provides memory tracking, debugging support, and proper cleanup on connection end.
26962698

26972699
void *dbmem_alloc (uint64_t size) {
2698-
return malloc(size);
2700+
MemoryContext old = MemoryContextSwitchTo(TopMemoryContext);
2701+
void *ptr = palloc(size);
2702+
MemoryContextSwitchTo(old);
2703+
return ptr;
26992704
}
27002705

27012706
void *dbmem_zeroalloc (uint64_t size) {
2702-
void *ptr = malloc(size);
2703-
if (ptr) {
2704-
memset(ptr, 0, (size_t)size);
2705-
}
2707+
MemoryContext old = MemoryContextSwitchTo(TopMemoryContext);
2708+
void *ptr = palloc0(size);
2709+
MemoryContextSwitchTo(old);
27062710
return ptr;
27072711
}
27082712

27092713
void *dbmem_realloc (void *ptr, uint64_t new_size) {
2710-
return realloc(ptr, new_size);
2714+
// repalloc doesn't accept NULL, unlike realloc
2715+
if (!ptr) return dbmem_alloc(new_size);
2716+
MemoryContext old = MemoryContextSwitchTo(TopMemoryContext);
2717+
void *newptr = repalloc(ptr, new_size);
2718+
MemoryContextSwitchTo(old);
2719+
return newptr;
27112720
}
27122721

27132722
char *dbmem_mprintf (const char *format, ...) {
@@ -2727,8 +2736,10 @@ char *dbmem_mprintf (const char *format, ...) {
27272736
return NULL;
27282737
}
27292738

2730-
// Allocate buffer and format string
2731-
char *result = (char*)malloc(len + 1);
2739+
// Allocate buffer in TopMemoryContext and format string
2740+
MemoryContext old = MemoryContextSwitchTo(TopMemoryContext);
2741+
char *result = (char*)palloc(len + 1);
2742+
MemoryContextSwitchTo(old);
27322743
if (!result) {va_end(args); return NULL;}
27332744
vsnprintf(result, len + 1, format, args);
27342745

@@ -2747,8 +2758,10 @@ char *dbmem_vmprintf (const char *format, va_list list) {
27472758

27482759
if (len < 0) return NULL;
27492760

2750-
// Allocate buffer and format string
2751-
char *result = (char*)malloc(len + 1);
2761+
// Allocate buffer in TopMemoryContext and format string
2762+
MemoryContext old = MemoryContextSwitchTo(TopMemoryContext);
2763+
char *result = (char*)palloc(len + 1);
2764+
MemoryContextSwitchTo(old);
27522765
if (!result) return NULL;
27532766
vsnprintf(result, len + 1, format, list);
27542767

@@ -2757,12 +2770,12 @@ char *dbmem_vmprintf (const char *format, va_list list) {
27572770

27582771
void dbmem_free (void *ptr) {
27592772
if (ptr) {
2760-
free(ptr);
2773+
pfree(ptr);
27612774
}
27622775
}
27632776

27642777
uint64_t dbmem_size (void *ptr) {
2765-
// PostgreSQL memory alloc doesn't expose allocated size directly
2778+
// palloc doesn't expose allocated size directly
27662779
// Return 0 as a safe default
27672780
return 0;
27682781
}

0 commit comments

Comments
 (0)