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

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