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

     1  //go:build windows && cgo && freemem && go1.11 && !go1.12
     2  // +build windows,cgo,freemem,go1.11,!go1.12
     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  	_         [128][2]uintptr
    36  	freeLarge *treapNode
    37  	_         [128][2]uintptr
    38  	_, _      uintptr
    39  	_, _, _   uint32
    40  	allspans  []*mspan
    41  	_         [2]struct {
    42  		_, _, _, _ uintptr
    43  		_          uint32
    44  	}
    45  	_, _, _, _     uint64
    46  	_              float64
    47  	_, _, _, _     uint64
    48  	_              [67]uint64
    49  	arenas         [arenaL1Bits]*[arenaL2Bits]uintptr
    50  	heapArenaAlloc linearAlloc
    51  	arenaHints     *arenaHint
    52  	area           linearAlloc
    53  	_              [134]struct {
    54  		_          uintptr
    55  		_          uint8
    56  		_, _, _, _ uintptr
    57  		_          uint64
    58  		_          [cacheLineSize - ((5*ptrSize)+9)%cacheLineSize]byte
    59  	}
    60  	spanalloc             fixalloc
    61  	cachealloc            fixalloc
    62  	treapalloc            fixalloc
    63  	specialfinalizeralloc fixalloc
    64  	specialprofilealloc   fixalloc
    65  	_                     uintptr
    66  	arenaHintAlloc        fixalloc
    67  }
    68  type mspan struct {
    69  	_, _      *mspan
    70  	_         uintptr
    71  	startAddr uintptr
    72  }
    73  type fixalloc struct {
    74  	_, _, _, _ uintptr
    75  	chunk      uintptr
    76  	_          uint32
    77  	inuse      uintptr
    78  	_          uintptr
    79  	_          bool
    80  }
    81  type treapNode struct {
    82  	_, _    uintptr
    83  	parent  *treapNode
    84  	_       uintptr
    85  	spanKey uintptr
    86  }
    87  type arenaHint struct {
    88  	addr uintptr
    89  	_    bool
    90  	next *arenaHint
    91  }
    92  type linearAlloc struct {
    93  	next      uintptr
    94  	mapped, _ uintptr
    95  }
    96  
    97  func enumRuntimeMemory(h *mheap, m memoryMap) {
    98  	for x := h.freeLarge; x != nil; x = x.parent {
    99  		m.add(x.spanKey)
   100  	}
   101  	for i := 1; i < len(gcBitsArenas); i++ {
   102  		m.add(gcBitsArenas[i])
   103  	}
   104  	if len(h.allspans) > 0 {
   105  		for i := range h.allspans {
   106  			if h.allspans[i] != nil {
   107  				m.add(h.allspans[i].startAddr)
   108  			}
   109  		}
   110  		m.add(uintptr(unsafe.Pointer(&h.allspans[0])))
   111  	}
   112  	for i := range h.arenas {
   113  		if h.arenas[i] == nil {
   114  			continue
   115  		}
   116  		if m.add(uintptr(unsafe.Pointer(h.arenas[i]))); x64 == 0 {
   117  			continue
   118  		}
   119  		for z := range h.arenas[i] {
   120  			if h.arenas[i][z] == 0 {
   121  				continue
   122  			}
   123  			m.add(uintptr(unsafe.Pointer(h.arenas[i][z])))
   124  		}
   125  	}
   126  	if m.add(h.area.next); h.area.mapped > 2 {
   127  		m.add(h.area.mapped - 2)
   128  	}
   129  	if m.add(h.heapArenaAlloc.next); h.heapArenaAlloc.mapped > 2 {
   130  		m.add(h.heapArenaAlloc.mapped - 2)
   131  	}
   132  	for x := h.arenaHints; x != nil; x = x.next {
   133  		m.add(x.addr)
   134  	}
   135  	m.add(h.spanalloc.chunk)
   136  	m.add(h.spanalloc.inuse)
   137  	m.add(h.cachealloc.chunk)
   138  	m.add(h.cachealloc.inuse)
   139  	m.add(h.treapalloc.chunk)
   140  	m.add(h.treapalloc.inuse)
   141  	m.add(h.specialfinalizeralloc.chunk)
   142  	m.add(h.specialfinalizeralloc.inuse)
   143  	m.add(h.specialprofilealloc.chunk)
   144  	m.add(h.specialprofilealloc.inuse)
   145  	m.add(h.arenaHintAlloc.chunk)
   146  	m.add(h.arenaHintAlloc.inuse)
   147  }