int SetBit(struct BitMap *psMap, int iIndex) { BITCHK_INDEX(psMap->_iSize, iIndex); psMap->_szData[BIT_OFFSET(iIndex)] |= (0x01 << BIT_POS(iIndex)); return 0; }
bool ht_acquire_slot(struct t_hash_table * ht, const uint8_t infohash[20], const uint32_t slot) { /* may miss to lookup due to multithread issue, which would not affect functionality */ if (memcmp(ht->slot[slot].infohash, infohash, 20) == 0) { return true; } while (1) { uint8_t oldval = ht->used[BYTE_POS(slot)]; uint8_t newval = oldval | (1 << BIT_POS(slot)); if (oldval & (1 << BIT_POS(slot))) { /* failed to acquire, already occupied */ return false; } /* try set used bit to 1 with CAS */ if (__sync_bool_compare_and_swap(&ht->used[BYTE_POS(slot)], oldval, newval)) { memcpy(&ht->slot[slot].infohash, infohash, 20); return true; } } }
int main(int argc, const char * argv[]) { int i, j = 15, k = 1; i = (j++, k++); #define SET_FLAG(N) (N) |= 1 #define BIT_POS(N) ( 1U << (N) ) // insert code here... printf("k = %d\n", SET_FLAG(i)); printf("bit_ops = 0%X\n", BIT_POS(j)); printbinary(j); return 0; }
int GetBit(struct BitMap *psMap, int iIndex) { BITCHK_INDEX(psMap->_iSize, iIndex); return (psMap->_szData[BIT_OFFSET(iIndex)] & (0x01 << BIT_POS(iIndex))) != 0; }
void ps_slot_update(struct t_peer_storage * ps, uint32_t slot, uint8_t val) { uint8_t mask = 0xFF - (0x0F << BIT_POS(slot)); mask |= (val << BIT_POS(slot)); __sync_fetch_and_and((uint8_t *)&ps->cycle_set[BYTE_POS(slot)], mask); }
uint8_t ps_slot_read(struct t_peer_storage * ps, uint32_t slot) { uint8_t mask = 0x0F << BIT_POS(slot); uint8_t val = ps->cycle_set[BYTE_POS(slot)] & mask; val >>= BIT_POS(slot); return val; }
uint8_t ps_slot_acquire(struct t_peer_storage * ps, uint32_t slot) { uint8_t mask = 0x0F << BIT_POS(slot); uint8_t val = __sync_fetch_and_or((uint8_t *)&ps->cycle_set[BYTE_POS(slot)], mask) & mask; val >>= BIT_POS(slot); return val; }