/
objectmap.go
60 lines (48 loc) · 1.32 KB
/
objectmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2020 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package image
import (
"reflect"
"unsafe"
"gate.computer/internal/file"
"gate.computer/wag/object"
)
const callSiteSize = 8 // The struct size or layout will not change between minor versions.
func callSitesSize(m *object.CallMap) int {
return len(m.CallSites) * callSiteSize
}
func callSitesBytes(m *object.CallMap) []byte {
size := callSitesSize(m)
if size == 0 {
return nil
}
b := make([]byte, 0)
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
h.Len = size
h.Cap = size
h.Data = uintptr(unsafe.Pointer(&m.CallSites[0]))
return b
}
func funcAddrsSize(m *object.CallMap) int {
return len(m.FuncAddrs) * 4
}
func funcAddrsBytes(m *object.CallMap) []byte {
size := funcAddrsSize(m)
if size == 0 {
return nil
}
b := make([]byte, 0)
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
h.Len = size
h.Cap = size
h.Data = uintptr(unsafe.Pointer(&m.FuncAddrs[0]))
return b
}
func copyObjectMapTo(b []byte, m *object.CallMap) {
copy(b, callSitesBytes(m))
copy(b[callSitesSize(m):], funcAddrsBytes(m))
}
func writeObjectMapAt(f *file.File, m *object.CallMap, offset int64) error {
return f.WriteVecAt([2][]byte{callSitesBytes(m), funcAddrsBytes(m)}, offset)
}