Skip to content

Commit b8b9fc7

Browse files
committed
Wrap allocations/metadatas
1 parent 501a468 commit b8b9fc7

File tree

3 files changed

+65
-19
lines changed

3 files changed

+65
-19
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Ben Cohen on 5/16/20.
6+
//
7+
8+
import SwiftRemoteMirror
9+
10+
struct Allocation {
11+
let allocation_t: swift_metadata_allocation_t
12+
13+
var tag: swift_metadata_allocation_tag_t { allocation_t.Tag }
14+
var ptr: swift_reflection_ptr_t { allocation_t.Ptr }
15+
var size: Int { Int(allocation_t.Size) }
16+
}
17+
18+
extension Allocation: Comparable {
19+
static func == (lhs: Self, rhs: Self) -> Bool {
20+
lhs.ptr == rhs.ptr
21+
}
22+
23+
static func < (lhs: Self, rhs: Self) -> Bool {
24+
lhs.ptr < rhs.ptr
25+
}
26+
}
27+
28+
extension Allocation {
29+
func metadata(in context: SwiftReflectionContextRef) -> Metadata? {
30+
let ptr = context.metadataPointer(allocation: allocation_t)
31+
if ptr != 0 {
32+
let name = context.name(metadata: ptr) ?? "<unknown>"
33+
return .init(ptr: ptr, name: name)
34+
} else {
35+
return nil
36+
}
37+
}
38+
}
39+
40+
struct Metadata {
41+
let ptr: swift_reflection_ptr_t
42+
let name: String
43+
}

tools/swiftdt/Sources/swiftdt/RemoteMirrorExtensions.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,12 @@ extension SwiftReflectionContextRef {
6262
throw Error(cString: str)
6363
}
6464
}
65+
66+
var allocations: [Allocation] {
67+
var allocations: [Allocation] = []
68+
try! iterateMetadataAllocations { allocation_t in
69+
allocations.append(.init(allocation_t: allocation_t))
70+
}
71+
return allocations
72+
}
6573
}

tools/swiftdt/Sources/swiftdt/main.swift

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,23 @@ func dumpConformanceCache(context: SwiftReflectionContextRef) throws {
2222
}
2323

2424
func dumpMetadataAllocations(context: SwiftReflectionContextRef) throws {
25-
var allocations: [swift_metadata_allocation_t] = []
26-
var metadatas: [swift_reflection_ptr_t] = []
27-
try context.iterateMetadataAllocations { allocation in
28-
allocations.append(allocation)
29-
print("Metadata allocation at: \(hex: allocation.Ptr) " +
30-
"size: \(allocation.Size) tag: \(allocation.Tag)")
31-
let ptr = context.metadataPointer(allocation: allocation)
32-
if ptr != 0 {
33-
metadatas.append(ptr)
34-
}
25+
var allocations = context.allocations
26+
for allocation in allocations {
27+
print("Metadata allocation at: \(hex: allocation.ptr) " +
28+
"size: \(allocation.size) tag: \(allocation.tag)")
3529
}
30+
31+
allocations.sort()
32+
let metadatas = allocations.compactMap { $0.metadata(in: context) }
3633

37-
allocations.sort { $0.Ptr < $1.Ptr }
3834
for metadata in metadatas {
39-
let name = context.name(metadata: metadata) ?? "<unknown>"
40-
print("Metadata \(hex: metadata)")
41-
print(" Name: \(name)")
42-
43-
if let allocation = allocations.last(where: { metadata >= $0.Ptr }) {
44-
let offset = metadata - allocation.Ptr
45-
print(" In allocation \(hex: allocation.Ptr) " +
46-
"size \(allocation.Size) at offset \(offset)")
35+
print("Metadata \(hex: metadata.ptr)")
36+
print(" Name: \(metadata.name)")
37+
38+
if let allocation = allocations.last(where: { metadata.ptr >= $0.ptr }) {
39+
let offset = metadata.ptr - allocation.ptr
40+
print(" In allocation \(hex: allocation.ptr) " +
41+
"size \(allocation.size) at offset \(offset)")
4742
} else {
4843
print(" Not in any known metadata allocation. How strange.")
4944
}

0 commit comments

Comments
 (0)