2025-11-08 23:53:25 +09:00
|
|
|
#include "pool_refill.h"
|
|
|
|
|
#include "pool_tls.h"
|
2025-11-09 16:49:34 +09:00
|
|
|
#include "pool_tls_arena.h"
|
|
|
|
|
#include "pool_tls_remote.h"
|
2025-11-08 23:53:25 +09:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
// Get refill count from Box 1
|
|
|
|
|
extern int pool_get_refill_count(int class_idx);
|
|
|
|
|
|
|
|
|
|
// Refill and return first block
|
|
|
|
|
void* pool_refill_and_alloc(int class_idx) {
|
|
|
|
|
int count = pool_get_refill_count(class_idx);
|
|
|
|
|
if (count <= 0) return NULL;
|
|
|
|
|
|
2025-11-09 16:49:34 +09:00
|
|
|
// Refill boundary integration: try draining remote frees first.
|
|
|
|
|
// If we can satisfy from remote queue, avoid backend carve (OS pressure).
|
|
|
|
|
{
|
|
|
|
|
void* rchain = NULL;
|
|
|
|
|
int rgot = pool_remote_pop_chain(class_idx, count, &rchain);
|
|
|
|
|
if (rgot > 0 && rchain) {
|
|
|
|
|
// Pop one to return, install the rest into TLS
|
|
|
|
|
void* ret = rchain;
|
|
|
|
|
rchain = *(void**)rchain;
|
|
|
|
|
rgot--;
|
|
|
|
|
if (rgot > 0 && rchain) {
|
|
|
|
|
pool_install_chain(class_idx, rchain, rgot);
|
|
|
|
|
}
|
|
|
|
|
#if POOL_USE_HEADERS
|
|
|
|
|
*((uint8_t*)ret - POOL_HEADER_SIZE) = POOL_MAGIC | class_idx;
|
|
|
|
|
#endif
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 23:53:25 +09:00
|
|
|
// Batch allocate from existing Pool backend
|
|
|
|
|
void* chain = backend_batch_carve(class_idx, count);
|
|
|
|
|
if (!chain) return NULL; // OOM
|
|
|
|
|
|
|
|
|
|
// Pop first block for return
|
|
|
|
|
void* ret = chain;
|
|
|
|
|
chain = *(void**)chain;
|
|
|
|
|
count--;
|
|
|
|
|
|
|
|
|
|
#if POOL_USE_HEADERS
|
|
|
|
|
// Write header for the block we're returning
|
|
|
|
|
*((uint8_t*)ret - POOL_HEADER_SIZE) = POOL_MAGIC | class_idx;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Install rest in TLS (if any)
|
|
|
|
|
if (count > 0 && chain) {
|
|
|
|
|
pool_install_chain(class_idx, chain, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-09 16:49:34 +09:00
|
|
|
// Backend batch carve - Phase 1.5a: TLS Arena with chunk carving
|
2025-11-08 23:53:25 +09:00
|
|
|
void* backend_batch_carve(int class_idx, int count) {
|
|
|
|
|
if (class_idx < 0 || class_idx >= POOL_SIZE_CLASSES || count <= 0) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-09 16:49:34 +09:00
|
|
|
// Allocate blocks array on stack
|
|
|
|
|
void* blocks[64]; // Max refill count is 64
|
|
|
|
|
if (count > 64) count = 64;
|
2025-11-08 23:53:25 +09:00
|
|
|
|
2025-11-09 16:49:34 +09:00
|
|
|
// Carve from TLS Arena (Phase 1.5a)
|
|
|
|
|
int carved = arena_batch_carve(class_idx, blocks, count);
|
|
|
|
|
if (carved == 0) {
|
|
|
|
|
return NULL; // OOM
|
2025-11-08 23:53:25 +09:00
|
|
|
}
|
|
|
|
|
|
2025-11-09 16:49:34 +09:00
|
|
|
// Chain the carved blocks
|
2025-11-08 23:53:25 +09:00
|
|
|
void* head = NULL;
|
|
|
|
|
void* tail = NULL;
|
|
|
|
|
|
2025-11-09 16:49:34 +09:00
|
|
|
for (int i = 0; i < carved; i++) {
|
|
|
|
|
void* block = blocks[i];
|
2025-11-08 23:53:25 +09:00
|
|
|
|
2025-11-09 16:49:34 +09:00
|
|
|
// Chain the block
|
2025-11-08 23:53:25 +09:00
|
|
|
if (!head) {
|
2025-11-09 16:49:34 +09:00
|
|
|
head = block;
|
|
|
|
|
tail = block;
|
2025-11-08 23:53:25 +09:00
|
|
|
} else {
|
2025-11-09 16:49:34 +09:00
|
|
|
*(void**)tail = block;
|
|
|
|
|
tail = block;
|
2025-11-08 23:53:25 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Terminate chain
|
|
|
|
|
if (tail) {
|
|
|
|
|
*(void**)tail = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return head;
|
2025-11-09 16:49:34 +09:00
|
|
|
}
|