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