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