source: UsbWattMeter/trunk/wolfssl-3.7.0/wolfcrypt/src/memory.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 7 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 4.1 KB
Line 
1/* memory.c
2 *
3 * Copyright (C) 2006-2015 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL. (formerly known as CyaSSL)
6 *
7 * wolfSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * wolfSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22#ifdef HAVE_CONFIG_H
23 #include <config.h>
24#endif
25
26#include <wolfssl/wolfcrypt/settings.h>
27
28/* check old macros @wc_fips */
29#if defined(USE_CYASSL_MEMORY) && !defined(USE_WOLFSSL_MEMORY)
30 #define USE_WOLFSSL_MEMORY
31#endif
32#if defined(CYASSL_MALLOC_CHECK) && !defined(WOLFSSL_MALLOC_CHECK)
33 #define WOLFSSL_MALLOC_CHECK
34#endif
35
36#ifdef USE_WOLFSSL_MEMORY
37
38#include <wolfssl/wolfcrypt/memory.h>
39#include <wolfssl/wolfcrypt/error-crypt.h>
40
41#ifdef WOLFSSL_MALLOC_CHECK
42 #include <stdio.h>
43#endif
44
45/* Set these to default values initially. */
46static wolfSSL_Malloc_cb malloc_function = 0;
47static wolfSSL_Free_cb free_function = 0;
48static wolfSSL_Realloc_cb realloc_function = 0;
49
50int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf,
51 wolfSSL_Free_cb ff,
52 wolfSSL_Realloc_cb rf)
53{
54 int res = 0;
55
56 if (mf)
57 malloc_function = mf;
58 else
59 res = BAD_FUNC_ARG;
60
61 if (ff)
62 free_function = ff;
63 else
64 res = BAD_FUNC_ARG;
65
66 if (rf)
67 realloc_function = rf;
68 else
69 res = BAD_FUNC_ARG;
70
71 return res;
72}
73
74
75void* wolfSSL_Malloc(size_t size)
76{
77 void* res = 0;
78
79 if (malloc_function)
80 res = malloc_function(size);
81 else
82 res = malloc(size);
83
84 #ifdef WOLFSSL_MALLOC_CHECK
85 if (res == NULL)
86 puts("wolfSSL_malloc failed");
87 #endif
88
89 return res;
90}
91
92void wolfSSL_Free(void *ptr)
93{
94 if (free_function)
95 free_function(ptr);
96 else
97 free(ptr);
98}
99
100void* wolfSSL_Realloc(void *ptr, size_t size)
101{
102 void* res = 0;
103
104 if (realloc_function)
105 res = realloc_function(ptr, size);
106 else
107 res = realloc(ptr, size);
108
109 return res;
110}
111
112#endif /* USE_WOLFSSL_MEMORY */
113
114
115#ifdef HAVE_IO_POOL
116
117/* Example for user io pool, shared build may need definitions in lib proper */
118
119#include <wolfssl/wolfcrypt/types.h>
120#include <stdlib.h>
121
122#ifndef HAVE_THREAD_LS
123 #error "Oops, simple I/O pool example needs thread local storage"
124#endif
125
126
127/* allow simple per thread in and out pools */
128/* use 17k size sense max record size is 16k plus overhead */
129static THREAD_LS_T byte pool_in[17*1024];
130static THREAD_LS_T byte pool_out[17*1024];
131
132
133void* XMALLOC(size_t n, void* heap, int type)
134{
135 (void)heap;
136
137 if (type == DYNAMIC_TYPE_IN_BUFFER) {
138 if (n < sizeof(pool_in))
139 return pool_in;
140 else
141 return NULL;
142 }
143
144 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
145 if (n < sizeof(pool_out))
146 return pool_out;
147 else
148 return NULL;
149 }
150
151 return malloc(n);
152}
153
154void* XREALLOC(void *p, size_t n, void* heap, int type)
155{
156 (void)heap;
157
158 if (type == DYNAMIC_TYPE_IN_BUFFER) {
159 if (n < sizeof(pool_in))
160 return pool_in;
161 else
162 return NULL;
163 }
164
165 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
166 if (n < sizeof(pool_out))
167 return pool_out;
168 else
169 return NULL;
170 }
171
172 return realloc(p, n);
173}
174
175
176/* unit api calls, let's make sure visible with WOLFSSL_API */
177WOLFSSL_API void XFREE(void *p, void* heap, int type)
178{
179 (void)heap;
180
181 if (type == DYNAMIC_TYPE_IN_BUFFER)
182 return; /* do nothing, static pool */
183
184 if (type == DYNAMIC_TYPE_OUT_BUFFER)
185 return; /* do nothing, static pool */
186
187 free(p);
188}
189
190#endif /* HAVE_IO_POOL */
191
Note: See TracBrowser for help on using the repository browser.