| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #include "memory.h" | 
| 2 |  |  |  |  |  |  | #include "string.h" | 
| 3 |  |  |  |  |  |  | #include | 
| 4 |  |  |  |  |  |  | #include | 
| 5 |  |  |  |  |  |  | #include | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | namespace panda { | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 18 |  |  |  |  |  | static std::map global_ptrs; | 
| 10 |  |  |  |  |  |  | static std::mutex              global_ptrs_mutex; | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 16 |  |  |  |  |  | static thread_local std::map global_tls_ptrs; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | static const int START_SIZE = 16; | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 18 | 50 |  |  |  |  | DynamicMemoryPool* DynamicMemoryPool::_global_instance = new DynamicMemoryPool(); | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 0 |  |  |  |  |  | void* detail::__get_global_ptr (const std::type_info& ti, const char* name, void* val) { | 
| 19 | 0 | 0 |  |  |  |  | string key(ti.name()); | 
| 20 | 0 | 0 |  |  |  |  | if (name) key += name; | 
|  |  | 0 |  |  |  |  |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 0 | 0 |  |  |  |  | std::lock_guard guard(global_ptrs_mutex); | 
| 23 | 0 | 0 |  |  |  |  | auto it = global_ptrs.find(key); | 
| 24 | 0 | 0 |  |  |  |  | if (it != global_ptrs.end()) return it->second; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 0 | 0 |  |  |  |  | global_ptrs.emplace(key, val); | 
| 27 | 0 |  |  |  |  |  | return val; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 4 |  |  |  |  |  | void* detail::__get_global_tls_ptr (const std::type_info& ti, const char* name, void* val) { | 
| 31 | 8 | 50 |  |  |  |  | string key(ti.name()); | 
| 32 | 4 | 50 |  |  |  |  | if (name) key += name; | 
|  |  | 50 |  |  |  |  |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 16 | 50 |  |  |  |  | auto it = global_tls_ptrs.find(key); | 
| 35 | 4 | 50 |  |  |  |  | if (it != global_tls_ptrs.end()) return it->second; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 4 | 50 |  |  |  |  | global_tls_ptrs.emplace(key, val); | 
| 38 | 4 |  |  |  |  |  | return val; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 4 |  |  |  |  |  | void MemoryPool::grow () { | 
| 42 | 4 |  |  |  |  |  | size_t pools_cnt = pools.size(); | 
| 43 | 4 | 50 |  |  |  |  | if (pools_cnt) { | 
| 44 | 0 |  |  |  |  |  | pools.resize(pools_cnt+1); | 
| 45 | 0 |  |  |  |  |  | pools[pools_cnt].size = pools[pools_cnt-1].size*2; | 
| 46 |  |  |  |  |  |  | } else { | 
| 47 | 4 |  |  |  |  |  | pools.resize(1); | 
| 48 | 4 |  |  |  |  |  | pools[0].size = START_SIZE; | 
| 49 |  |  |  |  |  |  | } | 
| 50 | 4 |  |  |  |  |  | Pool* pool = &pools.back(); | 
| 51 | 4 |  |  |  |  |  | pool->len = pool->size * blocksize; | 
| 52 | 4 |  |  |  |  |  | char* elem = pool->list = new char[pool->len]; | 
| 53 | 4 |  |  |  |  |  | char* end  = elem + pool->len; | 
| 54 | 68 | 100 |  |  |  |  | while (elem < end) { | 
| 55 | 64 |  |  |  |  |  | *((void**)elem) = elem + blocksize; // set next free for each free element | 
| 56 | 64 |  |  |  |  |  | elem += blocksize; | 
| 57 |  |  |  |  |  |  | } | 
| 58 | 4 |  |  |  |  |  | *((void**)(elem-blocksize)) = NULL; // last element has no next free | 
| 59 | 4 |  |  |  |  |  | first_free = pool->list; | 
| 60 | 4 |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 0 |  |  |  |  |  | bool MemoryPool::is_mine (void* elem) { | 
| 63 | 0 |  |  |  |  |  | Pool* first = &pools.front(); | 
| 64 | 0 |  |  |  |  |  | Pool* pool  = &pools.back(); | 
| 65 | 0 | 0 |  |  |  |  | while (pool >= first) { // from last to first, because most possibility that elem is in latest pools | 
| 66 | 0 | 0 |  |  |  |  | if (elem >= pool->list && elem < pool->list + pool->len) return true; | 
|  |  | 0 |  |  |  |  |  | 
| 67 | 0 |  |  |  |  |  | pool--; | 
| 68 |  |  |  |  |  |  | } | 
| 69 | 0 |  |  |  |  |  | return false; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 0 | 0 |  |  |  |  | MemoryPool::~MemoryPool () { | 
| 73 | 0 | 0 |  |  |  |  | if (!pools.size()) return; | 
| 74 | 0 |  |  |  |  |  | Pool* pool = &pools.front(); | 
| 75 | 0 |  |  |  |  |  | Pool* last = &pools.back(); | 
| 76 | 0 | 0 |  |  |  |  | while (pool <= last) { | 
| 77 | 0 | 0 |  |  |  |  | delete[] pool->list; | 
| 78 | 0 |  |  |  |  |  | pool++; | 
| 79 |  |  |  |  |  |  | } | 
| 80 | 0 |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 22 |  |  |  |  |  | DynamicMemoryPool::DynamicMemoryPool () { | 
| 83 | 22 |  |  |  |  |  | memset(small_pools,  0, POOLS_CNT*sizeof(MemoryPool*)); | 
| 84 | 22 |  |  |  |  |  | memset(medium_pools, 0, POOLS_CNT*sizeof(MemoryPool*)); | 
| 85 | 22 |  |  |  |  |  | memset(big_pools,    0, POOLS_CNT*sizeof(MemoryPool*)); | 
| 86 | 22 | 50 |  |  |  |  | small_pools[0] = small_pools[1] = new MemoryPool(8); // min bytes = 8, make 4-byte and 8-byte requests shared | 
| 87 | 22 |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 0 |  |  |  |  |  | DynamicMemoryPool::~DynamicMemoryPool () { | 
| 90 | 0 | 0 |  |  |  |  | for (int i = 0; i < POOLS_CNT; ++i) { | 
| 91 | 0 | 0 |  |  |  |  | if (i) delete small_pools[i]; | 
|  |  | 0 |  |  |  |  |  | 
| 92 | 0 | 0 |  |  |  |  | delete medium_pools[i]; | 
| 93 | 0 | 0 |  |  |  |  | delete big_pools[i]; | 
| 94 |  |  |  |  |  |  | } | 
| 95 | 0 |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 96 | 100 |  |  |  |  | } | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  |