150-vasprintf_size_reduce.patch 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. Reduce the initial buffer size for open_memstream (used by vasprintf),
  2. as most strings are usually smaller than that.
  3. Realloc the buffer after finishing the string to further reduce size.
  4. Signed-off-by: Felix Fietkau <nbd@openwrt.org>
  5. --- a/libc/stdio/vasprintf.c
  6. +++ b/libc/stdio/vasprintf.c
  7. @@ -39,6 +39,8 @@ int vasprintf(char **__restrict buf, con
  8. if (rv < 0) {
  9. free(*buf);
  10. *buf = NULL;
  11. + } else {
  12. + *buf = realloc(*buf, rv + 1);
  13. }
  14. }
  15. --- a/libc/stdio/open_memstream.c
  16. +++ b/libc/stdio/open_memstream.c
  17. @@ -17,6 +17,8 @@
  18. #define COOKIE ((__oms_cookie *) cookie)
  19. +#define MEMSTREAM_BUFSIZ 256
  20. +
  21. typedef struct {
  22. char *buf;
  23. size_t len;
  24. @@ -134,7 +136,7 @@ FILE *open_memstream(char **__restrict b
  25. register FILE *fp;
  26. if ((cookie = malloc(sizeof(__oms_cookie))) != NULL) {
  27. - if ((cookie->buf = malloc(cookie->len = BUFSIZ)) == NULL) {
  28. + if ((cookie->buf = malloc(cookie->len = MEMSTREAM_BUFSIZ)) == NULL) {
  29. goto EXIT_cookie;
  30. }
  31. *cookie->buf = 0; /* Set nul terminator for buffer. */