diff options
Diffstat (limited to 'a6/noalloc.cc')
-rw-r--r-- | a6/noalloc.cc | 119 |
1 files changed, 83 insertions, 36 deletions
diff --git a/a6/noalloc.cc b/a6/noalloc.cc index 0da4025..898efb4 100644 --- a/a6/noalloc.cc +++ b/a6/noalloc.cc @@ -1,17 +1,12 @@ #include <iostream> #include <functional> -// Universal allocator -namespace memory -{ -void* ptr{}; -} +#include "generator.h" -void* operator new([[maybe_unused]]std::size_t n) // throw(std::bad_alloc) - don't throw +void* operator new([[maybe_unused]]std::size_t n) { std::cout << "new\n"; - // Just return the supplied stack pointer - return memory::ptr; + throw std::bad_alloc(); } void operator delete(void*) throw() @@ -26,43 +21,95 @@ void operator delete(void*, std::size_t) throw() // Do nothing. actual memory is allocated on the stack } -//void foo() __attribute__((noinline)) +template<typename T> +Generator<T> iota(T start) +{ + while(true) + { + co_yield start++; + } +} + int main() { - char buf[256]; - memory::ptr = buf; + std::cout << " ** std::string:\n"; + { + std::string str(15, 'a'); // 15 characters + zero termination - ok + str = "123456789012345"; // 15 characters + zero termination - ok + try + { + str = "1234567890123456"; // 16 characters + zero termination + } + catch(std::bad_alloc &e) + { + std::cout << "Too big for SSO!\n"; + } + } + std::cout << '\n'; - std::cout << " ** strings:\n"; - { // strings - // now this is ok: - std::string str(32, 'a'); - std::cout << str << '\n'; + std::cout << " ** std::vector:\n"; + { + try + { + std::vector<int> vec{42}; // even one element is too much, no SBO + } + catch(std::bad_alloc &e) + { + std::cout << "No SBO for std::vector!\n"; + } + } + std::cout << '\n'; - std::string str2(24, 'b'); - std::cout << str << '\n'; // the contents of str has been overwritten - // this will also allocate, but supply the same buffer - ok - str = "hello world hello world hello world"; + std::cout << " ** std::function:\n"; + { + { + std::function<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; + }; + } - // this is also ok, but due to SSO - std::string sso{"hello"}; +// try // this line seem to break gcc... +// { +// std::function<int()> f; +// char foo[17]{}; +// f = [foo]() +// { +// int i = 0; +// for(auto v : foo) i += v; +// return i; +// }; +// } +// catch(std::bad_alloc &e) +// { +// std::cout << "Too big for SBO in std::function!\n"; +// } } + std::cout << '\n'; + - std::cout << " ** lambdas:\n"; - { // lambdas - std::function<int()> f; + std::cout << " ** co-routines:\n"; + { + try { - char foo[16]{}; - f = [=]()__attribute__((noinline)) // capture up 16 bytes - ok - { - int i = 0; - for(auto v : foo) - { - i += v; - } - return i; - }; // capture foo by copy - inlined + 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"; } - [[maybe_unused]]auto x = f(); } + std::cout << '\n'; } |