Skip to content

Commit 3a94c90

Browse files
committed
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 1ef1aa7 commit 3a94c90

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
@@ -10,6 +10,8 @@ import (
1010
"unsafe"
1111
)
1212

13+
const needsStaticHeap = true
14+
1315
// Ever-incrementing pointer: no memory is freed.
1416
var heapptr = heapStart
1517

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
@@ -71,7 +71,10 @@ var stackTop uintptr
7171
//
7272
//export main
7373
func main(argc int32, argv *unsafe.Pointer) int {
74-
preinit()
74+
if needsStaticHeap {
75+
// Allocate area for the heap if the GC needs it.
76+
allocateHeap()
77+
}
7578

7679
// Store argc and argv for later use.
7780
main_argc = argc
@@ -267,7 +270,7 @@ var heapMaxSize uintptr
267270

268271
var heapStart, heapEnd uintptr
269272

270-
func preinit() {
273+
func allocateHeap() {
271274
// Allocate a large chunk of virtual memory. Because it is virtual, it won't
272275
// really be allocated in RAM. Memory will only be allocated when it is
273276
// first touched.

0 commit comments

Comments
 (0)