Skip to content

Commit 32f81f6

Browse files
Merge pull request #91 from swift-developer-tools/ops/readme-and-cleanup
Update memory-safety overview
2 parents ae0725e + 21752a9 commit 32f81f6

4,089 files changed

Lines changed: 4132 additions & 4120 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ initialization macros. Direct access to the libgit2 C library is also provided
1414
by the package. See [Usage](#Usage) for an example of how to import and use
1515
either library.
1616

17+
The bindings use memory-safe Swift types wherever possible, while preserving
18+
libgit2's behavior and semantics. For example, some bindings use native Swift
19+
types like `String` instead of `UnsafePointer<CChar>`, and accept `inout`
20+
parameters to memory-safe Swift structs instead of unsafe pointers to C structs.
21+
In these cases, swift-libgit2 safely converts types between Swift and C,
22+
managing and freeing memory as needed. See
23+
[Memory Management](#Memory-Management) for more information.
24+
1725
The bindings use the same signatures and names as their C equivalents, but are
1826
written using [camel case](https://en.wikipedia.org/wiki/Camel_case) instead of
19-
[snake case](https://en.wikipedia.org/wiki/Snake_case).
20-
21-
Similar to libgit2, the bindings do not use
22-
[namespaces](https://en.wikipedia.org/wiki/Namespace). All bindings are
23-
available globally.
24-
25-
The bindings use native Swift types wherever possible, while preserving
26-
libgit2's behavior and semantics. For example, some bindings use Swift types
27-
like `String` instead of `UnsafePointer<CChar>`.
27+
[snake case](https://en.wikipedia.org/wiki/Snake_case). Similar to libgit2, the
28+
bindings do not use [namespaces](https://en.wikipedia.org/wiki/Namespace). All
29+
bindings are available globally.
2830

2931

3032

@@ -103,13 +105,17 @@ converted from Swift to C, or in other cases specific to individual functions.
103105

104106
### Memory Management
105107

106-
swift-libgit2 directly invokes libgit2 C code. The caller is responsible for
107-
freeing memory allocated by libgit2, unless otherwise specified.
108+
swift-libgit2 handles memory management for most bindings. For example, `inout`
109+
Swift types are safely converted between Swift and C, with any allocated
110+
memory being freed as needed.
111+
112+
However, some bindings require manual memory management, particularly when
113+
working with opaque or unsafe pointers. The caller is responsible for freeing
114+
the memory using the appropriate swift-libgit2 or libgit2 function.
108115

109116
Consider using
110117
[`defer`](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/statements/#Defer-Statement)
111-
statements with memory-freeing function bindings to consistently and safely
112-
free memory.
118+
statements with memory-freeing functions to consistently and safely free memory.
113119

114120
### Thread Safety
115121

Sources/SwiftLibgit2/Diff-Advanced/Diff-Advanced-Structs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public struct GitDiffPerfData: CStructInternalMutable, CConvertible, Sendable
3535

3636

3737

38-
/// Initializes a default ``GitDiffPerfData``.
38+
/// Initializes a default ``GitDiffPerfData`` instance.
3939
public init() { }
4040

4141

Sources/SwiftLibgit2/Diff/Diff-Functions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,13 +784,13 @@ public func gitDiffBlobToBuffer(
784784

785785
return try buffer.withOptionalCString
786786
{
787-
cBuffer, cBufferLength in
787+
cBuffer, cBufferCount in
788788

789789
return git_diff_blob_to_buffer(
790790
oldBlob,
791791
oldAsPath,
792792
cBuffer,
793-
cBufferLength,
793+
cBufferCount,
794794
bufferAsPath,
795795
cOptions,
796796
fileCB,

Sources/SwiftLibgit2/Diff/Diff-Structs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ public struct GitDiffHunk: CStructInternalMutable, CConvertible, Sendable
635635

636636

637637

638-
/// Initializes a default ``GitDiffHunk``.
638+
/// Initializes a default ``GitDiffHunk`` instance.
639639
public init() { }
640640

641641

Sources/SwiftLibgit2/SwiftLibgit2.docc/SwiftLibgit2.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ initialization macros. Direct access to the libgit2 C library is also provided
1818
by the package. See [Usage](#Usage) for an example of how to import and use
1919
either library.
2020

21+
The bindings use memory-safe Swift types wherever possible, while preserving
22+
libgit2's behavior and semantics. For example, some bindings use native Swift
23+
types like `String` instead of `UnsafePointer<CChar>`, and accept `inout`
24+
parameters to memory-safe Swift structs instead of unsafe pointers to C structs.
25+
In these cases, swift-libgit2 safely converts types between Swift and C,
26+
managing and freeing memory as needed. See
27+
[Memory Management](#Memory-Management) for more information.
28+
2129
The bindings use the same signatures and names as their C equivalents, but are
2230
written using [camel case](https://en.wikipedia.org/wiki/Camel_case) instead of
23-
[snake case](https://en.wikipedia.org/wiki/Snake_case).
24-
25-
Similar to libgit2, the bindings do not use
26-
[namespaces](https://en.wikipedia.org/wiki/Namespace). All bindings are
27-
available globally.
28-
29-
The bindings use native Swift types wherever possible, while preserving
30-
libgit2's behavior and semantics. For example, some bindings use Swift types
31-
like `String` instead of `UnsafePointer<CChar>`.
31+
[snake case](https://en.wikipedia.org/wiki/Snake_case). Similar to libgit2, the
32+
bindings do not use [namespaces](https://en.wikipedia.org/wiki/Namespace). All
33+
bindings are available globally.
3234

3335

3436

@@ -107,13 +109,17 @@ converted from Swift to C, or in other cases specific to individual functions.
107109

108110
### Memory Management
109111

110-
swift-libgit2 directly invokes libgit2 C code. The caller is responsible for
111-
freeing memory allocated by libgit2, unless otherwise specified.
112+
swift-libgit2 handles memory management for most bindings. For example, `inout`
113+
Swift types are safely converted between Swift and C, with any allocated
114+
memory being freed as needed.
115+
116+
However, some bindings require manual memory management, particularly when
117+
working with opaque or unsafe pointers. The caller is responsible for freeing
118+
the memory using the appropriate swift-libgit2 or libgit2 function.
112119

113120
Consider using
114121
[`defer`](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/statements/#Defer-Statement)
115-
statements with memory-freeing function bindings to consistently and safely
116-
free memory.
122+
statements with memory-freeing functions to consistently and safely free memory.
117123

118124
### Thread Safety
119125

Sources/SwiftLibgit2/Utilities/Extensions/NSError+MakeError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal extension NSError
3434
return NSError(
3535
domain: Bundle.main.bundleIdentifier ?? "swift-libgit2",
3636
code: Int(code),
37-
userInfo: [NSLocalizedDescriptionKey: message]
37+
userInfo: [NSLocalizedDescriptionKey : message]
3838
)
3939
}
4040

Sources/SwiftLibgit2/Utilities/Protocols/CFreeable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
///
1313
/// A struct that conforms to ``CStructInternalMutable`` may also need to
1414
/// conform to ``CFreeable`` if libgit2 provides a corresponding memory-freeing
15-
/// function. See the ``CStruct`` documenation for more information.
15+
/// function. See the ``CStruct`` documentation for more information.
1616
internal protocol CFreeable: CStruct
1717
{
1818
/// The type of the pointer passed to ``freeCValue(_:)`` to free the memory

Tests/SwiftLibgit2Tests/Commit-Tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ final class CommitTests: XCTestCaseStopOnFail
326326

327327
XCTAssertGreaterThan(callbackData.callCount, 0)
328328
XCTAssertNotNil(callbackData.lastMessage)
329-
XCTAssertFalse(callbackData.lastMessage?.isEmpty ?? false)
329+
XCTAssertFalse(callbackData.lastMessage?.isEmpty ?? true)
330330
}
331331
}
332332

Tests/SwiftLibgit2Tests/Diff-Tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ final class DiffTests: XCTestCaseStopOnFail
172172
_ = gitDiffBlobs(
173173
oldBlob: oldBlobPointer,
174174
oldAsPath: "old.txt",
175-
newBlob: newBlobPointer,
175+
newBlob: newBlobPointer,
176176
newAsPath: "new.txt",
177177
options: nil,
178178
fileCB: nil,

Tests/Utilities/Repository.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ internal extension Repository
10381038
_ body: (Repository, OpaquePointer) throws -> T
10391039
) throws -> T
10401040
{
1041-
return try Repository.withRepository
1041+
return try withRepository
10421042
{
10431043
repository in
10441044

@@ -1128,7 +1128,7 @@ internal extension Repository
11281128
_ body: (Repository, OpaquePointer) throws -> T
11291129
) throws -> T
11301130
{
1131-
return try Repository.withRepository
1131+
return try withRepository
11321132
{
11331133
repository in
11341134

@@ -1180,7 +1180,7 @@ internal extension Repository
11801180
_ body: (Repository, OpaquePointer) throws -> T
11811181
) throws -> T
11821182
{
1183-
return try Repository.withRepository
1183+
return try withRepository
11841184
{
11851185
repository in
11861186

@@ -1239,7 +1239,7 @@ internal extension Repository
12391239
_ body : (Repository, OpaquePointer) throws -> T
12401240
) throws -> T
12411241
{
1242-
return try Repository.withRepository
1242+
return try withRepository
12431243
{
12441244
repository in
12451245

@@ -1272,7 +1272,7 @@ internal extension Repository
12721272
repo: repository.pointer,
12731273
name: Self.remoteName,
12741274
url: Self.remoteURL,
1275-
fetch: Repository.fetchRefspec
1275+
fetch: fetchRefspec
12761276
)
12771277
}
12781278

@@ -1368,7 +1368,7 @@ internal extension Repository
13681368
_ body: (Repository, OpaquePointer) throws -> T
13691369
) throws -> T
13701370
{
1371-
return try Repository.withRepository
1371+
return try withRepository
13721372
{
13731373
repository in
13741374

@@ -1426,7 +1426,7 @@ internal extension Repository
14261426
_ body : (Repository, OpaquePointer) throws -> T
14271427
) throws -> T
14281428
{
1429-
return try Repository.withRepository
1429+
return try withRepository
14301430
{
14311431
repository in
14321432

0 commit comments

Comments
 (0)