Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memory leak #45

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/1_resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <redGrapes/redGrapes.hpp>
#include <redGrapes/resource/ioresource.hpp>
#include <redGrapes/resource/fieldresource.hpp>
#include <redGrapes/resource/resource_user.hpp>

int main(int, char*[])
{
redGrapes::init(1);
redGrapes::FieldResource< std::vector<int> > a;
redGrapes::IOResource<int> b;
redGrapes::IOResource<int> c;
Expand All @@ -35,6 +37,7 @@ int main(int, char*[])
std::cout << "is_serial(user1,user3) = " << redGrapes::ResourceUser::is_serial(user1,user3) << std::endl;
std::cout << "is_serial(user2,user3) = " << redGrapes::ResourceUser::is_serial(user2,user3) << std::endl;

redGrapes::finalize();
return 0;
}

53 changes: 29 additions & 24 deletions redGrapes/util/chunked_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ template <
>
struct ChunkedList
{
using chunk_offset_t = int16_t;
using chunk_offset_t = uint16_t;
using refcount_t = uint16_t;

struct Item
Expand Down Expand Up @@ -163,14 +163,11 @@ struct ChunkedList

struct Chunk
{
/* counts the number of alive elements in this chunk.
* Whenever `item_count` reaches zero, the chunk will be deleted.
* `item_count` starts with 1 to keep the chunk at least until
* its full capacity is used. This initial offset is
* compensated by not increasing the count when the last
* element is inserted.
/* counts the number of freed elements in this chunk.
* Whenever `freed_items` reaches the last item in this chunk, the chunk
* will be deleted.
*/
std::atomic< chunk_offset_t > item_count{ 0 };
std::atomic< chunk_offset_t > freed_items{ 0 };

/* lowest index with free slot that can
* be used to add a new element
Expand Down Expand Up @@ -425,6 +422,9 @@ struct ChunkedList
size_t items_capacity = (chunks.get_chunk_capacity() - sizeof(Chunk));
this->chunk_size = items_capacity / sizeof(Item);
assert( chunk_size < std::numeric_limits< chunk_offset_t >::max() );

// start with one batch of chunks
chunks.allocate_item();
}

ChunkedList( ChunkedList && other ) = default;
Expand All @@ -440,7 +440,7 @@ struct ChunkedList
*/
void release_chunk( typename memory::AtomicList< Chunk, Allocator >::MutBackwardIterator chunk )
{
if( chunk->item_count.fetch_sub(1) == 0 )
if( chunk->freed_items.fetch_add(1) == chunk_size - 1u )
chunks.erase( chunk );
}

Expand All @@ -453,25 +453,30 @@ struct ChunkedList
auto chunk = chunks.rbegin();
if( chunk != chunks.rend() )
{
if( chunk->item_count.fetch_add(1) < chunk_size )
{
unsigned chunk_off = chunk->next_idx.fetch_add(1);

if( chunk_off < chunk_size )
{

unsigned chunk_off = chunk->next_idx.fetch_add(1);

if( chunk_off < chunk_size )
{
chunk->items()[ chunk_off ] = item;
chunk->last_idx ++;
if( chunk_off == chunk_size - 1)
{
// the thread who took the last item of this chunk must allocate
// the next batch of items
chunks.allocate_item();
}
return MutBackwardIterator( chunk_size, chunk, chunk_off );
}
}

release_chunk(chunk);
}
else
{
// avoid that the counter can overflow
chunk->next_idx--;
}
}
else
{
throw std::runtime_error("chunk_list: invalid state, there should always be at least one chunk available.");
}

auto prev_chunk = chunks.allocate_item();
if( prev_chunk != chunks.rend() )
release_chunk( prev_chunk );
}
}

Expand Down
Loading