You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

10 years ago
10 years ago
10 years ago
  1. /* -*- coding: utf-8 -*-
  2. * ----------------------------------------------------------------------
  3. * Copyright © 2013-2014, RedJack, LLC.
  4. * All rights reserved.
  5. *
  6. * Please see the COPYING file in this distribution for license details.
  7. * ----------------------------------------------------------------------
  8. */
  9. #include <string.h>
  10. #include "libcork/core/allocator.h"
  11. #include "libcork/core/api.h"
  12. #include "libcork/core/types.h"
  13. #include "libcork/ds/bitset.h"
  14. static size_t
  15. bytes_needed(size_t bit_count)
  16. {
  17. /* We need one byte for every bit... */
  18. size_t bytes_needed = bit_count / 8;
  19. /* Plus one extra for the leftovers that don't fit into a whole byte. */
  20. bytes_needed += ((bit_count % 8) > 0);
  21. return bytes_needed;
  22. }
  23. struct cork_bitset *
  24. cork_bitset_new(size_t bit_count)
  25. {
  26. struct cork_bitset *set = cork_new(struct cork_bitset);
  27. set->bit_count = bit_count;
  28. set->byte_count = bytes_needed(bit_count);
  29. set->bits = cork_malloc(set->byte_count);
  30. memset(set->bits, 0, set->byte_count);
  31. return set;
  32. }
  33. void
  34. cork_bitset_free(struct cork_bitset *set)
  35. {
  36. cork_free(set->bits, set->byte_count);
  37. cork_delete(struct cork_bitset, set);
  38. }
  39. void
  40. cork_bitset_clear(struct cork_bitset *set)
  41. {
  42. memset(set->bits, 0, set->byte_count);
  43. }