pass_escape_via_pointer.mx raw

   1  package main
   2  
   3  // Tests that heap allocations returned from functions survive the
   4  // dealloc sweep (escape detection works). Global store test removed
   5  // because package globals are immutable after init().
   6  
   7  type Data struct {
   8  	Val int32
   9  }
  10  
  11  func returnPtr(v int32) (result *Data) {
  12  	result = &Data{Val: v}
  13  	return result
  14  }
  15  
  16  func allocAndRead(v int32) (out int32) {
  17  	p := &Data{Val: v}
  18  	out = p.Val
  19  	return out
  20  }
  21  
  22  func main() {
  23  	p := returnPtr(99)
  24  	if p.Val == 99 {
  25  		println("PASS: return escape survives dealloc")
  26  	} else {
  27  		println("FAIL: return escape corrupted")
  28  	}
  29  
  30  	v := allocAndRead(42)
  31  	if v == 42 {
  32  		println("PASS: local alloc read correctly")
  33  	} else {
  34  		println("FAIL: local alloc read corrupted")
  35  	}
  36  }
  37