#ifndef BITMAP_BITMAP_H_
#define BITMAP_BITMAP_H_

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

#include "hal/hal.h"

typedef struct {
  uint8_t *map;
  size_t nblocks;
  size_t nbytes;
  size_t alloc_blocks;
  size_t last_deep_frag;
  uintptr_t mem_start;
} BitMap;

#define BITMAP_BLOCKS_PER_BYTE 8
#define BITMAP_BLOCK_SIZE HAL_PAGE_SIZE
#define BITMAP_INVALID_BLOCK ((size_t)-1)

void *bitmap_toptr(BitMap *bm, size_t block);
size_t bitmap_toblock(BitMap *bm, void *ptr);
size_t bitmap_toblock_roundup(BitMap *bm, void *ptr);
size_t bitmap_calcsize(size_t total);
bool bitmap_get(BitMap *bm, size_t block);
void bitmap_set(BitMap *bm, size_t block, bool value);
void bitmap_markblocks(BitMap *bm, size_t start, size_t size, bool value);
void bitmap_markregion(BitMap *bm, void *baseptr, size_t size, bool is_used);
size_t bitmap_freeregion(BitMap *bm, size_t blocks);
void *bitmap_alloc(BitMap *bm, size_t blocks);
void bitmap_free(BitMap *bm, void *base, size_t blocks);

#endif // BITMAP_BITMAP_H_
