summaryrefslogtreecommitdiff
path: root/a6/custom.cc
blob: 4b95e0b39ba7c4f0ffa75327ba95f07a8e812c72 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <functional>
#include <limits>
#include <memory>
#include <vector>
#include <cstddef>
#include <memory>

#include "generator_stack.h"

void* operator new([[maybe_unused]]std::size_t n)
{
	std::cout << "new\n";
	throw std::bad_alloc();
}

void operator delete(void*) throw()
{
	std::cout << "delete\n";
	// Do nothing. actual memory is allocated on the stack
}

void operator delete(void*, std::size_t) throw()
{
	std::cout << "delete[]\n";
	// Do nothing. actual memory is allocated on the stack
}

// Code heavily inspired by:
//  https://stackoverflow.com/questions/66891368/template-parametric-type-allocator-in-c
template <typename T, std::size_t S>
struct StackAllocator
{
	using value_type = T;
	using size_type = size_t;
	using pointer = value_type*;

	template<class U> struct rebind { using other = StackAllocator<U, S>; };

	StackAllocator() = default;
	StackAllocator(const StackAllocator&) = default;
	template <typename U> StackAllocator(const StackAllocator<U, S>&) {}

	pointer allocate(size_type n)
	{
		if(n > S) throw std::bad_alloc();
		return buf;
	}

	void deallocate(void*, size_type)
	{
	}

private:
	T buf[S];
};

template<typename T>
Generator<T, 64> iota(T start)
{
	while(true)
	{
		co_yield start++;
	}
}

int main()
{
	std::cout << " ** std::string:\n";
	{
		std::basic_string<char, std::char_traits<char>, StackAllocator<char, 32>> str(31, 'a');
		std::cout << str << " " << sizeof(str) << '\n';

		std::basic_string<char, std::char_traits<char>, StackAllocator<char, 64>> str2(31, 'a');
		std::cout << str2 << " " << sizeof(str2) << '\n';
	}
	std::cout << '\n';

	std::cout << " ** std::vector:\n";
	{
		try
		{
			std::vector<int, StackAllocator<int, 10>> vec{42};
		}
		catch(std::bad_alloc &e)
		{
			std::cout << "No SBO for std::vector!\n";
		}
	}
	std::cout << '\n';


	std::cout << " ** std::function:\n";
	{
//		{ // Support for allocators with std::function removed in C++17
//			std::function<std::allocator_arg, StackAllocator<int, 10>, int()> f;
//			char foo[16]{};
//			f = [foo]() // capture up to 16 bytes is ok for std::function
//			    {
//				    int i = 0;
//				    for(auto v : foo) i += v;
//				    return i;
//			    };
//		}
	}
	std::cout << '\n';


	std::cout << " ** co-routines:\n";
	{
		try
		{
			auto gen = iota(0);
			while(true)
			{
				auto i = gen();
				if(i > 10) break;
				std::cout << i << " ";
			}
			std::cout << '\n';
		}
		catch(std::bad_alloc &e)
		{
			std::cout << "Co-routines always allocate!\n";
		}
	}
	std::cout << '\n';
}