[WIP] Saved messages

This commit is contained in:
Isaac
2024-01-06 00:43:58 +04:00
parent c2b9891de1
commit 6a89f938db
86 changed files with 1660 additions and 478 deletions

View File

@@ -51,7 +51,7 @@ public func persistentHash32(_ string: String) -> Int32 {
private let emptyMemory = malloc(1)!
public class MemoryBuffer: Equatable, CustomStringConvertible {
public class MemoryBuffer: Comparable, Hashable, CustomStringConvertible {
public internal(set) var memory: UnsafeMutableRawPointer
var capacity: Int
public internal(set) var length: Int
@@ -122,9 +122,25 @@ public class MemoryBuffer: Equatable, CustomStringConvertible {
f(Data(bytesNoCopy: self.memory, count: self.length, deallocator: .none))
}
public func hash(into hasher: inout Hasher) {
if self.length == 0 {
hasher.combine(0 as Int)
} else {
hasher.combine(bytes: UnsafeRawBufferPointer(start: self.memory, count: self.length))
}
}
public func withRawBufferPointer(_ f: (UnsafeRawBufferPointer) -> Void) {
f(UnsafeRawBufferPointer(start: self.memory, count: self.length))
}
public static func ==(lhs: MemoryBuffer, rhs: MemoryBuffer) -> Bool {
return lhs.length == rhs.length && memcmp(lhs.memory, rhs.memory, lhs.length) == 0
}
public static func <(lhs: MemoryBuffer, rhs: MemoryBuffer) -> Bool {
return mdb_cmp_memn(lhs.memory, lhs.length, rhs.memory, rhs.length) < 0
}
}
public final class WriteBuffer: MemoryBuffer {
@@ -134,6 +150,10 @@ public final class WriteBuffer: MemoryBuffer {
super.init(memory: malloc(32), capacity: 32, length: 0, freeWhenDone: true)
}
public init(capacity: Int) {
super.init(memory: malloc(capacity), capacity: capacity, length: 0, freeWhenDone: true)
}
public func makeReadBufferAndReset() -> ReadBuffer {
let buffer = ReadBuffer(memory: self.memory, length: self.offset, freeWhenDone: true)
self.memory = malloc(32)
@@ -222,6 +242,12 @@ public final class ReadBuffer: MemoryBuffer {
return result
}
public func readMemoryBuffer(length: Int) -> MemoryBuffer {
let result = MemoryBuffer(memory: malloc(length)!, capacity: length, length: length, freeWhenDone: true)
self.read(result.memory, offset: 0, length: length)
return result
}
public func skip(_ length: Int) {
self.offset += length
}