summaryrefslogtreecommitdiff
path: root/lib/core/c++/memory.hpp
blob: b6a089e449fc40d1563cade3b35dae5e7e6ca686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once

#include "common.hpp"
#include "chunk.hpp"

namespace kel {
namespace lbm {

namespace impl {

template<typename Sch>
struct memory_size_helper;

template<typename Sch, uint64_t N>
struct memory_size_helper<sch::Primitive<Sch,N>> {
	static constexpr uint64_t bytes = N;
};

template<typename Sch, uint64_t... N>
struct memory_size_helper<sch::FixedArray<Sch,N...>> {
	static constexpr uint64_t bytes = saw::ct_multiply<uint64_t,N...>::value * memory_size_helper<Sch>::bytes;
};

template<typename Sch, uint64_t Ghost, uint64_t... N>
struct memory_size_helper<sch::Chunk<Sch,Ghost,N...>> {
	static constexpr uint64_t bytes = memory_size_helper<typename sch::Chunk<Sch,Ghost,N...>::InnerSchema>::bytes;
};

template<typename... T>
class memory_estimate_helper final {
private:

	template<uint64_t i>
	static void apply_i(saw::data<sch::UInt64>& bytes){

		if constexpr ( i < sizeof...(T)){
			using T_I = typename saw::parameter_pack_type<i,T...>::type;

			bytes.set(bytes.get() + memory_size_helper<T_I>::bytes);

			apply_i<i+1u>(bytes);
		}
	}

public:

	static void apply(saw::data<sch::UInt64>& bytes){
		apply_i<0u>(bytes);
	}
};
}

template<typename... T>
saw::data<sch::UInt64> memory_estimate(){
	saw::data<sch::UInt64> bytes;

	impl::memory_estimate_helper<T...>::apply(bytes);

	return bytes;
}
}
}