Fix incorrect default state and bit math for bitsets
This commit is contained in:
parent
d6d1b2080c
commit
cb8f821489
@ -2,15 +2,22 @@
|
||||
* This file is part of ToaruOS and is released under the terms
|
||||
* of the NCSA / University of Illinois License - see LICENSE.md
|
||||
* Copyright (C) 2015 Dale Weiler
|
||||
* 2015 Kevin Lange
|
||||
*/
|
||||
#include "bitset.h"
|
||||
|
||||
#define CEIL(NUMBER, BASE) \
|
||||
(((NUMBER) + (BASE) - 1) & ~((BASE) - 1))
|
||||
|
||||
#define iom \
|
||||
size_t index = bit >> 3; \
|
||||
bit = bit >> index; \
|
||||
size_t offset = bit & 7; \
|
||||
size_t mask = 1 << offset;
|
||||
|
||||
void bitset_init(bitset_t *set, size_t size) {
|
||||
set->size = CEIL(size, 8);
|
||||
set->data = malloc(set->size);
|
||||
set->data = calloc(set->size, 1);
|
||||
}
|
||||
|
||||
void bitset_free(bitset_t *set) {
|
||||
@ -21,20 +28,17 @@ static void bitset_resize(bitset_t *set, size_t size) {
|
||||
if (set->size >= size) {
|
||||
return;
|
||||
}
|
||||
unsigned char *temp = malloc(size);
|
||||
memcpy(temp, set->data, set->size);
|
||||
free(set->data);
|
||||
set->data = temp;
|
||||
|
||||
set->data = realloc(set->data, size);
|
||||
memset(set->data + set->size, 0, size - set->size);
|
||||
set->size = size;
|
||||
}
|
||||
|
||||
void bitset_set(bitset_t *set, size_t bit) {
|
||||
size_t index = bit >> 3;
|
||||
iom;
|
||||
if (set->size <= index) {
|
||||
bitset_resize(set, set->size << 1);
|
||||
}
|
||||
size_t offset = index & 7;
|
||||
size_t mask = 1 << offset;
|
||||
set->data[index] |= mask;
|
||||
}
|
||||
|
||||
@ -49,16 +53,12 @@ int bitset_ffub(bitset_t *set) {
|
||||
}
|
||||
|
||||
void bitset_clear(bitset_t *set, size_t bit) {
|
||||
size_t index = bit >> 3;
|
||||
size_t offset = index & 7;
|
||||
size_t mask = 1 << offset;
|
||||
iom;
|
||||
set->data[index] &= ~mask;
|
||||
}
|
||||
|
||||
int bitset_test(bitset_t *set, size_t bit) {
|
||||
size_t index = bit >> 3;
|
||||
size_t offset = index & 7;
|
||||
size_t mask = 1 << offset;
|
||||
return mask & set->data[index];
|
||||
iom;
|
||||
return !!(mask & set->data[index]);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user