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