pbm.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <assert.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include "logger.h"
  7. #include "xalloc.h"
  8. #define NUM_VALUES 0xFFFFFFFF
  9. #define PAGE_SIZE_IN_BITS 0x10000
  10. #define PAGE_SIZE_IN_BYTES (PAGE_SIZE_IN_BITS/8)
  11. #define NUM_PAGES 0x10000
  12. #define PAGE_MASK 0xFFFF
  13. uint8_t** pbm_init(void)
  14. {
  15. uint8_t** retv = xcalloc(NUM_PAGES, sizeof(void*));
  16. return retv;
  17. }
  18. static inline int bm_check(uint8_t *bm, uint16_t v)
  19. {
  20. uint16_t page_idx = (v >> 3);
  21. uint8_t bit_idx = (uint8_t) (v & 0x07);
  22. return bm[page_idx] & (1 << bit_idx);
  23. }
  24. static inline void bm_set(uint8_t *bm, uint16_t v)
  25. {
  26. uint16_t page_idx = (v >> 3);
  27. uint8_t bit_idx = (uint8_t) (v & 0x07);
  28. bm[page_idx] |= (1 << bit_idx);
  29. }
  30. int pbm_check(uint8_t **b, uint32_t v)
  31. {
  32. uint32_t top = v >> 16;
  33. uint32_t bottom = v & PAGE_MASK;
  34. return b[top] && bm_check(b[top], bottom);
  35. }
  36. void pbm_set(uint8_t **b, uint32_t v)
  37. {
  38. uint16_t top = (uint16_t) (v >> 16);
  39. uint16_t bottom = (uint16_t) (v & PAGE_MASK);
  40. if (!b[top]) {
  41. uint8_t *bm = xmalloc(PAGE_SIZE_IN_BYTES);
  42. memset(bm, 0, PAGE_SIZE_IN_BYTES);
  43. b[top] = bm;
  44. }
  45. bm_set(b[top], bottom);
  46. }