patterncppMinor
Block allocator
Viewed 0 times
blockallocatorstackoverflow
Problem
I wrote this block allocator for exercise and would love to get your reviews.
Main features I wanted to have:
```
#include
#include
#include
#include
#include
class BadAddressException: public std::exception
{
public:
const char* what() const noexcept override
{
return "None-owned or incorrect pointer.";
}
};
class BufferOverflowException: public std::exception
{
public:
const char* what() const noexcept override
{
return "Buffer overflow detected.";
}
};
class BlockAllocator
{
public:
BlockAllocator( size_t blockSize, size_t maxBlocks, size_t blockAlignment )
:maxBlocks( maxBlocks )
,newBlockAlignment( SYSTEM_ALIGNMENT > blockAlignment ? SYSTEM_ALIGNMENT : blockAlignment )
,alignedBlockSize( roundUpTo( blockSize, blockAlignment ) )
,maxChunks( divideRoundUp( maxBlocks, 2 ) )
,availableBlocks( maxBlocks )
,chunkIndex( 0 )
,buffer( nullptr )
,controls( nullptr )
{
if ( !isValidAlignmentValue( blockAlignment ) )
{
throw std::bad_alloc{};
}
const size_t blocksPadding = padding( SYSTEM_ALIGNMENT, newBlockAlignment );
const size_t controlsPadding = padding( SYSTEM_ALIGNMENT, alignof( Control ) );
const size_t chunksPadding = padding( SYSTEM_ALIGNMENT, alignof( Chunk ) );
const size_t allocationSize = blocksPadding + alignedBlockSize * maxBlocks +
CANARY_TERMINATOR_SIZE +
controlsPadding + maxBlocks * sizeof( Control ) +
chunksPadding + maxChunks * sizeof( Chunk );
buffer = malloc( allocationSize );
if(
Main features I wanted to have:
- Detecting buffer overflows with canary terminator.
- Being able to allocate arbitrary types with template allocator and allow types that may fit more than one blocks (continues chunks).
```
#include
#include
#include
#include
#include
class BadAddressException: public std::exception
{
public:
const char* what() const noexcept override
{
return "None-owned or incorrect pointer.";
}
};
class BufferOverflowException: public std::exception
{
public:
const char* what() const noexcept override
{
return "Buffer overflow detected.";
}
};
class BlockAllocator
{
public:
BlockAllocator( size_t blockSize, size_t maxBlocks, size_t blockAlignment )
:maxBlocks( maxBlocks )
,newBlockAlignment( SYSTEM_ALIGNMENT > blockAlignment ? SYSTEM_ALIGNMENT : blockAlignment )
,alignedBlockSize( roundUpTo( blockSize, blockAlignment ) )
,maxChunks( divideRoundUp( maxBlocks, 2 ) )
,availableBlocks( maxBlocks )
,chunkIndex( 0 )
,buffer( nullptr )
,controls( nullptr )
{
if ( !isValidAlignmentValue( blockAlignment ) )
{
throw std::bad_alloc{};
}
const size_t blocksPadding = padding( SYSTEM_ALIGNMENT, newBlockAlignment );
const size_t controlsPadding = padding( SYSTEM_ALIGNMENT, alignof( Control ) );
const size_t chunksPadding = padding( SYSTEM_ALIGNMENT, alignof( Chunk ) );
const size_t allocationSize = blocksPadding + alignedBlockSize * maxBlocks +
CANARY_TERMINATOR_SIZE +
controlsPadding + maxBlocks * sizeof( Control ) +
chunksPadding + maxChunks * sizeof( Chunk );
buffer = malloc( allocationSize );
if(
Solution
If I am not mistaken copy constructor and assignment operator are going to be generated for
BlockAllocator class by compiler. You probably don't want the default implementation because of buffer pointer.Context
StackExchange Code Review Q#126272, answer score: 2
Revisions (0)
No revisions yet.