|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +#include <wolfssl/options.h> |
| 5 | +#include <wolfssl/wolfcrypt/wc_port.h> |
| 6 | +#include <wolfssl/wolfcrypt/memory.h> |
| 7 | + |
| 8 | +#ifndef WOLFSSL_STATIC_MEMORY |
| 9 | + #error requires --enable-staticmemory |
| 10 | +#endif |
| 11 | + |
| 12 | +#ifdef WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK |
| 13 | +static void MemoryDebug(size_t reqSz, int buckSz, byte memAction, int heapType) |
| 14 | +{ |
| 15 | + switch (memAction) { |
| 16 | + case WOLFSSL_DEBUG_MEMORY_ALLOC: |
| 17 | + printf("Malloc request of %zu bytes using bucket size of %d and heap" |
| 18 | + " type of %d\n", reqSz, buckSz, heapType); |
| 19 | + break; |
| 20 | + |
| 21 | + case WOLFSSL_DEBUG_MEMORY_FAIL: |
| 22 | + printf("Failed on malloc request of %zu bytes\n", reqSz); |
| 23 | + break; |
| 24 | + |
| 25 | + case WOLFSSL_DEBUG_MEMORY_FREE: |
| 26 | + #ifdef WOLFSSL_DEBUG_MEMORY |
| 27 | + printf("Free'ing %zu bytes, bucket size used was %d bytes\n", |
| 28 | + reqSz, buckSz); |
| 29 | + #else |
| 30 | + printf("Free'ing bucket size %d bytes\n", buckSz); |
| 31 | + #endif |
| 32 | + break; |
| 33 | + |
| 34 | + case WOLFSSL_DEBUG_MEMORY_INIT: |
| 35 | + printf("Creating bucket of size %d\n", buckSz); |
| 36 | + break; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +int main(int argc, char** argv) |
| 41 | +{ |
| 42 | + int padSz; |
| 43 | + const unsigned int dist[4] = {1, 1, 2, 1}; |
| 44 | + const unsigned int buck[4] = {10, 15, 20, 25}; |
| 45 | + byte* buf; |
| 46 | + byte* testPtr[5]; |
| 47 | + int totalMem; |
| 48 | + int i; |
| 49 | + WOLFSSL_HEAP_HINT* heapHint = NULL; |
| 50 | + |
| 51 | + wolfCrypt_Init(); |
| 52 | + |
| 53 | + wolfSSL_SetDebugMemoryCb(MemoryDebug); |
| 54 | + |
| 55 | + /* get padding size per bucket */ |
| 56 | + padSz = wolfSSL_MemoryPaddingSz(); |
| 57 | + totalMem = (padSz * 5) + buck[0] + buck[1] + buck[2] + buck[2] + buck[3] |
| 58 | + + sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT) |
| 59 | + + (WOLFSSL_STATIC_ALIGN - 1); |
| 60 | + |
| 61 | + |
| 62 | + buf = (byte*)malloc(totalMem); |
| 63 | + |
| 64 | + /* divide up the buffer into individual buckets and manager */ |
| 65 | + if (wc_LoadStaticMemory_ex(&heapHint, 4, buck, dist, buf, totalMem, 0, |
| 66 | + 0) != 0) { |
| 67 | + printf("Failed to load up static memory\n"); |
| 68 | + } |
| 69 | + |
| 70 | + /* test checking out all buckets */ |
| 71 | + testPtr[0] = XMALLOC(6, heapHint, DYNAMIC_TYPE_HASHES); |
| 72 | + testPtr[1] = XMALLOC(2, heapHint, DYNAMIC_TYPE_TMP_BUFFER); |
| 73 | + testPtr[2] = XMALLOC(200, heapHint, DYNAMIC_TYPE_LOG); |
| 74 | + testPtr[3] = XMALLOC(21, heapHint, DYNAMIC_TYPE_LOG); |
| 75 | + testPtr[4] = XMALLOC(1, heapHint, DYNAMIC_TYPE_HMAC); |
| 76 | + |
| 77 | + /* free all buckets back to heap hint manager */ |
| 78 | + for (i = 0; i < sizeof(testPtr)/sizeof(byte*); i++) { |
| 79 | + XFREE(testPtr[i], heapHint, DYNAMIC_TYPE_TMP_BUFFER); |
| 80 | + } |
| 81 | + |
| 82 | + free(buf); |
| 83 | + wolfCrypt_Cleanup(); |
| 84 | + return 0; |
| 85 | +} |
| 86 | +#else |
| 87 | +int main(int argc, char** argv) |
| 88 | +{ |
| 89 | + printf("Requires WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK defined\n"); |
| 90 | + return 0; |
| 91 | +} |
| 92 | +#endif |
0 commit comments