Skip to content

Commit dcffce2

Browse files
committed
Move Deleter into a separate header
1 parent dad1a4e commit dcffce2

7 files changed

Lines changed: 25 additions & 21 deletions

File tree

src/libfetchers/git-lfs-fetch.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "nix/util/util.hh"
99
#include "nix/util/hash.hh"
1010
#include "nix/store/ssh.hh"
11+
#include "nix/util/deleter.hh"
1112

1213
#include <git2/attr.h>
1314
#include <git2/config.h>

src/libfetchers/git-utils.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "nix/util/thread-pool.hh"
1515
#include "nix/util/pool.hh"
1616
#include "nix/util/executable-path.hh"
17+
#include "nix/util/deleter.hh"
1718

1819
#include <git2/attr.h>
1920
#include <git2/blob.h>

src/libfetchers/include/nix/fetchers/git-utils.hh

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,6 @@ struct GitRepo
136136
virtual Hash dereferenceSingletonDirectory(const Hash & oid) = 0;
137137
};
138138

139-
// A helper to ensure that the `git_*_free` functions get called.
140-
template<auto del>
141-
struct Deleter
142-
{
143-
template<typename T>
144-
void operator()(T * p) const
145-
{
146-
del(p);
147-
};
148-
};
149-
150139
// A helper to ensure that we don't leak objects returned by libgit2.
151140
template<typename T>
152141
struct Setter
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
namespace nix {
4+
5+
/**
6+
* A helper for `std::unique_ptr` that ensures that C APIs that require manual memory management get properly freed. The
7+
* template parameter `del` is a function that takes a pointer and frees it.
8+
*/
9+
template<auto del>
10+
struct Deleter
11+
{
12+
template<typename T>
13+
void operator()(T * p) const
14+
{
15+
del(p);
16+
};
17+
};
18+
19+
} // namespace nix

src/libutil/include/nix/util/file-system.hh

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "nix/util/types.hh"
1313
#include "nix/util/file-descriptor.hh"
1414
#include "nix/util/file-path.hh"
15+
#include "nix/util/deleter.hh"
1516

1617
#include <filesystem>
1718
#include <sys/types.h>
@@ -374,15 +375,7 @@ public:
374375
}
375376
};
376377

377-
struct DIRDeleter
378-
{
379-
void operator()(DIR * dir) const
380-
{
381-
closedir(dir);
382-
}
383-
};
384-
385-
typedef std::unique_ptr<DIR, DIRDeleter> AutoCloseDir;
378+
typedef std::unique_ptr<DIR, Deleter<closedir>> AutoCloseDir;
386379

387380
/**
388381
* Create a temporary directory.

src/libutil/include/nix/util/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ headers = [ config_pub_h ] + files(
3131
'config-impl.hh',
3232
'configuration.hh',
3333
'current-process.hh',
34+
'deleter.hh',
3435
'demangle.hh',
3536
'english.hh',
3637
'environment-variables.hh',

src/nix/serve.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "nix/cmd/command.hh"
22
#include "nix/util/file-system.hh"
33
#include "nix/util/signals.hh"
4-
#include "nix/fetchers/git-utils.hh" // for Deleter
4+
#include "nix/util/deleter.hh"
55
#include "nix/store/nar-info.hh"
66

77
#include <future>

0 commit comments

Comments
 (0)