Click here to Skip to main content
15,906,285 members

Comments by PhaazeReborn (Top 4 by date)

PhaazeReborn 24-Mar-24 19:49pm View    
You're right. I did not look that far down. In that case, thanks!
PhaazeReborn 24-Mar-24 18:42pm View    
Yes, locking both processes to the same CPU core. Also, wouldn't it work if the first argument is 0 as well? This is what the man page reads about it:

"sched_setaffinity() sets the CPU affinity mask of the thread whose ID is pid to the value specified by mask. If pid is zero, then the calling thread is used. The argument cpusetsize is the length (in bytes) of the data pointed to by mask. Normally this argument would be specified as sizeof(cpu_set_t)."
PhaazeReborn 30-Nov-20 14:17pm View    
Although I am interested in reading more about your post and check out the header file, I am going base on what the author asks to add to the already existing code for greater allocation. My main concern is knowing if my logic makes sense, or perhaps any improvement that can be made. I thank you again for your alternative!
PhaazeReborn 30-Nov-20 9:59am View    
Deleted
constexpr std::size_t small_size{ 4036 };
constexpr std::size_t large_size{ 1'000'000 };

struct SmallBucket
{
    const static std::size_t data_size{ small_size };
    std::byte data[data_size];
};

struct LargeBucket
{
    using byte = std::byte;

    const static std::size_t data_size{ large_size };
    byte data[data_size];
};

struct Heap
{
    void* allocate(std::size_t bytes)
    {
        if (bytes >= 1 && bytes <= SmallBucket::data_size) {

            for (int i{}; i < n_heap_buckets; ++i) {

                if (!buckets_used[i])
                {
                    buckets_used[i] = true;
                    return large_buckets[i].data;
                }
            }
        }
        else if (bytes > SmallBucket::data_size && bytes <= LargeBucket::data_size) {

            for (int i{}; i < n_heap_buckets; ++i) {

                if (!buckets_used[i])
                {
                    buckets_used[i] = true;
                    return small_buckets[i].data;
                }
            }
        }

        throw std::bad_alloc{};
    }

    void free(void* p)
    {
        for (std::size_t i{}; i < n_heap_buckets; ++i)
        {
            if (large_buckets[i].data == p)
            {
                buckets_used[i] = false;
                return;
            }
        }
    }

    const static std::size_t  n_heap_buckets{ 10 };
    SmallBucket small_buckets[n_heap_buckets];
    LargeBucket large_buckets[n_heap_buckets];
    bool buckets_used[n_heap_buckets];
};

Heap heap;

void* operator new(std::size_t n_bytes)
{
    return heap.allocate(n_bytes);
}

void operator delete(void* p)
{
    heap.free(p);
}