Skip to content

Commit f36ac8c

Browse files
Merge pull request #130 from jackctj117/ssl-static-memory
Updated Static Buffer Allocation section
2 parents 15f3faa + 217c92e commit f36ac8c

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

wolfSSL/src/CTX_structure.png

17.9 KB
Loading

wolfSSL/src/chapter04.md

+39-5
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,10 @@ By default, wolfSSL assumes that the execution environment provides dynamic memo
551551

552552
Using static-buffer-allocation is equivalent in API to using dynamic memory with wolfSSL. This functional equivalency is achieved in wolfSSL by abstracting memory allocation/free into XMALLOC/XFREE function calls. Once static-buffer-allocation is set, wolfSSL will use it from then on to allocate buffers and other structures used internally. Since this feature is set for WOLFSSL_CTX, it will continue to work for the lifetime of the context object.
553553

554-
The static-buffer-allocation set in a WOLFSSL_CTX is thread-safe. Even if the same WOLFSSL_CTX is shared by different threads, buffer allocation/free is used under exclusive control inside wolfSSL.
554+
The static-buffer-allocation set in a WOLFSSL_CTX is thread-safe. Even if the same WOLFSSL_CTX is shared by different threads, buffer allocation/free is used under exclusive control inside wolfSSL. The following is a visual representation of the CTX structure, the arrows indicate passing a pointer to the heap and "..." references all structs and not to the ones listed.
555+
556+
![Alt text](CTX_structure.png)
557+
555558
In comparison to a memory pool functionality offered by an RTOS implementation, memory functionality in an RTOS will commonly suspend a thread (task) if an unused memory block cannot be found when requested until a free block becomes available. wolfSSL’s static memory functionality has no such synchronization capability.
556559

557560
### Specifying Static Buffer Use
@@ -573,7 +576,7 @@ user_settings.h
573576
  #define WOLFSSL_STATIC_MEMORY
574577
```
575578

576-
The static-buffer-allocation option is implemented by default to additionally call the standard function malloc() without returning NULL when the memory block allocated from the given buffer is exhausted. If the environment does not provide dynamic memory management functionality, a link error will occur. Therefore, also define the **WOLFSSL_NO_MALLOC** macro to disable this feature if needed:
579+
The static-buffer-allocation option is implemented by default to fall back to the standard malloc() function when a NULL heap hint is passed in. If a heap hint is passed in and the memory associated with it is exhausted, an error will occur. If the environment does not provide dynamic memory management functionality, a link error will occur. Therefore, also define the **WOLFSSL_NO_MALLOC** macro to disable this feature if needed:
577580

578581
```
579582
user_settings.h
@@ -582,6 +585,36 @@ user_settings.h
582585
#define WOLFSSL_NO_MALLOC
583586
```
584587

588+
In addition there are two build configurations. `--enable-staticmemory=small` which is a
589+
smaller version that has smaller struct sizes and less supporting API's available. The other build
590+
configuration is `--enable-staticmemory=debug` that enables the ability to set a callback function. This is
591+
useful in cases where printf() is not available for determining what is being allocated and what
592+
bucket sizes are being used. Here is what the example client output looks like with example
593+
callback:
594+
595+
```
596+
./examples/client/client
597+
...
598+
...
599+
...
600+
Free'ing : 16128
601+
OUT BUFFER: Alloc'd 6 bytes using bucket size 16992
602+
Alloc'd 848 bytes using bucket size 1024
603+
OUT BUFFER: Alloc'd 150 bytes using bucket size 16992
604+
Alloc'd 13 bytes using bucket size 64
605+
Alloc'd 12 bytes using bucket size 64
606+
Alloc'd 848 bytes using bucket size 1024
607+
IN BUFFER: Alloc'd 40 bytes using bucket size 16992
608+
Alloc'd 13 bytes using bucket size 64
609+
Alloc'd 12 bytes using bucket size 64
610+
Free'ing : 1024
611+
Free'ing : 512
612+
613+
...
614+
...
615+
...
616+
```
617+
585618
### Using Static Buffer Allocation
586619

587620
This can be helpful for environments without dynamic memory support, or safety-critical applications where dynamic memory use is disallowed.
@@ -727,9 +760,10 @@ For DTLS server
727760

728761
|API|description|
729762
|:---|:---|
730-
|`wolfSSL_CTX_load_static_memory`|Set buffer for WOLFSSL_CTX as a heap memory.
731-
|`wolfSSL_CTX_is_static_memory`|Returns whether "Static buffer Allocation" is used. If it is the case, gets usage report.
732-
|`wolfSSL_is_static_memory`|Returns whether "Static buffer Allocation" is used. If it is the case, gets usage report. |
763+
|[`wolfSSL_CTX_load_static_memory`](group__Memory.md#function-wolfSSL_CTX_static_memory)| Set buffer for WOLFSSL_CTX as a heap memory. |
764+
|[`wolfSSL_CTX_is_static_memory`](group__Memory.md#function-wolfSSL_CTX_is_static_memory)| Returns whether "Static buffer Allocation" is used. If it is the case, gets usage report. |
765+
|[`wolfSSL_is_static_memory`](group__Memory.md#function-wolfSSL_is_static_memory)| Returns whether "Static buffer Allocation" is used. If it is the case, gets usage report. |
766+
|[`wc_LoadStaticMemory`](group__Memory.md#function-wc_LoadStaticMemory)| Used to set aside static memory for wolfCrypt use. |
733767
|[`wolfSSL_StaticBufferSz`](group__Memory.md#function-wolfssl_staticbuffersz)| Calculate required buffer size for "Static buffer Allocation" based on the macros defined in /wolfssl/wolfcrypt/memory.h. |
734768

735769

0 commit comments

Comments
 (0)