From 9523b6a85d3e708cb3cb2c7ead415b525d8c7b09 Mon Sep 17 00:00:00 2001 From: 3405691582 Date: Sat, 29 Mar 2025 13:12:52 -0400 Subject: [PATCH 1/2] ManagedBuffer.capacity is unavailable on OpenBSD. ManagedBuffer.capacity depends on malloc introspection (e.g., `malloc_size`), which is not available on this platform -- and is thus marked as such. Here, _HashNode.Storage is a ManagedBuffer. Thankfully, the uses of .capacity are fairly limited: one for statistic purposes, which we just completely omit on the platform, and the other, setting a minimum capacity in the header by the buffer's .capacity; the alternative being setting that minimum capacity directly. --- .../HashTreeCollections/HashNode/_HashNode+Storage.swift | 7 ++++++- .../HashTreeCollections/HashNode/_HashTreeStatistics.swift | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Sources/HashTreeCollections/HashNode/_HashNode+Storage.swift b/Sources/HashTreeCollections/HashNode/_HashNode+Storage.swift index 0aefeb84..44854293 100644 --- a/Sources/HashTreeCollections/HashNode/_HashNode+Storage.swift +++ b/Sources/HashTreeCollections/HashNode/_HashNode+Storage.swift @@ -77,10 +77,15 @@ extension _HashNode.Storage { bytes += itemAlignment - childAlignment } + let mincap = (bytes &+ childStride &- 1) / childStride let object = _HashNode.Storage.create( - minimumCapacity: (bytes &+ childStride &- 1) / childStride + minimumCapacity: mincap ) { buffer in +#if os(OpenBSD) + _HashNodeHeader(byteCapacity: mincap * childStride) +#else _HashNodeHeader(byteCapacity: buffer.capacity * childStride) +#endif } object.withUnsafeMutablePointers { header, elements in diff --git a/Sources/HashTreeCollections/HashNode/_HashTreeStatistics.swift b/Sources/HashTreeCollections/HashNode/_HashTreeStatistics.swift index 85468af5..6d15a735 100644 --- a/Sources/HashTreeCollections/HashNode/_HashTreeStatistics.swift +++ b/Sources/HashTreeCollections/HashNode/_HashTreeStatistics.swift @@ -115,12 +115,14 @@ extension _HashNode { let objectHeaderSize = 2 * MemoryLayout.stride +#if !os(OpenBSD) // Note: for simplicity, we assume that there is no padding between // the object header and the storage header. let start = _getUnsafePointerToStoredProperties(self.raw.storage) let capacity = self.raw.storage.capacity let end = $0._memory + capacity * MemoryLayout<_RawHashNode>.stride stats.grossBytes += objectHeaderSize + (end - start) +#endif for child in $0.children { child.gatherStatistics(level.descend(), &stats) From 25509c0b883738629b6a13c013b6274538112fbe Mon Sep 17 00:00:00 2001 From: 3405691582 Date: Fri, 2 May 2025 17:04:54 -0400 Subject: [PATCH 2/2] Update Sources/HashTreeCollections/HashNode/_HashTreeStatistics.swift Co-authored-by: Karoy Lorentey --- .../HashTreeCollections/HashNode/_HashTreeStatistics.swift | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Sources/HashTreeCollections/HashNode/_HashTreeStatistics.swift b/Sources/HashTreeCollections/HashNode/_HashTreeStatistics.swift index 6d15a735..0dd1efed 100644 --- a/Sources/HashTreeCollections/HashNode/_HashTreeStatistics.swift +++ b/Sources/HashTreeCollections/HashNode/_HashTreeStatistics.swift @@ -115,14 +115,11 @@ extension _HashNode { let objectHeaderSize = 2 * MemoryLayout.stride -#if !os(OpenBSD) // Note: for simplicity, we assume that there is no padding between // the object header and the storage header. let start = _getUnsafePointerToStoredProperties(self.raw.storage) - let capacity = self.raw.storage.capacity - let end = $0._memory + capacity * MemoryLayout<_RawHashNode>.stride + let end = $0._memory + $0.byteCapacity stats.grossBytes += objectHeaderSize + (end - start) -#endif for child in $0.children { child.gatherStatistics(level.descend(), &stats)