Extract out the implementation of 'allocate' so other instructions
(ahem, deep-copy) can use it.
This commit is contained in:
Kartik K. Agaram 2016-06-28 19:33:43 -07:00
parent 078f81134f
commit 9a6f87985c
2 changed files with 28 additions and 28 deletions

View File

@ -275,33 +275,38 @@ case ALLOCATE: {
trace(9999, "mem") << "array size is " << ingredients.at(1).at(0) << end();
size = /*space for length*/1 + size*ingredients.at(1).at(0);
}
// include space for refcount
size++;
trace(9999, "mem") << "allocating size " << size << end();
//? Total_alloc += size;
//? Num_alloc++;
// compute the region of memory to return
// really crappy at the moment
ensure_space(size);
const int result = Current_routine->alloc;
trace(9999, "mem") << "new alloc: " << result << end();
// save result
products.resize(1);
products.at(0).push_back(result);
// initialize allocated space
for (int address = result; address < result+size; ++address) {
trace(9999, "mem") << "storing 0 in location " << address << end();
put(Memory, address, 0);
}
int result = allocate(size);
if (SIZE(current_instruction().ingredients) > 1) {
// initialize array length
trace(9999, "mem") << "storing " << ingredients.at(1).at(0) << " in location " << result+/*skip refcount*/1 << end();
put(Memory, result+/*skip refcount*/1, ingredients.at(1).at(0));
}
products.resize(1);
products.at(0).push_back(result);
break;
}
:(code)
int allocate(int size) {
// include space for refcount
size++;
trace(9999, "mem") << "allocating size " << size << end();
//? Total_alloc += size;
//? Num_alloc++;
// Allocate Special-cases
// compute the region of memory to return
// really crappy at the moment
ensure_space(size);
const int result = Current_routine->alloc;
trace(9999, "mem") << "new alloc: " << result << end();
// initialize allocated space
for (int address = result; address < result+size; ++address) {
trace(9999, "mem") << "storing 0 in location " << address << end();
put(Memory, address, 0);
}
Current_routine->alloc += size;
// no support yet for reclaiming memory between routines
assert(Current_routine->alloc <= Current_routine->alloc_max);
break;
return result;
}
//: statistics for debugging

View File

@ -67,25 +67,20 @@ void abandon(int address, const type_tree* payload_type, int payload_size) {
put(Current_routine->free_list, payload_size, address);
}
:(before "ensure_space(size)" following "case ALLOCATE")
:(after "Allocate Special-cases")
if (get_or_insert(Current_routine->free_list, size)) {
trace(9999, "abandon") << "picking up space from free-list of size " << size << end();
int result = get_or_insert(Current_routine->free_list, size);
trace(9999, "mem") << "new alloc from free list: " << result << end();
put(Current_routine->free_list, size, get_or_insert(Memory, result));
for (int curr = result+1; curr < result+size; ++curr) {
put(Memory, result, 0);
for (int curr = result; curr < result+size; ++curr) {
if (get_or_insert(Memory, curr) != 0) {
raise << maybe(current_recipe_name()) << "memory in free list was not zeroed out: " << curr << '/' << result << "; somebody wrote to us after free!!!\n" << end();
break; // always fatal
}
}
if (SIZE(current_instruction().ingredients) > 1)
put(Memory, result+/*skip refcount*/1, ingredients.at(1).at(0));
else
put(Memory, result, 0);
products.resize(1);
products.at(0).push_back(result);
break;
return result;
}
:(scenario new_differing_size_no_reclaim)