Skip to content

Commit af4a649

Browse files
add size calculation example for static memory
1 parent f707ad8 commit af4a649

3 files changed

Lines changed: 138 additions & 0 deletions

File tree

staticmemory/Makefile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# ECC Examples Makefile
2+
CC = gcc
3+
WOLFSSL_INSTALL_DIR = /usr/local
4+
CFLAGS = -Wall -I$(WOLFSSL_INSTALL_DIR)/include
5+
ZLIB =
6+
#ZLIB += -lz
7+
LIBS = -L$(WOLFSSL_INSTALL_DIR)/lib -lm ${ZLIB}
8+
9+
# option variables
10+
DYN_LIB = -lwolfssl
11+
STATIC_LIB = $(WOLFSSL_INSTALL_DIR)/lib/libwolfssl.a
12+
DEBUG_FLAGS = -g -DDEBUG
13+
DEBUG_INC_PATHS = -MD
14+
OPTIMIZE = -Os
15+
16+
# Options
17+
#CFLAGS+=$(DEBUG_FLAGS)
18+
CFLAGS+=$(OPTIMIZE)
19+
#LIBS+=$(STATIC_LIB)
20+
LIBS+=$(DYN_LIB)
21+
22+
ifneq ($(PSA),)
23+
LIBS+=$(PSA_LIB)
24+
CFLAGS+=-DUSE_PSA
25+
endif
26+
27+
# build targets
28+
SRC=$(wildcard *.c)
29+
TARGETS=$(patsubst %.c, %, $(SRC))
30+
31+
.PHONY: clean all
32+
33+
all: $(TARGETS)
34+
35+
debug: CFLAGS+=$(DEBUG_FLAGS)
36+
debug: all
37+
38+
# build template
39+
%: %.c
40+
$(CC) -o $@ $< $(CFLAGS) $(LIBS)
41+
42+
clean:
43+
rm -f $(TARGETS)

staticmemory/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Examples of using wolfSSL's simple memory allocator. Enabled with --enable-staticmemory
2+
or WOLFSSL_STATIC_MEMORY when building wolfSSL. This is not a feature to reduce
3+
memory size!! It in fact uses a little more memory overhead. The staticmemory
4+
feature is used to have a simple fixed pool of memory available instead of using
5+
a systems malloc/free calls. Helpful in cases where dynamic memory is not
6+
allowed.
7+
8+
wolfSSL has support for XMALLOC_USER which could be used to instead map XMALLOC
9+
and XFREE to any desired implementation of malloc/free.

staticmemory/size-calculation.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)