Skip to content

Commit 8196ead

Browse files
committed
Replace CAtom's bitfield with packed struct
1 parent 6b689f0 commit 8196ead

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

atom/src/catom.h

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,27 @@
1313

1414

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

2418

2519
namespace atom
2620
{
2721

28-
22+
PACK(struct CAtomInfo {
23+
uint16_t slot_count;
24+
bool notifications_enabled: 1;
25+
bool has_guards: 1;
26+
bool has_atomref: 1;
27+
bool is_frozen: 1;
28+
uint16_t reserved: 12;
29+
});
2930

3031
struct CAtom
3132
{
3233
PyObject_HEAD
33-
uint32_t bitfield; // lower 16 == slot count, upper 16 == flags
3434
PyObject** slots;
3535
ObserverPool* observers;
36+
CAtomInfo info;
3637

3738
static PyType_Spec TypeObject_Spec;
3839

@@ -42,12 +43,12 @@ struct CAtom
4243

4344
uint32_t get_slot_count()
4445
{
45-
return bitfield & SLOT_COUNT_MASK;
46+
return info.slot_count;
4647
}
4748

48-
void set_slot_count( uint32_t count )
49+
void set_slot_count( uint16_t count )
4950
{
50-
bitfield = ( bitfield & FLAGS_MASK ) | ( count & SLOT_COUNT_MASK );
51+
info.slot_count = count;
5152
}
5253

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

6667
bool get_notifications_enabled()
6768
{
68-
return ( bitfield & NOTIFICATION_BIT ) != 0;
69+
return info.notifications_enabled;
6970
}
7071

7172
void set_notifications_enabled( bool enabled )
7273
{
73-
if( enabled )
74-
bitfield |= NOTIFICATION_BIT;
75-
else
76-
bitfield &= ~NOTIFICATION_BIT;
74+
info.notifications_enabled = enabled;
7775
}
7876

7977
bool has_guards()
8078
{
81-
return ( bitfield & GUARD_BIT ) != 0;
79+
return info.has_guards;
8280
}
8381

8482
void set_has_guards( bool has_guards )
8583
{
86-
if( has_guards )
87-
bitfield |= GUARD_BIT;
88-
else
89-
bitfield &= ~GUARD_BIT;
84+
info.has_guards = has_guards;
9085
}
9186

9287
bool has_atomref()
9388
{
94-
return ( bitfield & ATOMREF_BIT ) != 0;
89+
return info.has_atomref;
9590
}
9691

9792
void set_has_atomref( bool has_ref )
9893
{
99-
if( has_ref )
100-
bitfield |= ATOMREF_BIT;
101-
else
102-
bitfield &= ~ATOMREF_BIT;
94+
info.has_atomref = has_ref;
10395
}
10496

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

126118
bool is_frozen()
127119
{
128-
return ( bitfield & FROZEN_BIT ) != 0;
120+
return info.is_frozen;
129121
}
130122

131123
void set_frozen( bool frozen )
132124
{
133-
if( frozen )
134-
bitfield |= FROZEN_BIT;
135-
else
136-
bitfield &= ~FROZEN_BIT;
125+
info.is_frozen = frozen;
137126
}
138127

139128
bool observe( PyObject* topic, PyObject* callback )

0 commit comments

Comments
 (0)