ssh

Dependents:   OS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers memory.c Source File

memory.c

00001 /* memory.c 
00002  *
00003  * Copyright (C) 2014-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSH.
00006  *
00007  * wolfSSH is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSH is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with wolfSSH.  If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 
00022 /*
00023  * The memory module contains the interface to the custom memory handling hooks.
00024  */
00025 
00026 
00027 #ifdef HAVE_CONFIG_H
00028     #include <config.h>
00029 #endif
00030 
00031 #include <wolfssh/settings.h>
00032 
00033 
00034 #ifdef USE_WOLFSSH_MEMORY
00035 
00036 #include <wolfssh/memory.h>
00037 #include <wolfssh/error.h>
00038 
00039 /* Set these to default values initially. */
00040 static wolfSSH_Malloc_cb  malloc_function  = NULL;
00041 static wolfSSH_Free_cb    free_function    = NULL;
00042 static wolfSSH_Realloc_cb realloc_function = NULL;
00043 
00044 
00045 /* set wolfSSH allocator callbacks, 0 on success */
00046 int wolfSSH_SetAllocators(wolfSSH_Malloc_cb  mf,
00047                            wolfSSH_Free_cb    ff,
00048                            wolfSSH_Realloc_cb rf)
00049 {
00050     int res = 0;
00051 
00052     if (mf)
00053         malloc_function = mf;
00054     else
00055         res = WS_BAD_ARGUMENT;
00056 
00057     if (ff)
00058         free_function = ff;
00059     else
00060         res = WS_BAD_ARGUMENT;
00061 
00062     if (rf)
00063         realloc_function = rf;
00064     else
00065         res = WS_BAD_ARGUMENT;
00066 
00067     return res;
00068 }
00069 
00070 
00071 /* our malloc handler */
00072 void* wolfSSH_Malloc(size_t size)
00073 {
00074     void* res = 0;
00075 
00076     if (malloc_function)
00077         res = malloc_function(size);
00078     else
00079         res = malloc(size);
00080 
00081     return res;
00082 }
00083 
00084 
00085 /* our free handler */
00086 void wolfSSH_Free(void *ptr)
00087 {
00088     if (free_function)
00089         free_function(ptr);
00090     else
00091         free(ptr);
00092 }
00093 
00094 
00095 /* our realloc handler */
00096 void* wolfSSH_Realloc(void *ptr, size_t size)
00097 {
00098     void* res = 0;
00099 
00100     if (realloc_function)
00101         res = realloc_function(ptr, size);
00102     else
00103         res = realloc(ptr, size);
00104 
00105     return res;
00106 }
00107 
00108 #endif /* USE_WOLFSSH_MEMORY */
00109