2016-05-30 23:58:58 -04:00
|
|
|
#pragma once
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2017-01-10 23:10:51 -05:00
|
|
|
#include <cstdlib>
|
2016-12-14 09:06:46 -05:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
#include "common.hpp"
|
2016-06-23 16:26:19 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-12-21 17:22:02 -05:00
|
|
|
template <typename T>
|
|
|
|
using malloc_ptr_t = shared_ptr<T>;
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
namespace memory_util {
|
|
|
|
/**
|
|
|
|
* Create a shared pointer using malloc/free
|
2019-10-22 18:47:13 -04:00
|
|
|
*
|
|
|
|
* Generates a noexcept-type warning because the mangled name for
|
|
|
|
* malloc_ptr_t will change in C++17. This doesn't affect use because we have
|
|
|
|
* no public ABI, so we ignore it here.
|
|
|
|
* See also this SO answer: https://stackoverflow.com/a/46857525/5363071
|
2016-06-14 23:32:35 -04:00
|
|
|
*/
|
2019-10-22 18:47:13 -04:00
|
|
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wnoexcept-type"
|
2017-01-10 23:10:51 -05:00
|
|
|
template <typename T, size_t Size = sizeof(T), typename Deleter = decltype(std::free)>
|
|
|
|
inline malloc_ptr_t<T> make_malloc_ptr(Deleter deleter = std::free) {
|
2016-12-21 17:22:02 -05:00
|
|
|
return malloc_ptr_t<T>(static_cast<T*>(calloc(1, Size)), deleter);
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
2019-10-22 18:47:13 -04:00
|
|
|
#pragma GCC diagnostic pop
|
2016-06-23 16:26:19 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
/**
|
|
|
|
* Get the number of elements in T
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline auto countof(T& p) {
|
|
|
|
return sizeof(p) / sizeof(p[0]);
|
2016-06-23 16:26:19 -04:00
|
|
|
}
|
2019-10-22 18:47:13 -04:00
|
|
|
} // namespace memory_util
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|