From 2477b4efd8692dd7d9fab7e43f1de2df112ace43 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Thu, 5 Aug 2021 20:07:00 +0300 Subject: [PATCH] refactor: use libgit free() functions instead of calloc() --- example/config_example.dart | 8 ++++---- example/reference_example.dart | 2 +- example/repository_example.dart | 4 ++-- lib/src/bindings/config.dart | 3 +++ lib/src/bindings/reference.dart | 3 +++ lib/src/bindings/repository.dart | 3 +++ lib/src/config.dart | 6 +++--- lib/src/reference.dart | 2 +- lib/src/repository.dart | 6 +++--- test/config_test.dart | 2 +- test/odb_test.dart | 2 +- test/reference_test.dart | 2 +- test/reflog_test.dart | 2 +- test/repository_test.dart | 6 +++--- 14 files changed, 30 insertions(+), 21 deletions(-) diff --git a/example/config_example.dart b/example/config_example.dart index 57f02aa..079b996 100644 --- a/example/config_example.dart +++ b/example/config_example.dart @@ -9,8 +9,8 @@ void main() { for (final entry in entries.entries) { print('${entry.key}: ${entry.value}'); } - // .close should be called on object to free memory when done. - config.close(); + // free() should be called on object to free memory when done. + config.free(); // Open config file at provided path. // Exception is thrown if file not found. @@ -31,7 +31,7 @@ void main() { // Delete variable repoConfig.deleteEntry('core.variable'); - repoConfig.close(); + repoConfig.free(); } catch (e) { print(e); } @@ -45,7 +45,7 @@ void main() { final userName = globalConfig.getValue('user.name'); print('\nUser Name from global config: $userName'); - globalConfig.close(); + globalConfig.free(); } catch (e) { print('\n$e'); } diff --git a/example/reference_example.dart b/example/reference_example.dart index f9a71f7..40ffd38 100644 --- a/example/reference_example.dart +++ b/example/reference_example.dart @@ -11,5 +11,5 @@ void main() { print('Reference full name: ${ref.name}'); ref.free(); - repo.close(); + repo.free(); } diff --git a/example/repository_example.dart b/example/repository_example.dart index 75e7955..780aa1a 100644 --- a/example/repository_example.dart +++ b/example/repository_example.dart @@ -13,8 +13,8 @@ void main() { print('Is head detached: ${repo.isHeadDetached}'); print('Repository refs: ${repo.references}'); - // close() should be called on object to free memory when done. - repo.close(); + // free() should be called on object to free memory when done. + repo.free(); } catch (e) { print(e); } diff --git a/lib/src/bindings/config.dart b/lib/src/bindings/config.dart index c08b523..e35725c 100644 --- a/lib/src/bindings/config.dart +++ b/lib/src/bindings/config.dart @@ -284,3 +284,6 @@ void deleteMultivar( calloc.free(name); calloc.free(regexpC); } + +/// Free the configuration and its associated memory and files. +void free(Pointer cfg) => libgit2.git_config_free(cfg); diff --git a/lib/src/bindings/reference.dart b/lib/src/bindings/reference.dart index 22e8d2f..484a4ce 100644 --- a/lib/src/bindings/reference.dart +++ b/lib/src/bindings/reference.dart @@ -215,3 +215,6 @@ bool isValidName(String name) { calloc.free(refname); return result == 1 ? true : false; } + +/// Free the given reference. +void free(Pointer ref) => libgit2.git_reference_free(ref); diff --git a/lib/src/bindings/repository.dart b/lib/src/bindings/repository.dart index a08f75a..a1d1bf2 100644 --- a/lib/src/bindings/repository.dart +++ b/lib/src/bindings/repository.dart @@ -446,3 +446,6 @@ Pointer> revParseSingle( return out; } + +/// Free a previously allocated repository. +void free(Pointer repo) => libgit2.git_repository_free(repo); diff --git a/lib/src/config.dart b/lib/src/config.dart index 13942e2..23918c1 100644 --- a/lib/src/config.dart +++ b/lib/src/config.dart @@ -17,7 +17,7 @@ class Config { /// [path] should point to single on-disk file; it's expected to be a native /// Git config file following the default Git config syntax (see man git-config). /// - /// [Config] object should be closed with [close] function to release allocated memory. + /// [Config] object should be closed with [free] function to release allocated memory. Config.open({this.path}) { libgit2.git_libgit2_init(); @@ -145,8 +145,8 @@ class Config { } /// Releases memory allocated for config object. - void close() { - calloc.free(_configPointer); + void free() { + bindings.free(_configPointer); libgit2.git_libgit2_shutdown(); } } diff --git a/lib/src/reference.dart b/lib/src/reference.dart index de51b87..349296c 100644 --- a/lib/src/reference.dart +++ b/lib/src/reference.dart @@ -138,7 +138,7 @@ class Reference { /// Releases memory allocated for reference object. void free() { - calloc.free(_refPointer); + bindings.free(_refPointer); libgit2.git_libgit2_shutdown(); } } diff --git a/lib/src/repository.dart b/lib/src/repository.dart index bd2e67f..2c995ca 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -14,7 +14,7 @@ class Repository { /// or to the working directory. For a bare repository, [path] should directly /// point to the repository folder. /// - /// [Repository] object should be close with [close] function to release allocated memory. + /// [Repository] object should be close with [free] function to release allocated memory. /// /// Throws a [LibGit2Error] if error occured. Repository.open(String path) { @@ -237,8 +237,8 @@ class Repository { String get workdir => bindings.workdir(_repoPointer); /// Releases memory allocated for repository object. - void close() { - calloc.free(_repoPointer); + void free() { + bindings.free(_repoPointer); libgit2.git_libgit2_shutdown(); } diff --git a/test/config_test.dart b/test/config_test.dart index 8a4edf1..0043308 100644 --- a/test/config_test.dart +++ b/test/config_test.dart @@ -26,7 +26,7 @@ void main() { }); tearDown(() { - config.close(); + config.free(); File('$tmpDir/$configFileName').deleteSync(); }); diff --git a/test/odb_test.dart b/test/odb_test.dart index c39826f..ba2dd68 100644 --- a/test/odb_test.dart +++ b/test/odb_test.dart @@ -26,7 +26,7 @@ void main() { }); tearDownAll(() async { - repo.close(); + repo.free(); await Directory(tmpDir).delete(recursive: true); }); diff --git a/test/reference_test.dart b/test/reference_test.dart index a911e8d..2e2abfd 100644 --- a/test/reference_test.dart +++ b/test/reference_test.dart @@ -27,7 +27,7 @@ void main() { }); tearDownAll(() async { - repo.close(); + repo.free(); await Directory(tmpDir).delete(recursive: true); }); diff --git a/test/reflog_test.dart b/test/reflog_test.dart index c1a7d38..cbc893a 100644 --- a/test/reflog_test.dart +++ b/test/reflog_test.dart @@ -27,7 +27,7 @@ void main() { tearDownAll(() async { repo.head.free(); reflog.free(); - repo.close(); + repo.free(); await Directory(tmpDir).delete(recursive: true); }); diff --git a/test/repository_test.dart b/test/repository_test.dart index e6c4963..92e2c65 100644 --- a/test/repository_test.dart +++ b/test/repository_test.dart @@ -22,7 +22,7 @@ void main() { }); tearDown(() { - repo.close(); + repo.free(); }); test('opens successfully', () { @@ -67,7 +67,7 @@ void main() { }); tearDown(() { - repo.close(); + repo.free(); }); test('opens standart repository from working directory successfully', @@ -144,7 +144,7 @@ void main() { }); tearDownAll(() async { - repo.close(); + repo.free(); await Directory(tmpDir).delete(recursive: true); });