libfoedus-core
FOEDUS Core Library
shared_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_SHARED_MEMORY_HPP_
19 #define FOEDUS_MEMORY_SHARED_MEMORY_HPP_
20 
21 #include <stdint.h>
22 #include <sys/types.h>
23 
24 #include <iosfwd>
25 #include <string>
26 
27 #include "foedus/assert_nd.hpp"
28 #include "foedus/cxx11.hpp"
29 #include "foedus/error_stack.hpp"
30 
31 namespace foedus {
32 namespace memory {
93  public:
96  : size_(0), numa_node_(0), shmid_(0), shmkey_(0), owner_pid_(0), block_(CXX11_NULLPTR) {}
97 
98  // Disable copy constructor
101 
102 #ifndef DISABLE_CXX11_IN_PUBLIC_HEADERS
103 
106  SharedMemory(SharedMemory &&other) noexcept;
110  SharedMemory& operator=(SharedMemory &&other) noexcept;
111 #endif // DISABLE_CXX11_IN_PUBLIC_HEADERS
112 
115 
129  ErrorStack alloc(const std::string& meta_path, uint64_t size, int numa_node, bool use_hugepages);
139  void attach(const std::string& meta_path, bool use_hugepages);
140 
142  const std::string& get_meta_path() const { return meta_path_; }
144  char* get_block() const { return block_; }
146  int get_shmid() const { return shmid_; }
148  key_t get_shmkey() const { return shmkey_; }
150  pid_t get_owner_pid() const { return owner_pid_; }
152  bool is_null() const { return block_ == CXX11_NULLPTR; }
154  bool is_owned() const;
156  uint64_t get_size() const { return size_; }
158  int get_numa_node() const { return numa_node_; }
159 
172  void mark_for_release();
173 
175  void release_block();
176 
177  friend std::ostream& operator<<(std::ostream& o, const SharedMemory& v);
178 
179  private:
181  std::string meta_path_;
183  uint64_t size_;
185  int numa_node_;
187  int shmid_;
189  key_t shmkey_;
194  pid_t owner_pid_;
196  char* block_;
197 };
198 
199 } // namespace memory
200 } // namespace foedus
201 
202 #endif // FOEDUS_MEMORY_SHARED_MEMORY_HPP_
~SharedMemory()
Automatically releases the memory.
bool is_owned() const
Returns if this process owns this memory and is responsible to delete it.
#define CXX11_NULLPTR
Used in public headers in place of "nullptr" of C++11.
Definition: cxx11.hpp:132
key_t get_shmkey() const
Returns the key of this shared memory.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
friend std::ostream & operator<<(std::ostream &o, const SharedMemory &v)
#define CXX11_NOEXCEPT
Used in public headers in place of "noexcept" of C++11.
Definition: cxx11.hpp:133
char * get_block() const
Returns the memory block.
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
Represents memory shared between processes.
int get_numa_node() const
Where the physical memory is allocated.
#define CXX11_FINAL
Used in public headers in place of "final" of C++11.
Definition: cxx11.hpp:131
SharedMemory() noexcept
Empty constructor which allocates nothing.
void attach(const std::string &meta_path, bool use_hugepages)
Attach an already-allocated shared memory so that this object points to the memory.
#define CXX11_FUNC_DELETE
Used in public headers in place of " = delete" of C++11.
Definition: cxx11.hpp:128
const std::string & get_meta_path() const
Returns the path of the meta file.
int get_shmid() const
Returns the ID of this shared memory.
void mark_for_release()
Marks the shared memory as being removed so that it will be reclaimed when all processes detach it...
SharedMemory & operator=(const SharedMemory &other)=delete
bool is_null() const
Returns if this object doesn't hold a valid memory block.
ErrorStack alloc(const std::string &meta_path, uint64_t size, int numa_node, bool use_hugepages)
Newly allocate a shared memory of given size on given NUMA node.
pid_t get_owner_pid() const
If non-zero, it means the ID of the process that allocated the shared memory.
uint64_t get_size() const
Returns the byte size of the memory block.
void release_block()
Releases the memory block IF this process has an ownership.