Skip to content

Commit

Permalink
rest_client: Let libcurl use the system malloc functions
Browse files Browse the repository at this point in the history
The fact that libcurl spawns a thread in order to perform each transfer
is well known.  Also known was the fact that by instructing libcurl to
use the pkg_*alloc() functions, an OpenSIPS worker must synchronize with
the libcurl thread before they can both manipulate the PKG pool.  Commit
900f6c9 was an attempt to solve this, however the fix DOES NOT
hold, as it was implemented in that commit (the curl thread is aware of
the thread lock while the original process ignores it...).

The only way we can make libcurl use the opensips pkg mem allocation
functions is by changing pkg_malloc() itself to also grab a thread
lock.  And this is already too much, as it would unnecessarily slow down
the pkg allocator, while the benefits are minimal anyway: what do we
stand to gain if libcurl uses our mem pool?  Close to nothing.

We just let libcurl use the system memory manager (along with as much
memory as it wants) and we're done with this issue for good.

Fixes #1546

(cherry picked from commit 3b2586d)
  • Loading branch information
liviuchircu committed Jul 11, 2019
1 parent 99dd4fa commit 26d18c5
Showing 1 changed file with 0 additions and 82 deletions.
82 changes: 0 additions & 82 deletions modules/rest_client/rest_client.c
Expand Up @@ -215,79 +215,6 @@ struct module_exports exports = {
cfg_validate/* reload confirm function */
};

/*
* Since libcurl's "easy" interface spawns a separate thread to perform each
* transfer, we supply it with a set of allocation functions which make
* everyone happy:
* - thread-safe
* - faster than libc's malloc()
* - integrated with OpenSIPS's memory usage reporting
*/
static gen_lock_t thread_lock;

static void *osips_malloc(size_t size)
{
void *p;

lock_get(&thread_lock);
p = pkg_malloc(size);
lock_release(&thread_lock);

return p;
}

static void *osips_calloc(size_t nmemb, size_t size)
{
void *p;

lock_get(&thread_lock);
p = pkg_malloc(nmemb * size);
lock_release(&thread_lock);
if (p) {
memset(p, '\0', nmemb * size);
}

return p;
}

static void *osips_realloc(void *ptr, size_t size)
{
void *p;

lock_get(&thread_lock);
p = pkg_realloc(ptr, size);
lock_release(&thread_lock);

return p;
}

static char *osips_strdup(const char *cp)
{
char *rval;
int len;

len = strlen(cp) + 1;

lock_get(&thread_lock);
rval = pkg_malloc(len);
lock_release(&thread_lock);
if (!rval) {
return NULL;
}

memcpy(rval, cp, len);
return rval;
}

static void osips_free(void *ptr)
{
lock_get(&thread_lock);
if (ptr) {
pkg_free(ptr);
}
lock_release(&thread_lock);
}

static int mod_init(void)
{
LM_DBG("Initializing...\n");
Expand All @@ -306,15 +233,6 @@ static int mod_init(void)
connection_timeout = curl_timeout;
}

lock_init(&thread_lock);

curl_global_init_mem(CURL_GLOBAL_ALL,
osips_malloc,
osips_free,
osips_realloc,
osips_strdup,
osips_calloc);

INIT_LIST_HEAD(&multi_pool);

/* try loading the trace api */
Expand Down

0 comments on commit 26d18c5

Please sign in to comment.