37
37
* Default: 9 buckets (can be adjusted based on memory constraints)
38
38
*/
39
39
40
- /* Memory overhead constants - these should match wolfSSL's actual values */
41
- #define WOLFSSL_HEAP_SIZE 64 /* Approximate size of WOLFSSL_HEAP structure */
42
- #define WOLFSSL_HEAP_HINT_SIZE 32 /* Approximate size of WOLFSSL_HEAP_HINT structure */
43
-
44
40
/* Linked list node for allocation events */
45
41
typedef struct AllocationEventNode {
46
42
int size ;
@@ -178,11 +174,6 @@ int calculate_padding_size() {
178
174
return wolfSSL_MemoryPaddingSz ();
179
175
}
180
176
181
- /* Function to calculate bucket size including padding */
182
- int calculate_bucket_size_with_padding (int allocation_size ) {
183
- return allocation_size + calculate_padding_size ();
184
- }
185
-
186
177
/* Function to calculate total memory overhead */
187
178
int calculate_total_overhead (int num_buckets ) {
188
179
/* Total overhead includes:
@@ -191,8 +182,8 @@ int calculate_total_overhead(int num_buckets) {
191
182
* - Alignment padding
192
183
* Note: Padding is already included in bucket sizes
193
184
*/
194
- int total_overhead = WOLFSSL_HEAP_SIZE +
195
- WOLFSSL_HEAP_HINT_SIZE +
185
+ int total_overhead = sizeof ( WOLFSSL_HEAP ) +
186
+ sizeof ( WOLFSSL_HEAP_HINT ) +
196
187
(WOLFSSL_STATIC_ALIGN - 1 );
197
188
total_overhead += num_buckets * wolfSSL_MemoryPaddingSz ();
198
189
return total_overhead ;
@@ -372,6 +363,15 @@ static void sort_alloc_by_frequency(AllocSizeNode* alloc_sizes,
372
363
} while (max != NULL );
373
364
}
374
365
366
+ /* returns what the bucket size would be */
367
+ static int get_bucket_size (int size )
368
+ {
369
+ int padding ;
370
+
371
+ padding = size % WOLFSSL_STATIC_ALIGN ;
372
+ return size + (WOLFSSL_STATIC_ALIGN - padding ) + wolfSSL_MemoryPaddingSz ();
373
+ }
374
+
375
375
/* Function to optimize bucket sizes */
376
376
/*
377
377
* Optimization heuristic:
@@ -415,7 +415,7 @@ void optimize_buckets(AllocSizeNode* alloc_sizes, AllocSizeNode* alloc_sizes_by_
415
415
/* Always include the largest allocation sizes (with padding) */
416
416
current = alloc_sizes ;
417
417
for (i = 0 ; i < MAX_UNIQUE_BUCKETS /2 && current != NULL ; i ++ ) {
418
- buckets [* num_buckets ] = calculate_bucket_size_with_padding (current -> size );
418
+ buckets [* num_buckets ] = get_bucket_size (current -> size );
419
419
dist [* num_buckets ] = current -> max_concurrent ;
420
420
(* num_buckets )++ ;
421
421
current = current -> next ;
@@ -433,8 +433,7 @@ void optimize_buckets(AllocSizeNode* alloc_sizes, AllocSizeNode* alloc_sizes_by_
433
433
int already_included = 0 ;
434
434
for (j = 0 ; j < * num_buckets ; j ++ ) {
435
435
/* Compare original allocation sizes, not bucket sizes with padding */
436
- int bucket_data_size = buckets [j ] - calculate_padding_size ();
437
- if (bucket_data_size == current -> size ) {
436
+ if (buckets [j ] == get_bucket_size (current -> size )) {
438
437
already_included = 1 ;
439
438
break ;
440
439
}
@@ -447,7 +446,7 @@ void optimize_buckets(AllocSizeNode* alloc_sizes, AllocSizeNode* alloc_sizes_by_
447
446
current = current -> next ;
448
447
}
449
448
if (max != NULL ) {
450
- buckets [* num_buckets ] = calculate_bucket_size_with_padding (max -> size );
449
+ buckets [* num_buckets ] = get_bucket_size (max -> size );
451
450
dist [* num_buckets ] = max -> max_concurrent ;
452
451
* num_buckets += 1 ;
453
452
}
@@ -558,9 +557,10 @@ void calculate_memory_efficiency(AllocSizeNode* alloc_sizes, int num_sizes,
558
557
559
558
printf ("Total bucket memory: %d bytes\n" , total_bucket_memory );
560
559
printf ("Memory overhead: %d bytes\n" , total_overhead );
561
- printf (" - Padding per bucket: %d bytes (included in bucket sizes)\n" , calculate_padding_size ());
562
- printf (" - Total padding: %d bytes (included in bucket sizes)\n" , calculate_padding_size () * num_buckets );
563
- printf (" - Heap structures: %d bytes\n" , WOLFSSL_HEAP_SIZE + WOLFSSL_HEAP_HINT_SIZE );
560
+ printf (" - Padding per bucket: %d bytes (included in bucket sizes)\n" ,
561
+ calculate_padding_size ());
562
+ printf (" - Heap structures: %ld bytes\n" , sizeof (WOLFSSL_HEAP ) +
563
+ sizeof (WOLFSSL_HEAP_HINT ));
564
564
printf (" - Alignment: %d bytes\n" , WOLFSSL_STATIC_ALIGN - 1 );
565
565
printf ("Total memory needed: %d bytes\n" , total_memory_needed );
566
566
0 commit comments