Skip to content

Commit 15fda51

Browse files
committed
Code simplification and memory cleanup (wp)
1 parent 84ed166 commit 15fda51

File tree

5 files changed

+59
-90
lines changed

5 files changed

+59
-90
lines changed

docker/Makefile.postgresql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ PG_OBJS = $(PG_ALL_SRC:.c=.o)
3636
# Compiler flags
3737
# Define POSIX macros as compiler flags to ensure they're defined before any includes
3838
PG_CPPFLAGS = -I$(PG_INCLUDEDIR) -Isrc -Isrc/postgresql -DCLOUDSYNC_POSTGRESQL_BUILD -D_POSIX_C_SOURCE=200809L -D_GNU_SOURCE
39-
PG_CFLAGS = -fPIC -Wall -Wextra -std=c11 -O2
39+
PG_CFLAGS = -fPIC -Wall -Wextra -Wno-unused-parameter -std=c11 -O2
4040
PG_LDFLAGS = -shared
4141

4242
# Output files

src/postgresql/cloudsync_postgresql.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pg_cloudsync_siteid(PG_FUNCTION_ARGS)
129129
}
130130

131131
// Return as bytea (binary UUID)
132-
bytea *result = (bytea *)palloc(VARHDRSZ + UUID_LEN);
132+
bytea *result = (bytea *)cloudsync_memory_alloc(VARHDRSZ + UUID_LEN);
133133
SET_VARSIZE(result, VARHDRSZ + UUID_LEN);
134134
memcpy(VARDATA(result), siteid, UUID_LEN);
135135

@@ -147,7 +147,7 @@ cloudsync_uuid(PG_FUNCTION_ARGS)
147147
cloudsync_uuid_v7(uuid);
148148

149149
// Return as bytea
150-
bytea *result = (bytea *)palloc(VARHDRSZ + UUID_LEN);
150+
bytea *result = (bytea *)cloudsync_memory_alloc(VARHDRSZ + UUID_LEN);
151151
SET_VARSIZE(result, VARHDRSZ + UUID_LEN);
152152
memcpy(VARDATA(result), uuid, UUID_LEN);
153153

@@ -704,7 +704,7 @@ cloudsync_payload_encode_transfn(PG_FUNCTION_ARGS)
704704
// Get or allocate aggregate state
705705
if (PG_ARGISNULL(0)) {
706706
MemoryContext oldContext = MemoryContextSwitchTo(aggContext);
707-
payload = (cloudsync_payload_context *)palloc(cloudsync_payload_context_size(NULL));
707+
payload = (cloudsync_payload_context *)cloudsync_memory_alloc(cloudsync_payload_context_size(NULL));
708708
memset(payload, 0, cloudsync_payload_context_size(NULL));
709709
MemoryContextSwitchTo(oldContext);
710710
} else {
@@ -713,7 +713,7 @@ cloudsync_payload_encode_transfn(PG_FUNCTION_ARGS)
713713

714714
cloudsync_context *ctx = get_cloudsync_context();
715715
int argc = 0;
716-
pgvalue_t **argv = pgvalues_from_args(fcinfo, 1, &argc, aggContext);
716+
pgvalue_t **argv = pgvalues_from_args(fcinfo, 1, &argc);
717717

718718
// Wrap variadic args into pgvalue_t so pk/payload helpers can read types safely.
719719
if (argc > 0) {
@@ -729,7 +729,7 @@ cloudsync_payload_encode_transfn(PG_FUNCTION_ARGS)
729729
for (int i = 0; i < argc; i++) {
730730
database_value_free((dbvalue_t *)argv[i]);
731731
}
732-
if (argv) pfree(argv);
732+
if (argv) cloudsync_memory_free(argv);
733733

734734
PG_RETURN_POINTER(payload);
735735
}
@@ -760,7 +760,7 @@ cloudsync_payload_encode_finalfn(PG_FUNCTION_ARGS)
760760
PG_RETURN_NULL();
761761
}
762762

763-
bytea *result = (bytea *)palloc(VARHDRSZ + blob_size);
763+
bytea *result = (bytea *)cloudsync_memory_alloc(VARHDRSZ + blob_size);
764764
SET_VARSIZE(result, VARHDRSZ + blob_size);
765765
memcpy(VARDATA(result), blob, blob_size);
766766

@@ -874,14 +874,13 @@ PG_FUNCTION_INFO_V1(cloudsync_pk_encode);
874874
Datum
875875
cloudsync_pk_encode(PG_FUNCTION_ARGS)
876876
{
877-
MemoryContext mcxt = CurrentMemoryContext;
878877
int argc = 0;
879878
pgvalue_t **argv = NULL;
880879

881880
// Signature is VARIADIC anyarray, so arg 0 is an array of PK values.
882881
if (!PG_ARGISNULL(0)) {
883882
ArrayType *array = PG_GETARG_ARRAYTYPE_P(0);
884-
argv = pgvalues_from_array(array, &argc, mcxt);
883+
argv = pgvalues_from_array(array, &argc);
885884
}
886885

887886
size_t pklen = 0;
@@ -898,7 +897,7 @@ cloudsync_pk_encode(PG_FUNCTION_ARGS)
898897
for (int i = 0; i < argc; i++) {
899898
database_value_free((dbvalue_t *)argv[i]);
900899
}
901-
if (argv) pfree(argv);
900+
if (argv) cloudsync_memory_free(argv);
902901

903902
PG_RETURN_TEXT_P(result);
904903
}
@@ -941,11 +940,10 @@ cloudsync_insert(PG_FUNCTION_ARGS)
941940
// Extract PK values from VARIADIC anyarray (arg 1)
942941
int argc = 0;
943942
pgvalue_t **argv = NULL;
944-
MemoryContext mcxt = CurrentMemoryContext;
945943

946944
if (!PG_ARGISNULL(1)) {
947945
ArrayType *pk_array = PG_GETARG_ARRAYTYPE_P(1);
948-
argv = pgvalues_from_array(pk_array, &argc, mcxt);
946+
argv = pgvalues_from_array(pk_array, &argc);
949947
}
950948

951949
// Verify we have the correct number of PK columns
@@ -955,7 +953,7 @@ cloudsync_insert(PG_FUNCTION_ARGS)
955953
for (int i = 0; i < argc; i++) {
956954
database_value_free((dbvalue_t *)argv[i]);
957955
}
958-
if (argv) pfree(argv);
956+
if (argv) cloudsync_memory_free(argv);
959957

960958
ereport(ERROR,
961959
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@@ -969,7 +967,7 @@ cloudsync_insert(PG_FUNCTION_ARGS)
969967
for (int i = 0; i < argc; i++) {
970968
database_value_free((dbvalue_t *)argv[i]);
971969
}
972-
if (argv) pfree(argv);
970+
if (argv) cloudsync_memory_free(argv);
973971

974972
ereport(ERROR,
975973
(errcode(ERRCODE_INTERNAL_ERROR),
@@ -1022,7 +1020,7 @@ cloudsync_insert(PG_FUNCTION_ARGS)
10221020
for (int i = 0; i < argc; i++) {
10231021
database_value_free((dbvalue_t *)argv[i]);
10241022
}
1025-
if (argv) pfree(argv);
1023+
if (argv) cloudsync_memory_free(argv);
10261024

10271025
SPI_finish();
10281026

@@ -1040,7 +1038,7 @@ cloudsync_insert(PG_FUNCTION_ARGS)
10401038
for (int i = 0; i < argc; i++) {
10411039
database_value_free((dbvalue_t *)argv[i]);
10421040
}
1043-
if (argv) pfree(argv);
1041+
if (argv) cloudsync_memory_free(argv);
10441042

10451043
SPI_finish();
10461044
PG_RE_THROW();

0 commit comments

Comments
 (0)