Click here to Skip to main content
15,923,120 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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);
}


What I have tried:

I am working on an exercise (unrelated to school) that requires you to make your own heap allocation for dynamic memory management. My main concern is to know if the way I wrote the code seems plausible or not. Here is the description:

"Create a LargeBucket class that can store up to 1MB of data. Extend the
Heap class so it gives out a LargeBucket for allocations greater than 4096 bytes.
Make sure that you still throw std::bad_alloc whenever the Heap is unable to
allocate an appropriately sized bucket."
Posted
Updated 30-Nov-20 8:12am
v2
Comments
Rick York 29-Nov-20 23:59pm    
The first part is missing.
Rick York 30-Nov-20 13:23pm    
It would be best to edit the original post.
Richard MacCutchan 30-Nov-20 4:05am    
"My main concern is to know if the way I wrote the code seems plausible or not."
The only way to answer that question is by testing the code in lots of different scenarios.

1 solution

How far do you want to take this? I ask because if you want to test a heap management replacement I recommend you see the tip I posted here : Memory Allocation Tracking for C++ Code[^]. If you use those macros in a header file and implement your own version of what Microsoft did in CRTdbg.h you can have a full heap management replacement library. Then add this macro :
C++
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <CRTdbg.h>
#endif
with your header file instead and you are ready to go. You will have a drop-in replacement for new, delete, alloc, calloc, and free so you can investigate exactly what goes on in a memory manager. It only requires that you implement two functions (_malloc_dbg and _free_dbg in their implementation) and rewrite the macros similar to what CRTdbg.h has. You will need a copy of Visual Studio to see that file but it is available for free.

If you don't have that file you can still do what is necessary because in the tip I posted you can see how those functions are called and you can find the prototypes for the other functions so you just have to write translation macros to connect them all together.

I tend to recommend this path because you can then use just standard memory allocation code to test your stuff. You don't have to write anything 'special' - you just use standard stuff like new, delete, calloc, and free. If you have more questions about this then a separate question would probably be best.
 
Share this answer
 
Comments
PhaazeReborn 30-Nov-20 14:17pm    
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!
Rick York 30-Nov-20 15:37pm    
In my opinion your code looks OK. I can't state it will work but I don't see any glaring flaws.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900