pastebin - collaborative debugging tool
rovema.kpaste.net RSS


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

  1.  
  2. /*
  3.  * c++pmr2.cpp - Simple C++17 PMR allocate/deallocate from multiple allocators demo
  4.  *
  5.  * Compile and run with
  6.  * ---- snip ----
  7.  * rm -f a.exe* ; clang++ -target x86_64-w64-mingw32 -std=c++17 -Bstatic c++pmr2.cpp
  8.  * ---- snip ----
  9.  * OR (Cygwin 3.6 with gcc 15.0):
  10.  * ---- snip ----
  11.  * rm -f a.exe* ; g++ -std=c++20 -Wall -Wextra -D_GLIBCXX_USE_CXX11_ABI c++pmr2.cpp
  12.  * ---- snip ----
  13.  *
  14.  * Output:
  15.  * ---- snip ----
  16.  * $ PATH+=':/usr/x86_64-w64-mingw32/sys-root/mingw/bin/' ./a.exe
  17.  * # Cycle: 0: str='12'
  18.  * # Cycle: 1: str='1212'
  19.  * # Cycle: 2: str='12121212'
  20.  * >> ALLOC(name='smirre',size=31), p=0xa00026e70
  21.  * >> ALLOC(name='akka von kebnekaise',size=31), p=0xa000004f0
  22.  * # Cycle: 3: str='1212121212121212'
  23.  * >> ALLOC(name='smirre',size=31), p=0xa00026ef0
  24.  * >> ALLOC(name='akka von kebnekaise',size=31), p=0xa00026f20
  25.  * >> ALLOC(name='smirre',size=61), p=0xa00026f80
  26.  * <<  FREE(name='smirre',p=0xa00026e70)
  27.  * >> ALLOC(name='akka von kebnekaise',size=61), p=0xa00026fd0
  28.  * <<  FREE(name='akka von kebnekaise',p=0xa000004f0)
  29.  * # Cycle: 4: str='12121212121212121212121212121212'
  30.  * >> ALLOC(name='smirre',size=61), p=0xa00027020
  31.  * <<  FREE(name='smirre',p=0xa00026ef0)
  32.  * >> ALLOC(name='akka von kebnekaise',size=61), p=0xa00027070
  33.  * <<  FREE(name='akka von kebnekaise',p=0xa00026f20)
  34.  * >> ALLOC(name='smirre',size=121), p=0xa000270c0
  35.  * <<  FREE(name='smirre',p=0xa00026f80)
  36.  * >> ALLOC(name='akka von kebnekaise',size=121), p=0xa00026ef0
  37.  * <<  FREE(name='akka von kebnekaise',p=0xa00026fd0)
  38.  * # Cycle: 5: str='1212121212121212121212121212121212121212121212121212121212121212'
  39.  * >> ALLOC(name='smirre',size=121), p=0xa00026f80
  40.  * <<  FREE(name='smirre',p=0xa00027020)
  41.  * >> ALLOC(name='akka von kebnekaise',size=121), p=0xa00027150
  42.  * <<  FREE(name='akka von kebnekaise',p=0xa00027070)
  43.  * >> ALLOC(name='smirre',size=241), p=0xa000271e0
  44.  * <<  FREE(name='smirre',p=0xa000270c0)
  45.  * >> ALLOC(name='akka von kebnekaise',size=241), p=0xa00027020
  46.  * <<  FREE(name='akka von kebnekaise',p=0xa00026ef0)
  47.  * # Cycle: 6: str='12121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212'
  48.  * >> ALLOC(name='smirre',size=241), p=0xa000272e0
  49.  * <<  FREE(name='smirre',p=0xa00026f80)
  50.  * >> ALLOC(name='akka von kebnekaise',size=241), p=0xa00026ef0
  51.  * <<  FREE(name='akka von kebnekaise',p=0xa00027150)
  52.  * >> ALLOC(name='smirre',size=481), p=0xa000274f0
  53.  * <<  FREE(name='smirre',p=0xa000271e0)
  54.  * >> ALLOC(name='akka von kebnekaise',size=481), p=0xa000276e0
  55.  * <<  FREE(name='akka von kebnekaise',p=0xa00027020)
  56.  * # Cycle: 7: str='1212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212'
  57.  * >> ALLOC(name='smirre',size=481), p=0xa00026ff0
  58.  * <<  FREE(name='smirre',p=0xa000272e0)
  59.  * >> ALLOC(name='akka von kebnekaise',size=481), p=0xa000271e0
  60.  * <<  FREE(name='akka von kebnekaise',p=0xa00026ef0)
  61.  * >> ALLOC(name='smirre',size=961), p=0xa00027ae0
  62.  * <<  FREE(name='smirre',p=0xa000274f0)
  63.  * >> ALLOC(name='akka von kebnekaise',size=961), p=0xa00027eb0
  64.  * <<  FREE(name='akka von kebnekaise',p=0xa000276e0)
  65.  * 12121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212
  66.  * <<  FREE(name='smirre',p=0xa00027ae0)
  67.  * <<  FREE(name='akka von kebnekaise',p=0xa000271e0)
  68.  * <<  FREE(name='smirre',p=0xa00026ff0)
  69.  * <<  FREE(name='akka von kebnekaise',p=0xa00027eb0)
  70.  * ---- snip ----
  71.  *
  72.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  73.  */
  74.  
  75. #include <stdlib.h>
  76. #include <stdio.h>
  77. #include <string.h>
  78. #include <array>
  79. #include <cstddef>
  80. #include <iostream>
  81. #include <memory_resource>
  82.  
  83. #define ARG_NOT_USED(a) ((void)(a))
  84.  
  85. #if 1
  86. #define D(x) x
  87. #else
  88. #define D(x)
  89. #endif
  90.  
  91. /*
  92.  * C++ allocator implementation - do NOT use C++ functions here
  93.  * for logging etc.
  94.  */
  95. class here_be_monsters : public std::pmr::synchronized_pool_resource {
  96.         const char *name;
  97. public:
  98.         here_be_monsters(const char *name) {
  99.                 this->name = name;
  100.         }
  101. protected:
  102.         void* do_allocate( std::size_t bytes, std::size_t alignment ) {
  103.                 void *p;
  104.                 ARG_NOT_USED(alignment);
  105.                 p = malloc(bytes);
  106.                 D((void)fprintf(stderr, ">> ALLOC(name='%s',size=%ld), p=%p\n", name, (long)bytes, p));
  107.                 D((void)fflush(stderr));
  108.                 return p;
  109.         }
  110.        
  111.         void do_deallocate( void* p, std::size_t bytes, std::size_t alignment ) {
  112.                 ARG_NOT_USED(alignment);
  113.                 ARG_NOT_USED(bytes);
  114.                 D((void)fprintf(stderr, "<<  FREE(name='%s',p=%p)\n", name, p));
  115.                 D((void)fflush(stderr));
  116.                 free(p);
  117.         }
  118. };
  119.  
  120. int main(int ac, char *av[])
  121. {
  122.         ARG_NOT_USED(ac);
  123.         ARG_NOT_USED(av);
  124.  
  125.         here_be_monsters pool1("akka von kebnekaise");
  126.         here_be_monsters pool2("smirre");
  127.  
  128.         std::pmr::polymorphic_allocator<char> pa1{&pool1};
  129.         std::pmr::polymorphic_allocator<char> pa2{&pool2};
  130.         std::pmr::string s{"12", pa1};
  131.         std::pmr::string s2{pa2};
  132.         std::pmr::string s3{pa1};
  133.         std::pmr::string s4{pa2};
  134.  
  135.         try {
  136.                 for (int i=0 ; i < 8 ; i++) {
  137.                         std::cout << "# Cycle: " << i << ": str='" << s << "'" << std::endl;
  138.                         D(std::cout.flush());
  139.                         s2 = s;
  140.                         s3 = s;
  141.                         s4 = s2 + s3;
  142.                         s = s4;
  143.                 }
  144.  
  145.                 std::cout << s << std::endl;
  146.                 D(std::cout.flush());
  147.         } catch(const std::exception &e) {
  148.                 std::cerr << "DIE!!" << e.what() << std::endl;
  149.                 D(std::cerr.flush());
  150.         }
  151.  
  152.         return EXIT_SUCCESS;
  153. }

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at