mem functions cleanup
[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 flags);
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 Finer-grain control of the tracking can be done with CRYPTO_mem_ctrl().
116 The most common case is to enable tracking, which is done by using
117 the B<CRYPTO_MEM_CHECK_ON> constant; it can be turned off by using
118 the B<CRYPTO_MEM_CHECK_OFF> value.  The disable and enable values are
119 most commonly used within OpenSSL to termporarily suspend and restore
120 tracking of library internals.
121
122 While checking memory, it can be useful to store additional context
123 about what is being done.
124 For example, identifying the field names when parsing a complicated
125 data structure.
126 OPENSSL_mem_debug_push() (which calls CRYPTO_mem_debug_push())
127 attachs an identifying string to the allocation stack.
128 This must be a global or other static string; it is not copied.
129 OPENSSL_mem_debug_pop() removes identifying state from the stack.
130
131 At the end of the program, calling CRYPTO_mem_leaks() or
132 CRYPTO_mem_leaks_fp() will
133 report all "leaked" memory, writing it to the specified BIO B<b>
134 or FILE B<fp>.
135 Depending on how OpenSSL is built, it may then abort if there
136 are any unfree'd allocations, for debugging.
137
138 =head1 RETURN VALUES
139
140 OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free()
141 CRYPTO_free(), CRYPTO_clear_free(),
142 CRYPTO_get_mem_functions(), and
143 CRYPTO_mem_leaks()
144 return no value.
145
146 OPENSSL_malloc(), OPENSSL_zalloc(), OPENSSL_realloc(),
147 OPENSSL_clear_realloc(),
148 CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(),
149 CRYPTO_clear_realloc(),
150 OPENSSL_strdup(), and OPENSSL_strndup()
151 return a pointer to allocated memory or NULL on error.
152
153 CRYPTO_set_mem_functions() and CRYPTO_set_mem_debug()
154 return 1 on success or 0 on failure (almost
155 always because allocations have already happened).
156
157 CRYPTO_mem_ctrl() return the previous value of the flag.
158
159 OPENSSL_mem_debug_push() and OPENSSL_mem_debug_pop()
160 return 1 on success or 0 on failure.
161
162 =cut