add cyassl runtime alloc routines override, move to ctaocrypt so both can use, submitted by eof

This commit is contained in:
Todd A Ouska
2011-04-15 16:43:00 -07:00
parent 7014d6bbc1
commit 651b793791
7 changed files with 230 additions and 41 deletions

View File

@@ -0,0 +1,52 @@
/* cyassl_memory.h
*
* Copyright (C) 2006-2011 Sawtooth Consulting Ltd.
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifndef CYASSL_MEMORY_H
#define CYASSL_MEMORY_H
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef void *(*CyaSSL_Malloc_cb)(size_t size);
typedef void (*CyaSSL_Free_cb)(void *ptr);
typedef void *(*CyaSSL_Realloc_cb)(void *ptr, size_t size);
int CyaSSL_SetAllocators(CyaSSL_Malloc_cb malloc_function,
CyaSSL_Free_cb free_function,
CyaSSL_Realloc_cb realloc_function);
void* CyaSSL_Malloc(size_t size);
void CyaSSL_Free(void *ptr);
void* CyaSSL_Realloc(void *ptr, size_t size);
#ifdef __cplusplus
}
#endif
#endif /* CYASSL_MEMORY_H */

View File

@@ -1,3 +1,24 @@
/* logging.h
*
* Copyright (C) 2006-2011 Sawtooth Consulting Ltd.
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifndef CYASSL_LOGGING_H #ifndef CYASSL_LOGGING_H
#define CYASSL_LOGGING_H #define CYASSL_LOGGING_H

View File

@@ -260,6 +260,11 @@
#endif /* MICRIUM */ #endif /* MICRIUM */
#if !defined(XMALLOC_USER) && !defined(MICRIUM_MALLOC)
#define USE_CYASSL_MEMORY
#endif
/* Place any other flags or defines here */ /* Place any other flags or defines here */

View File

@@ -134,11 +134,12 @@ enum {
extern void *XREALLOC(void *p, size_t n, void* heap, int type); extern void *XREALLOC(void *p, size_t n, void* heap, int type);
extern void XFREE(void *p, void* heap, int type); extern void XFREE(void *p, void* heap, int type);
#elif !defined(MICRIUM_MALLOC) #elif !defined(MICRIUM_MALLOC)
/* defaults to C runtime if user doesn't override and not Micrium */ /* default C runtime, can install different routines at runtime */
#include <stdlib.h> #define USE_CYASSL_MEMORY
#define XMALLOC(s, h, t) malloc((s)) #include <cyassl_memory.h>
#define XFREE(p, h, t) {void* xp = (p); if((xp)) free((xp));} #define XMALLOC(s, h, t) CyaSSL_Malloc((s))
#define XREALLOC(p, n, h, t) realloc((p), (n)) #define XFREE(p, h, t) {void* xp = (p); if((xp)) CyaSSL_Free((xp));}
#define XREALLOC(p, n, h, t) CyaSSL_Realloc((p), (n))
#endif #endif
#ifndef STRING_USER #ifndef STRING_USER

View File

@@ -0,0 +1,92 @@
/* cyassl_memory.c
*
* Copyright (C) 2006-2011 Sawtooth Consulting Ltd.
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "os_settings.h"
#ifdef USE_CYASSL_MEMORY
#include "cyassl_memory.h"
/* Set these to default values initially. */
static CyaSSL_Malloc_cb malloc_function = 0;
static CyaSSL_Free_cb free_function = 0;
static CyaSSL_Realloc_cb realloc_function = 0;
int CyaSSL_SetAllocators(CyaSSL_Malloc_cb mf,
CyaSSL_Free_cb ff,
CyaSSL_Realloc_cb rf)
{
int res = 0;
if (mf)
malloc_function = mf;
else
res = -1;
if (ff)
free_function = ff;
else
res = -1;
if (rf)
realloc_function = rf;
else
res = -1;
return res;
}
void* CyaSSL_Malloc(size_t size)
{
void* res = 0;
if (malloc_function)
res = malloc_function(size);
else
res = malloc(size);
return res;
}
void CyaSSL_Free(void *ptr)
{
if (free_function)
free_function(ptr);
else
free(ptr);
}
void* CyaSSL_Realloc(void *ptr, size_t size)
{
void* res = 0;
if (realloc_function)
res = realloc_function(ptr, size);
else
res = realloc(ptr, size);
return res;
}
#endif /* USE_CYASSL_MEMORY */

View File

@@ -1,8 +1,26 @@
#include "logging.h" /* logging.c
*
* Copyright (C) 2006-2011 Sawtooth Consulting Ltd.
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H #include "os_settings.h"
#include "config.h" #include "logging.h"
#endif
/* Set these to default values initially. */ /* Set these to default values initially. */
@@ -20,7 +38,6 @@ int CyaSSL_SetLoggingCb(CyaSSL_Logging_cb f)
res = -1; res = -1;
return res; return res;
} }

View File

@@ -8,7 +8,8 @@ libcyassl_la_SOURCES = \
../ctaocrypt/src/random.c ../ctaocrypt/src/rsa.c ../ctaocrypt/src/sha.c \ ../ctaocrypt/src/random.c ../ctaocrypt/src/rsa.c ../ctaocrypt/src/sha.c \
../ctaocrypt/src/aes.c ../ctaocrypt/src/sha256.c ../ctaocrypt/src/dh.c \ ../ctaocrypt/src/aes.c ../ctaocrypt/src/sha256.c ../ctaocrypt/src/dh.c \
../ctaocrypt/src/dsa.c ../ctaocrypt/src/arc4.c ../ctaocrypt/src/rabbit.c \ ../ctaocrypt/src/dsa.c ../ctaocrypt/src/arc4.c ../ctaocrypt/src/rabbit.c \
../ctaocrypt/src/pwdbased.c ../ctaocrypt/src/logging.c ../ctaocrypt/src/pwdbased.c ../ctaocrypt/src/logging.c \
../ctaocrypt/src/cyassl_memory.c
libcyassl_la_LDFLAGS = -no-undefined -version-info 1:0:0 libcyassl_la_LDFLAGS = -no-undefined -version-info 1:0:0
EXTRA_DIST = ../include/*.h ../include/openssl/*.h ../include/*.rc EXTRA_DIST = ../include/*.h ../include/openssl/*.h ../include/*.rc