Skip to content

Commit 198e4fa

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

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

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

Lines changed: 21 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,18 @@ 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+
if (padding > 0) {
373+
padding = WOLFSSL_STATIC_ALIGN - padding;
374+
}
375+
return size + padding + wolfSSL_MemoryPaddingSz();
376+
}
377+
375378
/* Function to optimize bucket sizes */
376379
/*
377380
* Optimization heuristic:
@@ -415,7 +418,7 @@ void optimize_buckets(AllocSizeNode* alloc_sizes, AllocSizeNode* alloc_sizes_by_
415418
/* Always include the largest allocation sizes (with padding) */
416419
current = alloc_sizes;
417420
for (i = 0; i < MAX_UNIQUE_BUCKETS/2 && current != NULL; i++) {
418-
buckets[*num_buckets] = calculate_bucket_size_with_padding(current->size);
421+
buckets[*num_buckets] = get_bucket_size(current->size);
419422
dist[*num_buckets] = current->max_concurrent;
420423
(*num_buckets)++;
421424
current = current->next;
@@ -433,8 +436,7 @@ void optimize_buckets(AllocSizeNode* alloc_sizes, AllocSizeNode* alloc_sizes_by_
433436
int already_included = 0;
434437
for (j = 0; j < *num_buckets; j++) {
435438
/* 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) {
439+
if (buckets[j] == get_bucket_size(current->size)) {
438440
already_included = 1;
439441
break;
440442
}
@@ -447,7 +449,7 @@ void optimize_buckets(AllocSizeNode* alloc_sizes, AllocSizeNode* alloc_sizes_by_
447449
current = current->next;
448450
}
449451
if (max != NULL) {
450-
buckets[*num_buckets] = calculate_bucket_size_with_padding(max->size);
452+
buckets[*num_buckets] = get_bucket_size(max->size);
451453
dist[*num_buckets] = max->max_concurrent;
452454
*num_buckets += 1;
453455
}
@@ -558,9 +560,10 @@ void calculate_memory_efficiency(AllocSizeNode* alloc_sizes, int num_sizes,
558560

559561
printf("Total bucket memory: %d bytes\n", total_bucket_memory);
560562
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);
563+
printf(" - Padding per bucket: %d bytes (included in bucket sizes)\n",
564+
calculate_padding_size());
565+
printf(" - Heap structures: %ld bytes\n", sizeof(WOLFSSL_HEAP) +
566+
sizeof(WOLFSSL_HEAP_HINT));
564567
printf(" - Alignment: %d bytes\n", WOLFSSL_STATIC_ALIGN - 1);
565568
printf("Total memory needed: %d bytes\n", total_memory_needed);
566569

0 commit comments

Comments
 (0)