pastebin - collaborative debugging tool
rovema.kpaste.net RSS


std_string_custom_new2.cpp - custom basic_string allocator demo
Posted by Anonymous on Mon 27th Jun 2022 12:54
raw | new post

  1. /*
  2.  * std_string_custom_new2.cpp - custom basic_string allocator demo
  3.  *
  4.  * Compile with:
  5.  * clang++ -std=c++17 -g -Wall std_string_custom_new2.cpp
  6.  * Written by Roland Mainy <roland.mainz@nrubsig.org>
  7.  *
  8.  */
  9. #include <iostream>
  10. #include <memory>
  11. #include <limits>
  12.  
  13.  
  14. /* required for |main()| test/demo code */
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <inttypes.h>
  19.  
  20. /* we need this for |std::numeric_limits<size_type>::max()| to work */
  21. #undef max
  22.  
  23. template <typename T> class EvilStrAllocator {
  24. public:
  25.         typedef T value_type;
  26.         typedef value_type* pointer;
  27.         typedef const value_type* const_pointer;
  28.         typedef value_type& reference;
  29.         typedef const value_type& const_reference;
  30.         typedef std::size_t size_type;
  31.         typedef std::ptrdiff_t difference_type;
  32.  
  33. public:
  34.         template<typename U>
  35.         struct rebind {
  36.                 typedef EvilStrAllocator<U> other;
  37.         };
  38.  
  39. public:
  40.         EvilStrAllocator() {}
  41.         ~EvilStrAllocator() {}
  42.         EvilStrAllocator(EvilStrAllocator const&) {}
  43.         template<typename U>
  44.         EvilStrAllocator(EvilStrAllocator<U> const&) {}
  45.  
  46.         pointer address(reference r) {
  47.                 return &r;
  48.         }
  49.  
  50.         const_pointer address(const_reference r) {
  51.                 return &r;
  52.         }
  53.  
  54.         pointer allocate(size_type cnt,
  55.                 typename std::allocator<void>::const_pointer = 0) {
  56.                 T *ptr = (T*)malloc(cnt * sizeof(T));
  57.                 (void)fprintf(stderr, "##allocate %p, cnt=%ld\n",
  58.                         (void*)ptr,
  59.                         (long)cnt);
  60.                 return ptr;
  61.         }
  62.  
  63.         void deallocate(pointer ptr, size_type cnt) {
  64.                 (void)fprintf(stderr, "##free %p, cnt=%ld\n",
  65.                         (void*)ptr,
  66.                         (long)cnt);
  67.                 free((void *)ptr);
  68.         }
  69.  
  70.         size_type max_size() const {
  71.                 return std::numeric_limits<size_type>::max() / sizeof(T);
  72.         }
  73.  
  74.         void construct(pointer p, const T& t) {
  75.                 new(p) T(t);
  76.         }
  77.  
  78.         void destroy(pointer p) {
  79.                 p->~T();
  80.         }
  81.  
  82.         bool operator==(EvilStrAllocator const& a) { return this == &a; }
  83.         bool operator!=(EvilStrAllocator const& a) { return !operator==(a); }
  84. };
  85.  
  86.  
  87. int main(int ac, char *av[])
  88. {
  89.         typedef std::basic_string<char, std::char_traits<char>, EvilStrAllocator<char>> myString;
  90.  
  91.         /*
  92.          * |basic_string| will not make use of an allocator if the
  93.          * string is too small, so we use whitespace as padding,
  94.          + as |strtol()|&co. allows leading whitespaces
  95.          */
  96.         myString str { "                                       72488" };
  97.         myString copy = str;
  98.        
  99.         for(int i=0 ; i < 3 ; i++)
  100.         {
  101.                 myString str2 = " " + copy;
  102.                
  103.                 /*
  104.                  * |std::string s_str = str2;| does not work yet, clang++
  105.                  * says:
  106.                  * std_string_custom_new2.cpp: error: no viable
  107.                  * conversion from 'basic_string<[2 * ...],
  108.                  * EvilStrAllocator<char>>' to 'basic_string<[2 * ...],
  109.                  * (default) std::allocator<char>>'
  110.                  */
  111.                 std::string s_str(str2);
  112.                 copy = s_str;
  113.         }
  114.  
  115.         const char* cstr = copy.c_str();
  116.         intmax_t out = strtoimax(cstr, NULL, 10);
  117.         (void)printf(("strlen(str=|%s|)=%" PRIdMAX ", result=%" PRIdMAX "\n"),
  118.                 cstr,
  119.                 (intmax_t)strlen(cstr),
  120.                 out);
  121.  
  122.         return EXIT_SUCCESS;
  123. }

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