Implement the use of heap manipulator implementions
[openssl.git] / doc / crypto / OPENSSL_malloc.pod
1 =pod
2
3 =head1 NAME
4
5 OPENSSL_malloc_init,
6 OPENSSL_malloc, OPENSSL_zalloc, OPENSSL_realloc, OPENSSL_free,
7 OPENSSL_clear_realloc, OPENSSL_clear_free,
8 CRYPTO_malloc, CRYPTO_zalloc, CRYPTO_realloc, CRYPTO_free,
9 OPENSSL_strdup, OPENSSL_strndup,
10 OPENSSL_memdup, OPENSSL_strlcpy, OPENSSL_strlcat,
11 CRYPTO_clear_realloc, CRYPTO_clear_free,
12 CRYPTO_get_mem_functions, CRYPTO_set_mem_functions,
13 CRYPTO_set_mem_debug, CRYPTO_mem_ctrl,
14 OPENSSL_mem_debug_push, OPENSSL_mem_debug_pop,
15 CRYPTO_mem_debug_push, CRYPTO_mem_debug_pop,
16 CRYPTO_mem_leaks, CRYPTO_mem_leaks_fp - Memory allocation functions
17
18 =head1 SYNOPSIS
19
20  #include <openssl/crypto.h>
21
22  int OPENSSL_malloc_init(void)
23
24  void *OPENSSL_malloc(size_t num)
25  void *OPENSSL_zalloc(size_t num)
26  void *OPENSSL_realloc(void *addr, size_t num)
27  void OPENSSL_free(void *addr)
28  char *OPENSSL_strdup(const char *str)
29  char *OPENSSL_strndup(const char *str, size_t s)
30  void *OPENSSL_clear_realloc(void *p, size_t old_len, size_t num)
31  void OPENSSL_clear_free(void *str, size_t num)
32  void OPENSSL_cleanse(void *ptr, size_t len);
33
34  void *CRYPTO_malloc(size_t num, const char *file, int line)
35  void *CRYPTO_zalloc(size_t num, const char *file, int line)
36  void *CRYPTO_realloc(void *p, size_t num, const char *file, int line)
37  void CRYPTO_free(void *str)
38  char *CRYPTO_strdup(const char *p, const char *file, int line)
39  char *CRYPTO_strndup(const char *p, size_t num, const char *file, int line)
40  void *CRYPTO_clear_realloc(void *p, size_t old_len, size_t num, const char *file, int line)
41  void CRYPTO_clear_free(void *str, size_t num)
42
43  void CRYPTO_get_mem_functions(
44          void *(**m)(size_t, const char *, int),
45          void *(**r)(void *, size_t, const char *, int),
46          void (**f)(void *))
47  int CRYPTO_set_mem_functions(
48          void *(*m)(size_t, const char *, int),
49          void *(*r)(void *, size_t, const char *, int),
50          void (*f)(void *))
51
52  int CRYPTO_set_mem_debug(int onoff)
53
54  #define CRYPTO_MEM_CHECK_OFF
55  #define CRYPTO_MEM_CHECK_ON
56  #define CRYPTO_MEM_CHECK_DISABLE
57  #define CRYPTO_MEM_CHECK_ENABLE
58
59  int CRYPTO_mem_ctrl(int mode);
60
61  int OPENSSL_mem_debug_push(const char *info)
62  int OPENSLS_mem_debug_pop)(void)
63
64  int CRYPTO_mem_debug_push(const char *info, const char *file, int line);
65
66  void CRYPTO_mem_leaks(BIO *b);
67  void CRYPTO_mem_leaks(FILE *fp);
68
69 =head1 DESCRIPTION
70
71 OpenSSL memory allocation is handled by the B<OPENSSL_xxx> API. These are
72 generally macro's that add the standard C B<__FILE__> and B<__LINE__>
73 parameters and call a lower-level B<CRYPTO_xxx> API.
74 Some functions do not add those parameters, but exist for consistency.
75
76 OPENSSL_malloc_init() sets the lower-level memory allocation functions
77 to their default implementation.
78 It is generally not necessary to call this, except perhaps in certain
79 shared-library situations.
80
81 OPENSSL_malloc(), OPENSSL_realloc(), and OPENSSL_free() are like the
82 C malloc(), realloc(), and free() functions.
83 OPENSSL_zalloc() calls memset() to zero the memory before returning.
84
85 OPENSSL_clear_realloc() and OPENSSL_clear_free() should be used
86 when the buffer at B<addr> holds sensitive information.
87 The old buffer is filled with arbitrary data by calling OPENSSL_cleanse()
88 before ultimately calling OPENSSL_free().
89
90 OPENSSL_strdup(), OPENSSL_strndup() and OPENSSL_memdup() are like the
91 equivalent C functions, except that memory is allocated by calling the
92 OPENSSL_malloc() and should be releaed by calling OPENSSL_free().
93
94 OPENSSL_strlcpy(),
95 OPENSSL_strlcat() and OPENSSL_strnlen() are equivalents of the common C
96 library functions and are provided for portability.
97
98 If no allocations have been done, it is possible to "swap out" the default
99 implementations and replace them with alternate versions, or wrappers that
100 do some additional housekeeping and then defer to the OpenSSL implementation.
101 The CRYPTO_get_mem_functions() function fills in the function pointers for
102 with the current functions (normally, and by default,
103 CRYPTO_malloc(), CRYPTO_realloc(), and CRYPTO_free()).
104 The CRYPTO_set_mem_functions() specifies a different set of functions.
105 If any of B<m>, B<r>, or B<f> are NULL, then the function is not changed.
106
107 The default implementation can include some debugging capability (if enabled
108 at build-time).
109 This adds some overhead by keeping a list of all memory allocations, and
110 removes items from the list when they are free'd.
111 This is most useful for identifying memory leaks.
112 CRYPTO_set_mem_debug() turns this tracking on and off.  It is normally
113 called at startup, but can be called at any time.
114
115 CRYPTO_mem_ctrl() provides fine-grained control of memory leak tracking.
116 To enable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
117 the B<CRYPTO_MEM_CHECK_ON>.
118 To disable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
119 the B<CRYPTO_MEM_CHECK_OFF>.
120 The B<CRYPTO_MEM_CHECK_DISABLE> and B<CRYPTO_MEM_CHECK_ENABLE> modes
121 are used internally within OpenSSL to temporarily suspend and resume
122 tracking.
123
124 While checking memory, it can be useful to store additional context
125 about what is being done.
126 For example, identifying the field names when parsing a complicated
127 data structure.
128 OPENSSL_mem_debug_push() (which calls CRYPTO_mem_debug_push())
129 attachs an identifying string to the allocation stack.
130 This must be a global or other static string; it is not copied.
131 OPENSSL_mem_debug_pop() removes identifying state from the stack.
132
133 At the end of the program, calling CRYPTO_mem_leaks() or
134 CRYPTO_mem_leaks_fp() will report all "leaked" memory, writing it
135 to the specified BIO B<b> or FILE B<fp>. These functions return 1 if
136 there are no leaks, 0 if there are leaks and -1 if an error occurred.
137
138 =head1 RETURN VALUES
139
140 OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free()
141 CRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions()
142 return no value.
143
144 CRYPTO_mem_leaks() and CRYPTO_mem_leaks_fp() return 1 if there
145 are no leaks, 0 if there are leaks and -1 if an error occurred.
146
147 OPENSSL_malloc(), OPENSSL_zalloc(), OPENSSL_realloc(),
148 OPENSSL_clear_realloc(),
149 CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(),
150 CRYPTO_clear_realloc(),
151 OPENSSL_strdup(), and OPENSSL_strndup()
152 return a pointer to allocated memory or NULL on error.
153
154 CRYPTO_set_mem_functions() and CRYPTO_set_mem_debug()
155 return 1 on success or 0 on failure (almost
156 always because allocations have already happened).
157
158 CRYPTO_mem_ctrl() returns the previous value of the mode.
159
160 OPENSSL_mem_debug_push() and OPENSSL_mem_debug_pop()
161 return 1 on success or 0 on failure.
162
163 =cut