X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=phpstr%2Fphpstr.c;fp=phpstr%2Fphpstr.c;h=98a8e63ebcc8bd52b83b1c93235b76a9e11074b1;hp=8ab8737d0d68e424426eda5ec1b75f12155de096;hb=3017eb87773ddee0b170d532ff92e195f2f69612;hpb=f3c78f084459a9f5b5b54233bbda879154cdcefa diff --git a/phpstr/phpstr.c b/phpstr/phpstr.c index 8ab8737..98a8e63 100644 --- a/phpstr/phpstr.c +++ b/phpstr/phpstr.c @@ -36,6 +36,9 @@ PHPSTR_API phpstr *phpstr_from_string_ex(phpstr *buf, const char *string, size_t PHPSTR_API size_t phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size) { +#if 0 + fprintf(stderr, "RESIZE: size=%lu, used=%lu, free=%lu\n", buf->size, buf->used, buf->free); +#endif if (buf->free < len) { size_t size = override_size ? override_size : buf->size; @@ -63,6 +66,22 @@ PHPSTR_API size_t phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size return 0; } +PHPSTR_API size_t phpstr_shrink(phpstr *buf) +{ + /* avoid another realloc on fixation */ + if (buf->free > 1) { + char *ptr = perealloc(buf->data, buf->used + 1, buf->pmem); + + if (ptr) { + buf->data = ptr; + } else { + return NOMEM; + } + buf->free = 1; + } + return buf->used; +} + PHPSTR_API size_t phpstr_append(phpstr *buf, const char *append, size_t append_len) { if (NOMEM == phpstr_resize(buf, append_len)) {