|
| 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 | +int main(int argc, char** argv) |
| 13 | +{ |
| 14 | + int padSz; |
| 15 | + const unsigned int dist[4] = {1, 1, 2, 1}; |
| 16 | + const unsigned int buck[4] = {10, 15, 20, 25}; |
| 17 | + byte* buf; |
| 18 | + byte* testPtr[5]; |
| 19 | + int buckTotalMem = 0; /* sum of bucket sizes */ |
| 20 | + int totalMem; /* sum of buckets and padding */ |
| 21 | + int totalMemAll; /* sum of buckets, padding, and managing structs */ |
| 22 | + int i, k = 0; |
| 23 | + WOLFSSL_HEAP_HINT* heapHint = NULL; |
| 24 | + |
| 25 | + wolfCrypt_Init(); |
| 26 | + |
| 27 | + /* get padding size per bucket */ |
| 28 | + padSz = wolfSSL_MemoryPaddingSz(); |
| 29 | + printf("Padding size per bucket is %d\n", padSz); |
| 30 | + |
| 31 | + /* calculate total size needed for individual bucket structs */ |
| 32 | + padSz = padSz * 5; |
| 33 | + printf("Total padding needed for 5 buckets would be %d\n", padSz); |
| 34 | + |
| 35 | + /* sum of all bucket sizes */ |
| 36 | + buckTotalMem = buck[0] + buck[1] + buck[2] + buck[2] + buck[3]; |
| 37 | + printf("Total memory in buckets is %d\n", buckTotalMem); |
| 38 | + |
| 39 | + /* adding sum of bucket sizes with struct padding size */ |
| 40 | + totalMem = buckTotalMem + padSz; |
| 41 | + printf("Calculated total memory for buckets is %d\n", totalMem); |
| 42 | + |
| 43 | + /* adding in size of over all managing structs used */ |
| 44 | + totalMemAll = totalMem + sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT); |
| 45 | + |
| 46 | + /* account for max size needed for alignment of pointer */ |
| 47 | + totalMemAll += WOLFSSL_STATIC_ALIGN - 1; |
| 48 | + |
| 49 | + printf("Calculated total memory for buckets, heap hint struct and " |
| 50 | + "alignment is %d\n", totalMemAll); |
| 51 | + |
| 52 | + buf = (byte*)malloc(totalMemAll); |
| 53 | + printf("Return of static buffer sz is %d\n", |
| 54 | + wolfSSL_StaticBufferSz_ex(4, buck, dist, buf, totalMem, 0)); |
| 55 | + printf("\n"); |
| 56 | + |
| 57 | + /* divide up the buffer into individual buckets and manager */ |
| 58 | + if (wc_LoadStaticMemory_ex(&heapHint, 4, buck, dist, buf, totalMemAll, 0, |
| 59 | + 0) != 0) { |
| 60 | + printf("Failed to load up static memory\n"); |
| 61 | + } |
| 62 | + |
| 63 | + /* test checking out all buckets */ |
| 64 | + for (i = 0; i < sizeof(testPtr)/sizeof(byte*); i++) { |
| 65 | + printf("Checking out bucket %d size of %d...", i, buck[k]); |
| 66 | + testPtr[i] = (byte*)XMALLOC(buck[k], heapHint, DYNAMIC_TYPE_TMP_BUFFER); |
| 67 | + if (testPtr[i] == NULL) { |
| 68 | + printf("fail\n"); |
| 69 | + } |
| 70 | + else { |
| 71 | + printf("ok\n"); |
| 72 | + } |
| 73 | + |
| 74 | + k++; |
| 75 | + if (i == 2) k--; |
| 76 | + } |
| 77 | + |
| 78 | + /* free all buckets back to heap hint manager */ |
| 79 | + for (i = 0; i < sizeof(testPtr)/sizeof(byte*); i++) { |
| 80 | + XFREE(testPtr[i], heapHint, DYNAMIC_TYPE_TMP_BUFFER); |
| 81 | + } |
| 82 | + |
| 83 | + free(buf); |
| 84 | + wolfCrypt_Cleanup(); |
| 85 | + return 0; |
| 86 | +} |
0 commit comments