|
| 1 | +/*- |
| 2 | + * Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 | + * IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#ifndef MEMORY_POOL_H |
| 24 | +#define MEMORY_POOL_H |
| 25 | + |
| 26 | +#include <limits.h> |
| 27 | +#include <stddef.h> |
| 28 | + |
| 29 | +template <typename T, size_t BlockSize = 4096> |
| 30 | +class MemoryPool |
| 31 | +{ |
| 32 | + public: |
| 33 | + /* Member types */ |
| 34 | + typedef T value_type; |
| 35 | + typedef T* pointer; |
| 36 | + typedef T& reference; |
| 37 | + typedef const T* const_pointer; |
| 38 | + typedef const T& const_reference; |
| 39 | + typedef size_t size_type; |
| 40 | + typedef ptrdiff_t difference_type; |
| 41 | + |
| 42 | + template <typename U> struct rebind { |
| 43 | + typedef MemoryPool<U> other; |
| 44 | + }; |
| 45 | + |
| 46 | + /* Member functions */ |
| 47 | + MemoryPool() throw(); |
| 48 | + MemoryPool(const MemoryPool& memoryPool) throw(); |
| 49 | + template <class U> MemoryPool(const MemoryPool<U>& memoryPool) throw(); |
| 50 | + |
| 51 | + ~MemoryPool() throw(); |
| 52 | + |
| 53 | + pointer address(reference x) const throw(); |
| 54 | + const_pointer address(const_reference x) const throw(); |
| 55 | + |
| 56 | + // Can only allocate one object at a time. n and hint are ignored |
| 57 | + pointer allocate(size_type n = 1, const_pointer hint = 0); |
| 58 | + void deallocate(pointer p, size_type n = 1); |
| 59 | + |
| 60 | + size_type max_size() const throw(); |
| 61 | + |
| 62 | + void construct(pointer p, const_reference val); |
| 63 | + void destroy(pointer p); |
| 64 | + |
| 65 | + pointer newElement(const_reference val); |
| 66 | + void deleteElement(pointer p); |
| 67 | + |
| 68 | + private: |
| 69 | + union Slot_ { |
| 70 | + value_type element; |
| 71 | + Slot_* next; |
| 72 | + }; |
| 73 | + |
| 74 | + typedef char* data_pointer_; |
| 75 | + typedef Slot_ slot_type_; |
| 76 | + typedef Slot_* slot_pointer_; |
| 77 | + |
| 78 | + slot_pointer_ currentBlock_; |
| 79 | + slot_pointer_ currentSlot_; |
| 80 | + slot_pointer_ lastSlot_; |
| 81 | + slot_pointer_ freeSlots_; |
| 82 | + |
| 83 | + size_type padPointer(data_pointer_ p, size_type align) const throw(); |
| 84 | + void allocateBlock(); |
| 85 | + /* |
| 86 | + static_assert(BlockSize >= 2 * sizeof(slot_type_), "BlockSize too small."); |
| 87 | + */ |
| 88 | +}; |
| 89 | + |
| 90 | +#include "MemoryPool.tcc" |
| 91 | + |
| 92 | +#endif // MEMORY_POOL_H |
0 commit comments