3 #include "StorageManager.h"
4 #include "asm/Operand.h"
5 #include "asm/Instruction.h"
6 #include "asm/InstructionList.h"
8 StorageManager::StorageManager() : data(NULL
), data_size(0), unused_data_offset(0) {
9 /* Start off with a megabyte of memory . . . */
11 data
= static_cast<Byte
*>(mmap(NULL
, data_size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0));
14 StorageManager::~StorageManager() {
15 munmap(data
, data_size
);
18 void StorageManager::alloc_more() {
19 /* Allocate another megabyte . . . */
20 data
= static_cast<Byte
*>(mremap(data
, data_size
, data_size
+= 1048576, MREMAP_MAYMOVE
));
23 ASM::Operand
*StorageManager::new_operand(std::string operand_string
) {
24 while(unsigned(data_size
- unused_data_offset
) < sizeof(ASM::Operand
)) alloc_more();
25 ASM::Operand
*operand
= new(data
+ unused_data_offset
) ASM::Operand(unused_data_offset
, operand_string
);
26 unused_data_offset
+= sizeof(ASM::Operand
);
30 ASM::Operand
*StorageManager::new_operand(ASM::Operand::operand_type_e type
, Word address
, ASM::Register reg
, SWord offset
) {
31 while(unsigned(data_size
- unused_data_offset
) < sizeof(ASM::Operand
)) alloc_more();
32 ASM::Operand
*operand
= new(data
+ unused_data_offset
) ASM::Operand(unused_data_offset
, type
, address
, reg
, offset
);
33 unused_data_offset
+= sizeof(ASM::Operand
);
34 type_map
[operand
->get_storage_offset()] = OPERAND_DATA
;
38 ASM::Instruction
*StorageManager::new_instruction(std::string instruction
, Word address
) {
39 while(unsigned(data_size
- unused_data_offset
) < sizeof(ASM::Instruction
)) alloc_more();
40 ASM::Instruction
*ins
= new(data
+ unused_data_offset
) ASM::Instruction(unused_data_offset
, instruction
, address
);
41 unused_data_offset
+= sizeof(ASM::Operand
);
42 type_map
[ins
->get_storage_offset()] = INSTRUCTION_DATA
;
46 ASM::InstructionList
*StorageManager::new_instruction_list(Word offset
) {
47 while(unsigned(data_size
- unused_data_offset
) < sizeof(ASM::InstructionList
)) alloc_more();
48 ASM::InstructionList
*il
= new(data
+ unused_data_offset
) ASM::InstructionList(unused_data_offset
, offset
);
49 unused_data_offset
+= sizeof(ASM::Operand
);
50 type_map
[il
->get_storage_offset()] = INSTRUCTION_LIST_DATA
;
54 ASM::Operand
*StorageManager::get_operand(StorageOffset storage_offset
) const {
55 if(type_map
.at(storage_offset
) != OPERAND_DATA
) return 0;
56 return reinterpret_cast<ASM::Operand
*>(data
+ storage_offset
);
59 ASM::Instruction
*StorageManager::get_instruction(StorageOffset storage_offset
) const {
60 if(type_map
.at(storage_offset
) != INSTRUCTION_DATA
) return 0;
61 return reinterpret_cast<ASM::Instruction
*>(data
+ storage_offset
);
64 ASM::InstructionList
*StorageManager::get_instruction_list(StorageOffset storage_offset
) const {
65 if(type_map
.at(storage_offset
) != INSTRUCTION_LIST_DATA
) return 0;
66 return reinterpret_cast<ASM::InstructionList
*>(data
+ storage_offset
);