mu/archive/1.vm/036abandon.cc

154 lines
5.2 KiB
C++
Raw Normal View History

//: Reclaiming memory when it's no longer used.
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
void test_new_reclaim() {
run(
"def main [\n"
" 10:&:num <- new number:type\n"
" 20:num <- deaddress 10:&:num\n"
" abandon 10:&:num\n"
" 30:&:num <- new number:type\n" // must be same size as abandoned memory to reuse
" 40:num <- deaddress 30:&:num\n"
" 50:bool <- equal 20:num, 40:num\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
// both allocations should have returned the same address
"mem: storing 1 in location 50\n"
);
}
//: When abandoning addresses we'll save them to a 'free list', segregated by size.
//: Before, suppose variable V contains address A which points to payload P:
//: location V contains an alloc-id N
//: location V+1 contains A
//: location A contains alloc-id N
//: location A+1 onwards contains P
//: Additionally, suppose the head of the free list is initially F.
//: After abandoning:
//: location V contains invalid alloc-id -1
//: location V+1 contains 0
//: location A contains invalid alloc-id N
//: location A+1 contains the previous head of free-list F
:(before "End routine Fields")
map<int, int> free_list;
:(before "End Primitive Recipe Declarations")
ABANDON,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "abandon", ABANDON);
:(before "End Primitive Recipe Checks")
case ABANDON: {
if (!inst.products.empty()) {
raise << maybe(get(Recipe, r).name) << "'abandon' shouldn't write to any products in '" << to_original_string(inst) << "'\n" << end();
break;
}
for (int i = 0; i < SIZE(inst.ingredients); ++i) {
if (!is_mu_address(inst.ingredients.at(i)))
raise << maybe(get(Recipe, r).name) << "ingredients of 'abandon' should be addresses, but ingredient " << i << " is '" << to_string(inst.ingredients.at(i)) << '\n' << end();
break;
2016-05-18 01:32:41 +00:00
}
break;
}
:(before "End Primitive Recipe Implementations")
case ABANDON: {
for (int i = 0; i < SIZE(current_instruction().ingredients); ++i) {
reagent/*copy*/ ingredient = current_instruction().ingredients.at(i);
canonize(ingredient);
abandon(get_or_insert(Memory, ingredient.value+/*skip alloc id*/1), payload_size(ingredient));
//? cerr << "clear after abandon: " << ingredient.value << '\n';
put(Memory, /*alloc id*/ingredient.value, /*invalid*/-1);
put(Memory, /*address*/ingredient.value+1, 0);
2016-05-18 01:32:41 +00:00
}
break;
}
:(code)
void abandon(int address, int payload_size) {
put(Memory, address, /*invalid alloc-id*/-1);
//? cerr << "abandon: " << address << '\n';
// clear rest of payload
for (int curr = address+1; curr < address+payload_size; ++curr)
put(Memory, curr, 0);
// append existing free list to address
trace(Callstack_depth+1, "abandon") << "saving " << address << " in free-list of size " << payload_size << end();
put(Memory, address+/*skip invalid alloc-id*/1, get_or_insert(Current_routine->free_list, payload_size));
2016-05-18 01:32:41 +00:00
put(Current_routine->free_list, payload_size, address);
}
2018-05-13 06:08:39 +00:00
int payload_size(reagent/*copy*/ x) {
x.properties.push_back(pair<string, string_tree*>("lookup", NULL));
lookup_memory_core(x, /*check_for_null*/false);
return size_of(x)+/*alloc id*/1;
2018-05-13 06:08:39 +00:00
}
:(after "Allocate Special-cases")
if (get_or_insert(Current_routine->free_list, size)) {
trace(Callstack_depth+1, "abandon") << "picking up space from free-list of size " << size << end();
int result = get_or_insert(Current_routine->free_list, size);
trace(Callstack_depth+1, "mem") << "new alloc from free list: " << result << end();
put(Current_routine->free_list, size, get_or_insert(Memory, result+/*skip alloc id*/1));
// clear 'deleted' tag
put(Memory, result, 0);
// clear next pointer
put(Memory, result+/*skip alloc id*/1, 0);
2016-10-20 05:10:35 +00:00
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
}
}
return result;
}
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
:(code)
void test_new_differing_size_no_reclaim() {
run(
"def main [\n"
" 1:&:num <- new number:type\n"
" 2:num <- deaddress 1:&:num\n"
" abandon 1:&:num\n"
" 3:&:@:num <- new number:type, 2\n" // different size
" 4:num <- deaddress 3:&:@:num\n"
" 5:bool <- equal 2:num, 4:num\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
// no reuse
"mem: storing 0 in location 5\n"
);
}
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
void test_new_reclaim_array() {
run(
"def main [\n"
" 10:&:@:num <- new number:type, 2\n"
" 20:num <- deaddress 10:&:@:num\n"
" abandon 10:&:@:num\n"
" 30:&:@:num <- new number:type, 2\n" // same size
" 40:num <- deaddress 30:&:@:num\n"
" 50:bool <- equal 20:num, 40:num\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
// both calls to new returned identical addresses
"mem: storing 1 in location 50\n"
);
}
5001 - drop the :(scenario) DSL I've been saying for a while[1][2][3] that adding extra abstractions makes things harder for newcomers, and adding new notations doubly so. And then I notice this DSL in my own backyard. Makes me feel like a hypocrite. [1] https://news.ycombinator.com/item?id=13565743#13570092 [2] https://lobste.rs/s/to8wpr/configuration_files_are_canary_warning [3] https://lobste.rs/s/mdmcdi/little_languages_by_jon_bentley_1986#c_3miuf2 The implementation of the DSL was also highly hacky: a) It was happening in the tangle/ tool, but was utterly unrelated to tangling layers. b) There were several persnickety constraints on the different kinds of lines and the specific order they were expected in. I kept finding bugs where the translator would silently do the wrong thing. Or the error messages sucked, and readers may be stuck looking at the generated code to figure out what happened. Fixing error messages would require a lot more code, which is one of my arguments against DSLs in the first place: they may be easy to implement, but they're hard to design to go with the grain of the underlying platform. They require lots of iteration. Is that effort worth prioritizing in this project? On the other hand, the DSL did make at least some readers' life easier, the ones who weren't immediately put off by having to learn a strange syntax. There were fewer quotes to parse, fewer backslash escapes. Anyway, since there are also people who dislike having to put up with strange syntaxes, we'll call that consideration a wash and tear this DSL out. --- This commit was sheer drudgery. Hopefully it won't need to be redone with a new DSL because I grow sick of backslashes.
2019-03-13 01:56:55 +00:00
void test_lookup_of_abandoned_address_raises_error() {
Hide_errors = true;
run(
"def main [\n"
" 1:&:num <- new num:type\n"
" 3:&:num <- copy 1:&:num\n"
" abandon 1:&:num\n"
" 5:num/raw <- copy *3:&:num\n"
"]\n"
);
CHECK_TRACE_CONTENTS(
"error: main: address is already abandoned in '5:num/raw <- copy *3:&:num'\n"
);
}