Skip to content

Commit afbe626

Browse files
make bucket sizes adhere to memory alignment
1 parent 359f318 commit afbe626

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

staticmemory/memory-bucket-optimizer/optimizer/memory_bucket_optimizer.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
* Default: 9 buckets (can be adjusted based on memory constraints)
3838
*/
3939

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-
4440
/* Linked list node for allocation events */
4541
typedef struct AllocationEventNode {
4642
int size;
@@ -178,11 +174,6 @@ int calculate_padding_size() {
178174
return wolfSSL_MemoryPaddingSz();
179175
}
180176

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-
186177
/* Function to calculate total memory overhead */
187178
int calculate_total_overhead(int num_buckets) {
188179
/* Total overhead includes:
@@ -191,8 +182,8 @@ int calculate_total_overhead(int num_buckets) {
191182
* - Alignment padding
192183
* Note: Padding is already included in bucket sizes
193184
*/
194-
int total_overhead = WOLFSSL_HEAP_SIZE +
195-
WOLFSSL_HEAP_HINT_SIZE +
185+
int total_overhead = sizeof(WOLFSSL_HEAP) +
186+
sizeof(WOLFSSL_HEAP_HINT) +
196187
(WOLFSSL_STATIC_ALIGN - 1);
197188
total_overhead += num_buckets * wolfSSL_MemoryPaddingSz();
198189
return total_overhead;
@@ -372,6 +363,15 @@ static void sort_alloc_by_frequency(AllocSizeNode* alloc_sizes,
372363
} while (max != NULL);
373364
}
374365

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+
375375
/* Function to optimize bucket sizes */
376376
/*
377377
* Optimization heuristic:
@@ -415,7 +415,7 @@ void optimize_buckets(AllocSizeNode* alloc_sizes, AllocSizeNode* alloc_sizes_by_
415415
/* Always include the largest allocation sizes (with padding) */
416416
current = alloc_sizes;
417417
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);
419419
dist[*num_buckets] = current->max_concurrent;
420420
(*num_buckets)++;
421421
current = current->next;
@@ -433,8 +433,7 @@ void optimize_buckets(AllocSizeNode* alloc_sizes, AllocSizeNode* alloc_sizes_by_
433433
int already_included = 0;
434434
for (j = 0; j < *num_buckets; j++) {
435435
/* 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)) {
438437
already_included = 1;
439438
break;
440439
}
@@ -447,7 +446,7 @@ void optimize_buckets(AllocSizeNode* alloc_sizes, AllocSizeNode* alloc_sizes_by_
447446
current = current->next;
448447
}
449448
if (max != NULL) {
450-
buckets[*num_buckets] = calculate_bucket_size_with_padding(max->size);
449+
buckets[*num_buckets] = get_bucket_size(max->size);
451450
dist[*num_buckets] = max->max_concurrent;
452451
*num_buckets += 1;
453452
}
@@ -558,9 +557,10 @@ void calculate_memory_efficiency(AllocSizeNode* alloc_sizes, int num_sizes,
558557

559558
printf("Total bucket memory: %d bytes\n", total_bucket_memory);
560559
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));
564564
printf(" - Alignment: %d bytes\n", WOLFSSL_STATIC_ALIGN - 1);
565565
printf("Total memory needed: %d bytes\n", total_memory_needed);
566566

0 commit comments

Comments
 (0)