-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix memory leaks and add helper memory.h util
Signed-off-by: Raul Metsma <raul@metsma.ee>
- Loading branch information
Showing
7 changed files
with
113 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* libcdoc | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
|
||
namespace libcdoc { | ||
|
||
template<auto D> | ||
struct free_deleter | ||
{ | ||
template<class T> | ||
void operator()(T *p) const noexcept | ||
{ | ||
D(p); | ||
} | ||
}; | ||
|
||
template<typename> struct free_argument; | ||
template<class T, class R> | ||
struct free_argument<R (*)(T *)> | ||
{ | ||
using type = T; | ||
}; | ||
|
||
template <class T> | ||
using unique_free_t = std::unique_ptr<T, void(*)(T*)>; | ||
|
||
template<class T, typename D> | ||
[[nodiscard]] | ||
constexpr std::unique_ptr<T, D> make_unique_ptr(T *p, D d) noexcept | ||
{ | ||
return {p, std::forward<D>(d)}; | ||
} | ||
|
||
template<auto D, class T> | ||
[[nodiscard]] | ||
constexpr auto make_unique_ptr(T *p) noexcept | ||
{ | ||
return std::unique_ptr<T, free_deleter<D>>(p); | ||
} | ||
|
||
template<auto D> | ||
[[nodiscard]] | ||
constexpr auto make_unique_ptr(nullptr_t) noexcept | ||
{ | ||
using T = typename free_argument<decltype(D)>::type; | ||
return std::unique_ptr<T, free_deleter<D>>(nullptr); | ||
} | ||
|
||
template<auto D, class P> | ||
[[nodiscard]] | ||
constexpr auto make_unique_cast(P *p) noexcept | ||
{ | ||
using T = typename free_argument<decltype(D)>::type; | ||
return make_unique_ptr<D>(static_cast<T*>(p)); | ||
} | ||
|
||
} |