about summary refs log tree commit diff stats
path: root/html/083scenario_screen_test.mu.html
Commit message (Expand)AuthorAgeFilesLines
* 3867Kartik K. Agaram2017-05-191-2/+2
* 3855Kartik K. Agaram2017-05-131-2/+2
* 3825Kartik K. Agaram2017-04-161-2/+2
* 3764 - better colors for cross-linksKartik K. Agaram2017-03-081-3/+4
* 3761Kartik K. Agaram2017-03-071-9/+10
* 3716Kartik K. Agaram2016-12-261-0/+2
* 3713 - cross-link calls with definitions in htmlKartik K. Agaram2016-12-261-4/+4
* 3710Kartik K. Agaram2016-12-261-32/+32
* 3709 - line numbers in htmlKartik K. Agaram2016-12-261-35/+59
* 3569Kartik K. Agaram2016-10-231-6/+6
* 3431Kartik K. Agaram2016-09-301-1/+1
* 3430Kartik K. Agaram2016-09-281-8/+7
* 3395Kartik K. Agaram2016-09-171-6/+6
* 3021Kartik K. Agaram2016-05-271-4/+6
* 2866Kartik K. Agaram2016-04-251-4/+4
* 2812Kartik K. Agaram2016-03-271-7/+16
* 2745Kartik K. Agaram2016-03-091-1/+1
* 2744Kartik K. Agaram2016-03-091-1/+1
* 2743Kartik K. Agaram2016-03-091-16/+7
* 2605Kartik K. Agaram2016-01-261-4/+6
* 2611Kartik K. Agaram2015-11-291-2/+2
* 2430 - make room for more transformsKartik K. Agaram2015-11-131-0/+63
4' href='#n254'>254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
 href='/akkartik/mu/commit/036abandon.cc?h=hlt&id=dc9afcbd7d7f1dcfae7b9ae659ccea4944b95a29'>dc9afcbd ^




9a6f8798 ^
dc9afcbd ^

4a943d4e ^
















dc9afcbd ^
4a943d4e ^















3ecee22a ^
4a943d4e ^













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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
                                               
 















                                                                                          
 

                                                                                   











                                                                            


                              













                                                                                                                                                                                   
   






                                                                         
                                                                                                  


                                                                    
   




                                             



                                                                   

                                         
                                                                                                                   
                                                                                                           
                                                         

 


                                                                     
                                  

 
                                 
                                                      
                                                                                                     
                                                               
                                                                                     

                                                                                          
                         

                                            
                                                         




                                                                                                                                                                        
                

 
















                                                              
 















                                                          
 













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