Skip to content

Commit a4cbe33

Browse files
aykevldeadprogram
authored andcommitted
runtime: only allocate heap memory when needed
For example, with -gc=none and -gc=leaking, no heap needs to be allocated when initializing the runtime. And some GCs (like -gc=custom) are responsible for allocating the heap themselves.
1 parent 5282b4e commit a4cbe33

File tree

5 files changed

+12
-2
lines changed

5 files changed

+12
-2
lines changed

src/runtime/gc_blocks.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
)
3838

3939
const gcDebug = false
40+
const needsStaticHeap = true
4041

4142
// Some globals + constants for the entire GC.
4243

src/runtime/gc_custom.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
"unsafe"
3737
)
3838

39+
const needsStaticHeap = false
40+
3941
// initHeap is called when the heap is first initialized at program start.
4042
func initHeap()
4143

src/runtime/gc_leaking.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"unsafe"
1212
)
1313

14+
const needsStaticHeap = true
15+
1416
// Ever-incrementing pointer: no memory is freed.
1517
var heapptr uintptr
1618

src/runtime/gc_none.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"unsafe"
1111
)
1212

13+
const needsStaticHeap = false
14+
1315
var gcTotalAlloc uint64 // for runtime.MemStats
1416
var gcMallocs uint64
1517
var gcFrees uint64

src/runtime/runtime_unix.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ var stackTop uintptr
7979
//
8080
//export main
8181
func main(argc int32, argv *unsafe.Pointer) int {
82-
preinit()
82+
if needsStaticHeap {
83+
// Allocate area for the heap if the GC needs it.
84+
allocateHeap()
85+
}
8386

8487
// Store argc and argv for later use.
8588
main_argc = argc
@@ -298,7 +301,7 @@ var heapMaxSize uintptr
298301

299302
var heapStart, heapEnd uintptr
300303

301-
func preinit() {
304+
func allocateHeap() {
302305
// Allocate a large chunk of virtual memory. Because it is virtual, it won't
303306
// really be allocated in RAM. Memory will only be allocated when it is
304307
// first touched.

0 commit comments

Comments
 (0)