]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/mem.c
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include <openssl/crypto.h>
14 #include "internal/cryptlib.h"
17 * the following pointers may be changed as long as 'allow_customize' is set
19 static int allow_customize
= 1;
21 static void *(*malloc_impl
)(size_t, const char *, int)
23 static void *(*realloc_impl
)(void *, size_t, const char *, int)
25 static void (*free_impl
)(void *, const char *, int)
28 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
29 static int call_malloc_debug
= 1;
31 static int call_malloc_debug
= 0;
34 int CRYPTO_set_mem_functions(
35 void *(*m
)(size_t, const char *, int),
36 void *(*r
)(void *, size_t, const char *, int),
37 void (*f
)(void *, const char *, int))
50 int CRYPTO_set_mem_debug(int flag
)
54 call_malloc_debug
= flag
;
58 void CRYPTO_get_mem_functions(
59 void *(**m
)(size_t, const char *, int),
60 void *(**r
)(void *, size_t, const char *, int),
61 void (**f
)(void *, const char *, int))
71 void *CRYPTO_malloc(size_t num
, const char *file
, int line
)
75 if (malloc_impl
!= NULL
&& malloc_impl
!= CRYPTO_malloc
)
76 return malloc_impl(num
, file
, line
);
81 if (allow_customize
) {
83 * Disallow customization after the first allocation. We only set this
84 * if necessary to avoid a store to the same cache line on every
89 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
90 if (call_malloc_debug
) {
91 CRYPTO_mem_debug_malloc(NULL
, num
, 0, file
, line
);
93 CRYPTO_mem_debug_malloc(ret
, num
, 1, file
, line
);
98 osslargused(file
); osslargused(line
);
105 void *CRYPTO_zalloc(size_t num
, const char *file
, int line
)
107 void *ret
= CRYPTO_malloc(num
, file
, line
);
114 void *CRYPTO_realloc(void *str
, size_t num
, const char *file
, int line
)
116 if (realloc_impl
!= NULL
&& realloc_impl
!= &CRYPTO_realloc
)
117 return realloc_impl(str
, num
, file
, line
);
120 return CRYPTO_malloc(num
, file
, line
);
123 CRYPTO_free(str
, file
, line
);
127 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
128 if (call_malloc_debug
) {
130 CRYPTO_mem_debug_realloc(str
, NULL
, num
, 0, file
, line
);
131 ret
= realloc(str
, num
);
132 CRYPTO_mem_debug_realloc(str
, ret
, num
, 1, file
, line
);
136 osslargused(file
); osslargused(line
);
138 return realloc(str
, num
);
142 void *CRYPTO_clear_realloc(void *str
, size_t old_len
, size_t num
,
143 const char *file
, int line
)
148 return CRYPTO_malloc(num
, file
, line
);
151 CRYPTO_clear_free(str
, old_len
, file
, line
);
155 /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
157 OPENSSL_cleanse((char*)str
+ num
, old_len
- num
);
161 ret
= CRYPTO_malloc(num
, file
, line
);
163 memcpy(ret
, str
, old_len
);
164 CRYPTO_clear_free(str
, old_len
, file
, line
);
169 void CRYPTO_free(void *str
, const char *file
, int line
)
171 if (free_impl
!= NULL
&& free_impl
!= &CRYPTO_free
) {
172 free_impl(str
, file
, line
);
176 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
177 if (call_malloc_debug
) {
178 CRYPTO_mem_debug_free(str
, 0, file
, line
);
180 CRYPTO_mem_debug_free(str
, 1, file
, line
);
189 void CRYPTO_clear_free(void *str
, size_t num
, const char *file
, int line
)
194 OPENSSL_cleanse(str
, num
);
195 CRYPTO_free(str
, file
, line
);