From af4a6494a763493b2542ad9eb6f0c1807df49b0b Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Tue, 6 Aug 2024 16:34:35 -0600 Subject: [PATCH] add size calculation example for static memory --- staticmemory/Makefile | 43 +++++++++++++++++ staticmemory/README.md | 9 ++++ staticmemory/size-calculation.c | 86 +++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 staticmemory/Makefile create mode 100644 staticmemory/README.md create mode 100644 staticmemory/size-calculation.c diff --git a/staticmemory/Makefile b/staticmemory/Makefile new file mode 100644 index 00000000..bb3cd53c --- /dev/null +++ b/staticmemory/Makefile @@ -0,0 +1,43 @@ +# ECC Examples Makefile +CC = gcc +WOLFSSL_INSTALL_DIR = /usr/local +CFLAGS = -Wall -I$(WOLFSSL_INSTALL_DIR)/include +ZLIB = +#ZLIB += -lz +LIBS = -L$(WOLFSSL_INSTALL_DIR)/lib -lm ${ZLIB} + +# option variables +DYN_LIB = -lwolfssl +STATIC_LIB = $(WOLFSSL_INSTALL_DIR)/lib/libwolfssl.a +DEBUG_FLAGS = -g -DDEBUG +DEBUG_INC_PATHS = -MD +OPTIMIZE = -Os + +# Options +#CFLAGS+=$(DEBUG_FLAGS) +CFLAGS+=$(OPTIMIZE) +#LIBS+=$(STATIC_LIB) +LIBS+=$(DYN_LIB) + +ifneq ($(PSA),) +LIBS+=$(PSA_LIB) +CFLAGS+=-DUSE_PSA +endif + +# build targets +SRC=$(wildcard *.c) +TARGETS=$(patsubst %.c, %, $(SRC)) + +.PHONY: clean all + +all: $(TARGETS) + +debug: CFLAGS+=$(DEBUG_FLAGS) +debug: all + +# build template +%: %.c + $(CC) -o $@ $< $(CFLAGS) $(LIBS) + +clean: + rm -f $(TARGETS) diff --git a/staticmemory/README.md b/staticmemory/README.md new file mode 100644 index 00000000..b5efb096 --- /dev/null +++ b/staticmemory/README.md @@ -0,0 +1,9 @@ +Examples of using wolfSSL's simple memory allocator. Enabled with --enable-staticmemory +or WOLFSSL_STATIC_MEMORY when building wolfSSL. This is not a feature to reduce +memory size!! It in fact uses a little more memory overhead. The staticmemory +feature is used to have a simple fixed pool of memory available instead of using +a systems malloc/free calls. Helpful in cases where dynamic memory is not +allowed. + +wolfSSL has support for XMALLOC_USER which could be used to instead map XMALLOC +and XFREE to any desired implementation of malloc/free. diff --git a/staticmemory/size-calculation.c b/staticmemory/size-calculation.c new file mode 100644 index 00000000..2762de97 --- /dev/null +++ b/staticmemory/size-calculation.c @@ -0,0 +1,86 @@ +#include +#include + +#include +#include +#include + +#ifndef WOLFSSL_STATIC_MEMORY + #error requires --enable-staticmemory +#endif + +int main(int argc, char** argv) +{ + int padSz; + const unsigned int dist[4] = {1, 1, 2, 1}; + const unsigned int buck[4] = {10, 15, 20, 25}; + byte* buf; + byte* testPtr[5]; + int buckTotalMem = 0; /* sum of bucket sizes */ + int totalMem; /* sum of buckets and padding */ + int totalMemAll; /* sum of buckets, padding, and managing structs */ + int i, k = 0; + WOLFSSL_HEAP_HINT* heapHint = NULL; + + wolfCrypt_Init(); + + /* get padding size per bucket */ + padSz = wolfSSL_MemoryPaddingSz(); + printf("Padding size per bucket is %d\n", padSz); + + /* calculate total size needed for individual bucket structs */ + padSz = padSz * 5; + printf("Total padding needed for 5 buckets would be %d\n", padSz); + + /* sum of all bucket sizes */ + buckTotalMem = buck[0] + buck[1] + buck[2] + buck[2] + buck[3]; + printf("Total memory in buckets is %d\n", buckTotalMem); + + /* adding sum of bucket sizes with struct padding size */ + totalMem = buckTotalMem + padSz; + printf("Calculated total memory for buckets is %d\n", totalMem); + + /* adding in size of over all managing structs used */ + totalMemAll = totalMem + sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT); + + /* account for max size needed for alignment of pointer */ + totalMemAll += WOLFSSL_STATIC_ALIGN - 1; + + printf("Calculated total memory for buckets, heap hint struct and " + "alignment is %d\n", totalMemAll); + + buf = (byte*)malloc(totalMemAll); + printf("Return of static buffer sz is %d\n", + wolfSSL_StaticBufferSz_ex(4, buck, dist, buf, totalMem, 0)); + printf("\n"); + + /* divide up the buffer into individual buckets and manager */ + if (wc_LoadStaticMemory_ex(&heapHint, 4, buck, dist, buf, totalMemAll, 0, + 0) != 0) { + printf("Failed to load up static memory\n"); + } + + /* test checking out all buckets */ + for (i = 0; i < sizeof(testPtr)/sizeof(byte*); i++) { + printf("Checking out bucket %d size of %d...", i, buck[k]); + testPtr[i] = (byte*)XMALLOC(buck[k], heapHint, DYNAMIC_TYPE_TMP_BUFFER); + if (testPtr[i] == NULL) { + printf("fail\n"); + } + else { + printf("ok\n"); + } + + k++; + if (i == 2) k--; + } + + /* free all buckets back to heap hint manager */ + for (i = 0; i < sizeof(testPtr)/sizeof(byte*); i++) { + XFREE(testPtr[i], heapHint, DYNAMIC_TYPE_TMP_BUFFER); + } + + free(buf); + wolfCrypt_Cleanup(); + return 0; +}