branch off v1 as R_1_7
[m6w6/ext-http] / phpstr / phpstr.c
index 45c9e2632bb6293a10765d59f6dd941e598b4335..eb2f87a715d29d6975f0528e479ee81c18da9bac 100644 (file)
@@ -4,46 +4,86 @@
 #include "php.h"
 #include "phpstr.h"
 
-PHPSTR_API phpstr *phpstr_init_ex(phpstr *buf, size_t chunk_size, int pre_alloc)
+PHPSTR_API phpstr *phpstr_init_ex(phpstr *buf, size_t chunk_size, int flags)
 {
        if (!buf) {
-               buf = emalloc(sizeof(phpstr));
+               buf = pemalloc(sizeof(phpstr), flags & PHPSTR_INIT_PERSISTENT);
        }
 
-       buf->size = chunk_size > 0 ? chunk_size : PHPSTR_DEFAULT_SIZE;
-       buf->data = pre_alloc ? emalloc(buf->size) : NULL;
-       buf->free = pre_alloc ? buf->size : 0;
-       buf->used = 0;
-
+       if (buf) {
+               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;
+               buf->used = 0;
+       }
+       
        return buf;
 }
 
 PHPSTR_API phpstr *phpstr_from_string_ex(phpstr *buf, const char *string, size_t length)
 {
-       buf = phpstr_init(buf);
-       phpstr_append(buf, string, length);
+       if ((buf = phpstr_init(buf))) {
+               if (PHPSTR_NOMEM == phpstr_append(buf, string, length)) {
+                       pefree(buf, buf->pmem);
+                       buf = NULL;
+               }
+       }
        return buf;
 }
 
-PHPSTR_API void 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)
 {
+       char *ptr = NULL;
+#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;
+               
                while ((size + buf->free) < len) {
-                       size *= 2;
+                       size <<= 1;
                }
-               if (buf->data) {
-                       buf->data = erealloc(buf->data, buf->used + buf->free + size);
+               
+               if (allow_error) {
+                       ptr = perealloc_recoverable(buf->data, buf->used + buf->free + size, buf->pmem);
                } else {
-                       buf->data = emalloc(size);
+                       ptr = perealloc(buf->data, buf->used + buf->free + size, buf->pmem);
                }
+               
+               if (ptr) {
+                       buf->data = ptr;
+               } else {
+                       return PHPSTR_NOMEM;
+               }
+               
                buf->free += size;
+               return 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 PHPSTR_NOMEM;
+               }
+               buf->free = 1;
+       }
+       return buf->used;
 }
 
 PHPSTR_API size_t phpstr_append(phpstr *buf, const char *append, size_t append_len)
 {
-       phpstr_resize(buf, append_len);
+       if (PHPSTR_NOMEM == phpstr_resize(buf, append_len)) {
+               return PHPSTR_NOMEM;
+       }
        memcpy(buf->data + buf->used, append, append_len);
        buf->used += append_len;
        buf->free -= append_len;
@@ -54,21 +94,26 @@ PHPSTR_API size_t phpstr_appendf(phpstr *buf, const char *format, ...)
 {
        va_list argv;
        char *append;
-       size_t append_len;
+       size_t append_len, alloc;
 
        va_start(argv, format);
        append_len = vspprintf(&append, 0, format, argv);
        va_end(argv);
 
-       phpstr_append(buf, append, append_len);
+       alloc = phpstr_append(buf, append, append_len);
        efree(append);
 
+       if (PHPSTR_NOMEM == alloc) {
+               return PHPSTR_NOMEM;
+       }
        return append_len;
 }
 
 PHPSTR_API size_t phpstr_insert(phpstr *buf, const char *insert, size_t insert_len, size_t offset)
 {
-       phpstr_resize(buf, insert_len);
+       if (PHPSTR_NOMEM == phpstr_resize(buf, insert_len)) {
+               return PHPSTR_NOMEM;
+       }
        memmove(buf->data + offset + insert_len, buf->data + offset, insert_len);
        memcpy(buf->data + offset, insert, insert_len);
        buf->used += insert_len;
@@ -80,21 +125,26 @@ PHPSTR_API size_t phpstr_insertf(phpstr *buf, size_t offset, const char *format,
 {
        va_list argv;
        char *insert;
-       size_t insert_len;
+       size_t insert_len, alloc;
 
        va_start(argv, format);
        insert_len = vspprintf(&insert, 0, format, argv);
        va_end(argv);
 
-       phpstr_insert(buf, insert, insert_len, offset);
+       alloc = phpstr_insert(buf, insert, insert_len, offset);
        efree(insert);
 
+       if (PHPSTR_NOMEM == alloc) {
+               return PHPSTR_NOMEM;
+       }
        return insert_len;
 }
 
 PHPSTR_API size_t phpstr_prepend(phpstr *buf, const char *prepend, size_t prepend_len)
 {
-       phpstr_resize(buf, prepend_len);
+       if (PHPSTR_NOMEM == phpstr_resize(buf, prepend_len)) {
+               return PHPSTR_NOMEM;
+       }
        memmove(buf->data + prepend_len, buf->data, buf->used);
        memcpy(buf->data, prepend, prepend_len);
        buf->used += prepend_len;
@@ -106,15 +156,18 @@ PHPSTR_API size_t phpstr_prependf(phpstr *buf, const char *format, ...)
 {
        va_list argv;
        char *prepend;
-       size_t prepend_len;
+       size_t prepend_len, alloc;
 
        va_start(argv, format);
        prepend_len = vspprintf(&prepend, 0, format, argv);
        va_end(argv);
 
-       phpstr_prepend(buf, prepend, prepend_len);
+       alloc = phpstr_prepend(buf, prepend, prepend_len);
        efree(prepend);
 
+       if (PHPSTR_NOMEM == alloc) {
+               return PHPSTR_NOMEM;
+       }
        return prepend_len;
 }
 
@@ -134,7 +187,9 @@ PHPSTR_API char *phpstr_data(const phpstr *buf, char **into, size_t *len)
 PHPSTR_API phpstr *phpstr_dup(const phpstr *buf)
 {
        phpstr *dup = phpstr_clone(buf);
-       phpstr_append(dup, buf->data, buf->used);
+       if (PHPSTR_NOMEM == phpstr_append(dup, buf->data, buf->used)) {
+               phpstr_free(&dup);
+       }
        return dup;
 }
 
@@ -157,10 +212,15 @@ PHPSTR_API phpstr *phpstr_sub(const phpstr *buf, size_t offset, size_t length)
        if (offset >= buf->used) {
                return NULL;
        } else {
-               size_t need = (length + offset) > buf->used ? (buf->used - offset) : (length - offset);
-               phpstr *sub = phpstr_init_ex(NULL, need, 1);
-               phpstr_append(sub, buf->data + offset, need);
-               sub->size = buf->size;
+               size_t need = 1 + ((length + offset) > buf->used ? (buf->used - offset) : (length - offset));
+               phpstr *sub = phpstr_init_ex(NULL, need, PHPSTR_INIT_PREALLOC | (buf->pmem ? PHPSTR_INIT_PERSISTENT:0));
+               if (sub) {
+                       if (PHPSTR_NOMEM == phpstr_append(sub, buf->data + offset, need)) {
+                               phpstr_free(&sub);
+                       } else {
+                               sub->size = buf->size;
+                       }
+               }
                return sub;
        }
 }
@@ -180,11 +240,13 @@ PHPSTR_API phpstr *phpstr_merge_va(phpstr *buf, unsigned argc, va_list argv)
        unsigned i = 0;
        buf = phpstr_init(buf);
 
-       while (argc > i++) {
-               phpstr_free_t f = va_arg(argv, phpstr_free_t);
-               phpstr *current = va_arg(argv, phpstr *);
-               phpstr_append(buf, current->data, current->used);
-               FREE_PHPSTR(f, current);
+       if (buf) {
+               while (argc > i++) {
+                       phpstr_free_t f = va_arg(argv, phpstr_free_t);
+                       phpstr *current = va_arg(argv, phpstr *);
+                       phpstr_append(buf, current->data, current->used);
+                       FREE_PHPSTR(f, current);
+               }
        }
 
        return buf;
@@ -212,10 +274,13 @@ PHPSTR_API phpstr *phpstr_merge(unsigned argc, ...)
        return ret;
 }
 
-PHPSTR_API void phpstr_fix(phpstr *buf)
+PHPSTR_API phpstr *phpstr_fix(phpstr *buf)
 {
-       phpstr_resize_ex(buf, 1, 1);
+       if (PHPSTR_NOMEM == phpstr_resize_ex(buf, 1, 1, 0)) {
+               return NULL;
+       }
        buf->data[buf->used] = '\0';
+       return buf;
 }
 
 PHPSTR_API int phpstr_cmp(phpstr *left, phpstr *right)
@@ -229,9 +294,18 @@ PHPSTR_API int phpstr_cmp(phpstr *left, phpstr *right)
        }
 }
 
+PHPSTR_API void phpstr_reset(phpstr *buf)
+{
+       buf->free += buf->used;
+       buf->used = 0;
+}
+
 PHPSTR_API void phpstr_dtor(phpstr *buf)
 {
-       STR_SET(buf->data, NULL);
+       if (buf->data) {
+               pefree(buf->data, buf->pmem);
+               buf->data = NULL;
+       }
        buf->used = 0;
        buf->free = 0;
 }
@@ -240,7 +314,7 @@ PHPSTR_API void phpstr_free(phpstr **buf)
 {
        if (*buf) {
                phpstr_dtor(*buf);
-               efree(*buf);
+               pefree(*buf, (*buf)->pmem);
                *buf = NULL;
        }
 }
@@ -252,7 +326,7 @@ PHPSTR_API size_t phpstr_chunk_buffer(phpstr **s, const char *data, size_t data_
        *chunk = NULL;
        
        if (!*s) {
-               *s = phpstr_init_ex(NULL, chunk_size * 2, chunk_size ? 1 : 0);
+               *s = phpstr_init_ex(NULL, chunk_size << 1, chunk_size ? PHPSTR_INIT_PREALLOC : 0);
        }
        storage = *s;
        
@@ -266,33 +340,32 @@ PHPSTR_API size_t phpstr_chunk_buffer(phpstr **s, const char *data, size_t data_
                return chunk_size;
        }
        
-       if (storage->used >= storage->size/2) {
-               phpstr *avail = phpstr_left(storage, storage->size/2);
-               *chunk = estrndup(PHPSTR_VAL(avail), PHPSTR_LEN(avail));
-               phpstr_free(&avail);
-               phpstr_cut(storage, 0, storage->size/2);
-               return storage->size/2;
+       if (storage->used >= (chunk_size = storage->size >> 1)) {
+               *chunk = estrndup(storage->data, chunk_size);
+               phpstr_cut(storage, 0, chunk_size);
+               return chunk_size;
        }
        
        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);
-               efree(chunk);
-               data = NULL;
-               data_len = 0;
+       while ((got = phpstr_chunk_buffer(s, data, data_len, &chunk, chunk_len))) {
+               passthru(opaque, chunk, got TSRMLS_CC);
                if (!chunk_len) {
                        /*      we already got the last chunk,
                                and freed all resources */
                        break;
                }
+               data = NULL;
+               data_len = 0;
+               STR_SET(chunk, NULL);
        }
+       STR_FREE(chunk);
 }
 
 /*