Atlas Runtime
pmalloc_util.hpp
Go to the documentation of this file.
1 /*
2  * (c) Copyright 2016 Hewlett Packard Enterprise Development LP
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version. This program is
8  * distributed in the hope that it will be useful, but WITHOUT ANY
9  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11  * for more details. You should have received a copy of the GNU Lesser
12  * General Public License along with this program. If not, see
13  * <http://www.gnu.org/licenses/>.
14  */
15 
16 
17 #ifndef PMALLOC_UTIL_HPP
18 #define PMALLOC_UTIL_HPP
19 
20 namespace Atlas {
21 
22 class PMallocUtil {
23 public:
25  { TL_CurrArena_[rid] = kNumArenas_; /* set to invalid */}
26 
27  static void set_tl_curr_arena(region_id_t rid, uint32_t val)
28  { TL_CurrArena_[rid] = val; }
29 
30  static uint32_t get_tl_curr_arena(region_id_t rid)
31  { return TL_CurrArena_[rid]; }
32 
33  static uint32_t get_tl_next_arena(region_id_t rid)
34  { return (get_tl_curr_arena(rid) + 1) % kNumArenas_; }
35 
37  { return TL_CurrArena_[rid] != kNumArenas_; }
38 
39  static void set_cache_line_size(uint32_t sz)
40  { CacheLineSize_ = sz; }
41 
42  static void set_cache_line_mask(uintptr_t mask)
43  { CacheLineMask_ = mask; }
44 
45  static uint32_t get_cache_line_size()
46  { assert(CacheLineSize_ != UINT32_MAX); return CacheLineSize_; }
47 
48  static uintptr_t get_cache_line_mask()
49  { assert(CacheLineMask_ != UINTPTR_MAX); return CacheLineMask_; }
50 
51  static void *mem2ptr(void *mem)
52  { return static_cast<void*>(
53  static_cast<char*>(mem) + get_metadata_size()); }
54 
55  static void *ptr2mem(void *ptr)
56  { return static_cast<void*>(
57  static_cast<char*>(ptr) - get_metadata_size()); }
58 
59  static size_t get_alignment()
60  { return 2*sizeof(size_t); }
61 
62  static size_t get_alignment_mask()
63  { return get_alignment() - 1; }
64 
65  static size_t get_metadata_size()
66  { return 2*sizeof(size_t); }
67 
69  { return get_metadata_size(); }
70 
71  static size_t get_actual_alloc_size(size_t sz)
72  { return (sz + get_metadata_size() + get_alignment_mask()) &
73  ~get_alignment_mask(); }
74 
75  static size_t get_requested_alloc_size_from_mem(void *mem)
76  { return *(static_cast<size_t*>(mem)); }
77 
78  static size_t get_requested_alloc_size_from_ptr(void *ptr)
79  { void *mem = ptr2mem(ptr);
80  return *(static_cast<size_t*>(mem)); }
81 
82  static size_t *get_is_allocated_ptr_from_mem(void *mem)
83  { return reinterpret_cast<size_t*>(
84  static_cast<char*>(mem) + sizeof(size_t)); }
85 
86  static size_t *get_is_allocated_ptr_from_ptr(void *ptr)
87  { void *mem = ptr2mem(ptr);
88  return reinterpret_cast<size_t*>(
89  static_cast<char*>(mem) + sizeof(size_t)); }
90 
91  static bool is_mem_allocated(void *mem)
92  { return *get_is_allocated_ptr_from_mem(mem) == true; }
93 
94  static bool is_ptr_allocated(void *ptr)
95  { return *get_is_allocated_ptr_from_ptr(ptr) == true; }
96 
97  static uint32_t get_next_bin_number(uint32_t bin_number)
98  {
99  assert(bin_number && "Non-zero bin number!");
100  assert(!(bin_number % get_alignment()) && "Unaligned bin number!");
101  return bin_number + get_alignment();
102  }
103 
104  static bool is_cache_line_aligned(void *p)
105  { return (reinterpret_cast<uintptr_t>(p) &
107  reinterpret_cast<uintptr_t>(p); }
108 
109  static bool is_on_different_cache_line(void *p1, void *p2)
110  { return (reinterpret_cast<uintptr_t>(p1) &
112  (reinterpret_cast<uintptr_t>(p2) &
114 
115  static uint32_t get_bin_number(size_t sz)
116  {
117  return (sz < kMaxFreeCategory_) ?
118  (sz + get_alignment_mask()) & ~get_alignment_mask() :
120  }
121 private:
122  static uint32_t CacheLineSize_;
123  static uintptr_t CacheLineMask_;
124  static thread_local uint32_t TL_CurrArena_[kMaxNumPRegions_];
125 };
126 
127 } // namespace Atlas
128 
129 #endif
static void * ptr2mem(void *ptr)
Definition: pmalloc_util.hpp:55
static void set_cache_line_mask(uintptr_t mask)
Definition: pmalloc_util.hpp:42
static size_t get_requested_alloc_size_from_mem(void *mem)
Definition: pmalloc_util.hpp:75
const uint32_t kMaxNumPRegions_
Definition: pregion_configs.hpp:29
static size_t get_alignment()
Definition: pmalloc_util.hpp:59
const uint32_t kNumArenas_
Definition: pregion_configs.hpp:30
const uint32_t kMaxFreeCategory_
Definition: pregion_configs.hpp:32
uint32_t region_id_t
Definition: pregion_configs.hpp:22
static void * mem2ptr(void *mem)
Definition: pmalloc_util.hpp:51
static bool is_ptr_allocated(void *ptr)
Definition: pmalloc_util.hpp:94
static size_t get_metadata_size()
Definition: pmalloc_util.hpp:65
static size_t * get_is_allocated_ptr_from_mem(void *mem)
Definition: pmalloc_util.hpp:82
static bool is_cache_line_aligned(void *p)
Definition: pmalloc_util.hpp:104
static uint32_t get_tl_next_arena(region_id_t rid)
Definition: pmalloc_util.hpp:33
static uint32_t get_tl_curr_arena(region_id_t rid)
Definition: pmalloc_util.hpp:30
static void set_cache_line_size(uint32_t sz)
Definition: pmalloc_util.hpp:39
static bool is_mem_allocated(void *mem)
Definition: pmalloc_util.hpp:91
static size_t get_smallest_actual_alloc_size()
Definition: pmalloc_util.hpp:68
static uintptr_t get_cache_line_mask()
Definition: pmalloc_util.hpp:48
static uint32_t get_bin_number(size_t sz)
Definition: pmalloc_util.hpp:115
static uint32_t get_cache_line_size()
Definition: pmalloc_util.hpp:45
Definition: pmalloc_util.hpp:22
static size_t * get_is_allocated_ptr_from_ptr(void *ptr)
Definition: pmalloc_util.hpp:86
static bool is_valid_tl_curr_arena(region_id_t rid)
Definition: pmalloc_util.hpp:36
static void set_tl_curr_arena(region_id_t rid, uint32_t val)
Definition: pmalloc_util.hpp:27
static size_t get_alignment_mask()
Definition: pmalloc_util.hpp:62
static size_t get_requested_alloc_size_from_ptr(void *ptr)
Definition: pmalloc_util.hpp:78
static size_t get_actual_alloc_size(size_t sz)
Definition: pmalloc_util.hpp:71
static uint32_t get_next_bin_number(uint32_t bin_number)
Definition: pmalloc_util.hpp:97
static void set_default_tl_curr_arena(region_id_t rid)
Definition: pmalloc_util.hpp:24
static bool is_on_different_cache_line(void *p1, void *p2)
Definition: pmalloc_util.hpp:109
Definition: atlas_alloc_cpp.hpp:21