Skip to content

Commit cdc3115

Browse files
authored
Merge pull request ruby#119 from Shopify/code_pages
Code page allocation code
2 parents dd1885e + 36a77b6 commit cdc3115

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

yjit_asm.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,59 @@ uint8_t* alloc_exec_mem(uint32_t mem_size)
218218
#endif
219219
}
220220

221+
// Size of code pages to allocate
222+
#define CODE_PAGE_SIZE 16 * 1024
223+
224+
// How many code pages to allocate at once
225+
#define PAGES_PER_ALLOC 512
226+
227+
typedef struct free_list_node
228+
{
229+
uint8_t* page_ptr;
230+
231+
struct free_list_node *next;
232+
233+
} freelist_t;
234+
235+
freelist_t *freelist = NULL;
236+
237+
// Allocate a single code page from a pool of free pages
238+
uint8_t* alloc_code_page()
239+
{
240+
fprintf(stderr, "allocating code page\n");
241+
242+
// If the free list is empty
243+
if (!freelist) {
244+
// Allocate many pages at once
245+
uint8_t* code_chunk = alloc_exec_mem(PAGES_PER_ALLOC * CODE_PAGE_SIZE);
246+
247+
// Do this in reverse order so we allocate our pages in order
248+
for (int i = PAGES_PER_ALLOC - 1; i >= 0; --i) {
249+
freelist_t* node = malloc(sizeof(freelist_t));
250+
node->page_ptr = code_chunk + i * CODE_PAGE_SIZE;
251+
node->next = freelist;
252+
freelist = node;
253+
}
254+
}
255+
256+
freelist_t* free_node = freelist;
257+
uint8_t* page_ptr = freelist->page_ptr;
258+
259+
freelist = freelist->next;
260+
free(free_node);
261+
262+
return page_ptr;
263+
}
264+
265+
// Put a code page back into the allocation pool
266+
void free_code_page(uint8_t* page_ptr)
267+
{
268+
freelist_t* node = malloc(sizeof(freelist_t));
269+
node->page_ptr = page_ptr;
270+
node->next = freelist;
271+
freelist = node;
272+
}
273+
221274
// Initialize a code block object
222275
void cb_init(codeblock_t* cb, uint8_t* mem_block, uint32_t mem_size)
223276
{

yjit_asm.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,12 @@ x86opnd_t const_ptr_opnd(const void *ptr);
240240
sizeof(((struct_type*)0)->member_name[0]) * idx) \
241241
)
242242

243-
// Code block methods
243+
// Machine code allocation
244244
uint8_t* alloc_exec_mem(uint32_t mem_size);
245+
uint8_t* alloc_code_page();
246+
void free_code_page(uint8_t* page_ptr);
247+
248+
// Code block methods
245249
void cb_init(codeblock_t* cb, uint8_t* mem_block, uint32_t mem_size);
246250
void cb_align_pos(codeblock_t* cb, uint32_t multiple);
247251
void cb_set_pos(codeblock_t* cb, uint32_t pos);

0 commit comments

Comments
 (0)