- /*
- * c++pmr2.cpp - Simple C++17 PMR allocate/deallocate from multiple allocators demo
- *
- * Compile and run with
- * ---- snip ----
- * rm -f a.exe* ; clang++ -target x86_64-w64-mingw32 -std=c++17 -Bstatic c++pmr2.cpp
- * ---- snip ----
- * OR (Cygwin 3.6 with gcc 15.0):
- * ---- snip ----
- * rm -f a.exe* ; g++ -std=c++20 -Wall -Wextra -D_GLIBCXX_USE_CXX11_ABI c++pmr2.cpp
- * ---- snip ----
- *
- * Output:
- * ---- snip ----
- * $ PATH+=':/usr/x86_64-w64-mingw32/sys-root/mingw/bin/' ./a.exe
- * # Cycle: 0: str='12'
- * # Cycle: 1: str='1212'
- * # Cycle: 2: str='12121212'
- * >> ALLOC(name='smirre',size=31), p=0xa00026e70
- * >> ALLOC(name='akka von kebnekaise',size=31), p=0xa000004f0
- * # Cycle: 3: str='1212121212121212'
- * >> ALLOC(name='smirre',size=31), p=0xa00026ef0
- * >> ALLOC(name='akka von kebnekaise',size=31), p=0xa00026f20
- * >> ALLOC(name='smirre',size=61), p=0xa00026f80
- * << FREE(name='smirre',p=0xa00026e70)
- * >> ALLOC(name='akka von kebnekaise',size=61), p=0xa00026fd0
- * << FREE(name='akka von kebnekaise',p=0xa000004f0)
- * # Cycle: 4: str='12121212121212121212121212121212'
- * >> ALLOC(name='smirre',size=61), p=0xa00027020
- * << FREE(name='smirre',p=0xa00026ef0)
- * >> ALLOC(name='akka von kebnekaise',size=61), p=0xa00027070
- * << FREE(name='akka von kebnekaise',p=0xa00026f20)
- * >> ALLOC(name='smirre',size=121), p=0xa000270c0
- * << FREE(name='smirre',p=0xa00026f80)
- * >> ALLOC(name='akka von kebnekaise',size=121), p=0xa00026ef0
- * << FREE(name='akka von kebnekaise',p=0xa00026fd0)
- * # Cycle: 5: str='1212121212121212121212121212121212121212121212121212121212121212'
- * >> ALLOC(name='smirre',size=121), p=0xa00026f80
- * << FREE(name='smirre',p=0xa00027020)
- * >> ALLOC(name='akka von kebnekaise',size=121), p=0xa00027150
- * << FREE(name='akka von kebnekaise',p=0xa00027070)
- * >> ALLOC(name='smirre',size=241), p=0xa000271e0
- * << FREE(name='smirre',p=0xa000270c0)
- * >> ALLOC(name='akka von kebnekaise',size=241), p=0xa00027020
- * << FREE(name='akka von kebnekaise',p=0xa00026ef0)
- * # Cycle: 6: str='12121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212'
- * >> ALLOC(name='smirre',size=241), p=0xa000272e0
- * << FREE(name='smirre',p=0xa00026f80)
- * >> ALLOC(name='akka von kebnekaise',size=241), p=0xa00026ef0
- * << FREE(name='akka von kebnekaise',p=0xa00027150)
- * >> ALLOC(name='smirre',size=481), p=0xa000274f0
- * << FREE(name='smirre',p=0xa000271e0)
- * >> ALLOC(name='akka von kebnekaise',size=481), p=0xa000276e0
- * << FREE(name='akka von kebnekaise',p=0xa00027020)
- * # Cycle: 7: str='1212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212'
- * >> ALLOC(name='smirre',size=481), p=0xa00026ff0
- * << FREE(name='smirre',p=0xa000272e0)
- * >> ALLOC(name='akka von kebnekaise',size=481), p=0xa000271e0
- * << FREE(name='akka von kebnekaise',p=0xa00026ef0)
- * >> ALLOC(name='smirre',size=961), p=0xa00027ae0
- * << FREE(name='smirre',p=0xa000274f0)
- * >> ALLOC(name='akka von kebnekaise',size=961), p=0xa00027eb0
- * << FREE(name='akka von kebnekaise',p=0xa000276e0)
- * 12121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212
- * << FREE(name='smirre',p=0xa00027ae0)
- * << FREE(name='akka von kebnekaise',p=0xa000271e0)
- * << FREE(name='smirre',p=0xa00026ff0)
- * << FREE(name='akka von kebnekaise',p=0xa00027eb0)
- * ---- snip ----
- *
- * Written by Roland Mainz <roland.mainz@nrubsig.org>
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <array>
- #include <cstddef>
- #include <iostream>
- #include <memory_resource>
- #define ARG_NOT_USED(a) ((void)(a))
- #if 1
- #define D(x) x
- #else
- #define D(x)
- #endif
- /*
- * C++ allocator implementation - do NOT use C++ functions here
- * for logging etc.
- */
- class here_be_monsters : public std::pmr::synchronized_pool_resource {
- const char *name;
- public:
- here_be_monsters(const char *name) {
- this->name = name;
- }
- protected:
- void* do_allocate( std::size_t bytes, std::size_t alignment ) {
- void *p;
- ARG_NOT_USED(alignment);
- p = malloc(bytes);
- D((void)fprintf(stderr, ">> ALLOC(name='%s',size=%ld), p=%p\n", name, (long)bytes, p));
- D((void)fflush(stderr));
- return p;
- }
- void do_deallocate( void* p, std::size_t bytes, std::size_t alignment ) {
- ARG_NOT_USED(alignment);
- ARG_NOT_USED(bytes);
- D((void)fprintf(stderr, "<< FREE(name='%s',p=%p)\n", name, p));
- D((void)fflush(stderr));
- free(p);
- }
- };
- int main(int ac, char *av[])
- {
- ARG_NOT_USED(ac);
- ARG_NOT_USED(av);
- here_be_monsters pool1("akka von kebnekaise");
- here_be_monsters pool2("smirre");
- std::pmr::polymorphic_allocator<char> pa1{&pool1};
- std::pmr::polymorphic_allocator<char> pa2{&pool2};
- std::pmr::string s{"12", pa1};
- std::pmr::string s2{pa2};
- std::pmr::string s3{pa1};
- std::pmr::string s4{pa2};
- try {
- for (int i=0 ; i < 8 ; i++) {
- std::cout << "# Cycle: " << i << ": str='" << s << "'" << std::endl;
- D(std::cout.flush());
- s2 = s;
- s3 = s;
- s4 = s2 + s3;
- s = s4;
- }
- std::cout << s << std::endl;
- D(std::cout.flush());
- } catch(const std::exception &e) {
- std::cerr << "DIE!!" << e.what() << std::endl;
- D(std::cerr.flush());
- }
- return EXIT_SUCCESS;
- }
c++pmr2.cpp - Simple C++17 PMR allocate/deallocate from multiple allocators demo
Posted by Anonymous on Tue 19th Nov 2024 10:00
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.