about summary refs log blame commit diff stats
path: root/archive/2.vm/036abandon.cc
blob: a1f665233401c7e0b06bd7baa235846536918527 (plain) (tree)
1
2
                                               
 















                                                                                          
 

                                                                                   











                                                                            


                              













                                                                                                                                                                                   
   






                                                                         
                                                                                                  


                                                                    
   




                                             



                                                                   

                                         
                                                                                                                   
                                                                                                           
                                                         

 


                                                                     
                                  

 
                                 
                                                      
                                                                                                     
                                                               
                                                                                     

                                                                                          
                         

                                            
                                                         




                                                                                                                                                                        
                

 
















                                                              
 















                                                          
 













                                                                                   
//: Reclaiming memory when it's no longer used.

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;
  }
  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);
  }
  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));
  put(Current_routine->free_list, payload_size, address);
}

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;
}

:(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);
  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;
}

:(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"
  );
}

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"
  );
}

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"
  );
}
class="o">="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># compare eax with ecx</span> <span id="L41" class="LineNr">41 </span> 73/jump-if-addr&gt;= $clear-stream:end/disp8 <span id="L42" class="LineNr">42 </span> <span class="subxComment"># *curr = 0</span> <span id="L43" class="LineNr">43 </span> c6 0/subop/copy-byte 0/mod/direct 0/rm32/eax <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/imm8 <span class="subxComment"># copy byte to *eax</span> <span id="L44" class="LineNr">44 </span> <span class="subxComment"># ++curr</span> <span id="L45" class="LineNr">45 </span> 40/increment-eax <span id="L46" class="LineNr">46 </span> eb/jump $clear-stream:<span class="Constant">loop</span>/disp8 <span id="L47" class="LineNr">47 </span><span class="Constant">$clear-stream:end</span>: <span id="L48" class="LineNr">48 </span> <span class="subxS1Comment"># . restore registers</span> <span id="L49" class="LineNr">49 </span> 59/pop-to-ecx <span id="L50" class="LineNr">50 </span> 58/pop-to-eax <span id="L51" class="LineNr">51 </span> <span class="subxS1Comment"># . epilogue</span> <span id="L52" class="LineNr">52 </span> 89/copy 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 5/r32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy ebp to esp</span> <span id="L53" class="LineNr">53 </span> 5d/pop-to-ebp <span id="L54" class="LineNr">54 </span> c3/return <span id="L55" class="LineNr">55 </span> <span id="L56" class="LineNr">56 </span><span class="subxFunction">rewind-stream</span>: <span class="subxComment"># f: (addr stream byte)</span> <span id="L57" class="LineNr">57 </span> <span class="subxS1Comment"># . prologue</span> <span id="L58" class="LineNr">58 </span> 55/push-ebp <span id="L59" class="LineNr">59 </span> 89/copy 3/mod/direct 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 4/r32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy esp to ebp</span> <span id="L60" class="LineNr">60 </span> <span class="subxS1Comment"># . save registers</span> <span id="L61" class="LineNr">61 </span> 50/push-eax <span id="L62" class="LineNr">62 </span> <span class="subxComment"># eax = f</span> <span id="L63" class="LineNr">63 </span> 8b/copy 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> 0/r32/eax 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(ebp+8) to eax</span> <span id="L64" class="LineNr">64 </span> <span class="subxComment"># f-&gt;read = 0</span> <span id="L65" class="LineNr">65 </span> c7 0/subop/copy 1/mod/*+disp8 0/rm32/eax <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 4/disp8 0/imm32 <span class="subxComment"># copy to *(eax+4)</span> <span id="L66" class="LineNr">66 </span><span class="Constant">$rewind-stream:end</span>: <span id="L67" class="LineNr">67 </span> <span class="subxS1Comment"># . restore registers</span> <span id="L68" class="LineNr">68 </span> 58/pop-to-eax <span id="L69" class="LineNr">69 </span> <span class="subxS1Comment"># . epilogue</span> <span id="L70" class="LineNr">70 </span> 89/copy 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 5/r32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy ebp to esp</span> <span id="L71" class="LineNr">71 </span> 5d/pop-to-ebp <span id="L72" class="LineNr">72 </span> c3/return <span id="L73" class="LineNr">73 </span> <span id="L74" class="LineNr">74 </span><span class="subxS2Comment"># . . vim&#0058;nowrap:textwidth=0</span> </pre> </body> </html> <!-- vim: set foldmethod=manual : -->