From bbafb2eb27954c34967f91c705e74cc0c186970d Mon Sep 17 00:00:00 2001 From: Daniel Stenberg <daniel@haxx.se> Date: Tue, 11 May 2004 11:30:23 +0000 Subject: [PATCH] curl_global_init_mem() allows the memory functions to be replaced. memory.h is included everywhere for this. --- CHANGES | 4 + RELEASE-NOTES | 4 +- TODO-RELEASE | 3 - docs/libcurl/Makefile.am | 6 +- docs/libcurl/curl_global_init.3 | 5 +- include/curl/curl.h | 32 ++++ lib/Makefile.am | 4 +- lib/base64.c | 5 +- lib/connect.c | 3 +- lib/content_encoding.c | 3 + lib/cookie.c | 1 + lib/easy.c | 51 ++++- lib/escape.c | 3 +- lib/file.c | 4 +- lib/ftp.c | 1 + lib/getdate.y | 3 +- lib/getenv.c | 3 +- lib/getinfo.c | 10 +- lib/hash.c | 4 +- lib/hostares.c | 4 +- lib/hostasyn.c | 3 +- lib/hostip.c | 3 +- lib/hostip4.c | 3 +- lib/hostip6.c | 3 +- lib/hostsyn.c | 3 +- lib/hostthre.c | 3 +- lib/http.c | 4 +- lib/http_chunks.c | 3 +- lib/http_digest.c | 3 +- lib/http_negotiate.c | 3 +- lib/http_ntlm.c | 3 +- lib/if2ip.c | 3 +- lib/krb4.c | 3 +- lib/ldap.c | 3 +- lib/llist.c | 4 +- lib/memdebug.c | 9 +- lib/mprintf.c | 3 +- lib/multi.c | 3 +- lib/netrc.c | 3 +- lib/nwlib.c | 330 +++++++++++++++----------------- lib/security.c | 3 +- lib/sendf.c | 3 +- lib/share.c | 3 +- lib/ssluse.c | 4 +- lib/telnet.c | 3 +- lib/transfer.c | 3 +- lib/url.c | 3 +- tests/runtests.pl | 4 +- 48 files changed, 299 insertions(+), 275 deletions(-) diff --git a/CHANGES b/CHANGES index a17d3c3a60..5db781a37b 100644 --- a/CHANGES +++ b/CHANGES @@ -7,6 +7,10 @@ Changelog Daniel (11 May 2004) +- Seshubabu Pasam provided a patch that introduces curl_global_init_mem() - + like normal curl_global_init() but allows the app to replace all memory + functions with its own set. I modified it slightly. + - Based on Luca Alteas' comments, I modified the curllib.dsp generation code. Daniel (10 May 2004) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index a5dd602752..8decfda8ae 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -8,10 +8,12 @@ Curl and libcurl 7.12.0. This release includes the following changes: + o added curl_global_init_mem() + o removed curl_formparse() o the MSVC project file in the release archive is automatically built o curl --proxy-digest is a new command line option o the Windows version of libcurl can use wldap32.dll for LDAP - o curl_easy_strerror(), curl_multi_strerror() and curl_share_strerror() + o added curl_easy_strerror(), curl_multi_strerror() and curl_share_strerror() o IPv6-enabled Windows hosts now resolves names threaded/asynch as well o configure --with-libidn can be used to point out the root dir of a libidn installation for curl to use, then libcurl can resolve and use IDNA names diff --git a/TODO-RELEASE b/TODO-RELEASE index 07806b713a..0f25d2eef5 100644 --- a/TODO-RELEASE +++ b/TODO-RELEASE @@ -5,6 +5,3 @@ Issues not sorted in any particular order. To get fixed in 7.12.0 (planned release: June 2004) ====================== - -33. Add a function to replace the malloc-calls within libcurl. - Low prio. Seshubabu Pasam works on this? diff --git a/docs/libcurl/Makefile.am b/docs/libcurl/Makefile.am index eff5e51d99..81ef28f86f 100644 --- a/docs/libcurl/Makefile.am +++ b/docs/libcurl/Makefile.am @@ -15,7 +15,7 @@ man_MANS = curl_easy_cleanup.3 curl_easy_getinfo.3 curl_easy_init.3 \ curl_multi_perform.3 curl_multi_remove_handle.3 curl_share_cleanup.3 \ curl_share_init.3 curl_share_setopt.3 libcurl.3 libcurl-easy.3 \ libcurl-multi.3 libcurl-share.3 libcurl-errors.3 curl_easy_strerror.3 \ - curl_multi_strerror.3 curl_share_strerror.3 + curl_multi_strerror.3 curl_share_strerror.3 curl_global_init_mem.3 HTMLPAGES = curl_easy_cleanup.html curl_easy_getinfo.html \ curl_easy_init.html curl_easy_perform.html curl_easy_setopt.html \ @@ -30,7 +30,7 @@ HTMLPAGES = curl_easy_cleanup.html curl_easy_getinfo.html \ curl_share_cleanup.html curl_share_init.html curl_share_setopt.html \ libcurl.html libcurl-multi.html libcurl-easy.html libcurl-share.html \ libcurl-errors.html curl_easy_strerror.html curl_multi_strerror.html \ - curl_share_strerror.html + curl_share_strerror.html curl_global_init_mem.html PDFPAGES = curl_easy_cleanup.pdf curl_easy_getinfo.pdf \ curl_easy_init.pdf curl_easy_perform.pdf curl_easy_setopt.pdf \ @@ -45,7 +45,7 @@ PDFPAGES = curl_easy_cleanup.pdf curl_easy_getinfo.pdf \ curl_share_init.pdf curl_share_setopt.pdf libcurl.pdf \ libcurl-multi.pdf libcurl-easy.pdf libcurl-share.pdf \ libcurl-errors.pdf curl_easy_strerror.pdf curl_multi_strerror.pdf \ - curl_share_strerror.pdf + curl_share_strerror.pdf curl_global_init_mem.pdf CLEANFILES = $(HTMLPAGES) $(PDFPAGES) diff --git a/docs/libcurl/curl_global_init.3 b/docs/libcurl/curl_global_init.3 index 04a0b02386..b408850ad8 100644 --- a/docs/libcurl/curl_global_init.3 +++ b/docs/libcurl/curl_global_init.3 @@ -2,7 +2,7 @@ .\" nroff -man [file] .\" $Id$ .\" -.TH curl_global_init 3 "13 Nov 2001" "libcurl 7.9.1" "libcurl Manual" +.TH curl_global_init 3 "11 May 2004" "libcurl 7.12" "libcurl Manual" .SH NAME curl_global_init - Global libcurl initialisation .SH SYNOPSIS @@ -42,6 +42,5 @@ Initialise nothing extra. This sets no bit. If this function returns non-zero, something went wrong and you cannot use the other curl functions. .SH "SEE ALSO" +.BR curl_global_init_mem "(3), " .BR curl_global_cleanup "(3), " - - diff --git a/include/curl/curl.h b/include/curl/curl.h index a0ef1a3c6f..5f85ffff6c 100644 --- a/include/curl/curl.h +++ b/include/curl/curl.h @@ -154,6 +154,18 @@ typedef int (*curl_passwd_callback)(void *clientp, char *buffer, int buflen); +/* + * The following typedef's are signatures of malloc, free, realloc, strdup and + * calloc respectively. Function pointers of these types can be passed to the + * curl_global_init_mem() function to set user defined memory management + * callback routines. + */ +typedef void *(*curl_malloc_callback)(size_t size); +typedef void (*curl_free_callback)(void *ptr); +typedef void *(*curl_realloc_callback)(void *ptr, size_t size); +typedef char *(*curl_strdup_callback)(const char *str); +typedef void *(*curl_calloc_callback)(size_t nmemb, size_t size); + /* the kind of data that is passed to information_callback*/ typedef enum { CURLINFO_TEXT = 0, @@ -1012,6 +1024,26 @@ void curl_free(void *p); */ CURLcode curl_global_init(long flags); +/* + * NAME curl_global_init_mem() + * + * DESCRIPTION + * + * curl_global_init() or curl_global_init_mem() should be invoked exactly once + * for each application that uses libcurl. This function can be used to + * initialize libcurl and set user defined memory management callback + * functions. Users can implement memory management routines to check for + * memory leaks, check for mis-use of the curl library etc. User registered + * callback routines with be invoked by this library instead of the system + * memory management routines like malloc, free etc. + */ +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); + /* * NAME curl_global_cleanup() * diff --git a/lib/Makefile.am b/lib/Makefile.am index d9038f7d55..d1dc1910f2 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -91,8 +91,8 @@ HHEADERS = arpa_telnet.h netrc.h file.h timeval.h base64.h hostip.h \ telnet.h getinfo.h strequal.h security.h krb4.h memdebug.h \ inet_ntoa_r.h http_chunks.h strtok.h connect.h llist.h hash.h \ content_encoding.h share.h md5.h http_digest.h http_negotiate.h \ - http_ntlm.hca-bundle.h inet_pton.h strtoofft.h strerror.h \ - inet_ntop.h curlx.h + http_ntlm.h ca-bundle.h inet_pton.h strtoofft.h strerror.h \ + inet_ntop.h curlx.h memory.h CSOURCES = file.c timeval.c base64.c hostip.c progress.c formdata.c \ cookie.c http.c sendf.c ftp.c url.c dict.c if2ip.c speedcheck.c \ diff --git a/lib/base64.c b/lib/base64.c index 4af1d72e76..70e5e5cc24 100644 --- a/lib/base64.c +++ b/lib/base64.c @@ -41,10 +41,11 @@ #include <curl/mprintf.h> #include "base64.h" +#include "memory.h" -#ifdef CURLDEBUG +/* include memdebug.h last */ #include "memdebug.h" -#endif + static void decodeQuantum(unsigned char *dest, const char *src) { diff --git a/lib/connect.c b/lib/connect.c index c7dee3ce2d..0eec75a483 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -96,11 +96,10 @@ #include "if2ip.h" #include "strerror.h" #include "connect.h" +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif static bool verifyconnect(curl_socket_t sockfd); diff --git a/lib/content_encoding.c b/lib/content_encoding.c index a36bba7786..2a0aaf4021 100644 --- a/lib/content_encoding.c +++ b/lib/content_encoding.c @@ -32,6 +32,9 @@ #include <curl/curl.h> #include "sendf.h" #include "content_encoding.h" +#include "memory.h" + +#include "memdebug.h" #define DSIZ 0x10000 /* buffer size for decompressed data */ diff --git a/lib/cookie.c b/lib/cookie.c index 41f14011ff..4e5818c720 100644 --- a/lib/cookie.c +++ b/lib/cookie.c @@ -92,6 +92,7 @@ Example set of cookies: #include "strequal.h" #include "strtok.h" #include "sendf.h" +#include "memory.h" /* The last #include file should be: */ #ifdef CURLDEBUG diff --git a/lib/easy.c b/lib/easy.c index c04f01e2af..c162ef5314 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 <curl/mprintf.h> /* 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. diff --git a/lib/escape.c b/lib/escape.c index 23c3821fa3..b233600af7 100644 --- a/lib/escape.c +++ b/lib/escape.c @@ -31,11 +31,10 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif char *curl_escape(const char *string, int length) { diff --git a/lib/file.c b/lib/file.c index 004718cd4a..0edd3d972f 100644 --- a/lib/file.c +++ b/lib/file.c @@ -73,7 +73,6 @@ #include <fcntl.h> #endif - #endif #include "urldata.h" @@ -85,14 +84,13 @@ #include "speedcheck.h" #include "getinfo.h" #include "transfer.h" /* for Curl_readwrite_init() */ +#include "memory.h" #define _MPRINTF_REPLACE /* use our functions only */ #include <curl/mprintf.h> /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /* * Curl_file_connect() gets called from Curl_protocol_connect() to allow us to diff --git a/lib/ftp.c b/lib/ftp.c index 17632044b4..e4a6878840 100644 --- a/lib/ftp.c +++ b/lib/ftp.c @@ -92,6 +92,7 @@ #include "ssluse.h" #include "connect.h" #include "strerror.h" +#include "memory.h" #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL) #include "inet_ntoa_r.h" diff --git a/lib/getdate.y b/lib/getdate.y index 0e508e4a40..291b62990c 100644 --- a/lib/getdate.y +++ b/lib/getdate.y @@ -81,10 +81,9 @@ # include <string.h> #endif +#include "memory.h" /* The last #include file should be: */ -#ifdef MALLOCDEBUG #include "memdebug.h" -#endif #ifndef YYMAXDEPTH #define YYMAXDEPTH 0 diff --git a/lib/getenv.c b/lib/getenv.c index d29689fd1b..abea29a31e 100644 --- a/lib/getenv.c +++ b/lib/getenv.c @@ -36,10 +36,9 @@ #endif #include <curl/curl.h> +#include "memory.h" -#ifdef CURLDEBUG #include "memdebug.h" -#endif static char *GetEnv(const char *variable) diff --git a/lib/getinfo.c b/lib/getinfo.c index 9e41d7f2d5..bdb909e3c5 100644 --- a/lib/getinfo.c +++ b/lib/getinfo.c @@ -31,17 +31,11 @@ #include <stdio.h> #include <string.h> #include <stdarg.h> - -#ifdef VMS -#include <stdlib.h> -#endif +#include <stdlib.h> +#include "memory.h" /* Make this the last #include */ -#ifdef CURLDEBUG #include "memdebug.h" -#else -#include <stdlib.h> -#endif /* * This is supposed to be called in the beginning of a permform() session diff --git a/lib/hash.c b/lib/hash.c index 51634e0375..f33d91dc8f 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -28,12 +28,10 @@ #include "hash.h" #include "llist.h" +#include "memory.h" -#ifdef CURLDEBUG /* this must be the last include file */ #include "memdebug.h" -#endif - static unsigned long hash_str(const char *key, size_t key_length) diff --git a/lib/hostares.c b/lib/hostares.c index f0e98ad354..07a79fd963 100644 --- a/lib/hostares.c +++ b/lib/hostares.c @@ -87,10 +87,10 @@ #include "inet_ntoa_r.h" #endif +#include "memory.h" + /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /*********************************************************************** * Only for ares-enabled builds diff --git a/lib/hostasyn.c b/lib/hostasyn.c index a38c772d20..1c22c53e25 100644 --- a/lib/hostasyn.c +++ b/lib/hostasyn.c @@ -87,10 +87,9 @@ #include "inet_ntoa_r.h" #endif +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /*********************************************************************** * Only for builds using asynchronous name resolves diff --git a/lib/hostip.c b/lib/hostip.c index 4adcc1455f..c4e5ebfeef 100644 --- a/lib/hostip.c +++ b/lib/hostip.c @@ -87,10 +87,9 @@ #include "inet_ntoa_r.h" #endif +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /* * hostip.c explained diff --git a/lib/hostip4.c b/lib/hostip4.c index 92a76baeec..eef1f1ab64 100644 --- a/lib/hostip4.c +++ b/lib/hostip4.c @@ -87,10 +87,9 @@ #include "inet_ntoa_r.h" #endif +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /*********************************************************************** * Only for plain-ipv4 builds diff --git a/lib/hostip6.c b/lib/hostip6.c index 8b3f611466..9698ef3c98 100644 --- a/lib/hostip6.c +++ b/lib/hostip6.c @@ -88,10 +88,9 @@ #include "inet_ntoa_r.h" #endif +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /*********************************************************************** * Only for ipv6-enabled builds diff --git a/lib/hostsyn.c b/lib/hostsyn.c index 7ffe222f2a..559043d381 100644 --- a/lib/hostsyn.c +++ b/lib/hostsyn.c @@ -87,10 +87,9 @@ #include "inet_ntoa_r.h" #endif +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /*********************************************************************** * Only for builds using synchronous name resolves diff --git a/lib/hostthre.c b/lib/hostthre.c index 2a52ff329e..360cad1299 100644 --- a/lib/hostthre.c +++ b/lib/hostthre.c @@ -85,10 +85,9 @@ #include "inet_ntop.h" +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /*********************************************************************** * Only for Windows threaded name resolves builds diff --git a/lib/http.c b/lib/http.c index 388f7debf8..48bffa9046 100644 --- a/lib/http.c +++ b/lib/http.c @@ -74,7 +74,6 @@ #include <sys/select.h> #endif - #endif #include "urldata.h" @@ -94,14 +93,13 @@ #include "share.h" #include "hostip.h" #include "http.h" +#include "memory.h" #define _MPRINTF_REPLACE /* use our functions only */ #include <curl/mprintf.h> /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /* * checkheaders() checks the linked list of custom HTTP headers for a diff --git a/lib/http_chunks.c b/lib/http_chunks.c index 673e8a0fe3..f939d2d06a 100644 --- a/lib/http_chunks.c +++ b/lib/http_chunks.c @@ -35,14 +35,13 @@ #include "content_encoding.h" /* 08/29/02 jhrg */ #include "http.h" +#include "memory.h" #define _MPRINTF_REPLACE /* use our functions only */ #include <curl/mprintf.h> /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /* * Chunk format (simplified): diff --git a/lib/http_digest.c b/lib/http_digest.c index bdf60d7918..638f69d380 100644 --- a/lib/http_digest.c +++ b/lib/http_digest.c @@ -38,14 +38,13 @@ #include "http_digest.h" #include "strtok.h" #include "url.h" /* for Curl_safefree() */ +#include "memory.h" #define _MPRINTF_REPLACE /* use our functions only */ #include <curl/mprintf.h> /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /* Test example headers: diff --git a/lib/http_negotiate.c b/lib/http_negotiate.c index df0d6ab841..ece40692bc 100644 --- a/lib/http_negotiate.c +++ b/lib/http_negotiate.c @@ -41,14 +41,13 @@ #include "strequal.h" #include "base64.h" #include "http_negotiate.h" +#include "memory.h" #define _MPRINTF_REPLACE /* use our functions only */ #include <curl/mprintf.h> /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif static int get_gss_name(struct connectdata *conn, gss_name_t *server) diff --git a/lib/http_ntlm.c b/lib/http_ntlm.c index 9629b34d19..5c8d5ad2de 100644 --- a/lib/http_ntlm.c +++ b/lib/http_ntlm.c @@ -46,6 +46,7 @@ #include "base64.h" #include "http_ntlm.h" #include "url.h" +#include "memory.h" #define _MPRINTF_REPLACE /* use our functions only */ #include <curl/mprintf.h> @@ -71,9 +72,7 @@ #endif /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /* Define this to make the type-3 message include the NT response message */ #define USE_NTRESPONSES 1 diff --git a/lib/if2ip.c b/lib/if2ip.c index 94de81f8a5..b816d246f3 100644 --- a/lib/if2ip.c +++ b/lib/if2ip.c @@ -73,11 +73,10 @@ #endif #include "if2ip.h" +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif #define SYS_ERROR -1 diff --git a/lib/krb4.c b/lib/krb4.c index 3ff22c9ad6..994e6111fd 100644 --- a/lib/krb4.c +++ b/lib/krb4.c @@ -60,15 +60,14 @@ #include "ftp.h" #include "sendf.h" #include "krb4.h" +#include "memory.h" #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL) #include "inet_ntoa_r.h" #endif /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif #define LOCAL_ADDR (&conn->local_addr) #define REMOTE_ADDR (&conn->serv_addr) diff --git a/lib/ldap.c b/lib/ldap.c index e773551b9a..968b7ac3b3 100644 --- a/lib/ldap.c +++ b/lib/ldap.c @@ -56,13 +56,12 @@ #include "strequal.h" #include "strtok.h" #include "ldap.h" +#include "memory.h" #define _MPRINTF_REPLACE /* use our functions only */ #include <curl/mprintf.h> -#ifdef CURLDEBUG #include "memdebug.h" -#endif /* WLdap32.dll functions are *not* stdcall. Must call these via __cdecl * pointers in case libcurl was compiled as fastcall (-Gr). diff --git a/lib/llist.c b/lib/llist.c index 0f347acb92..2f059991db 100644 --- a/lib/llist.c +++ b/lib/llist.c @@ -27,11 +27,11 @@ #include <stdlib.h> #include "llist.h" +#include "memory.h" -#ifdef CURLDEBUG /* this must be the last include file */ #include "memdebug.h" -#endif + void Curl_llist_init(curl_llist *l, curl_llist_dtor dtor) { diff --git a/lib/memdebug.c b/lib/memdebug.c index 23f975ef67..718391cdb4 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -42,6 +42,7 @@ #endif #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */ +#include "memory.h" #include "memdebug.h" struct memdebug { @@ -120,7 +121,7 @@ void *curl_domalloc(size_t wantedsize, int line, const char *source) /* alloc at least 64 bytes */ size = sizeof(struct memdebug)+wantedsize; - mem=(struct memdebug *)(malloc)(size); + mem=(struct memdebug *)(Curl_cmalloc)(size); if(mem) { /* fill memory with junk */ memset(mem->mem, 0xA5, wantedsize); @@ -146,7 +147,7 @@ void *curl_docalloc(size_t wanted_elements, size_t wanted_size, user_size = wanted_size * wanted_elements; size = sizeof(struct memdebug) + user_size; - mem = (struct memdebug *)(malloc)(size); + mem = (struct memdebug *)(Curl_cmalloc)(size); if(mem) { /* fill memory with zeroes */ memset(mem->mem, 0, user_size); @@ -197,7 +198,7 @@ void *curl_dorealloc(void *ptr, size_t wantedsize, if(ptr) mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem)); - mem=(struct memdebug *)(realloc)(mem, size); + mem=(struct memdebug *)(Curl_crealloc)(mem, size); if(logfile) fprintf(logfile, "MEM %s:%d realloc(0x%x, %zd) = %p\n", source, line, ptr, wantedsize, mem?mem->mem:NULL); @@ -222,7 +223,7 @@ void curl_dofree(void *ptr, int line, const char *source) memset(mem->mem, 0x13, mem->size); /* free for real */ - (free)(mem); + (Curl_cfree)(mem); if(logfile) fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr); diff --git a/lib/mprintf.c b/lib/mprintf.c index a9c3757783..c9264e2cd8 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -55,10 +55,9 @@ #define ENABLE_64BIT #endif +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif #define BUFFSIZE 256 /* buffer for long-to-str and float-to-str calcs */ #define MAX_PARAMETERS 128 /* lame static limit */ diff --git a/lib/multi.c b/lib/multi.c index 7d86d202ef..0476b1d19c 100644 --- a/lib/multi.c +++ b/lib/multi.c @@ -42,11 +42,10 @@ #include "url.h" #include "connect.h" #include "progress.h" +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif struct Curl_message { /* the 'CURLMsg' is the part that is visible to the external user */ diff --git a/lib/netrc.c b/lib/netrc.c index 1496f3ff18..233e9018f9 100644 --- a/lib/netrc.c +++ b/lib/netrc.c @@ -45,14 +45,13 @@ #include "strequal.h" #include "strtok.h" +#include "memory.h" #define _MPRINTF_REPLACE /* use our functions only */ #include <curl/mprintf.h> /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /* Debug this single source file with: 'make netrc' then run './netrc'! diff --git a/lib/nwlib.c b/lib/nwlib.c index d54f4ac963..2dd0f4fa55 100644 --- a/lib/nwlib.c +++ b/lib/nwlib.c @@ -30,21 +30,23 @@ #include <nks/thread.h> #include <nks/synch.h> +#include "memory.h" +#include "memdebug.h" typedef struct { - int _errno; - void *twentybytes; + int _errno; + void *twentybytes; } libthreaddata_t; typedef struct { - int x; - int y; - int z; - void *tenbytes; - NXKey_t perthreadkey; /* if -1, no key obtained... */ - NXMutex_t *lock; + int x; + int y; + int z; + void *tenbytes; + NXKey_t perthreadkey; /* if -1, no key obtained... */ + NXMutex_t *lock; } libdata_t; int gLibId = -1; @@ -58,24 +60,24 @@ void DisposeThreadData ( void * ); int GetOrSetUpData ( int id, libdata_t **data, libthreaddata_t **threaddata ); -int _NonAppStart -( - void *NLMHandle, - void *errorScreen, - const char *cmdLine, - const char *loadDirPath, - size_t uninitializedDataLength, - void *NLMFileHandle, - int (*readRoutineP)( int conn, void *fileHandle, size_t offset, - size_t nbytes, size_t *bytesRead, void *buffer ), - size_t customDataOffset, - size_t customDataSize, - int messageCount, - const char **messages -) +int _NonAppStart( void *NLMHandle, + void *errorScreen, + const char *cmdLine, + const char *loadDirPath, + size_t uninitializedDataLength, + void *NLMFileHandle, + int (*readRoutineP)( int conn, + void *fileHandle, size_t offset, + size_t nbytes, + size_t *bytesRead, + void *buffer ), + size_t customDataOffset, + size_t customDataSize, + int messageCount, + const char **messages ) { - NX_LOCK_INFO_ALLOC(liblock, "Per-Application Data Lock", 0); - + NX_LOCK_INFO_ALLOC(liblock, "Per-Application Data Lock", 0); + #ifndef __GNUC__ #pragma unused(cmdLine) #pragma unused(loadDirPath) @@ -94,35 +96,33 @@ int _NonAppStart ** to accept calls into us. If we succeed, we return non-zero and the NetWare ** Loader will leave us up, otherwise we fail to load and get dumped. */ - gAllocTag = AllocateResourceTag(NLMHandle, - "<library-name> memory allocations", AllocSignature); + gAllocTag = AllocateResourceTag(NLMHandle, + "<library-name> memory allocations", + AllocSignature); - if (!gAllocTag) - { - OutputToScreen(errorScreen, "Unable to allocate resource tag for " - "library memory allocations.\n"); - return -1; - } + if (!gAllocTag) { + OutputToScreen(errorScreen, "Unable to allocate resource tag for " + "library memory allocations.\n"); + return -1; + } - gLibId = register_library(DisposeLibraryData); + gLibId = register_library(DisposeLibraryData); - if (gLibId < -1) - { - OutputToScreen(errorScreen, "Unable to register library with kernel.\n"); - return -1; - } + if (gLibId < -1) { + OutputToScreen(errorScreen, "Unable to register library with kernel.\n"); + return -1; + } - gLibHandle = NLMHandle; + gLibHandle = NLMHandle; - gLibLock = NXMutexAlloc(0, 0, &liblock); - - if (!gLibLock) - { - OutputToScreen(errorScreen, "Unable to allocate library data lock.\n"); - return -1; - } + gLibLock = NXMutexAlloc(0, 0, &liblock); + + if (!gLibLock) { + OutputToScreen(errorScreen, "Unable to allocate library data lock.\n"); + return -1; + } - return 0; + return 0; } /* @@ -131,8 +131,8 @@ int _NonAppStart */ void _NonAppStop( void ) { - (void) unregister_library(gLibId); - NXMutexFree(gLibLock); + (void) unregister_library(gLibId); + NXMutexFree(gLibLock); } /* @@ -151,31 +151,26 @@ int _NonAppCheckUnload( void ) return 0; } -int GetOrSetUpData -( - int id, - libdata_t **appData, - libthreaddata_t **threadData -) +int GetOrSetUpData(int id, libdata_t **appData, + libthreaddata_t **threadData ) { - int err; - libdata_t *app_data; - libthreaddata_t *thread_data; - NXKey_t key; - NX_LOCK_INFO_ALLOC(liblock, "Application Data Lock", 0); + int err; + libdata_t *app_data; + libthreaddata_t *thread_data; + NXKey_t key; + NX_LOCK_INFO_ALLOC(liblock, "Application Data Lock", 0); - err = 0; - thread_data = (libthreaddata_t *) NULL; + err = 0; + thread_data = (libthreaddata_t *) NULL; /* ** Attempt to get our data for the application calling us. This is where we ** store whatever application-specific information we need to carry in support ** of calling applications. */ - app_data = (libdata_t *) get_app_data(id); + app_data = (libdata_t *) get_app_data(id); - if (!app_data) - { + if (!app_data) { /* ** This application hasn't called us before; set up application AND per-thread ** data. Of course, just in case a thread from this same application is calling @@ -184,31 +179,27 @@ int GetOrSetUpData ** that other thread that was too late to create the data and the first thread ** in will have created it. */ - NXLock(gLibLock); - - if (!(app_data = (libdata_t *) get_app_data(id))) - { - app_data = (libdata_t *) malloc(sizeof(libdata_t)); - - if (app_data) - { - memset(app_data, 0, sizeof(libdata_t)); - - app_data->tenbytes = malloc(10); - app_data->lock = NXMutexAlloc(0, 0, &liblock); - - if (!app_data->tenbytes || !app_data->lock) - { - if (app_data->lock) - NXMutexFree(app_data->lock); - - free(app_data); - app_data = (libdata_t *) NULL; - err = ENOMEM; - } - - if (app_data) - { + NXLock(gLibLock); + + if (!(app_data = (libdata_t *) get_app_data(id))) { + app_data = (libdata_t *) malloc(sizeof(libdata_t)); + + if (app_data) { + memset(app_data, 0, sizeof(libdata_t)); + + app_data->tenbytes = malloc(10); + app_data->lock = NXMutexAlloc(0, 0, &liblock); + + if (!app_data->tenbytes || !app_data->lock) { + if (app_data->lock) + NXMutexFree(app_data->lock); + + free(app_data); + app_data = (libdata_t *) NULL; + err = ENOMEM; + } + + if (app_data) { /* ** Here we burn in the application data that we were trying to get by calling ** get_app_data(). Next time we call the first function, we'll get this data @@ -216,39 +207,35 @@ int GetOrSetUpData ** for the calling thread, something we'll have to do on each application ** thread the first time it calls us. */ - err = set_app_data(gLibId, app_data); - - if (err) - { - free(app_data); - app_data = (libdata_t *) NULL; - err = ENOMEM; - } - else - { - /* create key for thread-specific data... */ - err = NXKeyCreate(DisposeThreadData, (void *) NULL, &key); - - if (err) /* (no more keys left?) */ - key = -1; - - app_data->perthreadkey = key; - } - } - } + err = set_app_data(gLibId, app_data); + + if (err) { + free(app_data); + app_data = (libdata_t *) NULL; + err = ENOMEM; + } + else { + /* create key for thread-specific data... */ + err = NXKeyCreate(DisposeThreadData, (void *) NULL, &key); + + if (err) /* (no more keys left?) */ + key = -1; + + app_data->perthreadkey = key; + } } - - NXUnlock(gLibLock); + } } - - if (app_data) - { - key = app_data->perthreadkey; - - if ( key != -1 /* couldn't create a key? no thread data */ - && !(err = NXKeyGetValue(key, (void **) &thread_data)) - && !thread_data) - { + + NXUnlock(gLibLock); + } + + if (app_data) { + key = app_data->perthreadkey; + + if (key != -1 /* couldn't create a key? no thread data */ + && !(err = NXKeyGetValue(key, (void **) &thread_data)) + && !thread_data) { /* ** Allocate the per-thread data for the calling thread. Regardless of whether ** there was already application data or not, this may be the first call by a @@ -256,71 +243,58 @@ int GetOrSetUpData ** important, this just helps to demonstrate that we can have arbitrarily ** complex per-thread data. */ - thread_data = (libthreaddata_t *) malloc(sizeof(libthreaddata_t)); - - if (thread_data) - { - thread_data->_errno = 0; - thread_data->twentybytes = malloc(20); - - if (!thread_data->twentybytes) - { - free(thread_data); - thread_data = (libthreaddata_t *) NULL; - err = ENOMEM; - } - - if ((err = NXKeySetValue(key, thread_data))) - { - free(thread_data->twentybytes); - free(thread_data); - thread_data = (libthreaddata_t *) NULL; - } - } + thread_data = (libthreaddata_t *) malloc(sizeof(libthreaddata_t)); + + if (thread_data) { + thread_data->_errno = 0; + thread_data->twentybytes = malloc(20); + + if (!thread_data->twentybytes) { + free(thread_data); + thread_data = (libthreaddata_t *) NULL; + err = ENOMEM; + } + + if ((err = NXKeySetValue(key, thread_data))) { + free(thread_data->twentybytes); + free(thread_data); + thread_data = (libthreaddata_t *) NULL; } + } } + } - if (appData) - *appData = app_data; + if (appData) + *appData = app_data; - if (threadData) - *threadData = thread_data; + if (threadData) + *threadData = thread_data; - return err; + return err; } -int DisposeLibraryData -( - void *data -) +int DisposeLibraryData( void *data) { - if (data) - { - void *tenbytes = ((libdata_t *) data)->tenbytes; - - if (tenbytes) - free(tenbytes); - - free(data); - } - - return 0; + if (data) { + void *tenbytes = ((libdata_t *) data)->tenbytes; + + if (tenbytes) + free(tenbytes); + + free(data); + } + + return 0; } -void DisposeThreadData -( - void *data -) +void DisposeThreadData(void *data) { - if (data) - { - void *twentybytes = ((libthreaddata_t *) data)->twentybytes; - - if (twentybytes) - free(twentybytes); - - free(data); - } + if (data) { + void *twentybytes = ((libthreaddata_t *) data)->twentybytes; + + if (twentybytes) + free(twentybytes); + + free(data); + } } - - diff --git a/lib/security.c b/lib/security.c index 16746cdc95..f1af44201b 100644 --- a/lib/security.c +++ b/lib/security.c @@ -58,11 +58,10 @@ #include "base64.h" #include "sendf.h" #include "ftp.h" +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif #define min(a, b) ((a) < (b) ? (a) : (b)) diff --git a/lib/sendf.c b/lib/sendf.c index 4c3405566b..1f6665eddb 100644 --- a/lib/sendf.c +++ b/lib/sendf.c @@ -52,10 +52,9 @@ #include "security.h" #endif #include <string.h> +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /* returns last node in linked list */ static struct curl_slist *slist_get_last(struct curl_slist *list) diff --git a/lib/share.c b/lib/share.c index e554339dec..ced7e188da 100644 --- a/lib/share.c +++ b/lib/share.c @@ -28,11 +28,10 @@ #include <curl/curl.h> #include "urldata.h" #include "share.h" +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif CURLSH * curl_share_init(void) diff --git a/lib/ssluse.c b/lib/ssluse.c index c27eb918c0..c2a16d1364 100644 --- a/lib/ssluse.c +++ b/lib/ssluse.c @@ -50,10 +50,10 @@ #include <openssl/rand.h> #include <openssl/x509v3.h> +#include "memory.h" + /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif #if OPENSSL_VERSION_NUMBER >= 0x0090581fL #define HAVE_SSL_GET1_SESSION 1 diff --git a/lib/telnet.c b/lib/telnet.c index c133b5fd61..0408d73208 100644 --- a/lib/telnet.c +++ b/lib/telnet.c @@ -81,11 +81,10 @@ #define TELCMDS #include "arpa_telnet.h" +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif #define SUBBUFSIZE 512 diff --git a/lib/transfer.c b/lib/transfer.c index 5d8fd73066..1371e8de5a 100644 --- a/lib/transfer.c +++ b/lib/transfer.c @@ -101,14 +101,13 @@ #include "http_ntlm.h" #include "http_negotiate.h" #include "share.h" +#include "memory.h" #define _MPRINTF_REPLACE /* use our functions only */ #include <curl/mprintf.h> /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif #define CURL_TIMEOUT_EXPECT_100 1000 /* counting ms here */ diff --git a/lib/url.c b/lib/url.c index fc7a9d4e0b..66012ef32a 100644 --- a/lib/url.c +++ b/lib/url.c @@ -134,11 +134,10 @@ #ifdef HAVE_KRB4 #include "security.h" #endif +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif /* Local static prototypes */ static int ConnectionKillOne(struct SessionHandle *data); diff --git a/tests/runtests.pl b/tests/runtests.pl index dbe5978f8f..0df1e0550d 100755 --- a/tests/runtests.pl +++ b/tests/runtests.pl @@ -635,7 +635,7 @@ sub checkcurl { my $curl; my $libcurl; - my @version=`$CURL -V 2>/dev/null`; + my @version=`strace $CURL --version 2>fump`; for(@version) { chomp; @@ -727,7 +727,7 @@ sub checkcurl { } } if(!$curl) { - die "couldn't run curl!" + die "couldn't run '$CURL'" } my $hostname=`hostname`; -- GitLab