ssldump/common/lib/r_bitfield.c

97 lines
1.6 KiB
C
Raw Normal View History

2015-01-31 09:13:33 +00:00
/**
r_bitfield.c
Copyright (C) 2001 RTFM, Inc.
All Rights Reserved.
ekr@rtfm.com Wed Oct 3 11:15:23 2001
*/
#include <r_common.h>
#include "r_bitfield.h"
2023-08-14 10:37:08 +00:00
int r_bitfield_create(r_bitfield **setp, UINT4 size) {
r_bitfield *set = 0;
int _status;
int num_words = size / 32 + !!(size % 32);
2015-01-31 09:13:33 +00:00
2023-08-14 10:37:08 +00:00
if(!(set = (r_bitfield *)RMALLOC(sizeof(r_bitfield))))
ABORT(R_NO_MEMORY);
2015-01-31 09:13:33 +00:00
2023-08-14 10:37:08 +00:00
if(!(set->data = (UINT4 *)RMALLOC(num_words * 4)))
ABORT(R_NO_MEMORY);
memset(set->data, 0, 4 * num_words);
2015-01-31 09:13:33 +00:00
2023-08-14 10:37:08 +00:00
set->base = 0;
set->len = num_words;
2015-01-31 09:13:33 +00:00
2023-08-14 10:37:08 +00:00
*setp = set;
2015-01-31 09:13:33 +00:00
2023-08-14 10:37:08 +00:00
_status = 0;
abort:
if(_status) {
r_bitfield_destroy(&set);
2015-01-31 09:13:33 +00:00
}
return _status;
2023-08-14 10:37:08 +00:00
}
2015-01-31 09:13:33 +00:00
2023-08-14 10:37:08 +00:00
int r_bitfield_destroy(r_bitfield **setp) {
r_bitfield *set;
if(!setp || !*setp)
return 0;
2023-08-14 10:37:08 +00:00
set = *setp;
RFREE(set->data);
RFREE(set);
*setp = 0;
return 0;
2023-08-14 10:37:08 +00:00
}
2015-01-31 09:13:33 +00:00
2023-08-14 10:37:08 +00:00
int r_bitfield_set(r_bitfield *set, int bit) {
int word = (bit - set->base) / 32;
int bbit = (bit - set->base) % 32;
int _status;
2015-01-31 09:13:33 +00:00
2023-08-14 10:37:08 +00:00
/* Resize? */
if(word > set->len) {
UINT4 newlen = set->len;
UINT4 *tmp;
2015-01-31 09:13:33 +00:00
2023-08-14 10:37:08 +00:00
while(newlen < word)
newlen *= 2;
2015-01-31 09:13:33 +00:00
2023-08-14 10:37:08 +00:00
if(!(tmp = (UINT4 *)RMALLOC(newlen)))
ABORT(R_NO_MEMORY);
memcpy(tmp, set->data, set->len * 4);
memset(tmp + set->len * 4, 0, (newlen - set->len) * 4);
RFREE(set->data);
set->data = tmp;
2015-01-31 09:13:33 +00:00
}
2023-08-14 10:37:08 +00:00
set->data[word] |= 1 << bbit;
_status = 0;
abort:
return _status;
2023-08-14 10:37:08 +00:00
}
int r_bitfield_isset(r_bitfield *set, int bit) {
int word = (bit - set->base) / 32;
int bbit = (bit - set->base) % 32;
int _status;
if(bit < set->base)
return 0;
2023-08-14 10:37:08 +00:00
/* Resize? */
if(word > set->len)
return 0;
2023-08-14 10:37:08 +00:00
return set->data[word] & (1 << bbit);
2023-08-14 10:37:08 +00:00
}