From bbafb2eb27954c34967f91c705e74cc0c186970d Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 11 May 2004 11:30:23 +0000 Subject: curl_global_init_mem() allows the memory functions to be replaced. memory.h is included everywhere for this. --- lib/easy.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'lib/easy.c') diff --git a/lib/easy.c b/lib/easy.c index c04f01e2a..c162ef531 100644 --- a/lib/easy.c +++ b/lib/easy.c @@ -76,14 +76,13 @@ #include "getinfo.h" #include "hostip.h" #include "share.h" +#include "memory.h" #define _MPRINTF_REPLACE /* use our functions only */ #include /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__) /* win32_cleanup() is for win32 socket cleanup functionality, the opposite @@ -164,6 +163,16 @@ static void idna_init (void) static unsigned int initialized = 0; static long init_flags = 0; +/* + * If a memory-using function (like curl_getenv) is used before + * curl_global_init() is called, we need to have these pointers set already. + */ +curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc; +curl_free_callback Curl_cfree = (curl_free_callback)free; +curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc; +curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)strdup; +curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc; + /** * curl_global_init() globally initializes cURL given a bitwise set of the * different features of what to initialize. @@ -173,6 +182,13 @@ CURLcode curl_global_init(long flags) if (initialized) return CURLE_OK; + /* Setup the default memory functions here (again) */ + Curl_cmalloc = (curl_malloc_callback)malloc; + Curl_cfree = (curl_free_callback)free; + Curl_crealloc = (curl_realloc_callback)realloc; + Curl_cstrdup = (curl_strdup_callback)strdup; + Curl_ccalloc = (curl_calloc_callback)calloc; + if (flags & CURL_GLOBAL_SSL) Curl_SSL_init(); @@ -195,6 +211,37 @@ CURLcode curl_global_init(long flags) return CURLE_OK; } +/* + * curl_global_init_mem() globally initializes cURL and also registers the + * user provided callback routines. + */ +CURLcode curl_global_init_mem(long flags, curl_malloc_callback m, + curl_free_callback f, curl_realloc_callback r, + curl_strdup_callback s, curl_calloc_callback c) +{ + CURLcode code = CURLE_OK; + + /* Invalid input, return immediately */ + if (!m || !f || !r || !s || !c) + return CURLE_FAILED_INIT; + + /* Already initialized, don't do it again */ + if ( initialized ) + return CURLE_OK; + + /* Call the actual init function first */ + code = curl_global_init(flags); + if (code == CURLE_OK) { + Curl_cmalloc = m; + Curl_cfree = f; + Curl_cstrdup = s; + Curl_crealloc = r; + Curl_ccalloc = c; + } + + return code; +} + /** * curl_global_cleanup() globally cleanups cURL, uses the value of * "init_flags" to determine what needs to be cleaned up and what doesn't. -- cgit v1.2.3