Ejemplo n.º 1
0
        ~one_size_heap_list()
        {
            LOSH_(info)
                << (boost::format(
                   "%1%::~%1%: size(%2%), max_count(%3%), alloc_count(%4%), "
                   "free_count(%5%)")
                   % name()
                   % heap_count_
                   % max_alloc_count_
                   % alloc_count_
                   % free_count_);

            if (alloc_count_ > free_count_)
            {
                LOSH_(warning)
                    << (boost::format(
                       "%1%::~%1%: releasing with %2% allocated objects")
                       % name()
                       % (alloc_count_ - free_count_));
            }
        }
Ejemplo n.º 2
0
    one_size_heap_list::~one_size_heap_list() noexcept
    {
#if defined(HPX_DEBUG)
        LOSH_(info) << hpx::util::format(
            "{1}::~{1}: size({2}), max_count({3}), alloc_count({4}), "
            "free_count({5})",
            name(),
            heap_count_,
            max_alloc_count_,
            alloc_count_,
            free_count_);

        if (alloc_count_ > free_count_)
        {
            LOSH_(warning) << hpx::util::format(
                "{1}::~{1}: releasing with {2} allocated objects",
                name(),
                alloc_count_ - free_count_);
        }
#endif
    }
Ejemplo n.º 3
0
        // operations
        void* alloc(std::size_t count = 1)
        {
            unique_lock_type guard(mtx_);

            if (HPX_UNLIKELY(0 == count))
            {
                HPX_THROW_EXCEPTION(bad_parameter,
                    name() + "::alloc",
                    "cannot allocate 0 objects");
            }

            //std::size_t size = 0;
            value_type* p = NULL;
            {
                if (!heap_list_.empty())
                {
                    //size = heap_list_.size();
                    for (iterator it = heap_list_.begin(); it != heap_list_.end(); ++it)
                    {
                        typename list_type::value_type heap = *it;
                        bool allocated = false;

                        {
                            util::scoped_unlock<unique_lock_type> ul(guard);
                            allocated = heap->alloc(&p, count);
                        }

                        if (allocated)
                        {
#if defined(HPX_DEBUG)
                            // Allocation succeeded, update statistics.
                            alloc_count_ += count;
                            if (alloc_count_ - free_count_ > max_alloc_count_)
                                max_alloc_count_ = alloc_count_- free_count_;
#endif
                            return p;
                        }

#if defined(HPX_DEBUG)
                        LOSH_(info)
                            << (boost::format(
                                "%1%::alloc: failed to allocate from heap[%2%] "
                                "(heap[%2%] has allocated %3% objects and has "
                                "space for %4% more objects)")
                                % name()
                                % (*it)->heap_count_
                                % (*it)->size()
                                % (*it)->free_size());
#endif
                    }
                }
            }

            // Create new heap.
            bool did_create = false;
            {
#if defined(HPX_DEBUG)
                heap_list_.push_front(typename list_type::value_type(
                    new heap_type(class_name_.c_str(), heap_count_ + 1, heap_step)));
#else
                heap_list_.push_front(typename list_type::value_type(
                    new heap_type(class_name_.c_str(), 0, heap_step)));
#endif

                iterator itnew = heap_list_.begin();
                typename list_type::value_type heap = *itnew;
                bool result = false;

                {
                    util::scoped_unlock<unique_lock_type> ul(guard);
                    result = heap->alloc(&p, count);
                }

                if (HPX_UNLIKELY(!result || NULL == p))
                {
                    // out of memory
                    HPX_THROW_EXCEPTION(out_of_memory,
                        name() + "::alloc",
                        boost::str(boost::format(
                            "new heap failed to allocate %1% objects")
                            % count));
                }

#if defined(HPX_DEBUG)
                alloc_count_ += count;
                ++heap_count_;

                LOSH_(info)
                    << (boost::format(
                        "%1%::alloc: creating new heap[%2%], size is now %3%")
                        % name()
                        % heap_count_
                        % heap_list_.size());
#endif
                did_create = true;
            }

            if (did_create)
                return p;

            guard.unlock();

            // Try again, we just got a new heap, so we should be good.
            return alloc(count);
        }
Ejemplo n.º 4
0
    void* one_size_heap_list::alloc(std::size_t count)
    {
        unique_lock_type guard(mtx_);

        if (HPX_UNLIKELY(0 == count))
        {
            guard.unlock();
            HPX_THROW_EXCEPTION(bad_parameter,
                name() + "::alloc",
                "cannot allocate 0 objects");
        }

        //std::size_t size = 0;
        void* p = nullptr;
        {
            if (!heap_list_.empty())
            {
                //size = heap_list_.size();
                for (auto & heap : heap_list_)
                {
                    bool allocated = false;

                    {
                        util::unlock_guard<unique_lock_type> ul(guard);
                        allocated = heap->alloc(&p, count);
                    }

                    if (allocated)
                    {
#if defined(HPX_DEBUG)
                        // Allocation succeeded, update statistics.
                        alloc_count_ += count;
                        if (alloc_count_ - free_count_ > max_alloc_count_)
                            max_alloc_count_ = alloc_count_- free_count_;
#endif
                        return p;
                    }

#if defined(HPX_DEBUG)
                    LOSH_(info) << hpx::util::format(
                        "{1}::alloc: failed to allocate from heap[{2}] "
                        "(heap[{2}] has allocated {3} objects and has "
                        "space for {4} more objects)",
                        name(),
                        heap->heap_count(),
                        heap->size(),
                        heap->free_size());
#endif
                }
            }
        }

        // Create new heap.
        bool did_create = false;
        {
#if defined(HPX_DEBUG)
            heap_list_.push_front(create_heap_(
                class_name_.c_str(), heap_count_ + 1, parameters_));
#else
            heap_list_.push_front(create_heap_(
                class_name_.c_str(), 0, parameters_));
#endif

            iterator itnew = heap_list_.begin();
            typename list_type::value_type heap = *itnew;
            bool result = false;

            {
                util::unlock_guard<unique_lock_type> ul(guard);
                result = heap->alloc((void**)&p, count);
            }

            if (HPX_UNLIKELY(!result || nullptr == p))
            {
                // out of memory
                guard.unlock();
                HPX_THROW_EXCEPTION(out_of_memory,
                    name() + "::alloc",
                    hpx::util::format(
                        "new heap failed to allocate {1} objects",
                        count));
            }

#if defined(HPX_DEBUG)
            alloc_count_ += count;
            ++heap_count_;

            LOSH_(info) << hpx::util::format(
                "{1}::alloc: creating new heap[{2}], size is now {3}",
                name(),
                heap_count_,
                heap_list_.size());
#endif
            did_create = true;
        }

        if (did_create)
            return p;

        guard.unlock();

        // Try again, we just got a new heap, so we should be good.
        return alloc(count);
    }
Ejemplo n.º 5
0
        // operations
        value_type* alloc(std::size_t count = 1)
        {
            if (HPX_UNLIKELY(0 == count))
            {
                HPX_THROW_EXCEPTION(bad_parameter,
                    name() + "::alloc",
                    "cannot allocate 0 objects");
            }

            std::size_t size = 0;
            value_type* p = NULL;
            {
                shared_lock_type guard(mtx_);

                size = heap_list_.size();
                if (size)
                {
                    for (iterator it = heap_list_.begin(); it != heap_list_.end(); ++it)
                    {
                        if ((*it)->alloc(&p, count))
                        {
                            // Allocation succeeded, update statistics.
                            alloc_count_ += count;

                            if (alloc_count_ - free_count_ > max_alloc_count_)
                                max_alloc_count_ = alloc_count_- free_count_;

                            return p;
                        }

                        LOSH_(info)
                            << (boost::format(
                                "%1%::alloc: failed to allocate from heap[%2%] "
                                "(heap[%2%] has allocated %3% objects and has "
                                "space for %4% more objects)")
                                % name()
                                % (*it)->heap_count_
                                % (*it)->size()
                                % (*it)->free_size());
                    }
                }
            }

            // Create new heap.
            bool did_create = false;
            {
                // Acquire exclusive access.
                unique_lock_type ul(mtx_);

                iterator itnew = heap_list_.insert(heap_list_.begin(),
                    typename list_type::value_type(new heap_type
                        (class_name_.c_str(), heap_count_ + 1, heap_step)));

                if (HPX_UNLIKELY(itnew == heap_list_.end()))
                {
                    HPX_THROW_EXCEPTION(out_of_memory,
                        name() + "::alloc",
                        "new heap could not be added");
                }

                bool result = (*itnew)->alloc(&p, count);

                if (HPX_UNLIKELY(!result || NULL == p))
                {
                    // out of memory
                    HPX_THROW_EXCEPTION(out_of_memory,
                        name() + "::alloc",
                        boost::str(boost::format(
                            "new heap failed to allocate %1% objects")
                            % count));
                }

                alloc_count_ += count;
                ++heap_count_;

                LOSH_(info)
                    << (boost::format(
                        "%1%::alloc: creating new heap[%2%], size is now %3%")
                        % name()
                        % heap_count_
                        % heap_list_.size());
                did_create = true;
            }

            if (did_create)
                return p;

            // Try again, we just got a new heap, so we should be good.
            return alloc(count);
        }