I am working on a free version of Simple Segregated Storage memory pool in C ++.
The SSS memory pool is similar to the slab allocator: it is basically just a piece of memory that is divided into blocks of equal sizes, and we have a free list pointer pointing to the first available block. Highlighting simply moves the pointer to the next block, and freeing just sets the free list pointer to the freed block and points the βnextβ pointer to the freed block to the old value of the freelist pointer.
So this is basically a singly linked list.
Now I'm trying to encode an unencrypted version of the Simple Segregated Storage algorithm. Assuming that the SEGREGATION of the initial block of memory (i.e., creating a linked list) is always performed before entering a multi-threaded environment, we only need to worry about the allocation and release of blocks - in this case this problem will be very similar to blocking - a free, simply connected list, which is a well understood problem.
So, it seems to me that the distribution and release from unauthorized access can be easily done without blocking, using simple comparison and swap commands.
Assuming we have the following free list pointer:
std::atomic<unsigned char*> m_first
, , nextof(), " ". , nextof :
unsigned char*& nextof(void* ptr)
{
return *static_cast<unsigned char**>(ptr);
}
, Boost Simple Segregated Storage.
, , allocate :
void* malloc()
{
unsigned char* first = m_first.load();
if (!first) return nullptr;
while (!m_first.compare_exchange_strong(first, nextof(first)))
{
if (!first) break;
}
return first;
}
- . , , m_first , , m_first. , , m_first.store(nextof(m_first)) - , , , , - m_first, m_first, .
, , Compare and Swap. m_first . , null, m_first CAS, , . , , m_first, , - .
- m_first , , , m_first.
:
void free(void* chunk)
{
unsigned char* first = m_first.load();
nextof(chunk) = first;
while (!m_first.compare_exchange_strong(first, static_cast<unsigned char*>(chunk)))
{
nextof(chunk) = first;
}
}
, , , . -, m_first . --free'd m_first. , m_first - , CAS, , m_first , . , .
, , CAS malloc free.
: , , . - , ?