]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/ctaocrypt/src/memory.c
Final commit before tagging - cosmetic changes only.
[freertos] / FreeRTOS-Plus / Source / CyaSSL / ctaocrypt / src / memory.c
1 /* memory.c 
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
7  * CyaSSL 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  * CyaSSL 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 <cyassl/ctaocrypt/settings.h>
27
28 #ifdef USE_CYASSL_MEMORY
29
30 #include <cyassl/ctaocrypt/memory.h>
31 #include <cyassl/ctaocrypt/error-crypt.h>
32
33 #ifdef CYASSL_MALLOC_CHECK
34     #include <stdio.h>
35 #endif
36
37 /* Set these to default values initially. */
38 static CyaSSL_Malloc_cb  malloc_function = 0;
39 static CyaSSL_Free_cb    free_function = 0;
40 static CyaSSL_Realloc_cb realloc_function = 0;
41
42 int CyaSSL_SetAllocators(CyaSSL_Malloc_cb  mf,
43                          CyaSSL_Free_cb    ff,
44                          CyaSSL_Realloc_cb rf)
45 {
46     int res = 0;
47
48     if (mf)
49         malloc_function = mf;
50         else
51         res = BAD_FUNC_ARG;
52
53     if (ff)
54         free_function = ff;
55     else
56         res = BAD_FUNC_ARG;
57
58     if (rf)
59         realloc_function = rf;
60     else
61         res = BAD_FUNC_ARG;
62
63     return res;
64 }
65
66
67 void* CyaSSL_Malloc(size_t size)
68 {
69     void* res = 0;
70
71     if (malloc_function)
72         res = malloc_function(size);
73     else
74         res = malloc(size);
75
76     #ifdef CYASSL_MALLOC_CHECK
77         if (res == NULL)
78             puts("CyaSSL_malloc failed");
79     #endif
80                                 
81     return res;
82 }
83
84 void CyaSSL_Free(void *ptr)
85 {
86     if (free_function)
87         free_function(ptr);
88     else
89         free(ptr);
90 }
91
92 void* CyaSSL_Realloc(void *ptr, size_t size)
93 {
94     void* res = 0;
95
96     if (realloc_function)
97         res = realloc_function(ptr, size);
98     else
99         res = realloc(ptr, size);
100
101     return res;
102 }
103
104 #endif /* USE_CYASSL_MEMORY */
105
106
107 #ifdef HAVE_IO_POOL
108
109 /* Example for user io pool, shared build may need definitions in lib proper */
110
111 #include <cyassl/ctaocrypt/types.h>
112 #include <stdlib.h>
113
114 #ifndef HAVE_THREAD_LS
115     #error "Oops, simple I/O pool example needs thread local storage"
116 #endif
117
118
119 /* allow simple per thread in and out pools */
120 /* use 17k size sense max record size is 16k plus overhead */
121 static THREAD_LS_T byte pool_in[17*1024];
122 static THREAD_LS_T byte pool_out[17*1024];
123
124
125 void* XMALLOC(size_t n, void* heap, int type)
126 {
127     (void)heap;
128
129     if (type == DYNAMIC_TYPE_IN_BUFFER) {
130         if (n < sizeof(pool_in))
131             return pool_in;
132         else
133             return NULL;
134     }
135
136     if (type == DYNAMIC_TYPE_OUT_BUFFER) {
137         if (n < sizeof(pool_out))
138             return pool_out;
139         else
140             return NULL;
141     }
142
143     return malloc(n);
144 }
145
146 void* XREALLOC(void *p, size_t n, void* heap, int type)
147 {
148     (void)heap;
149
150     if (type == DYNAMIC_TYPE_IN_BUFFER) {
151         if (n < sizeof(pool_in))
152             return pool_in;
153         else
154             return NULL;
155     }
156
157     if (type == DYNAMIC_TYPE_OUT_BUFFER) {
158         if (n < sizeof(pool_out))
159             return pool_out;
160         else
161             return NULL;
162     }
163
164     return realloc(p, n);
165 }
166
167
168 /* unit api calls, let's make sure visisble with CYASSL_API */
169 CYASSL_API void XFREE(void *p, void* heap, int type)
170 {
171     (void)heap;
172
173     if (type == DYNAMIC_TYPE_IN_BUFFER)
174         return;  /* do nothing, static pool */
175
176     if (type == DYNAMIC_TYPE_OUT_BUFFER)
177         return;  /* do nothing, static pool */
178
179     free(p);
180 }
181
182 #endif /* HAVE_IO_POOL */
183