- Timestamp:
- Feb 7, 2019, 8:36:33 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
asp3_tinet_ecnl_arm/trunk/wolfssl-3.12.2/wolfcrypt/src/memory.c
r352 r372 35 35 #endif 36 36 37 38 /* 39 Possible memory options: 40 * NO_WOLFSSL_MEMORY: Disables wolf memory callback support. When not defined settings.h defines USE_WOLFSSL_MEMORY. 41 * WOLFSSL_STATIC_MEMORY: Turns on the use of static memory buffers and functions. 42 This allows for using static memory instead of dynamic. 43 * WOLFSSL_STATIC_ALIGN: Define defaults to 16 to indicate static memory alignment. 44 * HAVE_IO_POOL: Enables use of static thread safe memory pool for input/output buffers. 45 * XMALLOC_OVERRIDE: Allows override of the XMALLOC, XFREE and XREALLOC macros. 46 * XMALLOC_USER: Allows custom XMALLOC, XFREE and XREALLOC functions to be defined. 47 * WOLFSSL_NO_MALLOC: Disables the fall-back case to use STDIO malloc/free when no callbacks are set. 48 * WOLFSSL_TRACK_MEMORY: Enables memory tracking for total stats and list of allocated memory. 49 * WOLFSSL_DEBUG_MEMORY: Enables extra function and line number args for memory callbacks. 50 * WOLFSSL_DEBUG_MEMORY_PRINT: Enables printing of each malloc/free. 51 * WOLFSSL_MALLOC_CHECK: Reports malloc or alignment failure using WOLFSSL_STATIC_ALIGN 52 * WOLFSSL_FORCE_MALLOC_FAIL_TEST: Used for internal testing to induce random malloc failures. 53 * WOLFSSL_HEAP_TEST: Used for internal testing of heap hint 54 */ 55 56 37 57 #ifdef USE_WOLFSSL_MEMORY 38 58 … … 41 61 #include <wolfssl/wolfcrypt/logging.h> 42 62 43 #if defined(WOLFSSL_MALLOC_CHECK) || defined(WOLFSSL_TRACK_MEMORY_FULL) 63 #if defined(WOLFSSL_DEBUG_MEMORY) && defined(WOLFSSL_DEBUG_MEMORY_PRINT) 64 #include <stdio.h> 65 #endif 66 67 #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST 68 static int gMemFailCountSeed; 69 static int gMemFailCount; 70 void wolfSSL_SetMemFailCount(int memFailCount) 71 { 72 if (gMemFailCountSeed == 0) { 73 gMemFailCountSeed = memFailCount; 74 gMemFailCount = memFailCount; 75 } 76 } 77 #endif 78 #if defined(WOLFSSL_MALLOC_CHECK) || defined(WOLFSSL_TRACK_MEMORY_FULL) || \ 79 defined(WOLFSSL_MEMORY_LOG) 44 80 #include <stdio.h> 45 81 #endif … … 47 83 48 84 /* Set these to default values initially. */ 49 static wolfSSL_Malloc_cb malloc_function = 0;50 static wolfSSL_Free_cb free_function = 0;51 static wolfSSL_Realloc_cb realloc_function = 0;85 static wolfSSL_Malloc_cb malloc_function = NULL; 86 static wolfSSL_Free_cb free_function = NULL; 87 static wolfSSL_Realloc_cb realloc_function = NULL; 52 88 53 89 int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf, … … 55 91 wolfSSL_Realloc_cb rf) 56 92 { 57 int res = 0;58 59 if (mf)60 93 malloc_function = mf; 61 else62 res = BAD_FUNC_ARG;63 64 if (ff)65 94 free_function = ff; 66 else67 res = BAD_FUNC_ARG;68 69 if (rf)70 95 realloc_function = rf; 71 else 72 res = BAD_FUNC_ARG; 73 74 return res; 96 return 0; 75 97 } 76 98 … … 102 124 } 103 125 else { 126 #ifndef WOLFSSL_NO_MALLOC 104 127 res = malloc(size); 105 } 128 #else 129 WOLFSSL_MSG("No malloc available"); 130 #endif 131 } 132 133 #ifdef WOLFSSL_DEBUG_MEMORY 134 #if defined(WOLFSSL_DEBUG_MEMORY_PRINT) && !defined(WOLFSSL_TRACK_MEMORY) 135 printf("Alloc: %p -> %u at %s:%d\n", res, (word32)size, func, line); 136 #else 137 (void)func; 138 (void)line; 139 #endif 140 #endif 106 141 107 142 #ifdef WOLFSSL_MALLOC_CHECK 108 143 if (res == NULL) 109 puts("wolfSSL_malloc failed"); 144 WOLFSSL_MSG("wolfSSL_malloc failed"); 145 #endif 146 147 #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST 148 if (res && --gMemFailCount == 0) { 149 printf("\n---FORCED MEM FAIL TEST---\n"); 150 if (free_function) { 151 #ifdef WOLFSSL_DEBUG_MEMORY 152 free_function(res, func, line); 153 #else 154 free_function(res); 155 #endif 156 } 157 else { 158 free(res); /* clear */ 159 } 160 gMemFailCount = gMemFailCountSeed; /* reset */ 161 return NULL; 162 } 110 163 #endif 111 164 … … 119 172 #endif 120 173 { 174 #ifdef WOLFSSL_DEBUG_MEMORY 175 #if defined(WOLFSSL_DEBUG_MEMORY_PRINT) && !defined(WOLFSSL_TRACK_MEMORY) 176 printf("Free: %p at %s:%d\n", ptr, func, line); 177 #else 178 (void)func; 179 (void)line; 180 #endif 181 #endif 182 121 183 if (free_function) { 122 184 #ifdef WOLFSSL_DEBUG_MEMORY … … 127 189 } 128 190 else { 191 #ifndef WOLFSSL_NO_MALLOC 129 192 free(ptr); 193 #else 194 WOLFSSL_MSG("No free available"); 195 #endif 130 196 } 131 197 } … … 147 213 } 148 214 else { 215 #ifndef WOLFSSL_NO_MALLOC 149 216 res = realloc(ptr, size); 217 #else 218 WOLFSSL_MSG("No realloc available"); 219 #endif 150 220 } 151 221 … … 534 604 #else 535 605 #ifndef WOLFSSL_NO_MALLOC 606 #ifdef FREERTOS 607 res = pvPortMalloc(size); 608 #else 536 609 res = malloc(size); 610 #endif 537 611 #else 538 612 WOLFSSL_MSG("No heap hint found to use and no malloc"); … … 668 742 #endif 669 743 #ifndef WOLFSSL_NO_MALLOC 744 #ifdef FREERTOS 745 vPortFree(ptr); 746 #else 670 747 free(ptr); 748 #endif 671 749 #else 672 750 WOLFSSL_MSG("Error trying to call free when turned off"); … … 923 1001 #endif /* HAVE_IO_POOL */ 924 1002 1003 #ifdef WOLFSSL_MEMORY_LOG 1004 void *xmalloc(size_t n, void* heap, int type, const char* func, 1005 const char* file, unsigned int line) 1006 { 1007 void* p; 1008 word32* p32; 1009 1010 if (malloc_function) 1011 p32 = malloc_function(n + sizeof(word32) * 4); 1012 else 1013 p32 = malloc(n + sizeof(word32) * 4); 1014 1015 p32[0] = n; 1016 p = (void*)(p32 + 4); 1017 1018 fprintf(stderr, "Alloc: %p -> %u (%d) at %s:%s:%d\n", p, (word32)n, type, 1019 func, file, line); 1020 1021 (void)heap; 1022 1023 return p; 1024 } 1025 void *xrealloc(void *p, size_t n, void* heap, int type, const char* func, 1026 const char* file, unsigned int line) 1027 { 1028 void* newp = NULL; 1029 word32* p32; 1030 word32* oldp32 = NULL; 1031 word32 oldLen; 1032 1033 if (p != NULL) { 1034 oldp32 = (word32*)p; 1035 oldp32 -= 4; 1036 oldLen = oldp32[0]; 1037 } 1038 1039 if (realloc_function) 1040 p32 = realloc_function(oldp32, n + sizeof(word32) * 4); 1041 else 1042 p32 = realloc(oldp32, n + sizeof(word32) * 4); 1043 1044 if (p32 != NULL) { 1045 p32[0] = n; 1046 newp = (void*)(p32 + 4); 1047 1048 fprintf(stderr, "Alloc: %p -> %u (%d) at %s:%s:%d\n", newp, (word32)n, 1049 type, func, file, line); 1050 if (p != NULL) { 1051 fprintf(stderr, "Free: %p -> %u (%d) at %s:%s:%d\n", p, oldLen, 1052 type, func, file, line); 1053 } 1054 } 1055 1056 (void)heap; 1057 1058 return newp; 1059 } 1060 void xfree(void *p, void* heap, int type, const char* func, const char* file, 1061 unsigned int line) 1062 { 1063 word32* p32 = (word32*)p; 1064 1065 if (p != NULL) { 1066 p32 -= 4; 1067 1068 fprintf(stderr, "Free: %p -> %u (%d) at %s:%s:%d\n", p, p32[0], type, 1069 func, file, line); 1070 1071 if (free_function) 1072 free_function(p32); 1073 else 1074 free(p32); 1075 } 1076 1077 (void)heap; 1078 } 1079 #endif /* WOLFSSL_MEMORY_LOG */ 1080 1081 #ifdef WOLFSSL_STACK_LOG 1082 /* Note: this code only works with GCC using -finstrument-functions. */ 1083 void __attribute__((no_instrument_function)) 1084 __cyg_profile_func_enter(void *func, void *caller) 1085 { 1086 register void* sp asm("sp"); 1087 fprintf(stderr, "ENTER: %016lx %p\n", (size_t)func, sp); 1088 (void)caller; 1089 } 1090 1091 void __attribute__((no_instrument_function)) 1092 __cyg_profile_func_exit(void *func, void *caller) 1093 { 1094 register void* sp asm("sp"); 1095 fprintf(stderr, "EXIT: %016lx %p\n", (size_t)func, sp); 1096 (void)caller; 1097 } 1098 #endif 1099
Note:
See TracChangeset
for help on using the changeset viewer.