-
Couldn't load subscription status.
- Fork 711
byte-buffer: use malloc_good_size on Darwin to allocate memory
#3066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the SwiftNIO open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of SwiftNIO project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import XCTest | ||
|
|
||
| @testable import NIOCore | ||
|
|
||
| #if canImport(Darwin) | ||
|
||
| import Darwin | ||
|
|
||
| // Tests that ByteBuffer allocates memory in an optimal way depending on the host platform. | ||
|
|
||
| final class ByteBufferStorageMallocTest: XCTestCase { | ||
|
|
||
| func testInitialAllocationUsesGoodSize() { | ||
| let allocator = ByteBufferAllocator() | ||
| let requestedCapacity = 1000 | ||
| let expectedCapacity = malloc_good_size(requestedCapacity) | ||
|
|
||
| let buffer = allocator.buffer(capacity: requestedCapacity) | ||
| XCTAssertEqual(Int(buffer._storage.capacity), expectedCapacity) | ||
| } | ||
|
|
||
| func testReallocationUsesGoodSize() { | ||
| let allocator = ByteBufferAllocator() | ||
| var buffer = allocator.buffer(capacity: 16) | ||
| let initialCapacity = buffer.capacity | ||
|
|
||
| // Write more bytes than the current capacity to trigger reallocation | ||
| let newSize = initialCapacity + 100 | ||
| let expectedCapacity = malloc_good_size(Int(newSize)) | ||
|
|
||
| // This will trigger reallocation | ||
| buffer.writeBytes(Array(repeating: UInt8(0), count: Int(newSize))) | ||
|
|
||
| XCTAssertEqual(Int(buffer._storage.capacity), expectedCapacity) | ||
| } | ||
|
|
||
| func testZeroCapacity() { | ||
| let allocator = ByteBufferAllocator() | ||
| let buffer = allocator.buffer(capacity: 0) | ||
| XCTAssertEqual(buffer.capacity, 0) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we asking for capacity differently here than in the rest of the tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same approach as the rest of the tests would be fine, BUT zero being explicit feels nice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it might be nice to keep the tests discussing capacity the same way. |
||
| } | ||
|
|
||
| } | ||
| #endif | ||
Uh oh!
There was an error while loading. Please reload this page.