libfoedus-core
FOEDUS Core Library
aligned_memory.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_MEMORY_ALIGNED_MEMORY_HPP_
19 #define FOEDUS_MEMORY_ALIGNED_MEMORY_HPP_
20 
21 #include <stdint.h>
22 
23 #include <iosfwd>
24 
25 #include "foedus/assert_nd.hpp"
26 #include "foedus/cxx11.hpp"
27 #include "foedus/error_code.hpp"
28 
29 namespace foedus {
30 namespace memory {
68  public:
77  enum AllocType {
89  // kNormal,
91  // kVirtualAlloc,
101  };
102 
104  AlignedMemory() CXX11_NOEXCEPT : size_(0), alignment_(0), alloc_type_(kPosixMemalign),
105  numa_node_(0), block_(CXX11_NULLPTR) {}
106 
119  AlignedMemory(uint64_t size, uint64_t alignment,
120  AllocType alloc_type, int numa_node) CXX11_NOEXCEPT;
121 
122  // Disable default constructors
125 
126 #ifndef DISABLE_CXX11_IN_PUBLIC_HEADERS
127 
130  AlignedMemory(AlignedMemory &&other) noexcept;
134  AlignedMemory& operator=(AlignedMemory &&other) noexcept;
135 #endif // DISABLE_CXX11_IN_PUBLIC_HEADERS
136 
139 
141  void alloc(
142  uint64_t size,
143  uint64_t alignment,
144  AllocType alloc_type,
145  int numa_node) CXX11_NOEXCEPT;
147  void alloc_onnode(uint64_t size, uint64_t alignment, int numa_node) CXX11_NOEXCEPT {
148  alloc(size, alignment, kNumaAllocOnnode, numa_node);
149  }
150 
163  uint64_t required_size,
164  double expand_margin = 2.0,
165  bool retain_content = false) CXX11_NOEXCEPT;
166 
168  void* get_block() const { return block_; }
170  bool is_null() const { return block_ == CXX11_NULLPTR; }
172  uint64_t get_size() const { return size_; }
174  uint64_t get_alignment() const { return alignment_; }
176  AllocType get_alloc_type() const { return alloc_type_; }
178  int get_numa_node() const { return numa_node_; }
179 
181  void release_block();
182 
183  friend std::ostream& operator<<(std::ostream& o, const AlignedMemory& v);
184 
185  private:
187  uint64_t size_;
189  uint64_t alignment_;
191  AllocType alloc_type_;
193  int numa_node_;
195  void* block_;
196 };
197 
213 
216  : memory_(memory), offset_(0), count_(memory->get_size()) {}
217 
219  AlignedMemorySlice(AlignedMemory *memory, uint64_t offset, uint64_t count)
220  : memory_(memory), offset_(offset), count_(count) {
221  ASSERT_ND(memory->get_size() >= count + offset);
222  }
223 
225  AlignedMemorySlice(const AlignedMemorySlice &slice, uint64_t offset, uint64_t count)
226  : memory_(slice.memory_), offset_(slice.offset_ + offset), count_(count) {
227  ASSERT_ND(slice.get_size() >= count + offset);
228  }
229 
230  friend std::ostream& operator<<(std::ostream& o, const AlignedMemorySlice& v);
231 
233  bool is_valid() const { return memory_; }
234  uint64_t get_size() const { return count_; }
235  void* get_block() const { return reinterpret_cast<char*>(memory_->get_block()) + offset_; }
236 
240  uint64_t offset_;
242  uint64_t count_;
243 };
244 
247 
248 } // namespace memory
249 } // namespace foedus
250 
251 #endif // FOEDUS_MEMORY_ALIGNED_MEMORY_HPP_
numa_alloc_onnode() and numa_free().
#define CXX11_NULLPTR
Used in public headers in place of "nullptr" of C++11.
Definition: cxx11.hpp:132
uint64_t get_alignment() const
Returns the alignment of the memory block.
uint64_t count_
Byte count of this slice in memory_.
void release_block()
Releases the memory block.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
AlignedMemory & operator=(const AlignedMemory &other)=delete
AlignedMemorySlice()
Empty constructor.
#define CXX11_NOEXCEPT
Used in public headers in place of "noexcept" of C++11.
Definition: cxx11.hpp:133
AlignedMemory * memory_
The wrapped memory.
AllocType
Type of new/delete operation for the block.
friend std::ostream & operator<<(std::ostream &o, const AlignedMemory &v)
~AlignedMemory()
Automatically releases the memory.
ErrorCode assure_capacity(uint64_t required_size, double expand_margin=2.0, bool retain_content=false) noexcept
If the current size is smaller than the given size, automatically expands.
void alloc(uint64_t size, uint64_t alignment, AllocType alloc_type, int numa_node) noexcept
Allocate a memory, releasing the current memory if exists.
AllocType get_alloc_type() const
Returns type of new/delete operation for the block.
AlignedMemorySlice(const AlignedMemorySlice &slice, uint64_t offset, uint64_t count)
A slice that covers the specified region of another slice.
#define CXX11_FINAL
Used in public headers in place of "final" of C++11.
Definition: cxx11.hpp:131
A slice of foedus::memory::AlignedMemory.
void * get_block() const
Returns the memory block.
uint64_t get_size() const
Returns the byte size of the memory block.
#define CXX11_FUNC_DELETE
Used in public headers in place of " = delete" of C++11.
Definition: cxx11.hpp:128
uint64_t offset_
Byte offset of this slice in memory_.
friend std::ostream & operator<<(std::ostream &o, const AlignedMemorySlice &v)
Represents one memory block aligned to actual OS/hardware pages.
AlignedMemorySlice(AlignedMemory *memory)
A dummy slice that covers the memory entirely.
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
numa_alloc_interleaved() and numa_free().
bool is_1gb_hugepage_enabled()
Returns if 1GB hugepages were enabled.
void alloc_onnode(uint64_t size, uint64_t alignment, int numa_node) noexcept
Short for alloc(kNumaAllocOnnode)
ErrorCode
Enum of error codes defined in error_code.xmacro.
Definition: error_code.hpp:85
int get_numa_node() const
If alloc_type_ is kNumaAllocOnnode, returns the NUMA node this memory was allocated at...
AlignedMemory() noexcept
Empty constructor which allocates nothing.
AlignedMemorySlice(AlignedMemory *memory, uint64_t offset, uint64_t count)
A slice that covers the specified region of the memory.
bool is_null() const
Returns if this object doesn't hold a valid memory block.