add size calculation example for static memory
parent
f707ad83da
commit
af4a6494a7
|
@ -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)
|
|
@ -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.
|
|
@ -0,0 +1,86 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <wolfssl/options.h>
|
||||
#include <wolfssl/wolfcrypt/wc_port.h>
|
||||
#include <wolfssl/wolfcrypt/memory.h>
|
||||
|
||||
#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;
|
||||
}
|
Loading…
Reference in New Issue