Пример #1
0
void *MemoryPool::pop(size_t s, int loc) {
    void *addr = nullptr;

    if ((s > MIN_BLOCK_SIZE) && (s < MAX_BLOCK_SIZE)) {
        locker_.lock();

        // find MemoryPool block which is not smaller than demand size
        auto pt = pool_.lower_bound(s);

        if (pt != pool_.end()) {
            size_t ts = 0;
            std::tie(ts, addr) = *pt;
            if (ts < s * 2) {
                s = ts;
                pool_.erase(pt);
                pool_depth_ -= s;
            } else {
                addr = nullptr;
            }
        }
        locker_.unlock();
    }

    if (addr == nullptr) {
        try {
#ifdef __CUDA__
            SP_DEVICE_CALL(cudaMallocManaged(&addr, s));
#else
            addr = malloc(s);
#endif
        } catch (std::bad_alloc const &error) { THROW_EXCEPTION_BAD_ALLOC(s); }
    }
    return addr;
}
Пример #2
0
void *MemoryPool::pimpl_s::pop(size_t s)
{
    void *addr = nullptr;

    if ((s > MIN_BLOCK_SIZE) && (s < MAX_BLOCK_SIZE))
    {
        locker_.lock();

        // find memory block which is not smaller than demand size
        auto pt = pool_.lower_bound(s);

        if (pt != pool_.end())
        {
            size_t ts = 0;

            std::tie(ts, addr) = *pt;

            if (ts < s * 2)
            {
                s = ts;

                pool_.erase(pt);

                pool_depth_ -= s;
            }
            else
            {
                addr = nullptr;
            }
        }

        locker_.unlock();

    }

    if (addr == nullptr)
    {

        try
        {
            addr = reinterpret_cast<void *>(new byte_type[s]);

        } catch (std::bad_alloc const &error)
        {
            THROW_EXCEPTION_BAD_ALLOC(s);

        }

    }
    return addr;

}