You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After this, when you are done using the WOLFSSL_CTX structure, free it with the usual wolfSSL_CTX_free().
691
691
692
+
### Using Static Memory Without TLS (Crypto-Only)
693
+
694
+
wolfSSL also supports using static memory allocation for wolfCrypt operations without requiring TLS functionality. This is useful for applications that only need cryptographic operations (hashing, encryption, random number generation, etc.) without the overhead of TLS connections.
695
+
696
+
The `wc_LoadStaticMemory()` function is used to set aside static memory for wolfCrypt use. Memory can be used by passing the created heap hint into functions that support it. An example of this is when calling `wc_InitRng_ex()`.
697
+
698
+
**Example:**
699
+
```c
700
+
WOLFSSL_HEAP_HINT hint;
701
+
int ret;
702
+
unsignedchar memory[MAX];
703
+
int memorySz = MAX;
704
+
int flag = WOLFMEM_GENERAL;
705
+
706
+
// load in memory for use
707
+
ret = wc_LoadStaticMemory(&hint, memory, memorySz, flag, 0);
708
+
if (ret != SSL_SUCCESS) {
709
+
// handle error case
710
+
}
711
+
712
+
ret = wc_InitRng_ex(&rng, hint, 0);
713
+
// check ret value
714
+
```
715
+
716
+
**Example with Global Heap Hint:**
717
+
```c
718
+
WOLFSSL_HEAP_HINT hint;
719
+
void* oldHint;
720
+
int ret;
721
+
unsignedchar memory[MAX];
722
+
723
+
// Set up static memory
724
+
ret = wc_LoadStaticMemory(&hint, memory, MAX, WOLFMEM_GENERAL, 0);
725
+
if (ret != SSL_SUCCESS) {
726
+
// handle error case
727
+
}
728
+
729
+
// Set as global heap hint (not thread safe)
730
+
oldHint = wolfSSL_SetGlobalHeapHint(hint);
731
+
732
+
// Now all NULL heap hint calls will use this global hint
733
+
// ... use wolfSSL functions ...
734
+
735
+
// Restore previous global heap hint when done
736
+
wolfSSL_SetGlobalHeapHint(oldHint);
737
+
wc_UnloadStaticMemory(hint);
738
+
```
739
+
740
+
741
+
**Example with Custom Buffer Sizes:**
742
+
```c
743
+
WOLFSSL_HEAP_HINT hint;
744
+
word32 customSizes[] = {64, 128, 256, 512, 1024};
745
+
word32 customDist[] = {10, 5, 3, 2, 1};
746
+
int requiredSize, ret;
747
+
unsigned char memory[MAX];
748
+
749
+
// Calculate required buffer size for custom configuration
// Load static memory with custom bucket configuration
757
+
ret = wc_LoadStaticMemory_ex(&hint, 5, customSizes, customDist,
758
+
memory, requiredSize, WOLFMEM_GENERAL, 0);
759
+
if (ret != SSL_SUCCESS) {
760
+
// handle error case
761
+
}
762
+
763
+
// Use the custom-configured static memory
764
+
// ... use wolfSSL functions with hint ...
765
+
766
+
// Clean up when done
767
+
wc_UnloadStaticMemory(hint);
768
+
}
769
+
770
+
// Calculate padding overhead
771
+
int paddingSize = wolfSSL_MemoryPaddingSz();
772
+
printf("Memory padding per bucket: %d bytes\n", paddingSize);
773
+
```
774
+
692
775
#### Setting a Global Heap Hint
693
776
694
777
A global heap hint can be set using the API `void* wolfSSL_SetGlobalHeapHint(void* heap)`.
@@ -703,6 +786,41 @@ global heap hint set.
703
786
704
787
This functionality was added in versions of wolfSSL after version 5.7.0.
705
788
789
+
#### Debug Memory Callback
790
+
791
+
When using static memory allocation with the `WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK` build option, you can set a debug callback function using `void wolfSSL_SetDebugMemoryCb(DebugMemoryCb cb)`. This callback is called during memory allocation and deallocation operations to provide debugging information about memory usage.
792
+
793
+
The callback function signature is:
794
+
```c
795
+
typedefvoid (*DebugMemoryCb)(size_t sz, int bucketSz, byte st, int type);
796
+
```
797
+
798
+
Where:
799
+
- `sz` is the requested size
800
+
- `bucketSz` is the bucket size used
801
+
- `st` is the status (0=alloc, 1=fail, 2=free, 3=init)
802
+
- `type` is the memory type
803
+
804
+
#### Unloading Static Memory
805
+
806
+
When you're done using static memory allocation, you should call `void wc_UnloadStaticMemory(WOLFSSL_HEAP_HINT* heap)` to properly free the static memory heap and associated mutex. This ensures proper cleanup of resources.
807
+
808
+
#### Advanced Static Buffer Size Calculation
809
+
810
+
The function `int wolfSSL_StaticBufferSz_ex(unsigned int listSz, const word32 *sizeList, const word32 *distList, byte* buffer, word32 sz, int flag)` allows you to calculate the required buffer size for static memory allocation with custom bucket sizes and distributions. This is useful when you want to optimize memory usage for your specific application needs.
811
+
812
+
Parameters:
813
+
- `listSz`: Number of bucket sizes in the list
814
+
- `sizeList`: Array of bucket sizes
815
+
- `distList`: Array of distribution counts for each bucket size
816
+
- `buffer`: Buffer to test (can be NULL for calculation only)
817
+
- `sz`: Size of the buffer
818
+
- `flag`: Memory flag (WOLFMEM_GENERAL, WOLFMEM_IO_POOL, etc.)
819
+
820
+
#### Memory Padding Calculation
821
+
822
+
The function `int wolfSSL_MemoryPaddingSz(void)` calculates the size of management memory needed for each memory bucket, including alignment padding. This is useful for understanding the overhead of static memory allocation and for precise memory planning.
823
+
706
824
### Adjustment of Static Buffer Allocation
707
825
708
826
The static-buffer-allocation option provided by wolfSSL manages the specified buffer by dividing it into multiple areas called "buckets" as shown in the following diagram. Multiple memory blocks of the same size are linked within a bucket. The figure below omits the structure that manages the memory block, but a buffer with a size that includes the omitted structure is required.
@@ -775,11 +893,14 @@ For DTLS server:
775
893
|[`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. |
776
894
|[`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. |
777
895
|[`wc_LoadStaticMemory`](group__Memory.md#function-wc_LoadStaticMemory)| Used to set aside static memory for wolfCrypt use. |
896
+
|[`wc_LoadStaticMemory_ex`](group__Memory.md#function-wc_LoadStaticMemory_ex)| Used to set aside static memory for wolfCrypt use with custom bucket sizes and distributions. |
897
+
|[`wolfSSL_SetGlobalHeapHint`](group__Memory.md#function-wolfSSL_SetGlobalHeapHint)| Set a global heap hint that will be used when NULL heap hint is passed to memory allocation functions. Returns the previous global heap hint. |
898
+
|[`wolfSSL_GetGlobalHeapHint`](group__Memory.md#function-wolfSSL_GetGlobalHeapHint)| Get the current global heap hint that is used when NULL heap hint is passed to memory allocation functions. |
899
+
|[`wolfSSL_SetDebugMemoryCb`](group__Memory.md#function-wolfSSL_SetDebugMemoryCb)| Set a debug callback function for static memory allocation tracking. Used with WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK build option. |
900
+
|[`wc_UnloadStaticMemory`](group__Memory.md#function-wc_UnloadStaticMemory)| Free static memory heap and associated mutex. Should be called when done using static memory allocation. |
778
901
|[`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. |
779
-
780
-
781
-
782
-
902
+
|[`wolfSSL_StaticBufferSz_ex`](group__Memory.md#function-wolfSSL_StaticBufferSz_ex)| Calculate required buffer size for static memory allocation with custom bucket sizes and distributions. |
903
+
|[`wolfSSL_MemoryPaddingSz`](group__Memory.md#function-wolfSSL_MemoryPaddingSz)| Calculate the size of management memory needed for each memory bucket, including alignment padding. |
0 commit comments