pastebin - collaborative debugging tool
rovema.kpaste.net RSS


custom_c++_operator_new1.cpp - custom allocator demo
Posted by Anonymous on Mon 15th Aug 2022 16:51
raw | new post
view followups (newest first): custom_c++_operator_new1.cpp - custom allocator demo by Anonymous

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

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