pastebin - collaborative debugging tool
rovema.kpaste.net RSS


custom_c++_operator_new2.cpp - custom allocator demo
Posted by Anonymous on Mon 15th Aug 2022 20:30
raw | new post
modification of post by Anonymous (view diff)

  1. /*
  2.  * custom_c++_operator_new2.cpp - custom allocator demo
  3.  *
  4.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  5.  *
  6.  * - Compile with:
  7.  * $ clang++ -std=c++17 -g -Wall /home/rmainz/work/tlsf/try4/TLSF-2.4.6/src/tlsf.o custom_c++_operator_new2.cpp #
  8.  *
  9.  * - Watch TLSF-calls with:
  10.  * $ ltrace -C -x '*' ./a.out #
  11.  */
  12.  
  13. #define USE_STATIC_BUFFER_ALLOC 0
  14. #define USE_TLSF_ALLOC 1
  15. #define USE_LIBC_MALLOC 0
  16.  
  17. #define STATIC_BUFFER_ALLOC_BUFFER_SIZE (512)
  18.  
  19. #include <iostream>
  20. #include <memory>
  21. #include <limits>
  22.  
  23.  
  24. /* required for |main()| test/demo code */
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <inttypes.h>
  30.  
  31. #if USE_TLSF_ALLOC
  32. extern "C" {
  33. #include "/home/rmainz/work/tlsf/try4/TLSF-2.4.6/src/tlsf.h"
  34. }
  35. #endif /* USE_TLSF_ALLOC */
  36.  
  37. /* misc */
  38. #define STDERR_MSG(msg) \
  39.         (void)write(STDERR_FILENO, (msg), strlen(msg))
  40.  
  41.  
  42.  
  43. #if USE_STATIC_BUFFER_ALLOC
  44. extern "C" {
  45. static struct
  46. {
  47.         char buffer[STATIC_BUFFER_ALLOC_BUFFER_SIZE];
  48.         /* more complex cases would require |pthread_once()| */
  49.         char *buf_ptr = buffer;
  50. } buf_alloc;
  51.  
  52. void *my_buffer_alloc(size_t size)
  53. {
  54. #define SIZE_ALIGN(size, align) \
  55.         ((size)+((align)-1)-((size)-1)%(align))
  56.  
  57.         void *mem;
  58.         size_t bs;
  59.        
  60.         STDERR_MSG("my_buffer_alloc\n");
  61.        
  62.         bs = SIZE_ALIGN(size, 16);
  63.  
  64.         mem = buf_alloc.buf_ptr;
  65.         buf_alloc.buf_ptr += bs;
  66.        
  67.         /* could we satisfy the allocation request ? */
  68.         if ((buf_alloc.buf_ptr - buf_alloc.buffer) >= sizeof(buf_alloc.buffer))
  69.                 return NULL;
  70.                
  71. #if 0
  72.         (void)fprintf(stderr, "my_buffer_alloc(size=%ld, bs=%ld, mem=%lx\n",
  73.                 (long)size, (long)bs, (long)mem);
  74. #endif
  75.        
  76.         return mem;
  77. }
  78.  
  79. void my_buffer_dealloc(void *ptr)
  80. {
  81.         /*
  82.          * range check - pointer must point within |buf_alloc.buffer|!
  83.          *
  84.          * We use this simple (and cheap) check to catch invalid
  85.          * pointers, or (more important) pointers to memory from
  86.          * other allocators (if we mix&match multiple different
  87.          * allocators or different memory pools from the same
  88.          * allocator system).
  89.          */
  90.         ptrdiff_t x = (char *)ptr - buf_alloc.buffer;
  91.         if ((x < 0) || (x >= (ptrdiff_t)sizeof(buf_alloc.buffer))) {
  92.                 /* BUG: LAZY - we should not use stdio here... */
  93.                 (void)fprintf(stderr, "delete(): bad pointer %lx!\n", (long)x);
  94.         }
  95. }
  96. } /* extern "C" */
  97. #endif /* USE_STATIC_BUFFER_ALLOC */
  98.  
  99.  
  100. void *
  101. operator new(std::size_t size)
  102. //     throw(std::bad_alloc)
  103. {
  104.         STDERR_MSG("##new\n");
  105.         if (size == 0)
  106.                 size = 1;
  107.         void* p;
  108. #if USE_STATIC_BUFFER_ALLOC
  109.         if ((p = my_buffer_alloc(size)) == 0)
  110. #elif USE_TLSF_ALLOC
  111.         if ((p = ::tlsf_malloc(size)) == 0)
  112. #elif USE_LIBC_MALLOC
  113.         if ((p = ::malloc(size)) == 0)
  114. #else
  115. #error No allocator library selected.
  116. #endif /* USE_STATIC_BUFFER_ALLOC */
  117.         {
  118.                 throw std::bad_alloc();
  119.         }
  120.         return p;
  121. }
  122.  
  123.  
  124. void
  125. operator delete(void* ptr) _GLIBCXX_USE_NOEXCEPT
  126. {
  127.         STDERR_MSG("##delete\n");
  128. #if USE_STATIC_BUFFER_ALLOC
  129.         my_buffer_dealloc(ptr);
  130. #elif USE_TLSF_ALLOC
  131.         if (ptr)
  132.                 ::tlsf_free(ptr);
  133. #elif USE_LIBC_MALLOC
  134.         if (ptr)
  135.                 ::free(ptr);
  136. #else
  137. #error No allocator library selected
  138. #endif /* USE_STATIC_BUFFER_ALLOC */
  139. }
  140.  
  141.  
  142. /*
  143.  * static variables:
  144.  * Their memory is allocated/constructor called before calling
  145.  * |main()|, and their destructor is called and memory deallocated
  146.  * after |main()| returned.
  147.  * This causes some nasty headaches for custom |operator new|, e.g.
  148.  * in case we want to rely on stdio, C++ iostream, or other
  149.  * libc/libc++ modules. And don't even start to ask about headaches
  150.  * related to STL and C++ exceptions...
  151.  */
  152. static int *s_f = new int(59);
  153.  
  154. static std::string s = "xjsdsqwertzuiopasdfghjklxcvbnm,poiuztrewlkjhgfdmnbvcxjsddljkjklsdfjklsddfljkx";
  155.  
  156.  
  157. int main(int ac, char *av[])
  158. {
  159.         (void)puts("# >> main:start.");
  160.        
  161.         int *f = new int(84);
  162.        
  163.        
  164.         std::string s = "13123123123122354245234534534534534534656456456456456456242342341233423423524542hello";
  165.         s+=" ";
  166.         s+="world";
  167.  
  168.         std::cout << s << "#" << *f << "," << *s_f << "#" << std::endl;
  169.  
  170.         delete f;
  171.  
  172. #if 0
  173.         /* test if pointer validation of |my_buffer_dealloc()| works */
  174.         delete (int *)malloc(5);
  175. #endif
  176.  
  177.         (void)puts("# >> main:done.");
  178.         return EXIT_SUCCESS;
  179. }

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