+#define http_curl_startup(ch, clean_curl, URL, options) \
+ if (!ch) { \
+ if (!(ch = curl_easy_init())) { \
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl"); \
+ return FAILURE; \
+ } \
+ clean_curl = 1; \
+ } \
+ http_curl_initbuf(); \
+ http_curl_setopts(ch, URL, options);
+
+
+#define http_curl_cleanup(ch, clean_curl) \
+ http_curl_freestr(); \
+ http_curl_freebuf(); \
+ if (clean_curl) { \
+ curl_easy_cleanup(ch); \
+ ch = NULL; \
+ }
+
+#define http_curl_freestr() \
+ zend_llist_clean(&HTTP_G(to_free))
+
+#define http_curl_initbuf() http_curl_initbuf_ex(0)
+
+#define http_curl_initbuf_ex(chunk_size) \
+ { \
+ size_t size = (chunk_size > 0) ? chunk_size : HTTP_CURLBUF_SIZE; \
+ http_curl_freebuf(); \
+ HTTP_G(curlbuf).data = emalloc(size); \
+ HTTP_G(curlbuf).free = size; \
+ HTTP_G(curlbuf).size = size; \
+ }
+
+#define http_curl_freebuf() \
+ if (HTTP_G(curlbuf).data) { \
+ efree(HTTP_G(curlbuf).data); \
+ HTTP_G(curlbuf).data = NULL; \
+ } \
+ HTTP_G(curlbuf).used = 0; \
+ HTTP_G(curlbuf).free = 0; \
+ HTTP_G(curlbuf).size = 0;
+
+#define http_curl_copybuf(data, size) \
+ * size = HTTP_G(curlbuf).used; \
+ * data = ecalloc(1, HTTP_G(curlbuf).used + 1); \
+ memcpy(* data, HTTP_G(curlbuf).data, * size);
+
+#define http_curl_sizebuf(for_size) \
+ { \
+ size_t size = (for_size); \
+ if (size > HTTP_G(curlbuf).free) { \
+ size_t bsize = HTTP_G(curlbuf).size; \
+ while (size > bsize) { \
+ bsize *= 2; \
+ } \
+ HTTP_G(curlbuf).data = erealloc(HTTP_G(curlbuf).data, HTTP_G(curlbuf).used + bsize); \
+ HTTP_G(curlbuf).free += bsize; \
+ } \
+ }
+
+