cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: malloc/free.. callback functions

From: Seshubabu Pasam <pasam_at_seshubabu.com>
Date: Sat, 03 Apr 2004 14:50:18 -0800

CURLOPT was just a suggestion. Typically this is done like this:

typedef void *( *curlMalloc ) ( size_t size );
typedef void ( *curlFree ) ( size_t size );
typedef void *( *curlRealloc )( void *mem, size_t size );
typedef char *( *curlStrdup ) ( const char *str );

curlMalloc curl_malloc = malloc;
curlFree curl_free = free;
curlRealloc curl_realloc = realloc;
curlStrdup curl_strdup = strdup;

void curl_set_memory_callbacks ( curlMalloc m,
                                  curlFree f,
                                  curlRealloc r,
                                  curlStrdup s ) {
     curl_malloc = m;
     curl_free = f;
     curl_realloc = r;
     curl_strdup = s;
}

After adding this piece of code, all we have to do is replace malloc
with curl_malloc, free with curl_free ....

This should be very trival change. And there are no "if"s or overhead.
We use a number of 3rd party libraries in our application. Most of them
(libxml2, PCRE ...) provide this functionality. Our application has our
own memory management module (for various reasons like: checking for
leaks .... etc) Obviously we like to use our functions with curl
library, to check for bad stuff.

Regards
-Seshubabu Pasam

codemastr wrote:
>>Can we add CURLOPT or a function for applications to register their own
>>malloc/free/realloc/strdup callback functions. If these callback
>>functions are not set, then CURL can fallback to the system calls. I
>>know this is major change, but should be trivial. I can provide the
>>patch if you are interested.
>
> I agree it would be easy, but why is it necessary? The way I see it, at best
> this requires an extra if() every time memory is allocated/freed. That might
> not sound like a lot, but it could easily add up. And, to make the
> transition easy, it would probably require an extra if() and an extra
> function call. I mean this because things like char *buf = malloc(len) would
> not work since you need an if() in there. That seems like a lot of cost to
> incur unless there is a really good reason why you'd need this.
>
> The only reason I could see for doing this is for debugging, and curl
> already supports overriding it for debugging purposes. However, it overrides
> it at compiletime to minimize the cost.
>
> I just don't see the need to add 5 new CURLOPTs for this (you need 5 because
> you left out calloc) as it just doesn't seem necessary to me, and even if it
> is, it doesn't seem like the most efficient way to implement it.
>
> Dominick Meglio.
>
>
Received on 2004-04-04