手写移植SGI STL二级空间配置器内存池 笔者建议配合这两篇博客进行学习
侯捷 | C++ | 内存管理 | 学习笔记(二):第二章节 std::allocator-CSDN博客
施磊C++ | 项目实战 | SGI STL二级空间配置器源码剖析-CSDN博客
考虑的问题:多线程安全
空间配置器是容器使用的,而容器产生的对象是很有可能在多个线程中去操作的
1.大致框架 1.四个函数定义
2.重要的类型变量
3.两个辅助函数
4.静态成员函数初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 #pragma once #include <mutex> template <typename T>class myallocator { public : T* allocate (size_t __n) ; void deallocate (void * __p, size_t __n) ; void * reallocate (void * __P, size_t __old_sz, size_t __new_sz) ; void construct (T *__p,const T&val) { new (__p) T (val); } void destroy (T* __p) { __p->~T (); } private : enum { _ALIGN = 8 }; enum { _MAX_BYTES = 128 }; enum { _NFREELISTS = 16 }; union _Obj { union _Obj * _M_free_list_link; char _M_client_data[1 ]; }; static _Obj* volatile _S_free_list[_NFREELISTS]; static std::mutex mtx; static char * _S_start_free; static char * _S_end_free; static size_t _S_heap_size; static size_t _S_round_up(size_t __bytes) { return (((__bytes)+(size_t )_ALIGN - 1 ) & ~((size_t )_ALIGN - 1 )); } static size_t _S_freelist_index(size_t __bytes) { return (((__bytes)+(size_t )_ALIGN - 1 ) / (size_t )_ALIGN - 1 ); } static void * _S_refill(size_t __n); static char * _S_chunk_alloc(size_t __size,int & __nobjs); }; template <typename T>char * myallocator<T>::_S_start_free = nullptr ;template <typename T>char * myallocator<T>::_S_end_free = nullptr ;template <typename T>size_t myallocator<T>::_S_heap_size = 0 ;template <typename T>typename myallocator<T>::_Obj* volatile myallocator<T>::_S_free_list[_NFREELISTS]={nullptr ,nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr }; template <typename T>std::mutex myallocator<T>::mtx;
2.allocate函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 T* allocate (size_t __n) { __n = __n * sizeof (T); void * __ret = 0 ; if (__n > (size_t )_MAX_BYTES) { __ret = malloc_alloc::allocate (__n); } else { _Obj* volatile * __my_free_list = _S_free_list + _S_freelist_index(__n); std::lock_guard<std::mutex> guard (mtx) ; _Obj* __result = *__my_free_list; if (__result == 0 ) __ret = _S_refill(_S_round_up(__n)); else { *__my_free_list = __result->_M_free_list_link; __ret = __result; } } return (T*)__ret; }
注意点:
1.vector容器传入的__n是对象个数,还要乘以对象类型T的大小才是我们要开辟的字节数
2.最后指针要强转
3.把线程安全换成c++11的互斥锁
3.refill函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 static void * _S_refill(size_t __n){ int __nobjs = 20 ; char * __chunk = _S_chunk_alloc(__n, __nobjs); _Obj* volatile * __my_free_list; _Obj* __result; _Obj* __current_obj; _Obj* __next_obj; int __i; if (1 == __nobjs) return (__chunk); __my_free_list = _S_free_list + _S_freelist_index(__n); __result = (_Obj*)__chunk; *__my_free_list = __next_obj = (_Obj*)(__chunk + __n); for (__i = 1 ; ; __i++) { __current_obj = __next_obj; __next_obj = (_Obj*)((char *)__next_obj + __n); if (__nobjs - 1 == __i) { __current_obj->_M_free_list_link = 0 ; break ; } else { __current_obj->_M_free_list_link = __next_obj; } } return (__result); }
4._S_chunk_alloc函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 static char * _S_chunk_alloc(size_t __size,int & __nobjs){ char * __result; size_t __total_bytes = __size * __nobjs; size_t __bytes_left = _S_end_free - _S_start_free; if (__bytes_left >= __total_bytes) { __result = _S_start_free; _S_start_free += __total_bytes; return (__result); } else if (__bytes_left >= __size) { __nobjs = (int )(__bytes_left / __size); __total_bytes = __size * __nobjs; __result = _S_start_free; _S_start_free += __total_bytes; return (__result); } else { size_t __bytes_to_get = 2 * __total_bytes + _S_round_up(_S_heap_size >> 4 ); if (__bytes_left > 0 ) { _Obj* volatile * __my_free_list = _S_free_list + _S_freelist_index(__bytes_left); ((_Obj*)_S_start_free)->_M_free_list_link = *__my_free_list; *__my_free_list = (_Obj*)_S_start_free; } _S_start_free = (char *)malloc (__bytes_to_get); if (nullptr == _S_start_free) { size_t __i; _Obj* volatile * __my_free_list; _Obj* __p; for (__i = __size; __i <= (size_t )_MAX_BYTES; __i += (size_t )_ALIGN) { __my_free_list = _S_free_list + _S_freelist_index(__i); __p = *__my_free_list; if (0 != __p) { *__my_free_list = __p->_M_free_list_link; _S_start_free = (char *)__p; _S_end_free = _S_start_free + __i; return (_S_chunk_alloc(__size, __nobjs)); } } _S_end_free = 0 ; _S_start_free = (char *)malloc_alloc::allocate (__bytes_to_get); } _S_heap_size += __bytes_to_get; _S_end_free = _S_start_free + __bytes_to_get; return (_S_chunk_alloc(__size, __nobjs)); } }
5.malloc_alloc::allocate函数 最后一行typedef __malloc_alloc_template<0> malloc_alloc;
这个类就是malloc_alloc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 template <int __inst>class __malloc_alloc_template {private : static void * _S_oom_malloc(size_t ); static void * _S_oom_realloc(void *, size_t ); static void (*__malloc_alloc_oom_handler) () ; public : static void * allocate (size_t __n) { void * __result = malloc (__n); if (0 == __result) __result = _S_oom_malloc(__n); return __result; } static void deallocate (void * __p, size_t ) { free (__p); } static void * reallocate (void * __p, size_t , size_t __new_sz) { void * __result = realloc (__p, __new_sz); if (0 == __result) __result = _S_oom_realloc(__p, __new_sz); return __result; } static void (*__set_malloc_handler(void (*__f)())) () { void (*__old)() = __malloc_alloc_oom_handler; __malloc_alloc_oom_handler = __f; return (__old); } }; template <int __inst>void (*__malloc_alloc_template<__inst>::__malloc_alloc_oom_handler)() = 0 ;template <int __inst>void *__malloc_alloc_template<__inst>::_S_oom_malloc(size_t __n) { void (*__my_malloc_handler)(); void * __result; for (;;) { __my_malloc_handler = __malloc_alloc_oom_handler; if (0 == __my_malloc_handler) { throw std::bad_alloc (); } (*__my_malloc_handler)(); __result = malloc (__n); if (__result) return (__result); } } template <int __inst>void * __malloc_alloc_template<__inst>::_S_oom_realloc(void * __p, size_t __n){ void (*__my_malloc_handler)(); void * __result; for (;;) { __my_malloc_handler = __malloc_alloc_oom_handler; if (0 == __my_malloc_handler) { throw std::bad_alloc (); } (*__my_malloc_handler)(); __result = realloc (__p, __n); if (__result) return (__result); } } typedef __malloc_alloc_template<0 > malloc_alloc;
6.deallocate函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 void deallocate (void * __p, size_t __n) { if (__n > (size_t )_MAX_BYTES) malloc_alloc::deallocate (__p, __n); else { _Obj* volatile * __my_free_list = _S_free_list + _S_freelist_index(__n); _Obj* __q = (_Obj*)__p; std::lock_guard<std::mutex> guard (mtx) ; __q->_M_free_list_link = *__my_free_list; *__my_free_list = __q; } }
7.reallocate 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 void * reallocate (void * __p, size_t __old_sz, size_t __new_sz) { void * __result; size_t __copy_sz; if (__old_sz > (size_t )_MAX_BYTES && __new_sz > (size_t )_MAX_BYTES) { return (realloc (__p, __new_sz)); } if (_S_round_up(__old_sz) == _S_round_up(__new_sz)) return (__p); __result = allocate (__new_sz); __copy_sz = __new_sz > __old_sz ? __old_sz : __new_sz; memcpy (__result, __p, __copy_sz); deallocate (__p, __old_sz); return (__result); }
8.STL容器要求配置器里面必须有的东西 1 2 3 4 5 6 7 8 9 10 11 12 using value_type = T; constexpr myallocator () noexcept { } constexpr myallocator (const myallocator&) noexcept = default ; template <class _Other> constexpr myallocator (const myallocator<_Other>&) noexcept { }
9.完整版myallocator.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 #pragma once #include <mutex> #include <iostream> template <int __inst>class __malloc_alloc_template {private : static void * _S_oom_malloc(size_t ); static void * _S_oom_realloc(void *, size_t ); static void (*__malloc_alloc_oom_handler) () ; public : static void * allocate (size_t __n) { void * __result = malloc (__n); if (0 == __result) __result = _S_oom_malloc(__n); return __result; } static void deallocate (void * __p, size_t ) { free (__p); } static void * reallocate (void * __p, size_t , size_t __new_sz) { void * __result = realloc (__p, __new_sz); if (0 == __result) __result = _S_oom_realloc(__p, __new_sz); return __result; } static void (*__set_malloc_handler(void (*__f)())) () { void (*__old)() = __malloc_alloc_oom_handler; __malloc_alloc_oom_handler = __f; return (__old); } }; template <int __inst>void (*__malloc_alloc_template<__inst>::__malloc_alloc_oom_handler)() = 0 ;template <int __inst>void *__malloc_alloc_template<__inst>::_S_oom_malloc(size_t __n) { void (*__my_malloc_handler)(); void * __result; for (;;) { __my_malloc_handler = __malloc_alloc_oom_handler; if (0 == __my_malloc_handler) { throw std::bad_alloc (); } (*__my_malloc_handler)(); __result = malloc (__n); if (__result) return (__result); } } template <int __inst>void * __malloc_alloc_template<__inst>::_S_oom_realloc(void * __p, size_t __n){ void (*__my_malloc_handler)(); void * __result; for (;;) { __my_malloc_handler = __malloc_alloc_oom_handler; if (0 == __my_malloc_handler) { throw std::bad_alloc (); } (*__my_malloc_handler)(); __result = realloc (__p, __n); if (__result) return (__result); } } typedef __malloc_alloc_template<0 > malloc_alloc;template <typename T>class myallocator { public : using value_type = T; constexpr myallocator () noexcept { } constexpr myallocator (const myallocator&) noexcept = default ; template <class _Other> constexpr myallocator (const myallocator<_Other>&) noexcept { } T* allocate (size_t __n) { __n = __n * sizeof (T); void * __ret = 0 ; if (__n > (size_t )_MAX_BYTES) { __ret = malloc_alloc::allocate (__n); } else { _Obj* volatile * __my_free_list = _S_free_list + _S_freelist_index(__n); std::lock_guard<std::mutex> guard (mtx) ; _Obj* __result = *__my_free_list; if (__result == 0 ) __ret = _S_refill(_S_round_up(__n)); else { *__my_free_list = __result->_M_free_list_link; __ret = __result; } } return (T*)__ret; } void deallocate (void * __p, size_t __n) { if (__n > (size_t )_MAX_BYTES) malloc_alloc::deallocate (__p, __n); else { _Obj* volatile * __my_free_list = _S_free_list + _S_freelist_index(__n); _Obj* __q = (_Obj*)__p; std::lock_guard<std::mutex> guard (mtx) ; __q->_M_free_list_link = *__my_free_list; *__my_free_list = __q; } } void * reallocate (void * __p, size_t __old_sz, size_t __new_sz) { void * __result; size_t __copy_sz; if (__old_sz > (size_t )_MAX_BYTES && __new_sz > (size_t )_MAX_BYTES) { return (realloc (__p, __new_sz)); } if (_S_round_up(__old_sz) == _S_round_up(__new_sz)) return (__p); __result = allocate (__new_sz); __copy_sz = __new_sz > __old_sz ? __old_sz : __new_sz; memcpy (__result, __p, __copy_sz); deallocate (__p, __old_sz); return (__result); } void construct (T *__p,const T&val) { new (__p) T (val); } void destroy (T* __p) { __p->~T (); } private : enum { _ALIGN = 8 }; enum { _MAX_BYTES = 128 }; enum { _NFREELISTS = 16 }; union _Obj { union _Obj * _M_free_list_link; char _M_client_data[1 ]; }; static _Obj* volatile _S_free_list[_NFREELISTS]; static std::mutex mtx; static char * _S_start_free; static char * _S_end_free; static size_t _S_heap_size; static size_t _S_round_up(size_t __bytes) { return (((__bytes)+(size_t )_ALIGN - 1 ) & ~((size_t )_ALIGN - 1 )); } static size_t _S_freelist_index(size_t __bytes) { return (((__bytes)+(size_t )_ALIGN - 1 ) / (size_t )_ALIGN - 1 ); } static void * _S_refill(size_t __n) { int __nobjs = 20 ; char * __chunk = _S_chunk_alloc(__n, __nobjs); _Obj* volatile * __my_free_list; _Obj* __result; _Obj* __current_obj; _Obj* __next_obj; int __i; if (1 == __nobjs) return (__chunk); __my_free_list = _S_free_list + _S_freelist_index(__n); __result = (_Obj*)__chunk; *__my_free_list = __next_obj = (_Obj*)(__chunk + __n); for (__i = 1 ; ; __i++) { __current_obj = __next_obj; __next_obj = (_Obj*)((char *)__next_obj + __n); if (__nobjs - 1 == __i) { __current_obj->_M_free_list_link = 0 ; break ; } else { __current_obj->_M_free_list_link = __next_obj; } } return (__result); } static char * _S_chunk_alloc(size_t __size,int & __nobjs) { char * __result; size_t __total_bytes = __size * __nobjs; size_t __bytes_left = _S_end_free - _S_start_free; if (__bytes_left >= __total_bytes) { __result = _S_start_free; _S_start_free += __total_bytes; return (__result); } else if (__bytes_left >= __size) { __nobjs = (int )(__bytes_left / __size); __total_bytes = __size * __nobjs; __result = _S_start_free; _S_start_free += __total_bytes; return (__result); } else { size_t __bytes_to_get = 2 * __total_bytes + _S_round_up(_S_heap_size >> 4 ); if (__bytes_left > 0 ) { _Obj* volatile * __my_free_list = _S_free_list + _S_freelist_index(__bytes_left); ((_Obj*)_S_start_free)->_M_free_list_link = *__my_free_list; *__my_free_list = (_Obj*)_S_start_free; } _S_start_free = (char *)malloc (__bytes_to_get); if (nullptr == _S_start_free) { size_t __i; _Obj* volatile * __my_free_list; _Obj* __p; for (__i = __size; __i <= (size_t )_MAX_BYTES; __i += (size_t )_ALIGN) { __my_free_list = _S_free_list + _S_freelist_index(__i); __p = *__my_free_list; if (0 != __p) { *__my_free_list = __p->_M_free_list_link; _S_start_free = (char *)__p; _S_end_free = _S_start_free + __i; return (_S_chunk_alloc(__size, __nobjs)); } } _S_end_free = 0 ; _S_start_free = (char *)malloc_alloc::allocate (__bytes_to_get); } _S_heap_size += __bytes_to_get; _S_end_free = _S_start_free + __bytes_to_get; return (_S_chunk_alloc(__size, __nobjs)); } } }; template <typename T>char * myallocator<T>::_S_start_free = nullptr ;template <typename T>char * myallocator<T>::_S_end_free = nullptr ;template <typename T>size_t myallocator<T>::_S_heap_size = 0 ;template <typename T>typename myallocator<T>::_Obj* volatile myallocator<T>::_S_free_list[_NFREELISTS]={nullptr ,nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr , nullptr }; template <typename T>std::mutex myallocator<T>::mtx;
10.pch.h和pch.cpp 1 2 3 4 5 6 #pragma once #ifndef PCH_H #define PCH_H #endif
11.测试 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <iostream> #include <vector> #include "pch.h" #include "myallocator.h" using namespace std;int main () { vector<int , myallocator<int >> v; for (int i = 0 ; i < 100 ; i++) v.push_back (rand () % 1000 ); for (int val : v) cout << val << " " ; cout << endl; return 0 ; }
总结:
通过源码移植可以更加清楚内存池整个分配内存和释放的过程。
侯捷老师内存管理第二章和施磊老师的课程讲清楚了原理和流程。
施磊老师的手写移植内存池是进行实践。