@@ -44,49 +44,53 @@ var numRegisteredSignalInfo = 0
44
44
/// a kill signal, this handler will dump all the entries in the tack trace and
45
45
/// end the process.
46
46
private class PrettyStackTraceManager {
47
- struct RawStackEntry {
47
+ struct StackEntry {
48
+ var prev : UnsafeMutablePointer < StackEntry > ?
48
49
let data : UnsafeMutablePointer < Int8 >
49
50
let count : Int
50
51
}
51
52
52
53
/// Keeps a stack of serialized trace entries in reverse order.
53
54
/// - Note: This keeps strings, because it's not safe to
54
55
/// construct the strings in the signal handler directly.
55
- var stack = [ RawStackEntry ] ( )
56
+ var stack : UnsafeMutablePointer < StackEntry > ? = nil
56
57
57
- private let stackDumpMsg : RawStackEntry
58
+ private let stackDumpMsg : StackEntry
58
59
init ( ) {
59
60
let msg = " Stack dump: \n "
60
- stackDumpMsg = RawStackEntry ( data: strndup ( msg, msg. count) ,
61
- count: msg. count)
61
+ stackDumpMsg = StackEntry ( prev: nil ,
62
+ data: strndup ( msg, msg. count) ,
63
+ count: msg. count)
62
64
}
63
65
64
66
/// Pushes the description of a trace entry to the stack.
65
67
func push( _ entry: TraceEntry ) {
66
68
let str = " \( entry. description) \n "
67
- let entry = RawStackEntry ( data: strndup ( str, str. count) ,
69
+ let newEntry = StackEntry ( prev: stack,
70
+ data: strndup ( str, str. count) ,
68
71
count: str. count)
69
- stack. insert ( entry, at: 0 )
72
+ let newStack = UnsafeMutablePointer< StackEntry> . allocate( capacity: 1 )
73
+ newStack. pointee = newEntry
74
+ stack = newStack
70
75
}
71
76
72
77
/// Pops the latest trace entry off the stack.
73
78
func pop( ) {
74
- guard !stack. isEmpty else { return }
75
- stack. removeFirst ( )
79
+ guard let stack = stack else { return }
80
+ let prev = stack. pointee. prev
81
+ stack. deallocate ( capacity: 1 )
82
+ self . stack = prev
76
83
}
77
84
78
85
/// Dumps the stack entries to standard error, starting with the most
79
86
/// recent entry.
80
87
func dump( _ signal: Int32 ) {
81
88
write ( STDERR_FILENO, stackDumpMsg. data, stackDumpMsg. count)
82
- let stackLimit = stack. count
83
- stack. withUnsafeBufferPointer { buffer in
84
- var i = 0
85
- while i < stackLimit {
86
- let bufItem = buffer [ i]
87
- write ( STDERR_FILENO, bufItem. data, bufItem. count)
88
- i += 1
89
- }
89
+ var cur = stack
90
+ while cur != nil {
91
+ let entry = cur. unsafelyUnwrapped
92
+ write ( STDERR_FILENO, entry. pointee. data, entry. pointee. count)
93
+ cur = entry. pointee. prev
90
94
}
91
95
}
92
96
}
0 commit comments