Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-20 07:55:19

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_MEM_ALGO_DETAIL_SIMPLE_SEQ_FIT_IMPL_HPP
0012 #define BOOST_INTERPROCESS_MEM_ALGO_DETAIL_SIMPLE_SEQ_FIT_IMPL_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 0018 ">#
0019 #if defined(BOOST_HAS_PRAGMA_ONCE)
0020 #  pragma once
0021 #endif
0022 
0023 #include <boost/interprocess/detail/config_begin.hpp>
0024 #include <boost/interprocess/detail/workaround.hpp>
0025 
0026 #include <boost/intrusive/pointer_traits.hpp>
0027 
0028 #include <boost/interprocess/interprocess_fwd.hpp>
0029 #include <boost/interprocess/containers/allocation_type.hpp>
0030 #include <boost/container/detail/multiallocation_chain.hpp>
0031 #include <boost/interprocess/offset_ptr.hpp>
0032 #include <boost/interprocess/sync/interprocess_mutex.hpp>
0033 #include <boost/interprocess/exceptions.hpp>
0034 #include <boost/interprocess/detail/utilities.hpp>
0035 #include <boost/interprocess/detail/min_max.hpp>
0036 #include <boost/interprocess/detail/type_traits.hpp>
0037 #include <boost/interprocess/sync/scoped_lock.hpp>
0038 #include <boost/intrusive/pointer_traits.hpp>
0039 #include <boost/interprocess/mem_algo/detail/mem_algo_common.hpp>
0040 #include <boost/move/detail/type_traits.hpp> //make_unsigned, alignment_of
0041 #include <boost/move/detail/force_ptr.hpp>
0042 #include <boost/intrusive/detail/minimal_pair_header.hpp>
0043 #include <cstring>
0044 #include <boost/assert.hpp>
0045 
0046 //!\file
0047 //!Describes sequential fit algorithm used to allocate objects in shared memory.
0048 //!This class is intended as a base class for single segment and multi-segment
0049 //!implementations.
0050 
0051 namespace boost {
0052 namespace interprocess {
0053 namespace ipcdetail {
0054 
0055 //!This class implements the simple sequential fit algorithm with a simply
0056 //!linked list of free buffers.
0057 //!This class is intended as a base class for single segment and multi-segment
0058 //!implementations.
0059 template<class MutexFamily, class VoidPointer>
0060 class simple_seq_fit_impl
0061 {
0062    //Non-copyable
0063    simple_seq_fit_impl();
0064    simple_seq_fit_impl(const simple_seq_fit_impl &);
0065    simple_seq_fit_impl &operator=(const simple_seq_fit_impl &);
0066 
0067    typedef typename boost::intrusive::
0068       pointer_traits<VoidPointer>::template
0069          rebind_pointer<char>::type                         char_ptr;
0070 
0071    public:
0072 
0073    //!Shared interprocess_mutex family used for the rest of the Interprocess framework
0074    typedef MutexFamily        mutex_family;
0075    //!Pointer type to be used with the rest of the Interprocess framework
0076    typedef VoidPointer        void_pointer;
0077    typedef boost::container::dtl::
0078       basic_multiallocation_chain<VoidPointer>     multiallocation_chain;
0079 
0080    typedef typename boost::intrusive::pointer_traits<char_ptr>::difference_type difference_type;
0081    typedef typename boost::container::dtl::make_unsigned<difference_type>::type size_type;
0082 
0083 
0084    private:
0085    class block_ctrl;
0086    friend class block_ctrl;
0087 
0088    typedef typename boost::intrusive::
0089       pointer_traits<VoidPointer>::template
0090          rebind_pointer<block_ctrl>::type                   block_ctrl_ptr;
0091 
0092    //!Block control structure
0093    class block_ctrl
0094    {
0095       public:
0096       static const size_type size_mask = size_type(-1);
0097       //!Offset pointer to the next block.
0098       block_ctrl_ptr m_next;
0099       //!This block's memory size (including block_ctrl
0100       //!header) in BasicSize units
0101       size_type    m_size;
0102 
0103       size_type get_user_bytes() const
0104       {  return this->m_size*Alignment - BlockCtrlBytes; }
0105 
0106       size_type get_total_bytes() const
0107       {  return this->m_size*Alignment; }
0108    };
0109 
0110    //!Shared interprocess_mutex to protect memory allocate/deallocate
0111    typedef typename MutexFamily::mutex_type        interprocess_mutex;
0112 
0113    //!This struct includes needed data and derives from
0114    //!interprocess_mutex to allow EBO when using null interprocess_mutex
0115    struct header_t : public interprocess_mutex
0116    {
0117       //!Pointer to the first free block
0118       block_ctrl        m_root;
0119       //!Allocated bytes for internal checking
0120       size_type         m_allocated;
0121       //!The size of the memory segment
0122       size_type         m_size;
0123       //!The extra size required by the segment
0124       size_type         m_extra_hdr_bytes;
0125    }  m_header;
0126 
0127    friend class ipcdetail::memory_algorithm_common<simple_seq_fit_impl>;
0128 
0129    typedef ipcdetail::memory_algorithm_common<simple_seq_fit_impl> algo_impl_t;
0130 
0131    public:
0132    //!Constructor. "size" is the total size of the managed memory segment,
0133    //!"extra_hdr_bytes" indicates the extra bytes beginning in the sizeof(simple_seq_fit_impl)
0134    //!offset that the allocator should not use at all.
0135    simple_seq_fit_impl           (size_type size, size_type extra_hdr_bytes);
0136 
0137    //!Destructor
0138    ~simple_seq_fit_impl();
0139 
0140    //!Obtains the minimum size needed by the algorithm
0141    static size_type get_min_size (size_type extra_hdr_bytes);
0142 
0143    //Functions for single segment management
0144 
0145    //!Allocates bytes, returns 0 if there is not more memory
0146    void* allocate             (size_type nbytes);
0147 
0148    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0149 
0150    template<class T>
0151    T *allocation_command  (boost::interprocess::allocation_type command,   size_type limit_size,
0152                            size_type &prefer_in_recvd_out_size, T *&reuse);
0153 
0154    void * raw_allocation_command  (boost::interprocess::allocation_type command,   size_type limit_size,
0155                                size_type &prefer_in_recvd_out_size, void *&reuse_ptr, size_type sizeof_object = 1);
0156 
0157    //!Multiple element allocation, same size
0158    //!Experimental. Dont' use
0159    void allocate_many(size_type elem_bytes, size_type num_elements, multiallocation_chain &chain)
0160    {
0161       //-----------------------
0162       boost::interprocess::scoped_lock<interprocess_mutex> guard(m_header);
0163       //-----------------------
0164       algo_impl_t::allocate_many(this, elem_bytes, num_elements, chain);
0165    }
0166 
0167    //!Multiple element allocation, different size
0168    //!Experimental. Dont' use
0169    void allocate_many(const size_type *elem_sizes, size_type n_elements, size_type sizeof_element, multiallocation_chain &chain)
0170    {
0171       //-----------------------
0172       boost::interprocess::scoped_lock<interprocess_mutex> guard(m_header);
0173       //-----------------------
0174       algo_impl_t::allocate_many(this, elem_sizes, n_elements, sizeof_element, chain);
0175    }
0176 
0177    //!Multiple element deallocation
0178    //!Experimental. Dont' use
0179    void deallocate_many(multiallocation_chain &chain);
0180 
0181    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0182 
0183    //!Deallocates previously allocated bytes
0184    void   deallocate          (void *addr);
0185 
0186    //!Returns the size of the memory segment
0187    size_type get_size()  const;
0188 
0189    //!Returns the number of free bytes of the memory segment
0190    size_type get_free_memory()  const;
0191 
0192    //!Increases managed memory in extra_size bytes more
0193    void grow(size_type extra_size);
0194 
0195    //!Decreases managed memory as much as possible
0196    void shrink_to_fit();
0197 
0198    //!Returns true if all allocated memory has been deallocated
0199    bool all_memory_deallocated();
0200 
0201    //!Makes an internal sanity check and returns true if success
0202    bool check_sanity();
0203 
0204    //!Initializes to zero all the memory that's not in use.
0205    //!This function is normally used for security reasons.
0206    void zero_free_memory();
0207 
0208    //!Returns the size of the buffer previously allocated pointed by ptr
0209    size_type size(const void *ptr) const;
0210 
0211    //!Allocates aligned bytes, returns 0 if there is not more memory.
0212    //!Alignment must be power of 2
0213    void* allocate_aligned     (size_type nbytes, size_type alignment);
0214 
0215    private:
0216 
0217    //!Obtains the pointer returned to the user from the block control
0218    static void *priv_get_user_buffer(const block_ctrl *block);
0219 
0220    //!Obtains the block control structure of the user buffer
0221    static block_ctrl *priv_get_block(const void *ptr);
0222 
0223    //!Real allocation algorithm with min allocation option
0224    void * priv_allocate(boost::interprocess::allocation_type command
0225                         ,size_type min_size
0226                         ,size_type &prefer_in_recvd_out_size, void *&reuse_ptr);
0227 
0228    void * priv_allocation_command(boost::interprocess::allocation_type command
0229                                  ,size_type min_size
0230                                  ,size_type &prefer_in_recvd_out_size
0231                                  ,void *&reuse_ptr
0232                                  ,size_type sizeof_object);
0233 
0234    //!Returns the number of total units that a user buffer
0235    //!of "userbytes" bytes really occupies (including header)
0236    static size_type priv_get_total_units(size_type userbytes);
0237 
0238    static size_type priv_first_block_offset(const void *this_ptr, size_type extra_hdr_bytes);
0239    size_type priv_block_end_offset() const;
0240 
0241    //!Returns next block if it's free.
0242    //!Returns 0 if next block is not free.
0243    block_ctrl *priv_next_block_if_free(block_ctrl *ptr);
0244 
0245    //!Check if this block is free (not allocated)
0246    bool priv_is_allocated_block(block_ctrl *ptr);
0247 
0248    //!Returns previous block's if it's free.
0249    //!Returns 0 if previous block is not free.
0250    std::pair<block_ctrl*, block_ctrl*> priv_prev_block_if_free(block_ctrl *ptr);
0251 
0252    //!Real expand function implementation
0253    bool priv_expand(void *ptr, size_type min_size, size_type &prefer_in_recvd_out_size);
0254 
0255    //!Real expand to both sides implementation
0256    void* priv_expand_both_sides(boost::interprocess::allocation_type command
0257                                ,size_type min_size, size_type &prefer_in_recvd_out_size
0258                                ,void *reuse_ptr
0259                                ,bool only_preferred_backwards);
0260 
0261    //!Real private aligned allocation function
0262    //void* priv_allocate_aligned     (size_type nbytes, size_type alignment);
0263 
0264    //!Checks if block has enough memory and splits/unlinks the block
0265    //!returning the address to the users
0266    void* priv_check_and_allocate(size_type units
0267                                 ,block_ctrl* prev
0268                                 ,block_ctrl* block
0269                                 ,size_type &received_size);
0270    //!Real deallocation algorithm
0271    void priv_deallocate(void *addr);
0272 
0273    //!Makes a new memory portion available for allocation
0274    void priv_add_segment(void *addr, size_type size);
0275 
0276    void priv_mark_new_allocated_block(block_ctrl *block);
0277 
0278    public:
0279    static const size_type Alignment      = ::boost::container::dtl::alignment_of
0280       < ::boost::container::dtl::max_align_t>::value;
0281    private:
0282    static const size_type BlockCtrlBytes = ipcdetail::ct_rounded_size<sizeof(block_ctrl), Alignment>::value;
0283    static const size_type BlockCtrlUnits = BlockCtrlBytes/Alignment;
0284    static const size_type AllocatedCtrlBytes = BlockCtrlBytes;
0285    static const size_type AllocatedCtrlUnits = BlockCtrlUnits;
0286    static const size_type UsableByPreviousChunk = 0;
0287 
0288    public:
0289    static const size_type PayloadPerAllocation = BlockCtrlBytes;
0290 };
0291 
0292 template<class MutexFamily, class VoidPointer>
0293 inline typename simple_seq_fit_impl<MutexFamily, VoidPointer>::size_type
0294 simple_seq_fit_impl<MutexFamily, VoidPointer>
0295    ::priv_first_block_offset(const void *this_ptr, size_type extra_hdr_bytes)
0296 {
0297    //First align "this" pointer
0298    size_type uint_this         = (std::size_t)this_ptr;
0299    size_type uint_aligned_this = uint_this/Alignment*Alignment;
0300    size_type this_disalignment = (uint_this - uint_aligned_this);
0301    size_type block1_off =
0302       ipcdetail::get_rounded_size(sizeof(simple_seq_fit_impl) + extra_hdr_bytes + this_disalignment, Alignment)
0303       - this_disalignment;
0304    algo_impl_t::assert_alignment(this_disalignment + block1_off);
0305    return block1_off;
0306 }
0307 
0308 template<class MutexFamily, class VoidPointer>
0309 inline typename simple_seq_fit_impl<MutexFamily, VoidPointer>::size_type
0310 simple_seq_fit_impl<MutexFamily, VoidPointer>
0311    ::priv_block_end_offset() const
0312 {
0313    //First align "this" pointer
0314    size_type uint_this         = (std::size_t)this;
0315    size_type uint_aligned_this = uint_this/Alignment*Alignment;
0316    size_type this_disalignment = (uint_this - uint_aligned_this);
0317    size_type old_end =
0318       ipcdetail::get_truncated_size(m_header.m_size + this_disalignment, Alignment)
0319       - this_disalignment;
0320    algo_impl_t::assert_alignment(old_end + this_disalignment);
0321    return old_end;
0322 }
0323 
0324 template<class MutexFamily, class VoidPointer>
0325 inline simple_seq_fit_impl<MutexFamily, VoidPointer>::
0326    simple_seq_fit_impl(size_type segment_size, size_type extra_hdr_bytes)
0327 {
0328    //Initialize sizes and counters
0329    m_header.m_allocated = 0;
0330    m_header.m_size      = segment_size;
0331    m_header.m_extra_hdr_bytes = extra_hdr_bytes;
0332 
0333    //Initialize pointers
0334    size_type block1_off = priv_first_block_offset(this, extra_hdr_bytes);
0335 
0336    m_header.m_root.m_next  = move_detail::force_ptr<block_ctrl*>
0337       ((reinterpret_cast<char*>(this) + block1_off));
0338    algo_impl_t::assert_alignment(ipcdetail::to_raw_pointer(m_header.m_root.m_next));
0339    m_header.m_root.m_next->m_size  = (segment_size - block1_off)/Alignment;
0340    m_header.m_root.m_next->m_next  = &m_header.m_root;
0341 }
0342 
0343 template<class MutexFamily, class VoidPointer>
0344 inline simple_seq_fit_impl<MutexFamily, VoidPointer>::~simple_seq_fit_impl()
0345 {
0346    //There is a memory leak!
0347 //   BOOST_ASSERT(m_header.m_allocated == 0);
0348 //   BOOST_ASSERT(m_header.m_root.m_next->m_next == block_ctrl_ptr(&m_header.m_root));
0349 }
0350 
0351 template<class MutexFamily, class VoidPointer>
0352 inline void simple_seq_fit_impl<MutexFamily, VoidPointer>::grow(size_type extra_size)
0353 {
0354    //Old highest address block's end offset
0355    size_type old_end = this->priv_block_end_offset();
0356 
0357    //Update managed buffer's size
0358    m_header.m_size += extra_size;
0359 
0360    //We need at least MinBlockSize blocks to create a new block
0361    if((m_header.m_size - old_end) < BlockCtrlBytes){
0362       return;
0363    }
0364 
0365    //We'll create a new free block with extra_size bytes
0366 
0367    block_ctrl *new_block = move_detail::force_ptr<block_ctrl*>
0368       (reinterpret_cast<char*>(this) + old_end);
0369 
0370    algo_impl_t::assert_alignment(new_block);
0371    new_block->m_next = 0;
0372    new_block->m_size = (m_header.m_size - old_end)/Alignment;
0373    m_header.m_allocated += new_block->m_size*Alignment;
0374    this->priv_deallocate(priv_get_user_buffer(new_block));
0375 }
0376 
0377 template<class MutexFamily, class VoidPointer>
0378 void simple_seq_fit_impl<MutexFamily, VoidPointer>::shrink_to_fit()
0379 {
0380    //Get the root and the first memory block
0381    block_ctrl *prev                 = &m_header.m_root;
0382    block_ctrl *last                 = &m_header.m_root;
0383    block_ctrl *block                = ipcdetail::to_raw_pointer(last->m_next);
0384    block_ctrl *root                 = &m_header.m_root;
0385 
0386    //No free block?
0387    if(block == root) return;
0388 
0389    //Iterate through the free block list
0390    while(block != root){
0391       prev  = last;
0392       last  = block;
0393       block = ipcdetail::to_raw_pointer(block->m_next);
0394    }
0395 
0396    char *last_free_end_address   = reinterpret_cast<char*>(last) + last->m_size*Alignment;
0397    if(last_free_end_address != (reinterpret_cast<char*>(this) + priv_block_end_offset())){
0398       //there is an allocated block in the end of this block
0399       //so no shrinking is possible
0400       return;
0401    }
0402 
0403    //Check if have only 1 big free block
0404    void *unique_block = 0;
0405    if(!m_header.m_allocated){
0406       BOOST_ASSERT(prev == root);
0407       size_type ignore_recvd = 0;
0408       void *ignore_reuse = 0;
0409       unique_block = priv_allocate(boost::interprocess::allocate_new, 0, ignore_recvd, ignore_reuse);
0410       if(!unique_block)
0411          return;
0412       last = ipcdetail::to_raw_pointer(m_header.m_root.m_next);
0413       BOOST_ASSERT(last_free_end_address == (reinterpret_cast<char*>(last) + last->m_size*Alignment));
0414    }
0415    size_type last_units = last->m_size;
0416 
0417    size_type received_size;
0418    void *addr = priv_check_and_allocate(last_units, prev, last, received_size);
0419    (void)addr;
0420    BOOST_ASSERT(addr);
0421    BOOST_ASSERT(received_size == last_units*Alignment - AllocatedCtrlBytes);
0422 
0423    //Shrink it
0424    m_header.m_size /= Alignment;
0425    m_header.m_size -= last->m_size;
0426    m_header.m_size *= Alignment;
0427    m_header.m_allocated -= last->m_size*Alignment;
0428 
0429    if(unique_block)
0430       priv_deallocate(unique_block);
0431 }
0432 
0433 template<class MutexFamily, class VoidPointer>
0434 inline void simple_seq_fit_impl<MutexFamily, VoidPointer>::
0435    priv_mark_new_allocated_block(block_ctrl *new_block)
0436 {
0437    new_block->m_next = 0;
0438 }
0439 
0440 template<class MutexFamily, class VoidPointer>
0441 inline
0442 typename simple_seq_fit_impl<MutexFamily, VoidPointer>::block_ctrl *
0443    simple_seq_fit_impl<MutexFamily, VoidPointer>::priv_get_block(const void *ptr)
0444 {
0445    return const_cast<block_ctrl*>(move_detail::force_ptr<const block_ctrl*>
0446       (reinterpret_cast<const char*>(ptr) - AllocatedCtrlBytes));
0447 }
0448 
0449 template<class MutexFamily, class VoidPointer>
0450 inline
0451 void *simple_seq_fit_impl<MutexFamily, VoidPointer>::
0452       priv_get_user_buffer(const typename simple_seq_fit_impl<MutexFamily, VoidPointer>::block_ctrl *block)
0453 {
0454    return const_cast<char*>(reinterpret_cast<const char*>(block) + AllocatedCtrlBytes);
0455 }
0456 
0457 template<class MutexFamily, class VoidPointer>
0458 inline void simple_seq_fit_impl<MutexFamily, VoidPointer>::priv_add_segment(void *addr, size_type segment_size)
0459 {
0460    algo_impl_t::assert_alignment(addr);
0461    //Check size
0462    BOOST_ASSERT(!(segment_size < BlockCtrlBytes));
0463    if(segment_size < BlockCtrlBytes)
0464       return;
0465    //Construct big block using the new segment
0466    block_ctrl *new_block   = static_cast<block_ctrl *>(addr);
0467    new_block->m_size       = segment_size/Alignment;
0468    new_block->m_next       = 0;
0469    //Simulate this block was previously allocated
0470    m_header.m_allocated   += new_block->m_size*Alignment;
0471    //Return block and insert it in the free block list
0472    this->priv_deallocate(priv_get_user_buffer(new_block));
0473 }
0474 
0475 template<class MutexFamily, class VoidPointer>
0476 inline typename simple_seq_fit_impl<MutexFamily, VoidPointer>::size_type
0477 simple_seq_fit_impl<MutexFamily, VoidPointer>::get_size()  const
0478    {  return m_header.m_size;  }
0479 
0480 template<class MutexFamily, class VoidPointer>
0481 inline typename simple_seq_fit_impl<MutexFamily, VoidPointer>::size_type
0482 simple_seq_fit_impl<MutexFamily, VoidPointer>::get_free_memory()  const
0483 {
0484    return m_header.m_size - m_header.m_allocated -
0485       algo_impl_t::multiple_of_units(sizeof(*this) + m_header.m_extra_hdr_bytes);
0486 }
0487 
0488 template<class MutexFamily, class VoidPointer>
0489 inline typename simple_seq_fit_impl<MutexFamily, VoidPointer>::size_type
0490 simple_seq_fit_impl<MutexFamily, VoidPointer>::
0491    get_min_size (size_type extra_hdr_bytes)
0492 {
0493    return ipcdetail::get_rounded_size((size_type)sizeof(simple_seq_fit_impl),Alignment) +
0494           ipcdetail::get_rounded_size(extra_hdr_bytes,Alignment)
0495           + BlockCtrlBytes;
0496 }
0497 
0498 template<class MutexFamily, class VoidPointer>
0499 inline bool simple_seq_fit_impl<MutexFamily, VoidPointer>::
0500     all_memory_deallocated()
0501 {
0502    //-----------------------
0503    boost::interprocess::scoped_lock<interprocess_mutex> guard(m_header);
0504    //-----------------------
0505    return m_header.m_allocated == 0 &&
0506           ipcdetail::to_raw_pointer(m_header.m_root.m_next->m_next) == &m_header.m_root;
0507 }
0508 
0509 template<class MutexFamily, class VoidPointer>
0510 inline void simple_seq_fit_impl<MutexFamily, VoidPointer>::zero_free_memory()
0511 {
0512    //-----------------------
0513    boost::interprocess::scoped_lock<interprocess_mutex> guard(m_header);
0514    //-----------------------
0515    block_ctrl *block = ipcdetail::to_raw_pointer(m_header.m_root.m_next);
0516 
0517    //Iterate through all free portions
0518    do{
0519       //Just clear user the memory part reserved for the user
0520       std::memset( priv_get_user_buffer(block)
0521                  , 0
0522              , block->get_user_bytes());
0523       block = ipcdetail::to_raw_pointer(block->m_next);
0524    }
0525    while(block != &m_header.m_root);
0526 }
0527 
0528 template<class MutexFamily, class VoidPointer>
0529 inline bool simple_seq_fit_impl<MutexFamily, VoidPointer>::
0530     check_sanity()
0531 {
0532    //-----------------------
0533    boost::interprocess::scoped_lock<interprocess_mutex> guard(m_header);
0534    //-----------------------
0535    block_ctrl *block = ipcdetail::to_raw_pointer(m_header.m_root.m_next);
0536 
0537    size_type free_memory = 0;
0538 
0539    //Iterate through all blocks obtaining their size
0540    while(block != &m_header.m_root){
0541       algo_impl_t::assert_alignment(block);
0542       if(!algo_impl_t::check_alignment(block))
0543          return false;
0544       //Free blocks's next must be always valid
0545       block_ctrl *next = ipcdetail::to_raw_pointer(block->m_next);
0546       if(!next){
0547          return false;
0548       }
0549       free_memory += block->m_size*Alignment;
0550       block = next;
0551    }
0552 
0553    //Check allocated bytes are less than size
0554    if(m_header.m_allocated > m_header.m_size){
0555       return false;
0556    }
0557 
0558    //Check free bytes are less than size
0559    if(free_memory > m_header.m_size){
0560       return false;
0561    }
0562    return true;
0563 }
0564 
0565 template<class MutexFamily, class VoidPointer>
0566 inline void* simple_seq_fit_impl<MutexFamily, VoidPointer>::
0567    allocate(size_type nbytes)
0568 {
0569    //-----------------------
0570    boost::interprocess::scoped_lock<interprocess_mutex> guard(m_header);
0571    //-----------------------
0572    size_type ignore_recvd = nbytes;
0573    void *ignore_reuse = 0;
0574    return priv_allocate(boost::interprocess::allocate_new, nbytes, ignore_recvd, ignore_reuse);
0575 }
0576 
0577 template<class MutexFamily, class VoidPointer>
0578 inline void* simple_seq_fit_impl<MutexFamily, VoidPointer>::
0579    allocate_aligned(size_type nbytes, size_type alignment)
0580 {
0581    //-----------------------
0582    boost::interprocess::scoped_lock<interprocess_mutex> guard(m_header);
0583    //-----------------------
0584    return algo_impl_t::
0585       allocate_aligned(this, nbytes, alignment);
0586 }
0587 
0588 template<class MutexFamily, class VoidPointer>
0589 template<class T>
0590 inline T* simple_seq_fit_impl<MutexFamily, VoidPointer>::
0591    allocation_command  (boost::interprocess::allocation_type command,   size_type limit_size,
0592                         size_type &prefer_in_recvd_out_size, T *&reuse_ptr)
0593 {
0594    void *raw_reuse = reuse_ptr;
0595    void * const ret = priv_allocation_command
0596       (command, limit_size, prefer_in_recvd_out_size, raw_reuse, sizeof(T));
0597    BOOST_ASSERT(0 == ((std::size_t)ret % ::boost::container::dtl::alignment_of<T>::value));
0598    reuse_ptr = static_cast<T*>(raw_reuse);
0599    return static_cast<T*>(ret);
0600 }
0601 
0602 template<class MutexFamily, class VoidPointer>
0603 inline void* simple_seq_fit_impl<MutexFamily, VoidPointer>::
0604    raw_allocation_command  (boost::interprocess::allocation_type command, size_type limit_objects,
0605                         size_type &prefer_in_recvd_out_size, void *&reuse_ptr, size_type sizeof_object)
0606 {
0607    size_type const preferred_objects = prefer_in_recvd_out_size;
0608    if(!sizeof_object){
0609       return reuse_ptr = 0, static_cast<void*>(0);
0610   }
0611    if(command & boost::interprocess::try_shrink_in_place){
0612       if(!reuse_ptr) return static_cast<void*>(0);
0613       prefer_in_recvd_out_size = preferred_objects*sizeof_object;
0614       bool success = algo_impl_t::try_shrink
0615          ( this, reuse_ptr, limit_objects*sizeof_object, prefer_in_recvd_out_size);
0616       prefer_in_recvd_out_size /= sizeof_object;
0617       return success ? reuse_ptr : 0;
0618    }
0619    else{
0620       return priv_allocation_command
0621          (command, limit_objects, prefer_in_recvd_out_size, reuse_ptr, sizeof_object);
0622    }
0623 }
0624 
0625 template<class MutexFamily, class VoidPointer>
0626 inline void* simple_seq_fit_impl<MutexFamily, VoidPointer>::
0627    priv_allocation_command (boost::interprocess::allocation_type command,   size_type limit_size,
0628                        size_type &prefer_in_recvd_out_size, void *&reuse_ptr, size_type sizeof_object)
0629 {
0630    size_type const preferred_size = prefer_in_recvd_out_size;
0631    command &= ~boost::interprocess::expand_bwd;
0632    if(!command){
0633       return reuse_ptr = 0, static_cast<void*>(0);
0634    }
0635 
0636    size_type max_count = m_header.m_size/sizeof_object;
0637    if(limit_size > max_count || preferred_size > max_count){
0638       return reuse_ptr = 0, static_cast<void*>(0);
0639    }
0640    size_type l_size = limit_size*sizeof_object;
0641    size_type r_size = preferred_size*sizeof_object;
0642    void *ret = 0;
0643    {
0644       //-----------------------
0645       boost::interprocess::scoped_lock<interprocess_mutex> guard(m_header);
0646       //-----------------------
0647       ret = priv_allocate(command, l_size, r_size, reuse_ptr);
0648    }
0649    prefer_in_recvd_out_size = r_size/sizeof_object;
0650    return ret;
0651 }
0652 
0653 template<class MutexFamily, class VoidPointer>
0654 inline typename simple_seq_fit_impl<MutexFamily, VoidPointer>::size_type
0655 simple_seq_fit_impl<MutexFamily, VoidPointer>::size(const void *ptr) const
0656 {
0657    //We need no synchronization since this block is not going
0658    //to be modified
0659    //Obtain the real size of the block
0660    const block_ctrl *block = static_cast<const block_ctrl*>(priv_get_block(ptr));
0661    return block->get_user_bytes();
0662 }
0663 
0664 template<class MutexFamily, class VoidPointer>
0665 void* simple_seq_fit_impl<MutexFamily, VoidPointer>::
0666    priv_expand_both_sides(boost::interprocess::allocation_type command
0667                          ,size_type min_size
0668                          ,size_type &prefer_in_recvd_out_size
0669                          ,void *reuse_ptr
0670                          ,bool only_preferred_backwards)
0671 {
0672    size_type const preferred_size = prefer_in_recvd_out_size;
0673    typedef std::pair<block_ctrl *, block_ctrl *> prev_block_t;
0674    block_ctrl *reuse = priv_get_block(reuse_ptr);
0675    prefer_in_recvd_out_size = 0;
0676 
0677    if(this->size(reuse_ptr) > min_size){
0678       prefer_in_recvd_out_size = this->size(reuse_ptr);
0679       return reuse_ptr;
0680    }
0681 
0682    if(command & boost::interprocess::expand_fwd){
0683       if(priv_expand(reuse_ptr, min_size, prefer_in_recvd_out_size = preferred_size))
0684          return reuse_ptr;
0685    }
0686    else{
0687       prefer_in_recvd_out_size = this->size(reuse_ptr);
0688    }
0689    if(command & boost::interprocess::expand_bwd){
0690       size_type extra_forward = !prefer_in_recvd_out_size ? 0 : prefer_in_recvd_out_size + BlockCtrlBytes;
0691       prev_block_t prev_pair = priv_prev_block_if_free(reuse);
0692       block_ctrl *prev = prev_pair.second;
0693       if(!prev){
0694          return 0;
0695       }
0696 
0697       size_type needs_backwards =
0698          ipcdetail::get_rounded_size(preferred_size - extra_forward, Alignment);
0699 
0700       if(!only_preferred_backwards){
0701             max_value(ipcdetail::get_rounded_size(min_size - extra_forward, Alignment)
0702                      ,min_value(prev->get_user_bytes(), needs_backwards));
0703       }
0704 
0705       //Check if previous block has enough size
0706       if((prev->get_user_bytes()) >=  needs_backwards){
0707          //Now take all next space. This will succeed
0708          if(!priv_expand(reuse_ptr, prefer_in_recvd_out_size, prefer_in_recvd_out_size)){
0709             BOOST_ASSERT(0);
0710          }
0711 
0712          //We need a minimum size to split the previous one
0713          if((prev->get_user_bytes() - needs_backwards) > 2*BlockCtrlBytes){
0714              block_ctrl *new_block = move_detail::force_ptr<block_ctrl*>
0715                   (reinterpret_cast<char*>(reuse) - needs_backwards - BlockCtrlBytes);
0716 
0717             new_block->m_next = 0;
0718             new_block->m_size =
0719                BlockCtrlUnits + (needs_backwards + extra_forward)/Alignment;
0720             prev->m_size =
0721                (prev->get_total_bytes() - needs_backwards)/Alignment - BlockCtrlUnits;
0722             prefer_in_recvd_out_size = needs_backwards + extra_forward;
0723             m_header.m_allocated += needs_backwards + BlockCtrlBytes;
0724             return priv_get_user_buffer(new_block);
0725          }
0726          else{
0727             //Just merge the whole previous block
0728             block_ctrl *prev_2_block = prev_pair.first;
0729             //Update received size and allocation
0730             prefer_in_recvd_out_size = extra_forward + prev->get_user_bytes();
0731             m_header.m_allocated += prev->get_total_bytes();
0732             //Now unlink it from previous block
0733             prev_2_block->m_next = prev->m_next;
0734             prev->m_size = reuse->m_size + prev->m_size;
0735             prev->m_next = 0;
0736             priv_get_user_buffer(prev);
0737          }
0738       }
0739    }
0740    return 0;
0741 }
0742 
0743 template<class MutexFamily, class VoidPointer>
0744 inline void simple_seq_fit_impl<MutexFamily, VoidPointer>::
0745    deallocate_many(typename simple_seq_fit_impl<MutexFamily, VoidPointer>::multiallocation_chain &chain)
0746 {
0747    //-----------------------
0748    boost::interprocess::scoped_lock<interprocess_mutex> guard(m_header);
0749    //-----------------------
0750    while(!chain.empty()){
0751       this->priv_deallocate(to_raw_pointer(chain.pop_front()));
0752    }
0753 }
0754 
0755 template<class MutexFamily, class VoidPointer>
0756 inline typename simple_seq_fit_impl<MutexFamily, VoidPointer>::size_type
0757 simple_seq_fit_impl<MutexFamily, VoidPointer>::
0758    priv_get_total_units(size_type userbytes)
0759 {
0760    size_type s = ipcdetail::get_rounded_size(userbytes, Alignment)/Alignment;
0761    if(!s)   ++s;
0762    return BlockCtrlUnits + s;
0763 }
0764 
0765 template<class MutexFamily, class VoidPointer>
0766 void * simple_seq_fit_impl<MutexFamily, VoidPointer>::
0767    priv_allocate(boost::interprocess::allocation_type command
0768                 ,size_type limit_size, size_type &prefer_in_recvd_out_size, void *&reuse_ptr)
0769 {
0770    size_type const preferred_size = prefer_in_recvd_out_size;
0771    if(command & boost::interprocess::shrink_in_place){
0772       if(!reuse_ptr)  return static_cast<void*>(0);
0773       bool success = algo_impl_t::shrink(this, reuse_ptr, limit_size, prefer_in_recvd_out_size);
0774       return success ? reuse_ptr : 0;
0775    }
0776    prefer_in_recvd_out_size = 0;
0777 
0778    if(limit_size > preferred_size){
0779       return reuse_ptr = 0, static_cast<void*>(0);
0780    }
0781 
0782    //Number of units to request (including block_ctrl header)
0783    size_type nunits = ipcdetail::get_rounded_size(preferred_size, Alignment)/Alignment + BlockCtrlUnits;
0784 
0785    //Get the root and the first memory block
0786    block_ctrl *prev                 = &m_header.m_root;
0787    block_ctrl *block                = ipcdetail::to_raw_pointer(prev->m_next);
0788    block_ctrl *root                 = &m_header.m_root;
0789    block_ctrl *biggest_block        = 0;
0790    block_ctrl *prev_biggest_block   = 0;
0791    size_type biggest_size         = 0;
0792 
0793    //Expand in place
0794    if(reuse_ptr && (command & (boost::interprocess::expand_fwd | boost::interprocess::expand_bwd))){
0795       void *ret = priv_expand_both_sides(command, limit_size, prefer_in_recvd_out_size = preferred_size, reuse_ptr, true);
0796       if(ret){
0797          algo_impl_t::assert_alignment(ret);
0798          return ret;
0799       }
0800    }
0801 
0802    if(command & boost::interprocess::allocate_new){
0803       prefer_in_recvd_out_size = 0;
0804       while(block != root){
0805          //Update biggest block pointers
0806          if(block->m_size > biggest_size){
0807             prev_biggest_block = prev;
0808             biggest_size  = block->m_size;
0809             biggest_block = block;
0810          }
0811          algo_impl_t::assert_alignment(block);
0812          void *addr = this->priv_check_and_allocate(nunits, prev, block, prefer_in_recvd_out_size);
0813          if(addr){
0814             algo_impl_t::assert_alignment(addr);
0815             return reuse_ptr = 0, addr;
0816          }
0817          //Bad luck, let's check next block
0818          prev  = block;
0819          block = ipcdetail::to_raw_pointer(block->m_next);
0820       }
0821 
0822       //Bad luck finding preferred_size, now if we have any biggest_block
0823       //try with this block
0824       if(biggest_block){
0825          size_type limit_units = ipcdetail::get_rounded_size(limit_size, Alignment)/Alignment + BlockCtrlUnits;
0826          if(biggest_block->m_size < limit_units){
0827             return reuse_ptr = 0, static_cast<void*>(0);
0828          }
0829          void *ret = this->priv_check_and_allocate
0830             (biggest_block->m_size, prev_biggest_block, biggest_block, prefer_in_recvd_out_size = biggest_block->m_size*Alignment - BlockCtrlUnits);
0831          BOOST_ASSERT(ret != 0);
0832          algo_impl_t::assert_alignment(ret);
0833          return reuse_ptr = 0, ret;
0834       }
0835    }
0836    //Now try to expand both sides with min size
0837    if(reuse_ptr && (command & (boost::interprocess::expand_fwd | boost::interprocess::expand_bwd))){
0838       void *ret = priv_expand_both_sides (command, limit_size, prefer_in_recvd_out_size = preferred_size, reuse_ptr, false);
0839       algo_impl_t::assert_alignment(ret);
0840       return ret;
0841    }
0842    return reuse_ptr = 0, static_cast<void*>(0);
0843 }
0844 
0845 template<class MutexFamily, class VoidPointer> inline
0846 bool simple_seq_fit_impl<MutexFamily, VoidPointer>::priv_is_allocated_block
0847       (typename simple_seq_fit_impl<MutexFamily, VoidPointer>::block_ctrl *block)
0848 {  return block->m_next == 0;  }
0849 
0850 template<class MutexFamily, class VoidPointer>
0851 inline typename simple_seq_fit_impl<MutexFamily, VoidPointer>::block_ctrl *
0852    simple_seq_fit_impl<MutexFamily, VoidPointer>::
0853       priv_next_block_if_free
0854          (typename simple_seq_fit_impl<MutexFamily, VoidPointer>::block_ctrl *ptr)
0855 {
0856    //Take the address where the next block should go
0857    block_ctrl *next_block = move_detail::force_ptr<block_ctrl*>
0858       (reinterpret_cast<char*>(ptr) + ptr->m_size*Alignment);
0859 
0860    //Check if the adjacent block is in the managed segment
0861    char *this_char_ptr = reinterpret_cast<char*>(this);
0862    char *next_char_ptr = reinterpret_cast<char*>(next_block);
0863    size_type distance = (size_type)(next_char_ptr - this_char_ptr)/Alignment;
0864 
0865    if(distance >= (m_header.m_size/Alignment)){
0866       //"next_block" does not exist so we can't expand "block"
0867       return 0;
0868    }
0869 
0870    if(!next_block->m_next)
0871       return 0;
0872 
0873    return next_block;
0874 }
0875 
0876 template<class MutexFamily, class VoidPointer>
0877 inline
0878    std::pair<typename simple_seq_fit_impl<MutexFamily, VoidPointer>::block_ctrl *
0879             ,typename simple_seq_fit_impl<MutexFamily, VoidPointer>::block_ctrl *>
0880    simple_seq_fit_impl<MutexFamily, VoidPointer>::
0881       priv_prev_block_if_free
0882          (typename simple_seq_fit_impl<MutexFamily, VoidPointer>::block_ctrl *ptr)
0883 {
0884    typedef std::pair<block_ctrl *, block_ctrl *> prev_pair_t;
0885    //Take the address where the previous block should go
0886    block_ctrl *root           = &m_header.m_root;
0887    block_ctrl *prev_2_block   = root;
0888    block_ctrl *prev_block = ipcdetail::to_raw_pointer(root->m_next);
0889 
0890    while((reinterpret_cast<char*>(prev_block) + prev_block->m_size*Alignment)
0891             != reinterpret_cast<char*>(ptr)
0892          && prev_block != root){
0893       prev_2_block = prev_block;
0894       prev_block = ipcdetail::to_raw_pointer(prev_block->m_next);
0895    }
0896 
0897    if(prev_block == root || !prev_block->m_next)
0898       return prev_pair_t(static_cast<block_ctrl*>(0), static_cast<block_ctrl*>(0));
0899 
0900    //Check if the previous block is in the managed segment
0901    char *this_char_ptr = reinterpret_cast<char*>(this);
0902    char *prev_char_ptr = reinterpret_cast<char*>(prev_block);
0903    size_type distance = (size_type)(prev_char_ptr - this_char_ptr)/Alignment;
0904 
0905    if(distance >= (m_header.m_size/Alignment)){
0906       //"previous_block" does not exist so we can't expand "block"
0907       return prev_pair_t(static_cast<block_ctrl*>(0), static_cast<block_ctrl*>(0));
0908    }
0909    return prev_pair_t(prev_2_block, prev_block);
0910 }
0911 
0912 
0913 template<class MutexFamily, class VoidPointer>
0914 inline bool simple_seq_fit_impl<MutexFamily, VoidPointer>::
0915    priv_expand (void *ptr, size_type min_size, size_type &received_size)
0916 {
0917    size_type preferred_size = received_size;
0918    //Obtain the real size of the block
0919    block_ctrl *block = move_detail::force_ptr<block_ctrl*>(priv_get_block(ptr));
0920    size_type old_block_size = block->m_size;
0921 
0922    //All used blocks' next is marked with 0 so check it
0923    BOOST_ASSERT(block->m_next == 0);
0924 
0925    //Put this to a safe value
0926    received_size = old_block_size*Alignment - BlockCtrlBytes;
0927 
0928    //Now translate it to Alignment units
0929    min_size       = ipcdetail::get_rounded_size(min_size, Alignment)/Alignment;
0930    preferred_size = ipcdetail::get_rounded_size(preferred_size, Alignment)/Alignment;
0931 
0932    //Some parameter checks
0933    if(min_size > preferred_size)
0934       return false;
0935 
0936    size_type data_size = old_block_size - BlockCtrlUnits;
0937 
0938    if(data_size >= min_size)
0939       return true;
0940 
0941    block_ctrl *next_block = priv_next_block_if_free(block);
0942    if(!next_block){
0943       return false;
0944    }
0945 
0946    //Is "block" + "next_block" big enough?
0947    size_type merged_size = old_block_size + next_block->m_size;
0948 
0949    //Now we can expand this block further than before
0950    received_size = merged_size*Alignment - BlockCtrlBytes;
0951 
0952    if(merged_size < (min_size + BlockCtrlUnits)){
0953       return false;
0954    }
0955 
0956    //We can fill expand. Merge both blocks,
0957    block->m_next = next_block->m_next;
0958    block->m_size = merged_size;
0959 
0960    //Find the previous free block of next_block
0961    block_ctrl *prev = &m_header.m_root;
0962    while(ipcdetail::to_raw_pointer(prev->m_next) != next_block){
0963       prev = ipcdetail::to_raw_pointer(prev->m_next);
0964    }
0965 
0966    //Now insert merged block in the free list
0967    //This allows reusing allocation logic in this function
0968    m_header.m_allocated -= old_block_size*Alignment;
0969    prev->m_next = block;
0970 
0971    //Now use check and allocate to do the allocation logic
0972    preferred_size += BlockCtrlUnits;
0973    size_type nunits = preferred_size < merged_size ? preferred_size : merged_size;
0974 
0975    //This must success since nunits is less than merged_size!
0976    if(!this->priv_check_and_allocate (nunits, prev, block, received_size)){
0977       //Something very ugly is happening here. This is a bug
0978       //or there is memory corruption
0979       BOOST_ASSERT(0);
0980       return false;
0981    }
0982    return true;
0983 }
0984 
0985 template<class MutexFamily, class VoidPointer> inline
0986 void* simple_seq_fit_impl<MutexFamily, VoidPointer>::priv_check_and_allocate
0987    (size_type nunits
0988    ,typename simple_seq_fit_impl<MutexFamily, VoidPointer>::block_ctrl* prev
0989    ,typename simple_seq_fit_impl<MutexFamily, VoidPointer>::block_ctrl* block
0990    ,size_type &received_size)
0991 {
0992    size_type upper_nunits = nunits + BlockCtrlUnits;
0993    bool found = false;
0994 
0995    if (block->m_size > upper_nunits){
0996       //This block is bigger than needed, split it in
0997       //two blocks, the first's size will be "units"
0998       //the second's size will be "block->m_size-units"
0999       size_type total_size = block->m_size;
1000       block->m_size  = nunits;
1001 
1002       block_ctrl *new_block = move_detail::force_ptr<block_ctrl*>
1003          (reinterpret_cast<char*>(block) + Alignment*nunits);
1004       new_block->m_size  = total_size - nunits;
1005       new_block->m_next  = block->m_next;
1006       prev->m_next = new_block;
1007       found = true;
1008    }
1009    else if (block->m_size >= nunits){
1010       //This block has exactly the right size with an extra
1011       //unusable extra bytes.
1012       prev->m_next = block->m_next;
1013       found = true;
1014    }
1015 
1016    if(found){
1017       //We need block_ctrl for deallocation stuff, so
1018       //return memory user can overwrite
1019       m_header.m_allocated += block->m_size*Alignment;
1020       received_size =  block->get_user_bytes();
1021       //Mark the block as allocated
1022       block->m_next = 0;
1023       //Check alignment
1024       algo_impl_t::assert_alignment(block);
1025       return priv_get_user_buffer(block);
1026    }
1027    return 0;
1028 }
1029 
1030 template<class MutexFamily, class VoidPointer>
1031 void simple_seq_fit_impl<MutexFamily, VoidPointer>::deallocate(void* addr)
1032 {
1033    if(!addr)   return;
1034    //-----------------------
1035    boost::interprocess::scoped_lock<interprocess_mutex> guard(m_header);
1036    //-----------------------
1037    return this->priv_deallocate(addr);
1038 }
1039 
1040 template<class MutexFamily, class VoidPointer>
1041 void simple_seq_fit_impl<MutexFamily, VoidPointer>::priv_deallocate(void* addr)
1042 {
1043    if(!addr)   return;
1044 
1045    //Let's get free block list. List is always sorted
1046    //by memory address to allow block merging.
1047    //Pointer next always points to the first
1048    //(lower address) block
1049    block_ctrl * prev  = &m_header.m_root;
1050    block_ctrl * pos   = ipcdetail::to_raw_pointer(m_header.m_root.m_next);
1051    block_ctrl * block = move_detail::force_ptr<block_ctrl*>(priv_get_block(addr));
1052 
1053    //All used blocks' next is marked with 0 so check it
1054    BOOST_ASSERT(block->m_next == 0);
1055 
1056    //Check if alignment and block size are right
1057    algo_impl_t::assert_alignment(addr);
1058 
1059    size_type total_size = Alignment*block->m_size;
1060    BOOST_ASSERT(m_header.m_allocated >= total_size);
1061 
1062    //Update used memory count
1063    m_header.m_allocated -= total_size;
1064 
1065    //Let's find the previous and the next block of the block to deallocate
1066    //This ordering comparison must be done with original pointers
1067    //types since their mapping to raw pointers can be different
1068    //in each process
1069    while((ipcdetail::to_raw_pointer(pos) != &m_header.m_root) && (block > pos)){
1070       prev = pos;
1071       pos = ipcdetail::to_raw_pointer(pos->m_next);
1072    }
1073 
1074    //Try to combine with upper block
1075    char *block_char_ptr = reinterpret_cast<char*>(ipcdetail::to_raw_pointer(block));
1076 
1077    if ((block_char_ptr + Alignment*block->m_size) ==
1078          reinterpret_cast<char*>(ipcdetail::to_raw_pointer(pos))){
1079       block->m_size += pos->m_size;
1080       block->m_next  = pos->m_next;
1081    }
1082    else{
1083       block->m_next = pos;
1084    }
1085 
1086    //Try to combine with lower block
1087    if ((reinterpret_cast<char*>(ipcdetail::to_raw_pointer(prev))
1088             + Alignment*prev->m_size) ==
1089         block_char_ptr){
1090 
1091 
1092       prev->m_size += block->m_size;
1093       prev->m_next  = block->m_next;
1094    }
1095    else{
1096       prev->m_next = block;
1097    }
1098 }
1099 
1100 }  //namespace ipcdetail {
1101 
1102 }  //namespace interprocess {
1103 
1104 }  //namespace boost {
1105 
1106 #include <boost/interprocess/detail/config_end.hpp>
1107 
1108 #endif   //#ifndef BOOST_INTERPROCESS_MEM_ALGO_DETAIL_SIMPLE_SEQ_FIT_IMPL_HPP
1109