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

     1  //go:build windows && cgo && freemem && go1.15 && !go1.16
     2  // +build windows,cgo,freemem,go1.15,!go1.16
     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  	summarySize = 4 + (1 * x64)
    27  	chunkL1Bits = 1 << (13 * x64)
    28  	chunkL2Bits = 1 << ((x64*48 + (1-x64)*32) - 22 - (13 * x64))
    29  	arenaL1Bits = 1 << (6 * x64)
    30  	arenaL2Bits = 1 << ((x64*48 + (1-x64)*32) - 22 - (6 * x64))
    31  )
    32  
    33  //go:linkname gcBitsArenas runtime.gcBitsArenas
    34  var gcBitsArenas [4]uintptr
    35  
    36  type mheap struct {
    37  	_     uintptr
    38  	pages struct {
    39  		summary [summarySize][]uint64
    40  		chunks  [chunkL1Bits]*[chunkL2Bits][16]uint64
    41  		_       uintptr
    42  		_, _    uint
    43  		inUse   struct {
    44  			ranges [][2]uintptr
    45  			_, _   uintptr
    46  		}
    47  		scav struct {
    48  			_ struct {
    49  				_    [][2]uintptr
    50  				_, _ uintptr
    51  			}
    52  			_          uint32
    53  			_, _, _, _ uintptr
    54  		}
    55  		_, _ uintptr
    56  		_    bool
    57  	}
    58  	_, _, _  uint32
    59  	allspans []*mspan
    60  	_        [2]struct {
    61  		_, _, _, _ uintptr
    62  		_          uint32
    63  	}
    64  	_              uint32
    65  	_, _, _, _     uint64
    66  	_              float64
    67  	_, _           uint64
    68  	_              uintptr
    69  	_, _, _, _     uint64
    70  	_              [67]uint64
    71  	arenas         [arenaL1Bits]*[arenaL2Bits]uintptr
    72  	heapArenaAlloc linearAlloc
    73  	arenaHints     *arenaHint
    74  	area           linearAlloc
    75  	_, _, _        []uint
    76  	base           uintptr
    77  	_              uintptr
    78  	_              [134]struct {
    79  		_          uintptr
    80  		_          uint8
    81  		_, _, _, _ uintptr
    82  		_          [4]struct {
    83  			_, _, _, _ uintptr
    84  			_          uint64
    85  		}
    86  		_ uint64
    87  		_ [cacheLineSize - ((5*ptrSize)+9+(2*((4*ptrSize)+8)))%cacheLineSize]byte
    88  	}
    89  	spanalloc             fixalloc
    90  	cachealloc            fixalloc
    91  	specialfinalizeralloc fixalloc
    92  	specialprofilealloc   fixalloc
    93  	_                     uintptr
    94  	arenaHintAlloc        fixalloc
    95  }
    96  type mspan struct {
    97  	_, _      *mspan
    98  	_         uintptr
    99  	startAddr uintptr
   100  }
   101  type fixalloc struct {
   102  	_, _, _, _ uintptr
   103  	chunk      uintptr
   104  	_          uint32
   105  	inuse      uintptr
   106  	_          uintptr
   107  	_          bool
   108  }
   109  type arenaHint struct {
   110  	addr uintptr
   111  	_    bool
   112  	next *arenaHint
   113  }
   114  type linearAlloc struct {
   115  	next      uintptr
   116  	mapped, _ uintptr
   117  }
   118  
   119  func enumRuntimeMemory(h *mheap, m memoryMap) {
   120  	for i := range h.pages.summary {
   121  		m.add(uintptr(unsafe.Pointer(&h.pages.summary[i][0])))
   122  	}
   123  	for i := range h.pages.chunks {
   124  		if h.pages.chunks[i] == nil {
   125  			continue
   126  		}
   127  		m.add(uintptr(unsafe.Pointer(&h.pages.chunks[i])))
   128  	}
   129  	for i := range h.pages.inUse.ranges {
   130  		if h.pages.inUse.ranges[i][0] == 0 {
   131  			continue
   132  		}
   133  		m.add(h.pages.inUse.ranges[i][0])
   134  	}
   135  	for i := 1; i < len(gcBitsArenas); i++ {
   136  		m.add(gcBitsArenas[i])
   137  	}
   138  	if len(h.allspans) > 0 {
   139  		for i := range h.allspans {
   140  			if h.allspans[i] != nil {
   141  				m.add(h.allspans[i].startAddr)
   142  			}
   143  		}
   144  		m.add(uintptr(unsafe.Pointer(&h.allspans[0])))
   145  	}
   146  	for i := range h.arenas {
   147  		if h.arenas[i] == nil {
   148  			continue
   149  		}
   150  		if m.add(uintptr(unsafe.Pointer(h.arenas[i]))); x64 == 0 {
   151  			continue
   152  		}
   153  		for z := range h.arenas[i] {
   154  			if h.arenas[i][z] == 0 {
   155  				continue
   156  			}
   157  			m.add(uintptr(unsafe.Pointer(h.arenas[i][z])))
   158  		}
   159  	}
   160  	if m.add(h.area.next); h.area.mapped > 2 {
   161  		m.add(h.area.mapped - 2)
   162  	}
   163  	if m.add(h.heapArenaAlloc.next); h.heapArenaAlloc.mapped > 2 {
   164  		m.add(h.heapArenaAlloc.mapped - 2)
   165  	}
   166  	for x := h.arenaHints; x != nil; x = x.next {
   167  		m.add(x.addr)
   168  	}
   169  	m.add(h.base)
   170  	m.add(h.spanalloc.chunk)
   171  	m.add(h.spanalloc.inuse)
   172  	m.add(h.cachealloc.chunk)
   173  	m.add(h.cachealloc.inuse)
   174  	m.add(h.specialfinalizeralloc.chunk)
   175  	m.add(h.specialfinalizeralloc.inuse)
   176  	m.add(h.specialprofilealloc.chunk)
   177  	m.add(h.specialprofilealloc.inuse)
   178  	m.add(h.arenaHintAlloc.chunk)
   179  	m.add(h.arenaHintAlloc.inuse)
   180  }