-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_block.c
41 lines (36 loc) · 938 Bytes
/
code_block.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "p1s.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void * make_block_executable(struct code_block * block)
{
size_t size = ROUND_TO_PAGE(block->length); // round up to nearest page size
void * ptr = alloc_page(size);
memcpy(ptr, block->code, block->length);
protect_page(ptr, size);
block->executable_code = ptr;
return ptr;
}
struct code_block * create_code_block()
{
struct code_block * block = malloc(sizeof(struct code_block));
block->capacity = 1;
block->length = 0;
block->code = malloc(1);
block->executable_code = NULL;
return block;
}
void destroy_code_block(struct code_block * block)
{
if (block->code)
{
free(block->code);
block->code = NULL;
}
if (block->executable_code)
{
free_page(block->executable_code, ROUND_TO_PAGE(block->length));
block->executable_code = NULL;
}
free(block);
}