diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-03-28 10:33:40 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-03-28 10:38:45 +0200 |
commit | bec8a68ceee4cbf5de4d7026e750f11130c33ccd (patch) | |
tree | 055982c52eddf65486e752fe77c4b4385fa39fc4 /src/shared/specifier.c | |
parent | shared/specifier: be less extravagant with memory allocations (diff) | |
download | systemd-bec8a68ceee4cbf5de4d7026e750f11130c33ccd.tar.xz systemd-bec8a68ceee4cbf5de4d7026e750f11130c33ccd.zip |
shared/specifier: use realloc to free some memory after specifier expansion
This is a separate commit only because it actually *increases* memory allocations:
==3256== total heap usage: 100,120 allocs, 100,120 frees, 13,097,140 bytes allocated
to
==4690== total heap usage: 100,121 allocs, 100,121 frees, 14,198,329 bytes allocated
Essentially, we do a little more work to reduce the memory footprint a bit. For a
test where we just allocate the memory and drop it soon afterwards, this is not
beneficial, but it should still be useful for a long running program.
Diffstat (limited to 'src/shared/specifier.c')
-rw-r--r-- | src/shared/specifier.c | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/shared/specifier.c b/src/shared/specifier.c index c4a3ffd18c..a602413cd5 100644 --- a/src/shared/specifier.c +++ b/src/shared/specifier.c @@ -102,11 +102,18 @@ int specifier_printf(const char *text, const Specifier table[], void *userdata, else *(t++) = *f; - /* if string ended with a stray %, also end with % */ + /* If string ended with a stray %, also end with % */ if (percent) *(t++) = '%'; + *(t++) = 0; + + /* Try to deallocate unused bytes, but don't sweat it too much */ + if ((size_t)(t - ret) < allocated) { + t = realloc(ret, t - ret); + if (t) + ret = t; + } - *t = 0; *_ret = TAKE_PTR(ret); return 0; } |