managed_memory_impl.hpp
来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 740 行 · 第 1/2 页
HPP
740 行
//!-> Throws boost::interprocess::bad_alloc if there is no available memory //! //!-> If T's constructor throws, the function throws that exception. //! //!Memory is freed automatically if T's constructor throws and if an //!array was being constructed, destructors of created objects are called //!before freeing the memory. template <class T> typename segment_manager::template construct_proxy<T>::type find_or_construct(char_ptr_holder_t name) { return mp_header->template find_or_construct<T>(name); } //!Creates a named object or array in memory //! //!Allocates and constructs a T object or an array of T in memory, //!associates this with the given name and returns a pointer to the //!created object. If an array is being constructed all objects are //!created using the same parameters given to this function. //! //!-> If the name was previously used, returns 0. //! //!-> Returns 0 if there is no available memory //! //!-> If T's constructor throws, the function throws that exception. //! //!Memory is freed automatically if T's constructor throws and if an //!array was being constructed, destructors of created objects are called //!before freeing the memory. template <class T> typename segment_manager::template construct_proxy<T>::type construct(char_ptr_holder_t name, std::nothrow_t nothrow) { return mp_header->template construct<T>(name, nothrow); } //!Finds or creates a named object or array in memory //! //!Tries to find an object with the given name in memory. If //!found, returns the pointer to this pointer. If the object is not found, //!allocates and constructs a T object or an array of T in memory, //!associates this with the given name and returns a pointer to the //!created object. If an array is being constructed all objects are //!created using the same parameters given to this function. //! //!-> Returns 0 if there is no available memory //! //!-> If T's constructor throws, the function throws that exception. //! //!Memory is freed automatically if T's constructor throws and if an //!array was being constructed, destructors of created objects are called //!before freeing the memory. template <class T> typename segment_manager::template construct_proxy<T>::type find_or_construct(char_ptr_holder_t name, std::nothrow_t nothrow) { return mp_header->template find_or_construct<T>(name, nothrow); } //!Creates a named array from iterators in memory //! //!Allocates and constructs an array of T in memory, //!associates this with the given name and returns a pointer to the //!created object. Each element in the array is created using the //!objects returned when dereferencing iterators as parameters //!and incrementing all iterators for each element. //! //!-> If the name was previously used, returns 0. //! //!-> Throws boost::interprocess::bad_alloc if there is no available memory //! //!-> If T's constructor throws, the function throws that exception. //! //!Memory is freed automatically if T's constructor throws and //!destructors of created objects are called before freeing the memory. template <class T> typename segment_manager::template construct_iter_proxy<T>::type construct_it(char_ptr_holder_t name) { return mp_header->template construct_it<T>(name); } //!Finds or creates a named array from iterators in memory //! //!Tries to find an object with the given name in memory. If //!found, returns the pointer to this pointer. If the object is not found, //!allocates and constructs an array of T in memory, //!associates this with the given name and returns a pointer to the //!created object. Each element in the array is created using the //!objects returned when dereferencing iterators as parameters //!and incrementing all iterators for each element. //! //!-> If the name was previously used, returns 0. //! //!-> Throws boost::interprocess::bad_alloc if there is no available memory //! //!-> If T's constructor throws, the function throws that exception. //! //!Memory is freed automatically if T's constructor throws and //!destructors of created objects are called before freeing the memory. template <class T> typename segment_manager::template construct_iter_proxy<T>::type find_or_construct_it(char_ptr_holder_t name) { return mp_header->template find_or_construct_it<T>(name); } //!Creates a named array from iterators in memory //! //!Allocates and constructs an array of T in memory, //!associates this with the given name and returns a pointer to the //!created object. Each element in the array is created using the //!objects returned when dereferencing iterators as parameters //!and incrementing all iterators for each element. //! //!-> If the name was previously used, returns 0. //! //!-> If there is no available memory, returns 0. //! //!-> If T's constructor throws, the function throws that exception. //! //!Memory is freed automatically if T's constructor throws and //!destructors of created objects are called before freeing the memory.*/ template <class T> typename segment_manager::template construct_iter_proxy<T>::type construct_it(char_ptr_holder_t name, std::nothrow_t nothrow) { return mp_header->template construct_it<T>(name, nothrow); } //!Finds or creates a named array from iterators in memory //! //!Tries to find an object with the given name in memory. If //!found, returns the pointer to this pointer. If the object is not found, //!allocates and constructs an array of T in memory, //!associates this with the given name and returns a pointer to the //!created object. Each element in the array is created using the //!objects returned when dereferencing iterators as parameters //!and incrementing all iterators for each element. //! //!-> If the name was previously used, returns 0. //! //!-> If there is no available memory, returns 0. //! //!-> If T's constructor throws, the function throws that exception. //! //!Memory is freed automatically if T's constructor throws and //!destructors of created objects are called before freeing the memory.*/ template <class T> typename segment_manager::template construct_iter_proxy<T>::type find_or_construct_it(char_ptr_holder_t name, std::nothrow_t nothrow) { return mp_header->template find_or_construct_it<T>(name, nothrow); } //!Calls a functor and guarantees that no new construction, search or //!destruction will be executed by any process while executing the object //!function call. If the functor throws, this function throws. template <class Func> void atomic_func(Func &f) { mp_header->atomic_func(f); } //!Destroys a named memory object or array. //! //!Finds the object with the given name, calls its destructors, //!frees used memory and returns true. //! //!-> If the object is not found, it returns false. //! //!Exception Handling: //! //!When deleting a dynamically object or array, the Standard //!does not guarantee that dynamically allocated memory, will be released. //!Also, when deleting arrays, the Standard doesn't require calling //!destructors for the rest of the objects if for one of them the destructor //!terminated with an exception. //! //!Destroying an object: //! //!If the destructor throws, the memory will be freed and that exception //!will be thrown. //! //!Destroying an array: //! //!When destroying an array, if a destructor throws, the rest of //!destructors are called. If any of these throws, the exceptions are //!ignored. The name association will be erased, memory will be freed and //!the first exception will be thrown. This guarantees the unlocking of //!mutexes and other resources. //! //!For all theses reasons, classes with throwing destructors are not //!recommended. template <class T> bool destroy(const CharType *name) { return mp_header->template destroy<T>(name); } //!Destroys the unique instance of type T //! //!Calls the destructor, frees used memory and returns true. //! //!Exception Handling: //! //!When deleting a dynamically object, the Standard does not //!guarantee that dynamically allocated memory will be released. //! //!Destroying an object: //! //!If the destructor throws, the memory will be freed and that exception //!will be thrown. //! //!For all theses reasons, classes with throwing destructors are not //!recommended for memory. template <class T> bool destroy(const detail::unique_instance_t *const ) { return mp_header->template destroy<T>(unique_instance); } //!Destroys the object (named, unique, or anonymous) //! //!Calls the destructor, frees used memory and returns true. //! //!Exception Handling: //! //!When deleting a dynamically object, the Standard does not //!guarantee that dynamically allocated memory will be released. //! //!Destroying an object: //! //!If the destructor throws, the memory will be freed and that exception //!will be thrown. //! //!For all theses reasons, classes with throwing destructors are not //!recommended for memory. template <class T> void destroy_ptr(const T *ptr) { mp_header->template destroy_ptr<T>(ptr); } //!Returns the name of an object created with construct/find_or_construct //!functions. Does not throw template<class T> static const char_type *get_instance_name(const T *ptr) { return segment_manager::get_instance_name(ptr); } //!Returns is the type an object created with construct/find_or_construct //!functions. Does not throw. template<class T> static instance_type get_instance_type(const T *ptr) { return segment_manager::get_instance_type(ptr); } //!Returns the length of an object created with construct/find_or_construct //!functions (1 if is a single element, >=1 if it's an array). Does not throw. template<class T> static std::size_t get_instance_length(const T *ptr) { return segment_manager::get_instance_length(ptr); } //!Preallocates needed index resources to optimize the //!creation of "num" named objects in the memory segment. //!Can throw boost::interprocess::bad_alloc if there is no enough memory. void reserve_named_objects(std::size_t num) { mp_header->reserve_named_objects(num); } //!Preallocates needed index resources to optimize the //!creation of "num" unique objects in the memory segment. //!Can throw boost::interprocess::bad_alloc if there is no enough memory. void reserve_unique_objects(std::size_t num) { mp_header->reserve_unique_objects(num); } //!Calls shrink_to_fit in both named and unique object indexes //to try to free unused memory from those indexes. void shrink_to_fit_indexes() { mp_header->shrink_to_fit_indexes(); } //!Returns the number of named objects stored //!in the managed segment. std::size_t get_num_named_objects() { return mp_header->get_num_named_objects(); } //!Returns the number of unique objects stored //!in the managed segment. std::size_t get_num_unique_objects() { return mp_header->get_num_unique_objects(); } //!Returns a constant iterator to the index storing the //!named allocations. NOT thread-safe. Never throws. const_named_iterator named_begin() const { return mp_header->named_begin(); } //!Returns a constant iterator to the end of the index //!storing the named allocations. NOT thread-safe. Never throws. const_named_iterator named_end() const { return mp_header->named_end(); } //!Returns a constant iterator to the index storing the //!unique allocations. NOT thread-safe. Never throws. const_unique_iterator unique_begin() const { return mp_header->unique_begin(); } //!Returns a constant iterator to the end of the index //!storing the unique allocations. NOT thread-safe. Never throws. const_unique_iterator unique_end() const { return mp_header->unique_end(); } //!This is the default allocator to allocate types T //!from this managed segment template<class T> struct allocator { typedef typename segment_manager::template allocator<T>::type type; }; //!Returns an instance of the default allocator for type T //!initialized that allocates memory from this segment manager. template<class T> typename allocator<T>::type get_allocator() { return mp_header->get_allocator<T>(); } //!This is the default deleter to delete types T //!from this managed segment. template<class T> struct deleter { typedef typename segment_manager::template deleter<T>::type type; }; //!Returns an instance of the default allocator for type T //!initialized that allocates memory from this segment manager. template<class T> typename deleter<T>::type get_deleter() { return mp_header->get_deleter<T>(); } /// @cond //!Tries to find a previous named allocation address. Returns a memory //!buffer and the object count. If not found returned pointer is 0. //!Never throws. template <class T> std::pair<T*, std::size_t> find_no_lock (char_ptr_holder_t name) { return mp_header->template find_no_lock<T>(name); } /// @endcond protected: //!Swaps the segment manager's managed by this managed memory segment. //!NOT thread-safe. Never throws. void swap(basic_managed_memory_impl &other) { std::swap(mp_header, other.mp_header); } private: segment_manager *mp_header;};template<class BasicManagedMemoryImpl>class create_open_func{ public: create_open_func(BasicManagedMemoryImpl * const frontend, detail::create_enum_t type) : m_frontend(frontend), m_type(type){} bool operator()(void *addr, std::size_t size, bool created) const { if(((m_type == detail::DoOpen) && created) || ((m_type == detail::DoCreate) && !created)) return false; if(created) return m_frontend->create_impl(addr, size); else return m_frontend->open_impl (addr, size); } private: BasicManagedMemoryImpl *m_frontend; detail::create_enum_t m_type;};} //namespace detail {} //namespace interprocess {} //namespace boost {#include <boost/interprocess/detail/config_end.hpp>#endif //BOOST_INTERPROCESS_DETAIL_MANAGED_MEMORY_IMPL_HPP
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?