1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Calculate transient heap block usable size at compile time

This commit is contained in:
Jacob Matthews 2020-10-06 20:31:25 +13:00 committed by Koichi Sasada
parent da25affdac
commit 73834b5fc9
Notes: git 2020-10-21 11:44:37 +09:00

View file

@ -62,6 +62,7 @@
#define TRANSIENT_HEAP_TOTAL_SIZE (1024 * 1024 * 32) /* 32 MB */ #define TRANSIENT_HEAP_TOTAL_SIZE (1024 * 1024 * 32) /* 32 MB */
#define TRANSIENT_HEAP_ALLOC_MAX (1024 * 2 ) /* 2 KB */ #define TRANSIENT_HEAP_ALLOC_MAX (1024 * 2 ) /* 2 KB */
#define TRANSIENT_HEAP_BLOCK_NUM (TRANSIENT_HEAP_TOTAL_SIZE / TRANSIENT_HEAP_BLOCK_SIZE) #define TRANSIENT_HEAP_BLOCK_NUM (TRANSIENT_HEAP_TOTAL_SIZE / TRANSIENT_HEAP_BLOCK_SIZE)
#define TRANSIENT_HEAP_USABLE_SIZE TRANSIENT_HEAP_BLOCK_SIZE - sizeof(struct transient_heap_block_header)
#define TRANSIENT_HEAP_ALLOC_MAGIC 0xfeab #define TRANSIENT_HEAP_ALLOC_MAGIC 0xfeab
#define TRANSIENT_HEAP_ALLOC_ALIGN RUBY_ALIGNOF(void *) #define TRANSIENT_HEAP_ALLOC_ALIGN RUBY_ALIGNOF(void *)
@ -77,13 +78,12 @@ enum transient_heap_status {
struct transient_heap_block { struct transient_heap_block {
struct transient_heap_block_header { struct transient_heap_block_header {
int16_t size; /* sizeof(block) = TRANSIENT_HEAP_BLOCK_SIZE - sizeof(struct transient_heap_block_header) */
int16_t index; int16_t index;
int16_t last_marked_index; int16_t last_marked_index;
int16_t objects; int16_t objects;
struct transient_heap_block *next_block; struct transient_heap_block *next_block;
} info; } info;
char buff[TRANSIENT_HEAP_BLOCK_SIZE - sizeof(struct transient_heap_block_header)]; char buff[TRANSIENT_HEAP_USABLE_SIZE];
}; };
struct transient_heap { struct transient_heap {
@ -240,7 +240,6 @@ static void
reset_block(struct transient_heap_block *block) reset_block(struct transient_heap_block *block)
{ {
__msan_allocated_memory(block, sizeof block); __msan_allocated_memory(block, sizeof block);
block->info.size = TRANSIENT_HEAP_BLOCK_SIZE - sizeof(struct transient_heap_block_header);
block->info.index = 0; block->info.index = 0;
block->info.objects = 0; block->info.objects = 0;
block->info.last_marked_index = TRANSIENT_HEAP_ALLOC_MARKING_LAST; block->info.last_marked_index = TRANSIENT_HEAP_ALLOC_MARKING_LAST;
@ -345,9 +344,9 @@ transient_heap_allocatable_header(struct transient_heap* theap, size_t size)
struct transient_heap_block *block = theap->using_blocks; struct transient_heap_block *block = theap->using_blocks;
while (block) { while (block) {
TH_ASSERT(block->info.size >= block->info.index); TH_ASSERT(block->info.index <= TRANSIENT_HEAP_USABLE_SIZE);
if (block->info.size - block->info.index >= (int32_t)size) { if (TRANSIENT_HEAP_USABLE_SIZE - block->info.index >= size) {
struct transient_alloc_header *header = (void *)&block->buff[block->info.index]; struct transient_alloc_header *header = (void *)&block->buff[block->info.index];
block->info.index += size; block->info.index += size;
block->info.objects++; block->info.objects++;
@ -470,7 +469,7 @@ blocks_alloc_header_to_block(struct transient_heap *theap, struct transient_heap
struct transient_heap_block *block = blocks; struct transient_heap_block *block = blocks;
while (block) { while (block) {
if (block->buff <= (char *)header && (char *)header < block->buff + block->info.size) { if (block->buff <= (char *)header && (char *)header < block->buff + TRANSIENT_HEAP_USABLE_SIZE) {
return block; return block;
} }
block = block->info.next_block; block = block->info.next_block;