Skip to content

Commit

Permalink
remove last_item member and replace with get_last_item()
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsippel committed Dec 14, 2023
1 parent a25a596 commit 8608134
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions redGrapes/util/chunked_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ struct ChunkedList
iter_offset_t off = iter_offset.load();
refcount_t old_refcount = refcount.load();

if( iter_offset == 0 && old_refcount >= 0 )
if( off == 0 && old_refcount >= 0 )
{
old_refcount = refcount.fetch_add(1);
off = iter_offset.load();
Expand Down Expand Up @@ -236,11 +236,6 @@ struct ChunkedList
*/
std::atomic< Item * > first_item;

/* points to the latest item which was inserted
* and is already fully initialized
*/
std::atomic< Item * > last_item;

/* points to the next free storage slot,
* if available. Will be used to add a new element
*/
Expand All @@ -250,7 +245,6 @@ struct ChunkedList

Chunk( memory::Block blk )
: first_item( (Item*) blk.ptr )
, last_item( ((Item*)blk.ptr) - 1 )
, next_item( (Item*) blk.ptr )
{
for(Item * item = this->first_item; item < ( this->first_item + T_chunk_size ); item++ )
Expand All @@ -267,6 +261,19 @@ struct ChunkedList
{
return first_item;
}

/* returns the latest item which was inserted
*/
Item * get_last_item()
{
Item * limit = first_item + T_chunk_size;
Item * last_item = next_item - 1;

if( last_item >= limit )
last_item = limit - 1;

return last_item;
}
};

template < bool is_const >
Expand Down Expand Up @@ -300,7 +307,7 @@ struct ChunkedList
{
return ((bool)chunk)
&& ( get_item_ptr() >= chunk->first_item )
&& ( get_item_ptr() <= chunk->last_item );
&& ( get_item_ptr() < chunk->first_item+T_chunk_size );
}

/*!
Expand Down Expand Up @@ -365,7 +372,7 @@ struct ChunkedList
{
++chunk;
if( chunk )
cur_item = (uintptr_t) chunk->last_item.load();
cur_item = (uintptr_t) chunk->get_last_item();
else
cur_item = 0;
}
Expand Down Expand Up @@ -472,7 +479,7 @@ struct ChunkedList
{
++ this->chunk;
if( this->chunk )
this->cur_item = (uintptr_t) this->chunk->last_item.load();
this->cur_item = (uintptr_t) this->chunk->get_last_item();
else
this->cur_item = 0;
}
Expand Down Expand Up @@ -531,15 +538,7 @@ struct ChunkedList
{
/* successfully allocated a slot in the current chunk
*/

// initialize item value
*next_item = item;

/* allow iteration to start at the newly initialized item.
* in case it happens that
*/
chunk->last_item ++;

return MutBackwardIterator( chunk, next_item );
}
else if ( (uintptr_t)next_item == (uintptr_t)chunk_end )
Expand Down Expand Up @@ -615,9 +614,7 @@ struct ChunkedList
auto c = chunks.rbegin();
return MutBackwardIterator(
c,
( c != chunks.rend() ) ? c->last_item.load() : nullptr
// TODO: change to this when `last_item` is removed
// ( c != chunks.rend() ) ? min(c->last_item.load()-1, c->first_item+T_chunk_size) : nullptr
( c != chunks.rend() ) ? c->get_last_item() : nullptr
);
}

Expand All @@ -634,7 +631,7 @@ struct ChunkedList
auto c = chunks.rbegin();
return ConstBackwardIterator(
c,
( c != chunks.rend() ) ? c->last_item.load() : nullptr
( c != chunks.rend() ) ? c->get_last_item() : nullptr
);
}

Expand Down

0 comments on commit 8608134

Please sign in to comment.