File tree 3 files changed +24
-11
lines changed
3 files changed +24
-11
lines changed Original file line number Diff line number Diff line change @@ -418,9 +418,9 @@ func mHeap_SysAlloc(h *mheap, n uintptr) unsafe.Pointer {
418
418
// Keep taking from our reservation.
419
419
p := h .arena_used
420
420
sysMap ((unsafe .Pointer )(p ), n , h .arena_reserved , & memstats .heap_sys )
421
- h . arena_used += n
422
- mHeap_MapBits ( h )
423
- mHeap_MapSpans ( h )
421
+ mHeap_MapBits ( h , p + n )
422
+ mHeap_MapSpans ( h , p + n )
423
+ h . arena_used = p + n
424
424
if raceenabled {
425
425
racemapshadow ((unsafe .Pointer )(p ), n )
426
426
}
@@ -454,12 +454,12 @@ func mHeap_SysAlloc(h *mheap, n uintptr) unsafe.Pointer {
454
454
p_end := p + p_size
455
455
p += - p & (_PageSize - 1 )
456
456
if uintptr (p )+ n > uintptr (h .arena_used ) {
457
- h .arena_used = p + n
457
+ mHeap_MapBits (h , p + n )
458
+ mHeap_MapSpans (h , p + n )
459
+ h .arena_used = p + n
458
460
if p_end > h .arena_end {
459
461
h .arena_end = p_end
460
462
}
461
- mHeap_MapBits (h )
462
- mHeap_MapSpans (h )
463
463
if raceenabled {
464
464
racemapshadow ((unsafe .Pointer )(p ), n )
465
465
}
Original file line number Diff line number Diff line change @@ -118,15 +118,20 @@ func subtract1(p *byte) *byte {
118
118
119
119
// mHeap_MapBits is called each time arena_used is extended.
120
120
// It maps any additional bitmap memory needed for the new arena memory.
121
+ // It must be called with the expected new value of arena_used,
122
+ // *before* h.arena_used has been updated.
123
+ // Waiting to update arena_used until after the memory has been mapped
124
+ // avoids faults when other threads try access the bitmap immediately
125
+ // after observing the change to arena_used.
121
126
//
122
127
//go:nowritebarrier
123
- func mHeap_MapBits (h * mheap ) {
128
+ func mHeap_MapBits (h * mheap , arena_used uintptr ) {
124
129
// Caller has added extra mappings to the arena.
125
130
// Add extra mappings of bitmap words as needed.
126
131
// We allocate extra bitmap pieces in chunks of bitmapChunk.
127
132
const bitmapChunk = 8192
128
133
129
- n := (mheap_ . arena_used - mheap_ .arena_start ) / heapBitmapScale
134
+ n := (arena_used - mheap_ .arena_start ) / heapBitmapScale
130
135
n = round (n , bitmapChunk )
131
136
n = round (n , _PhysPageSize )
132
137
if h .bitmap_mapped >= n {
Original file line number Diff line number Diff line change @@ -279,10 +279,18 @@ func mHeap_Init(h *mheap, spans_size uintptr) {
279
279
sp .cap = int (spans_size / ptrSize )
280
280
}
281
281
282
- func mHeap_MapSpans (h * mheap ) {
282
+ // mHeap_MapSpans makes sure that the spans are mapped
283
+ // up to the new value of arena_used.
284
+ //
285
+ // It must be called with the expected new value of arena_used,
286
+ // *before* h.arena_used has been updated.
287
+ // Waiting to update arena_used until after the memory has been mapped
288
+ // avoids faults when other threads try access the bitmap immediately
289
+ // after observing the change to arena_used.
290
+ func mHeap_MapSpans (h * mheap , arena_used uintptr ) {
283
291
// Map spans array, PageSize at a time.
284
- n := uintptr ( unsafe . Pointer ( h . arena_used ))
285
- n -= uintptr ( unsafe . Pointer ( h .arena_start ))
292
+ n := arena_used
293
+ n -= h .arena_start
286
294
n = n / _PageSize * ptrSize
287
295
n = round (n , _PhysPageSize )
288
296
if h .spans_mapped >= n {
You can’t perform that action at this time.
0 commit comments