Rapicorn - Experimental UI Toolkit - Source Code
13.07.0
|
00001 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html 00002 #ifndef __RAPICORN_CORE_MEMORY_HH__ 00003 #define __RAPICORN_CORE_MEMORY_HH__ 00004 00005 #include <rcore/utilities.hh> 00006 00007 namespace Rapicorn { 00008 00010 class IdAllocator { 00011 RAPICORN_CLASS_NON_COPYABLE (IdAllocator); 00012 protected: 00013 explicit IdAllocator (); 00014 public: 00015 virtual ~IdAllocator (); 00016 virtual uint alloc_id () = 0; 00017 virtual void release_id (uint unique_id) = 0; 00018 virtual bool seen_id (uint unique_id) = 0; 00019 static IdAllocator* _new (uint startval = 1); 00020 }; 00021 00022 // == Utilities == 00023 int fmsb (uint64 value) RAPICORN_CONST; 00024 00025 // == Aligned Memory == 00026 void* aligned_alloc (size_t total_size, size_t alignment, uint8 **free_pointer); 00027 void aligned_free (uint8 **free_pointer); 00028 00030 template<class T, int ALIGNMENT> 00031 class AlignedArray { 00032 uint8 *unaligned_mem_; 00033 T *data_; 00034 size_t n_elements_; 00035 void 00036 allocate_aligned_data() 00037 { 00038 static_assert (ALIGNMENT % sizeof (T) == 0, "ALIGNMENT must exactly fit a multiple of sizeof (T)"); 00039 data_ = reinterpret_cast<T*> (aligned_alloc (n_elements_ * sizeof (T), ALIGNMENT, &unaligned_mem_)); 00040 } 00041 // disallow copy constructor assignment operator 00042 RAPICORN_CLASS_NON_COPYABLE (AlignedArray); 00043 public: 00044 AlignedArray (const vector<T>& elements) : 00045 n_elements_ (elements.size()) 00046 { 00047 allocate_aligned_data(); 00048 for (size_t i = 0; i < n_elements_; i++) 00049 new (data_ + i) T (elements[i]); 00050 } 00051 AlignedArray (size_t n_elements) : 00052 n_elements_ (n_elements) 00053 { 00054 allocate_aligned_data(); 00055 for (size_t i = 0; i < n_elements_; i++) 00056 new (data_ + i) T(); 00057 } 00058 ~AlignedArray() 00059 { 00060 // C++ destruction order: last allocated element is deleted first 00061 while (n_elements_) 00062 data_[--n_elements_].~T(); 00063 aligned_free (&unaligned_mem_); 00064 } 00065 T& operator[] (size_t pos) { return data_[pos]; } 00066 const T& operator[] (size_t pos) const { return data_[pos]; } 00067 size_t size () const { return n_elements_; } 00068 }; 00069 00070 } // Rapicorn 00071 00072 #endif /* __RAPICORN_CORE_MEMORY_HH__ */