• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_global_init_mem
5Section: 3
6Source: libcurl
7See-also:
8  - curl_global_cleanup (3)
9  - curl_global_init (3)
10---
11
12# NAME
13
14curl_global_init_mem - Global libcurl initialization with memory callbacks
15
16# SYNOPSIS
17
18~~~c
19#include <curl/curl.h>
20
21CURLcode curl_global_init_mem(long flags,
22                              curl_malloc_callback m,
23                              curl_free_callback f,
24                              curl_realloc_callback r,
25                              curl_strdup_callback s,
26                              curl_calloc_callback c);
27~~~
28
29# DESCRIPTION
30
31This function works exactly as curl_global_init(3) with one addition: it
32allows the application to set callbacks to replace the otherwise used internal
33memory functions.
34
35If you are using libcurl from multiple threads or libcurl was built with the
36threaded resolver option then the callback functions must be thread safe. The
37threaded resolver is a common build option to enable (and in some cases the
38default) so we strongly urge you to make your callback functions thread safe.
39
40All callback arguments must be set to valid function pointers. The
41prototypes for the given callbacks must match these:
42
43## void *malloc_callback(size_t size);
44
45To replace malloc()
46
47## void free_callback(void *ptr);
48
49To replace free()
50
51## void *realloc_callback(void *ptr, size_t size);
52
53To replace realloc()
54
55## char *strdup_callback(const char *str);
56
57To replace strdup()
58
59## void *calloc_callback(size_t nmemb, size_t size);
60
61To replace calloc()
62
63This function is otherwise the same as curl_global_init(3), please refer
64to that man page for documentation.
65
66# CAUTION
67
68Manipulating these gives considerable powers to the application to severely
69screw things up for libcurl. Take care!
70
71# EXAMPLE
72
73~~~c
74extern void *malloc_cb(size_t);
75extern void free_cb(void *);
76extern void *realloc_cb(void *, size_t);
77extern char *strdup_cb(const char *);
78extern void *calloc_cb(size_t, size_t);
79
80int main(void)
81{
82  curl_global_init_mem(CURL_GLOBAL_DEFAULT, malloc_cb,
83                       free_cb, realloc_cb,
84                       strdup_cb, calloc_cb);
85}
86~~~
87
88# AVAILABILITY
89
90Added in 7.12.0
91
92# RETURN VALUE
93
94CURLE_OK (0) means everything was OK, non-zero means an error occurred as
95*<curl/curl.h>* defines - see libcurl-errors(3).
96