github.com/iDigitalFlame/xmt@v0.5.4/device/winapi/mem_v12.go (about)

     1  //go:build windows && cgo && freemem && go1.12 && !go1.13
     2  // +build windows,cgo,freemem,go1.12,!go1.13
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package winapi
    21  
    22  import "unsafe"
    23  
    24  const (
    25  	x64         = 1 << (^uintptr(0) >> 63) / 2
    26  	arenaL1Bits = 1 << (6 * x64)
    27  	arenaL2Bits = 1 << ((x64*48 + (1-x64)*32) - 22 - (6 * x64))
    28  )
    29  
    30  //go:linkname gcBitsArenas runtime.gcBitsArenas
    31  var gcBitsArenas [4]uintptr
    32  
    33  type mheap struct {
    34  	_        uintptr
    35  	free     *treapNode
    36  	scav     *treapNode
    37  	_, _, _  uint32
    38  	allspans []*mspan
    39  	_        [2]struct {
    40  		_, _, _, _ uintptr
    41  		_          uint32
    42  	}
    43  	_              uint32
    44  	_, _, _, _     uint64
    45  	_              float64
    46  	_              uint64
    47  	_, _           uintptr
    48  	_, _, _, _     uint64
    49  	_              [67]uint64
    50  	arenas         [arenaL1Bits]*[arenaL2Bits]uintptr
    51  	heapArenaAlloc linearAlloc
    52  	arenaHints     *arenaHint
    53  	area           linearAlloc
    54  	_, _           []uint
    55  	_              [134]struct {
    56  		_          uintptr
    57  		_          uint8
    58  		_, _, _, _ uintptr
    59  		_          uint64
    60  		_          [cacheLineSize - ((5*ptrSize)+9)%cacheLineSize]byte
    61  	}
    62  	spanalloc             fixalloc
    63  	cachealloc            fixalloc
    64  	treapalloc            fixalloc
    65  	specialfinalizeralloc fixalloc
    66  	specialprofilealloc   fixalloc
    67  	_                     uintptr
    68  	arenaHintAlloc        fixalloc
    69  }
    70  type mspan struct {
    71  	_, _      *mspan
    72  	_         uintptr
    73  	startAddr uintptr
    74  }
    75  type fixalloc struct {
    76  	_, _, _, _ uintptr
    77  	chunk      uintptr
    78  	_          uint32
    79  	inuse      uintptr
    80  	_          uintptr
    81  	_          bool
    82  }
    83  type treapNode struct {
    84  	_, _    uintptr
    85  	parent  *treapNode
    86  	_       uintptr
    87  	spanKey uintptr
    88  }
    89  type arenaHint struct {
    90  	addr uintptr
    91  	_    bool
    92  	next *arenaHint
    93  }
    94  type linearAlloc struct {
    95  	next      uintptr
    96  	mapped, _ uintptr
    97  }
    98  
    99  func enumRuntimeMemory(h *mheap, m memoryMap) {
   100  	for x := h.free; x != nil; x = x.parent {
   101  		m.add(x.spanKey)
   102  	}
   103  	for x := h.scav; x != nil; x = x.parent {
   104  		m.add(x.spanKey)
   105  	}
   106  	for i := 1; i < len(gcBitsArenas); i++ {
   107  		m.add(gcBitsArenas[i])
   108  	}
   109  	if len(h.allspans) > 0 {
   110  		for i := range h.allspans {
   111  			if h.allspans[i] != nil {
   112  				m.add(h.allspans[i].startAddr)
   113  			}
   114  		}
   115  		m.add(uintptr(unsafe.Pointer(&h.allspans[0])))
   116  	}
   117  	for i := range h.arenas {
   118  		if h.arenas[i] == nil {
   119  			continue
   120  		}
   121  		if m.add(uintptr(unsafe.Pointer(h.arenas[i]))); x64 == 0 {
   122  			continue
   123  		}
   124  		for z := range h.arenas[i] {
   125  			if h.arenas[i][z] == 0 {
   126  				continue
   127  			}
   128  			m.add(uintptr(unsafe.Pointer(h.arenas[i][z])))
   129  		}
   130  	}
   131  	if m.add(h.area.next); h.area.mapped > 2 {
   132  		m.add(h.area.mapped - 2)
   133  	}
   134  	if m.add(h.heapArenaAlloc.next); h.heapArenaAlloc.mapped > 2 {
   135  		m.add(h.heapArenaAlloc.mapped - 2)
   136  	}
   137  	for x := h.arenaHints; x != nil; x = x.next {
   138  		m.add(x.addr)
   139  	}
   140  	m.add(h.spanalloc.chunk)
   141  	m.add(h.spanalloc.inuse)
   142  	m.add(h.cachealloc.chunk)
   143  	m.add(h.cachealloc.inuse)
   144  	m.add(h.treapalloc.chunk)
   145  	m.add(h.treapalloc.inuse)
   146  	m.add(h.specialfinalizeralloc.chunk)
   147  	m.add(h.specialfinalizeralloc.inuse)
   148  	m.add(h.specialprofilealloc.chunk)
   149  	m.add(h.specialprofilealloc.inuse)
   150  	m.add(h.arenaHintAlloc.chunk)
   151  	m.add(h.arenaHintAlloc.inuse)
   152  }