From 8fdb14330327046bd1890d32211622850309defa Mon Sep 17 00:00:00 2001 From: nekobit Date: Sat, 15 Oct 2022 21:05:44 +0000 Subject: [PATCH] Improve memory by not duplicating mstdnt perl strings FossilOrigin-Name: aaa960e3a3c6c89429dbb2210b9693bfbd79f84e824053cee9b5d2eb7dde7f8b --- src/global_perl.h | 9 ++++++--- src/main.c | 6 ++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/global_perl.h b/src/global_perl.h index 563d4ac..5cef801 100644 --- a/src/global_perl.h +++ b/src/global_perl.h @@ -22,9 +22,12 @@ #include #include -// hv_stores(ssn_hv, "id", newSVpv(acct->id, 0)); -// TODO use sv_usepvn_flags soon -#define hvstores_str(hv, key, val) if ((val)) { hv_stores((hv), key, newSVpvn((val), strlen(val))); } +// Note: val MUST be a pointer to a value, and must end with a \0 (hence SvSETMAGIC) +#define hvstores_str(hv, key, val) if (val) { \ + SV* tmpsv = newSV(0); \ + sv_usepvn_flags(tmpsv, val, strlen(val), SvSETMAGIC); \ + hv_stores((hv), key, tmpsv); \ + } #define hvstores_int(hv, key, val) hv_stores((hv), key, newSViv((val))) #define hvstores_ref(hv, key, val) if (1) { \ SV* tmp = (SV*)(val); \ diff --git a/src/main.c b/src/main.c index 1103115..003b619 100644 --- a/src/main.c +++ b/src/main.c @@ -16,8 +16,6 @@ * along with this program. If not, see . */ -#include -#include #include "global_perl.h" #include #include @@ -251,10 +249,10 @@ int main(int argc, char **argv, char **env) init_template_files(aTHX); - // Setup mstdnt hooks + // Setup mstdnt hooks to use Perl functions struct mstdnt_hooks hooks = { .malloc = safemalloc, - // Not sure how this differs from Safefree? + // Not sure how this differs from Safefree? That's undefined... (but used elsewhere in the code just fine) .free = safefree, .calloc = safecalloc, .realloc = saferealloc,