Skip to content

Commit 607588c

Browse files
authored
Merge pull request #449 from JacobBarthelmeh/staticmemory
add size calculation example for static memory
2 parents f707ad8 + 0594daf commit 607588c

4 files changed

Lines changed: 225 additions & 0 deletions

File tree

staticmemory/Makefile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
# build targets
23+
SRC=$(wildcard *.c)
24+
TARGETS=$(patsubst %.c, %, $(SRC))
25+
26+
.PHONY: clean all
27+
28+
all: $(TARGETS)
29+
30+
debug: CFLAGS+=$(DEBUG_FLAGS)
31+
debug: all
32+
33+
# build template
34+
%: %.c
35+
$(CC) -o $@ $< $(CFLAGS) $(LIBS)
36+
37+
clean:
38+
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.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

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)