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

Replace CAtom's bitfield with packed struct #243

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
51 changes: 20 additions & 31 deletions atom/src/catom.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@


#define MAX_MEMBER_COUNT ( static_cast<uint32_t>( 0xffff ) )
#define SLOT_COUNT_MASK ( static_cast<uint32_t>( 0xffff ) )
#define FLAGS_MASK ( static_cast<uint32_t>( 0xffff0000 ) )
#define NOTIFICATION_BIT ( static_cast<uint32_t>( 1 << 16 ) )
#define GUARD_BIT ( static_cast<uint32_t>( 1 << 17 ) )
#define ATOMREF_BIT ( static_cast<uint32_t>( 1 << 18 ) )
#define FROZEN_BIT ( static_cast<uint32_t>( 1 << 19 ) )
#define catom_cast( o ) ( reinterpret_cast<atom::CAtom*>( o ) )


namespace atom
{


PACK(struct CAtomInfo {
uint16_t slot_count;
bool notifications_enabled: 1;
bool has_guards: 1;
bool has_atomref: 1;
bool is_frozen: 1;
uint16_t reserved: 12;
});

struct CAtom
{
PyObject_HEAD
uint32_t bitfield; // lower 16 == slot count, upper 16 == flags
PyObject** slots;
ObserverPool* observers;
CAtomInfo info;

static PyType_Spec TypeObject_Spec;

Expand All @@ -42,12 +43,12 @@ struct CAtom

uint32_t get_slot_count()
{
return bitfield & SLOT_COUNT_MASK;
return info.slot_count;
}

void set_slot_count( uint32_t count )
void set_slot_count( uint16_t count )
{
bitfield = ( bitfield & FLAGS_MASK ) | ( count & SLOT_COUNT_MASK );
info.slot_count = count;
}

PyObject* get_slot( uint32_t index )
Expand All @@ -65,41 +66,32 @@ struct CAtom

bool get_notifications_enabled()
{
return ( bitfield & NOTIFICATION_BIT ) != 0;
return info.notifications_enabled;
}

void set_notifications_enabled( bool enabled )
{
if( enabled )
bitfield |= NOTIFICATION_BIT;
else
bitfield &= ~NOTIFICATION_BIT;
info.notifications_enabled = enabled;
}

bool has_guards()
{
return ( bitfield & GUARD_BIT ) != 0;
return info.has_guards;
}

void set_has_guards( bool has_guards )
{
if( has_guards )
bitfield |= GUARD_BIT;
else
bitfield &= ~GUARD_BIT;
info.has_guards = has_guards;
}

bool has_atomref()
{
return ( bitfield & ATOMREF_BIT ) != 0;
return info.has_atomref;
}

void set_has_atomref( bool has_ref )
{
if( has_ref )
bitfield |= ATOMREF_BIT;
else
bitfield &= ~ATOMREF_BIT;
info.has_atomref = has_ref;
}

bool has_observers( PyObject* topic )
Expand All @@ -125,15 +117,12 @@ struct CAtom

bool is_frozen()
{
return ( bitfield & FROZEN_BIT ) != 0;
return info.is_frozen;
}

void set_frozen( bool frozen )
{
if( frozen )
bitfield |= FROZEN_BIT;
else
bitfield &= ~FROZEN_BIT;
info.is_frozen = frozen;
}

bool observe( PyObject* topic, PyObject* callback )
Expand Down