X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=phpstr%2Fphpstr.c;h=4eaec1c0a9a300365ec1252a79f9944d3aef6e93;hb=e37040ebf8a470c77c7ae3498ee582ca20db259c;hp=8ab8737d0d68e424426eda5ec1b75f12155de096;hpb=a0bca521b491711e43aef74fe19c23a8eb4d0777;p=m6w6%2Fext-http diff --git a/phpstr/phpstr.c b/phpstr/phpstr.c index 8ab8737..4eaec1c 100644 --- a/phpstr/phpstr.c +++ b/phpstr/phpstr.c @@ -13,7 +13,7 @@ PHPSTR_API phpstr *phpstr_init_ex(phpstr *buf, size_t chunk_size, int flags) } if (buf) { - buf->size = (chunk_size > 0) ? chunk_size : PHPSTR_DEFAULT_SIZE; + buf->size = (chunk_size) ? chunk_size : PHPSTR_DEFAULT_SIZE; buf->pmem = (flags & PHPSTR_INIT_PERSISTENT) ? 1 : 0; buf->data = (flags & PHPSTR_INIT_PREALLOC) ? pemalloc(buf->size, buf->pmem) : NULL; buf->free = (flags & PHPSTR_INIT_PREALLOC) ? buf->size : 0; @@ -34,8 +34,11 @@ PHPSTR_API phpstr *phpstr_from_string_ex(phpstr *buf, const char *string, size_t return buf; } -PHPSTR_API size_t phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size) +PHPSTR_API size_t phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size, int allow_error) { +#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; @@ -43,7 +46,13 @@ PHPSTR_API size_t phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size size *= 2; } if (buf->data) { - char *ptr = perealloc(buf->data, buf->used + buf->free + size, buf->pmem); + char *ptr; + + if (allow_error) { + ptr = perealloc_recoverable(buf->data, buf->used + buf->free + size, buf->pmem); + } else { + ptr = perealloc(buf->data, buf->used + buf->free + size, buf->pmem); + } if (ptr) { buf->data = ptr; @@ -63,6 +72,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)) { @@ -221,22 +246,16 @@ PHPSTR_API phpstr *phpstr_right(const phpstr *buf, size_t length) PHPSTR_API phpstr *phpstr_merge_va(phpstr *buf, unsigned argc, va_list argv) { - unsigned f = 0, i = 0; + unsigned i = 0; buf = phpstr_init(buf); if (buf) { while (argc > i++) { phpstr_free_t f = va_arg(argv, phpstr_free_t); phpstr *current = va_arg(argv, phpstr *); - if (NOMEM == phpstr_append(buf, current->data, current->used)) { - f = 1; - } + phpstr_append(buf, current->data, current->used); FREE_PHPSTR(f, current); } - - if (f) { - phpstr_free(&buf); - } } return buf; @@ -266,7 +285,7 @@ PHPSTR_API phpstr *phpstr_merge(unsigned argc, ...) PHPSTR_API phpstr *phpstr_fix(phpstr *buf) { - if (NOMEM == phpstr_resize_ex(buf, 1, 1)) { + if (NOMEM == phpstr_resize_ex(buf, 1, 1, 0)) { return NULL; } buf->data[buf->used] = '\0'; @@ -341,13 +360,13 @@ PHPSTR_API size_t phpstr_chunk_buffer(phpstr **s, const char *data, size_t data_ return 0; } -PHPSTR_API void phpstr_chunked_output(phpstr **s, const char *data, size_t data_len, size_t chunk_len, void (*passthru)(const char *, size_t TSRMLS_DC) TSRMLS_DC) +PHPSTR_API void phpstr_chunked_output(phpstr **s, const char *data, size_t data_len, size_t chunk_len, phpstr_passthru_func passthru, void *opaque TSRMLS_DC) { char *chunk = NULL; size_t got = 0; while ((got = phpstr_chunk_buffer(s, data, data_len, &chunk, chunk_len))) { - passthru(chunk, got TSRMLS_CC); + passthru(opaque, chunk, got TSRMLS_CC); if (!chunk_len) { /* we already got the last chunk, and freed all resources */