Skip to content

Commit 1cc2469

Browse files
committed
runtime: simplify and cleanup mallocinit
mallocinit has evolved organically. Make a pass to clean it up in various ways: 1. Merge the computation of spansSize and bitmapSize. These were computed on every loop iteration of two different loops, but always have the same value, which can be derived directly from _MaxMem. This also avoids over-reserving these on MIPS, were _MaxArena32 is larger than _MaxMem. 2. Remove the ulimit -v logic. It's been disabled for many releases and the dead code paths to support it are even more wrong now than they were when it was first disabled, since now we *must* reserve spans and bitmaps for the full address space. 3. Make it clear that we're using a simple linear allocation to lay out the spans, bitmap, and arena spaces. Previously there were a lot of redundant pointer computations. Now we just bump p1 up as we reserve the spaces. In preparation for #18651. Updates #5049 (respect ulimit). Change-Id: Icbe66570d3a7a17bea227dc54fb3c4978b52a3af Reviewed-on: https://go-review.googlesource.com/35252 Reviewed-by: Russ Cox <[email protected]>
1 parent efb5eae commit 1cc2469

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

src/runtime/malloc.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,21 @@ func mallocinit() {
242242
throw("bad system page size")
243243
}
244244

245-
var p, bitmapSize, spansSize, pSize, limit uintptr
245+
// The auxiliary regions start at p and are laid out in the
246+
// following order: spans, bitmap, arena.
247+
var p, pSize uintptr
246248
var reserved bool
247249

248-
// limit = runtime.memlimit();
249-
// See https://golang.org/issue/5049
250-
// TODO(rsc): Fix after 1.1.
251-
limit = 0
250+
// The spans array holds one *mspan per _PageSize of arena.
251+
var spansSize uintptr = (_MaxMem + 1) / _PageSize * sys.PtrSize
252+
spansSize = round(spansSize, _PageSize)
253+
// The bitmap holds 2 bits per word of arena.
254+
var bitmapSize uintptr = (_MaxMem + 1) / (sys.PtrSize * 8 / 2)
255+
bitmapSize = round(bitmapSize, _PageSize)
252256

253257
// Set up the allocation arena, a contiguous area of memory where
254-
// allocated data will be found. The arena begins with a bitmap large
255-
// enough to hold 2 bits per allocated word.
256-
if sys.PtrSize == 8 && (limit == 0 || limit > 1<<30) {
258+
// allocated data will be found.
259+
if sys.PtrSize == 8 {
257260
// On a 64-bit machine, allocate from a single contiguous reservation.
258261
// 512 GB (MaxMem) should be big enough for now.
259262
//
@@ -284,9 +287,7 @@ func mallocinit() {
284287
// translation buffers, the user address space is limited to 39 bits
285288
// On darwin/arm64, the address space is even smaller.
286289
arenaSize := round(_MaxMem, _PageSize)
287-
bitmapSize = arenaSize / (sys.PtrSize * 8 / 2)
288-
spansSize = arenaSize / _PageSize * sys.PtrSize
289-
spansSize = round(spansSize, _PageSize)
290+
pSize = bitmapSize + spansSize + arenaSize + _PageSize
290291
for i := 0; i <= 0x7f; i++ {
291292
switch {
292293
case GOARCH == "arm64" && GOOS == "darwin":
@@ -296,7 +297,6 @@ func mallocinit() {
296297
default:
297298
p = uintptr(i)<<40 | uintptrMask&(0x00c0<<32)
298299
}
299-
pSize = bitmapSize + spansSize + arenaSize + _PageSize
300300
p = uintptr(sysReserve(unsafe.Pointer(p), pSize, &reserved))
301301
if p != 0 {
302302
break
@@ -327,15 +327,6 @@ func mallocinit() {
327327
}
328328

329329
for _, arenaSize := range arenaSizes {
330-
bitmapSize = (_MaxArena32 + 1) / (sys.PtrSize * 8 / 2)
331-
spansSize = (_MaxArena32 + 1) / _PageSize * sys.PtrSize
332-
if limit > 0 && arenaSize+bitmapSize+spansSize > limit {
333-
bitmapSize = (limit / 9) &^ ((1 << _PageShift) - 1)
334-
arenaSize = bitmapSize * 8
335-
spansSize = arenaSize / _PageSize * sys.PtrSize
336-
}
337-
spansSize = round(spansSize, _PageSize)
338-
339330
// SysReserve treats the address we ask for, end, as a hint,
340331
// not as an absolute requirement. If we ask for the end
341332
// of the data segment but the operating system requires
@@ -361,18 +352,21 @@ func mallocinit() {
361352
// so SysReserve can give us a PageSize-unaligned pointer.
362353
// To overcome this we ask for PageSize more and round up the pointer.
363354
p1 := round(p, _PageSize)
355+
pSize -= p1 - p
364356

365357
spansStart := p1
366-
mheap_.bitmap = p1 + spansSize + bitmapSize
358+
p1 += spansSize
359+
mheap_.bitmap = p1 + bitmapSize
360+
p1 += bitmapSize
367361
if sys.PtrSize == 4 {
368362
// Set arena_start such that we can accept memory
369363
// reservations located anywhere in the 4GB virtual space.
370364
mheap_.arena_start = 0
371365
} else {
372-
mheap_.arena_start = p1 + (spansSize + bitmapSize)
366+
mheap_.arena_start = p1
373367
}
374368
mheap_.arena_end = p + pSize
375-
mheap_.arena_used = p1 + (spansSize + bitmapSize)
369+
mheap_.arena_used = p1
376370
mheap_.arena_reserved = reserved
377371

378372
if mheap_.arena_start&(_PageSize-1) != 0 {

0 commit comments

Comments
 (0)