2016-05-30 23:58:58 -04:00
|
|
|
#pragma once
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-12-14 09:06:46 -05:00
|
|
|
#include <cstring>
|
|
|
|
|
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-06-14 23:32:35 -04:00
|
|
|
namespace memory_util {
|
|
|
|
/**
|
|
|
|
* Create a shared pointer using malloc/free
|
|
|
|
*/
|
2016-12-03 09:44:54 -05:00
|
|
|
template <typename T, typename Deleter = decltype(free)>
|
|
|
|
inline auto make_malloc_ptr(size_t size = sizeof(T), Deleter deleter = free) {
|
2016-12-14 09:06:46 -05:00
|
|
|
shared_ptr<T> ptr{static_cast<T*>(malloc(size)), deleter};
|
|
|
|
memset(ptr.get(), 0, size);
|
|
|
|
return ptr;
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
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
|
|
|
}
|
2016-11-18 22:03:18 -05:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using malloc_ptr_t = shared_ptr<T>;
|
2016-06-23 16:26:19 -04:00
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|