2323#include <stdlib.h>
2424#include <string.h>
2525#include <ctype.h>
26- #include <limits.h> // Required for INT_MAX
26+ #include <limits.h> /* Required for INT_MAX */
2727
2828#include <wolfssl/options.h>
2929#include <wolfssl/wolfcrypt/memory.h>
4040/* Linked list node for allocation events */
4141typedef struct AllocationEventNode {
4242 int size ;
43- int timestamp ; // Simple counter for allocation order
44- int active ; // 1 if allocated, 0 if freed
43+ int timestamp ; /* Simple counter for allocation order */
44+ int active ; /* 1 if allocated, 0 if freed */
4545 struct AllocationEventNode * next ;
4646} AllocationEventNode ;
4747AllocationEventNode * event_head = NULL ;
@@ -51,7 +51,7 @@ typedef struct AllocSizeNode {
5151 int size ;
5252 int count ;
5353 int concurrent ;
54- int max_concurrent ; // Maximum number of concurrent allocations of this size
54+ int max_concurrent ; /* Maximum number of concurrent allocations of this size */
5555 struct AllocSizeNode * next ; /* next in list of sizes sorted by size */
5656 struct AllocSizeNode * nextFreq ; /* sorted by count size descending */
5757} AllocSizeNode ;
@@ -70,8 +70,12 @@ typedef struct {
7070} AllocSizeList ;
7171
7272/* Helper functions for linked lists */
73- AllocationEventNode * create_allocation_event_node (int size , int timestamp , int active ) {
74- AllocationEventNode * node = (AllocationEventNode * )malloc (sizeof (AllocationEventNode ));
73+ AllocationEventNode * create_allocation_event_node (int size , int timestamp ,
74+ int active )
75+ {
76+ AllocationEventNode * node ;
77+
78+ node = (AllocationEventNode * )malloc (sizeof (AllocationEventNode ));
7579 if (node ) {
7680 node -> size = size ;
7781 node -> timestamp = timestamp ;
@@ -81,21 +85,28 @@ AllocationEventNode* create_allocation_event_node(int size, int timestamp, int a
8185 return node ;
8286}
8387
84- void add_allocation_event (AllocationEventNode * * list , int size , int timestamp , int active ) {
85- AllocationEventNode * node = create_allocation_event_node (size , timestamp , active );
88+ void add_allocation_event (AllocationEventNode * * list , int size , int timestamp ,
89+ int active )
90+ {
91+ AllocationEventNode * node ;
92+
93+ node = create_allocation_event_node (size , timestamp , active );
8694 if (node ) {
8795 if (* list == NULL ) {
8896 event_head = node ;
8997 * list = node ;
9098 } else {
9199 (* list )-> next = node ;
92- * list = node ; // Update the list pointer to point to the new node
100+ * list = node ; /* Update the list pointer to point to the new node */
93101 }
94102 }
95103}
96104
97- AllocSizeNode * create_alloc_size_node (int size ) {
98- AllocSizeNode * node = (AllocSizeNode * )malloc (sizeof (AllocSizeNode ));
105+ AllocSizeNode * create_alloc_size_node (int size )
106+ {
107+ AllocSizeNode * node ;
108+
109+ node = (AllocSizeNode * )malloc (sizeof (AllocSizeNode ));
99110 if (node ) {
100111 node -> size = size ;
101112 node -> count = 0 ;
@@ -107,9 +118,11 @@ AllocSizeNode* create_alloc_size_node(int size) {
107118 return node ;
108119}
109120
110- AllocSizeNode * find_or_create_alloc_size (AllocSizeNode * * list , int size ) {
121+ AllocSizeNode * find_or_create_alloc_size (AllocSizeNode * * list , int size )
122+ {
111123 AllocSizeNode * current = * list ;
112124 AllocSizeNode * previous = NULL ;
125+ AllocSizeNode * node = NULL ;
113126
114127 /* Look for existing size */
115128 while (current ) {
@@ -120,7 +133,7 @@ AllocSizeNode* find_or_create_alloc_size(AllocSizeNode** list, int size) {
120133 }
121134
122135 /* Create new size node */
123- AllocSizeNode * node = create_alloc_size_node (size );
136+ node = create_alloc_size_node (size );
124137 if (node ) {
125138 /* insert node into list ordered from largest size first to smallest */
126139 current = * list ;
@@ -151,7 +164,8 @@ AllocSizeNode* find_or_create_alloc_size(AllocSizeNode** list, int size) {
151164 return node ;
152165}
153166
154- void free_allocation_event_list (AllocationEventNode * list ) {
167+ void free_allocation_event_list (AllocationEventNode * list )
168+ {
155169 AllocationEventNode * current = list ;
156170 while (current ) {
157171 AllocationEventNode * next = current -> next ;
@@ -160,7 +174,8 @@ void free_allocation_event_list(AllocationEventNode* list) {
160174 }
161175}
162176
163- void free_alloc_size_list (AllocSizeNode * list ) {
177+ void free_alloc_size_list (AllocSizeNode * list )
178+ {
164179 AllocSizeNode * current = list ;
165180 while (current ) {
166181 AllocSizeNode * next = current -> next ;
@@ -170,12 +185,14 @@ void free_alloc_size_list(AllocSizeNode* list) {
170185}
171186
172187/* Function to calculate memory padding size per bucket */
173- int calculate_padding_size () {
188+ int calculate_padding_size ()
189+ {
174190 return wolfSSL_MemoryPaddingSz ();
175191}
176192
177193/* Function to calculate total memory overhead */
178- int calculate_total_overhead (int num_buckets ) {
194+ int calculate_total_overhead (int num_buckets )
195+ {
179196 /* Total overhead includes:
180197 * - WOLFSSL_HEAP structure
181198 * - WOLFSSL_HEAP_HINT structure
@@ -194,23 +211,25 @@ int parse_memory_logs(const char* filename, AllocationEventNode** events,
194211 int * peak_heap_usage , int * buckets )
195212{
196213 int current_heap_usage = 0 ;
197- FILE * file = fopen (filename , "r" );
214+ char line [MAX_LINE_LENGTH ];
215+ int timestamp = 0 ;
216+ FILE * file ;
217+
218+ file = fopen (filename , "r" );
198219 if (!file ) {
199220 printf ("Error: Could not open file %s\n" , filename );
200221 return -1 ;
201222 }
202-
203- char line [MAX_LINE_LENGTH ];
204- int timestamp = 0 ;
223+
205224 * peak_heap_usage = 0 ; /* Initialize peak heap usage */
206225
207226 while (fgets (line , sizeof (line ), file )) {
208227 /* Look for lines containing "Alloc:" or "Free:" */
209228 char * alloc_pos = strstr (line , "Alloc:" );
210229 char * free_pos = strstr (line , "Free:" );
230+ int size ;
211231
212232 if (alloc_pos ) {
213- int size ;
214233 /* Handle multiple formats:
215234 * Format 1: Alloc: 0x55fde046b490 -> 4 (11) at wolfTLSv1_3_client_method_ex:src/tls.c:16714
216235 * Format 2: [HEAP 0x1010e2110] Alloc: 0x101108a40 -> 1024 at simple_mem_test:18561
@@ -232,8 +251,8 @@ int parse_memory_logs(const char* filename, AllocationEventNode** events,
232251 }
233252 add_allocation_event (events , size , timestamp ++ , 1 );
234253 }
235- } else if ( free_pos ) {
236- int size ;
254+ }
255+ else if ( free_pos ) {
237256 /* Handle multiple formats:
238257 * Format 1: Free: 0x55fde046b490 -> 4 at wolfTLSv1_3_client_method_ex:src/tls.c:16714
239258 * Format 2: [HEAP 0x1010e2110] Free: 0x101108a40 -> 1024 at simple_mem_test:18576
@@ -261,15 +280,17 @@ static void find_max_concurent_allocations(AllocSizeNode** alloc_sizes)
261280 AllocationEventNode * current = event_head ;
262281 while (current != NULL ) {
263282 if (current -> active ) {
264- AllocSizeNode * alloc_size = find_or_create_alloc_size (alloc_sizes , current -> size );
283+ AllocSizeNode * alloc_size =
284+ find_or_create_alloc_size (alloc_sizes , current -> size );
265285 alloc_size -> concurrent ++ ;
266286 alloc_size -> count ++ ;
267287 if (alloc_size -> max_concurrent < alloc_size -> concurrent ) {
268288 alloc_size -> max_concurrent = alloc_size -> concurrent ;
269289 }
270290 }
271291 else {
272- AllocSizeNode * alloc_size = find_or_create_alloc_size (alloc_sizes , current -> size );
292+ AllocSizeNode * alloc_size =
293+ find_or_create_alloc_size (alloc_sizes , current -> size );
273294 alloc_size -> concurrent -- ;
274295 }
275296 current = current -> next ;
@@ -333,21 +354,22 @@ static void sort_alloc_by_frequency(AllocSizeNode* alloc_sizes,
333354 int current_count = 0 ;
334355 int current_upper_bound = INT_MAX ;
335356
336- * sorted = NULL ; // Initialize to NULL
357+ * sorted = NULL ; /* Initialize to NULL */
337358
338359 do {
339360 max = NULL ;
340- current = alloc_sizes ; // Reset current to beginning of list
361+ current = alloc_sizes ; /* Reset current to beginning of list */
341362 while (current != NULL ) {
342- if (current -> count > current_count && current -> size < current_upper_bound ) {
363+ if (current -> count > current_count &&
364+ current -> size < current_upper_bound ) {
343365 current_count = current -> count ;
344366 max = current ;
345367 }
346368 current = current -> next ;
347369 }
348370
349371 if (max == NULL ) {
350- break ; // No more nodes to process
372+ break ; /* No more nodes to process */
351373 }
352374
353375 current_upper_bound = max -> size ;
@@ -384,34 +406,13 @@ static int get_bucket_size(int size)
384406 * - This reduces bucket management overhead when waste is minimal
385407 * - Limited to MAX_UNIQUE_BUCKETS total unique bucket sizes
386408 */
387- void optimize_buckets (AllocSizeNode * alloc_sizes , AllocSizeNode * alloc_sizes_by_freq ,
388- int num_sizes , int * buckets , int * dist , int * num_buckets )
409+ void optimize_buckets (AllocSizeNode * alloc_sizes ,
410+ AllocSizeNode * alloc_sizes_by_freq , int num_sizes , int * buckets , int * dist ,
411+ int * num_buckets )
389412{
390413 int i , j ;
391414 AllocSizeNode * current ;
392415
393- /* Calculate total allocations and find significant sizes */
394- int total_allocations = 0 ;
395- current = alloc_sizes ;
396- while (current != NULL ) {
397- total_allocations += current -> count ;
398- current = current -> next ;
399- }
400-
401- /* Determine significant allocation sizes (those that represent >1% of total allocations) */
402- int significant_sizes [MAX_UNIQUE_BUCKETS ];
403- int num_significant = 0 ;
404- int min_threshold = total_allocations / 100 ; /* 1% threshold */
405-
406- /* Populate significant sizes from alloc_sizes */
407- current = alloc_sizes ;
408- while (current != NULL && num_significant < MAX_UNIQUE_BUCKETS ) {
409- if (current -> count >= min_threshold ) {
410- significant_sizes [num_significant ++ ] = current -> size ;
411- }
412- current = current -> next ;
413- }
414-
415416 /* Initialize bucket count */
416417 * num_buckets = 0 ;
417418
@@ -435,7 +436,6 @@ void optimize_buckets(AllocSizeNode* alloc_sizes, AllocSizeNode* alloc_sizes_by_
435436 /* Skip if already included */
436437 int already_included = 0 ;
437438 for (j = 0 ; j < * num_buckets ; j ++ ) {
438- /* Compare original allocation sizes, not bucket sizes with padding */
439439 if (buckets [j ] == get_bucket_size (current -> size )) {
440440 already_included = 1 ;
441441 break ;
@@ -474,7 +474,6 @@ void optimize_buckets(AllocSizeNode* alloc_sizes, AllocSizeNode* alloc_sizes_by_
474474 }
475475 }
476476 }
477-
478477 set_distributions (buckets , dist , * num_buckets );
479478
480479 /* Print optimization summary */
@@ -530,7 +529,8 @@ void calculate_memory_efficiency(AllocSizeNode* alloc_sizes, int num_sizes,
530529 allocations_handled += count ;
531530 total_waste += (float )min_waste * count ;
532531 printf ("%-7d %-7d %-10d %-7d %-7d %s\n" ,
533- size , count , current -> max_concurrent , buckets [best_bucket ], min_waste , "✓" );
532+ size , count , current -> max_concurrent , buckets [best_bucket ],
533+ min_waste , "✓" );
534534 } else {
535535 printf ("%-7d %-7d %-10d %-7s %-7s %s\n" ,
536536 size , count , current -> max_concurrent , "N/A" , "N/A" , "✗" );
@@ -541,7 +541,8 @@ void calculate_memory_efficiency(AllocSizeNode* alloc_sizes, int num_sizes,
541541 printf ("\nEfficiency Summary:\n" );
542542 printf ("Total allocations: %d\n" , total_allocations );
543543 printf ("Allocations handled: %d (%.1f%%)\n" ,
544- allocations_handled , (float )allocations_handled * 100 / total_allocations );
544+ allocations_handled ,
545+ (float )allocations_handled * 100 / total_allocations );
545546 printf ("Total memory waste: %.2f bytes\n" , total_waste );
546547 printf ("Average waste per allocation: %.2f bytes\n" ,
547548 total_waste / total_allocations );
@@ -601,7 +602,8 @@ void print_buffer_recommendations(int* buckets, int* dist, int num_buckets)
601602 printf ("byte staticBuffer[%d];\n" , total_memory_needed );
602603 printf ("\n// Load static memory\n" );
603604 printf ("WOLFSSL_HEAP_HINT* heapHint = NULL;\n" );
604- printf ("if (wc_LoadStaticMemory_ex(&heapHint, %d, bucket_sizes, bucket_dist,\n" , num_buckets );
605+ printf ("if (wc_LoadStaticMemory_ex(&heapHint, %d, bucket_sizes, bucket_dist,\n" ,
606+ num_buckets );
605607 printf (" staticBuffer, %d, 0, 0) != 0) {\n" , total_memory_needed );
606608 printf (" // Handle error\n" );
607609 printf ("}\n" );
@@ -647,21 +649,24 @@ int main(int argc, char** argv)
647649 }
648650
649651 printf ("Found %d unique allocation sizes\n" , num_sizes );
650- printf ("Peak heap usage: %d bytes (maximum concurrent memory usage)\n\n" , peak_heap_usage );
652+ printf ("Peak heap usage: %d bytes (maximum concurrent memory usage)\n\n" ,
653+ peak_heap_usage );
651654
652655 /* Print allocation sizes, frequencies, and concurrent usage */
653656 printf ("Allocation Sizes, Frequencies, and Concurrent Usage:\n" );
654657 printf ("Size Count Max Concurrent\n" );
655658 printf ("---- ----- --------------\n" );
656659 current = alloc_sizes ;
657660 while (current != NULL ) {
658- printf ("%-7d %-7d %d\n" , current -> size , current -> count , current -> max_concurrent );
661+ printf ("%-7d %-7d %d\n" , current -> size , current -> count ,
662+ current -> max_concurrent );
659663 current = current -> next ;
660664 }
661665 printf ("\n" );
662666
663667 /* Optimize bucket sizes */
664- optimize_buckets (alloc_sizes , alloc_sizes_by_freq , num_sizes , buckets , dist , & num_buckets );
668+ optimize_buckets (alloc_sizes , alloc_sizes_by_freq , num_sizes , buckets , dist ,
669+ & num_buckets );
665670
666671 /* Print optimized bucket sizes and distribution */
667672 printf ("Optimized Bucket Sizes and Distribution:\n" );
0 commit comments