Files
wolfssl/wolfcrypt/src/memory.c

185 lines
3.8 KiB
C
Raw Normal View History

2014-12-19 09:56:51 -07:00
/* memory.c
*
* Copyright (C) 2006-2014 wolfSSL Inc.
*
* This file is part of CyaSSL.
*
* CyaSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CyaSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
2014-12-19 10:47:38 -07:00
//#ifdef USE_WOLFSSL_MEMORY
//@TODO
2014-12-19 09:56:51 -07:00
#include <wolfssl/wolfcrypt/memory.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
2014-12-19 10:47:38 -07:00
#ifdef WOLFSSL_MALLOC_CHECK
2014-12-19 09:56:51 -07:00
#include <stdio.h>
#endif
/* Set these to default values initially. */
static wolfSSL_Malloc_cb malloc_function = 0;
static wolfSSL_Free_cb free_function = 0;
static wolfSSL_Realloc_cb realloc_function = 0;
2014-12-19 10:47:38 -07:00
int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf,
wolfSSL_Free_cb ff,
wolfSSL_Realloc_cb rf)
2014-12-19 09:56:51 -07:00
{
int res = 0;
if (mf)
malloc_function = mf;
else
res = BAD_FUNC_ARG;
if (ff)
free_function = ff;
else
res = BAD_FUNC_ARG;
if (rf)
realloc_function = rf;
else
res = BAD_FUNC_ARG;
return res;
}
void* wolfSSL_Malloc(size_t size)
{
void* res = 0;
if (malloc_function)
res = malloc_function(size);
else
res = malloc(size);
#ifdef CYASSL_MALLOC_CHECK
if (res == NULL)
puts("wolfSSL_malloc failed");
#endif
return res;
}
void wolfSSL_Free(void *ptr)
{
if (free_function)
free_function(ptr);
else
free(ptr);
}
void* wolfSSL_Realloc(void *ptr, size_t size)
{
void* res = 0;
if (realloc_function)
res = realloc_function(ptr, size);
else
res = realloc(ptr, size);
return res;
}
2014-12-19 10:47:38 -07:00
//#endif /* USE_WOLFSSL_MEMORY */
2014-12-19 09:56:51 -07:00
#ifdef HAVE_IO_POOL
/* Example for user io pool, shared build may need definitions in lib proper */
#include <wolfssl/wolfcrypt/types.h>
#include <stdlib.h>
#ifndef HAVE_THREAD_LS
#error "Oops, simple I/O pool example needs thread local storage"
#endif
/* allow simple per thread in and out pools */
/* use 17k size sense max record size is 16k plus overhead */
static THREAD_LS_T byte pool_in[17*1024];
static THREAD_LS_T byte pool_out[17*1024];
2014-12-19 10:47:38 -07:00
void* wc_MALLOC(size_t n, void* heap, int type)
2014-12-19 09:56:51 -07:00
{
(void)heap;
if (type == DYNAMIC_TYPE_IN_BUFFER) {
if (n < sizeof(pool_in))
return pool_in;
else
return NULL;
}
if (type == DYNAMIC_TYPE_OUT_BUFFER) {
if (n < sizeof(pool_out))
return pool_out;
else
return NULL;
}
return malloc(n);
}
2014-12-19 10:47:38 -07:00
void* wc_REALLOC(void *p, size_t n, void* heap, int type)
2014-12-19 09:56:51 -07:00
{
(void)heap;
if (type == DYNAMIC_TYPE_IN_BUFFER) {
if (n < sizeof(pool_in))
return pool_in;
else
return NULL;
}
if (type == DYNAMIC_TYPE_OUT_BUFFER) {
if (n < sizeof(pool_out))
return pool_out;
else
return NULL;
}
return realloc(p, n);
}
/* unit api calls, let's make sure visisble with CYASSL_API */
2014-12-19 10:47:38 -07:00
WOLFSSL_API void wc_FREE(void *p, void* heap, int type)
2014-12-19 09:56:51 -07:00
{
(void)heap;
if (type == DYNAMIC_TYPE_IN_BUFFER)
return; /* do nothing, static pool */
if (type == DYNAMIC_TYPE_OUT_BUFFER)
return; /* do nothing, static pool */
free(p);
}
#endif /* HAVE_IO_POOL */