ssh

Dependents:   OS

Committer:
sPymbed
Date:
Mon Nov 25 14:24:05 2019 +0000
Revision:
0:c4152c628df5
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sPymbed 0:c4152c628df5 1 /* memory.c
sPymbed 0:c4152c628df5 2 *
sPymbed 0:c4152c628df5 3 * Copyright (C) 2014-2016 wolfSSL Inc.
sPymbed 0:c4152c628df5 4 *
sPymbed 0:c4152c628df5 5 * This file is part of wolfSSH.
sPymbed 0:c4152c628df5 6 *
sPymbed 0:c4152c628df5 7 * wolfSSH is free software; you can redistribute it and/or modify
sPymbed 0:c4152c628df5 8 * it under the terms of the GNU General Public License as published by
sPymbed 0:c4152c628df5 9 * the Free Software Foundation; either version 3 of the License, or
sPymbed 0:c4152c628df5 10 * (at your option) any later version.
sPymbed 0:c4152c628df5 11 *
sPymbed 0:c4152c628df5 12 * wolfSSH is distributed in the hope that it will be useful,
sPymbed 0:c4152c628df5 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sPymbed 0:c4152c628df5 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
sPymbed 0:c4152c628df5 15 * GNU General Public License for more details.
sPymbed 0:c4152c628df5 16 *
sPymbed 0:c4152c628df5 17 * You should have received a copy of the GNU General Public License
sPymbed 0:c4152c628df5 18 * along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
sPymbed 0:c4152c628df5 19 */
sPymbed 0:c4152c628df5 20
sPymbed 0:c4152c628df5 21
sPymbed 0:c4152c628df5 22 /*
sPymbed 0:c4152c628df5 23 * The memory module contains the interface to the custom memory handling hooks.
sPymbed 0:c4152c628df5 24 */
sPymbed 0:c4152c628df5 25
sPymbed 0:c4152c628df5 26
sPymbed 0:c4152c628df5 27 #ifdef HAVE_CONFIG_H
sPymbed 0:c4152c628df5 28 #include <config.h>
sPymbed 0:c4152c628df5 29 #endif
sPymbed 0:c4152c628df5 30
sPymbed 0:c4152c628df5 31 #include <wolfssh/settings.h>
sPymbed 0:c4152c628df5 32
sPymbed 0:c4152c628df5 33
sPymbed 0:c4152c628df5 34 #ifdef USE_WOLFSSH_MEMORY
sPymbed 0:c4152c628df5 35
sPymbed 0:c4152c628df5 36 #include <wolfssh/memory.h>
sPymbed 0:c4152c628df5 37 #include <wolfssh/error.h>
sPymbed 0:c4152c628df5 38
sPymbed 0:c4152c628df5 39 /* Set these to default values initially. */
sPymbed 0:c4152c628df5 40 static wolfSSH_Malloc_cb malloc_function = NULL;
sPymbed 0:c4152c628df5 41 static wolfSSH_Free_cb free_function = NULL;
sPymbed 0:c4152c628df5 42 static wolfSSH_Realloc_cb realloc_function = NULL;
sPymbed 0:c4152c628df5 43
sPymbed 0:c4152c628df5 44
sPymbed 0:c4152c628df5 45 /* set wolfSSH allocator callbacks, 0 on success */
sPymbed 0:c4152c628df5 46 int wolfSSH_SetAllocators(wolfSSH_Malloc_cb mf,
sPymbed 0:c4152c628df5 47 wolfSSH_Free_cb ff,
sPymbed 0:c4152c628df5 48 wolfSSH_Realloc_cb rf)
sPymbed 0:c4152c628df5 49 {
sPymbed 0:c4152c628df5 50 int res = 0;
sPymbed 0:c4152c628df5 51
sPymbed 0:c4152c628df5 52 if (mf)
sPymbed 0:c4152c628df5 53 malloc_function = mf;
sPymbed 0:c4152c628df5 54 else
sPymbed 0:c4152c628df5 55 res = WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 56
sPymbed 0:c4152c628df5 57 if (ff)
sPymbed 0:c4152c628df5 58 free_function = ff;
sPymbed 0:c4152c628df5 59 else
sPymbed 0:c4152c628df5 60 res = WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 61
sPymbed 0:c4152c628df5 62 if (rf)
sPymbed 0:c4152c628df5 63 realloc_function = rf;
sPymbed 0:c4152c628df5 64 else
sPymbed 0:c4152c628df5 65 res = WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 66
sPymbed 0:c4152c628df5 67 return res;
sPymbed 0:c4152c628df5 68 }
sPymbed 0:c4152c628df5 69
sPymbed 0:c4152c628df5 70
sPymbed 0:c4152c628df5 71 /* our malloc handler */
sPymbed 0:c4152c628df5 72 void* wolfSSH_Malloc(size_t size)
sPymbed 0:c4152c628df5 73 {
sPymbed 0:c4152c628df5 74 void* res = 0;
sPymbed 0:c4152c628df5 75
sPymbed 0:c4152c628df5 76 if (malloc_function)
sPymbed 0:c4152c628df5 77 res = malloc_function(size);
sPymbed 0:c4152c628df5 78 else
sPymbed 0:c4152c628df5 79 res = malloc(size);
sPymbed 0:c4152c628df5 80
sPymbed 0:c4152c628df5 81 return res;
sPymbed 0:c4152c628df5 82 }
sPymbed 0:c4152c628df5 83
sPymbed 0:c4152c628df5 84
sPymbed 0:c4152c628df5 85 /* our free handler */
sPymbed 0:c4152c628df5 86 void wolfSSH_Free(void *ptr)
sPymbed 0:c4152c628df5 87 {
sPymbed 0:c4152c628df5 88 if (free_function)
sPymbed 0:c4152c628df5 89 free_function(ptr);
sPymbed 0:c4152c628df5 90 else
sPymbed 0:c4152c628df5 91 free(ptr);
sPymbed 0:c4152c628df5 92 }
sPymbed 0:c4152c628df5 93
sPymbed 0:c4152c628df5 94
sPymbed 0:c4152c628df5 95 /* our realloc handler */
sPymbed 0:c4152c628df5 96 void* wolfSSH_Realloc(void *ptr, size_t size)
sPymbed 0:c4152c628df5 97 {
sPymbed 0:c4152c628df5 98 void* res = 0;
sPymbed 0:c4152c628df5 99
sPymbed 0:c4152c628df5 100 if (realloc_function)
sPymbed 0:c4152c628df5 101 res = realloc_function(ptr, size);
sPymbed 0:c4152c628df5 102 else
sPymbed 0:c4152c628df5 103 res = realloc(ptr, size);
sPymbed 0:c4152c628df5 104
sPymbed 0:c4152c628df5 105 return res;
sPymbed 0:c4152c628df5 106 }
sPymbed 0:c4152c628df5 107
sPymbed 0:c4152c628df5 108 #endif /* USE_WOLFSSH_MEMORY */
sPymbed 0:c4152c628df5 109