polybar/include/utils/memory.hpp

31 lines
600 B
C++
Raw Normal View History

#pragma once
2016-05-19 14:41:06 +00:00
#include <cstring>
2016-06-15 03:32:35 +00:00
#include "common.hpp"
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-05-19 14:41:06 +00:00
2016-12-21 22:22:02 +00:00
template <typename T>
using malloc_ptr_t = shared_ptr<T>;
2016-06-15 03:32:35 +00:00
namespace memory_util {
/**
* Create a shared pointer using malloc/free
*/
2016-12-21 22:22:02 +00:00
template <typename T, size_t Size = sizeof(T), typename Deleter = decltype(free)>
inline malloc_ptr_t<T> make_malloc_ptr(Deleter deleter = free) {
return malloc_ptr_t<T>(static_cast<T*>(calloc(1, Size)), deleter);
2016-06-15 03:32:35 +00:00
}
2016-06-15 03:32:35 +00:00
/**
* Get the number of elements in T
*/
template <typename T>
inline auto countof(T& p) {
return sizeof(p) / sizeof(p[0]);
}
}
2016-06-15 03:32:35 +00:00
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END