<commit_before>#ifndef BLOOM_BITMAP_H
#define BLOOM_BITMAP_H
#include <stddef.h>
#include <stdlib.h>
#include <inttypes.h>

typedef enum {
    SHARED = 0,     // MAP_SHARED mmap used, file backed.
    PERSISTENT,     // MAP_ANONYMOUS used, file backed.
    ANONYMOUS       // MAP_ANONYMOUS mmap used. No file backing.
} bitmap_mode;

typedef struct {
    bitmap_mode mode;
    int fileno;          // Underlying fileno
    uint64_t size;       // Size of bitmap in bytes
    unsigned char* mmap; // Starting address of the bitmap region
    unsigned char* dirty_pages; // Used for the PERSISTENT mode.
} bloom_bitmap;

/**
 * Returns a bloom_bitmap pointer from a file handle
 * that is already opened with read/write privileges.
 * @arg fileno The fileno
 * @arg len The length of the bitmap in bytes.
 * @arg mode The mode to use for the bitmap.
 * @arg map The output map. Will be initialized.
 * @return 0 on success. Negative on error.
 */
int bitmap_from_file(int fileno, uint64_t len, bitmap_mode mode, bloom_bitmap *map);

/**
 * Returns a bloom_bitmap pointer from a filename.
 * Opens the file with read/write privileges. If create
 * is true, then a file will be created if it does not exist.
 * If the file cannot be opened, NULL will be returned.
 * @arg fileno The fileno
 * @arg len The length of the bitmap in bytes.
 * @arg create If 1, then the file will be created if it does not exist.
 * @arg resize If 1, then the file will be expanded to len
 * @arg mode The mode to use for the bitmap.
 * @arg map The output map. Will be initialized.
 * @return 0 on success. Negative on error.
 */
int bitmap_from_filename(char* filename, uint64_t len, int create, int resize, bitmap_mode mode, bloom_bitmap *map);

/**
 * Flushes the bitmap back to disk. This is
 * a syncronous operation. It is a no-op for
 * ANONYMOUS bitmaps.
 * @arg map The bitmap
 * @returns 0 on success, negative failure.
 */
int bitmap_flush(bloom_bitmap *map);

/**
 * * Closes and flushes the bitmap. This is
 * a syncronous operation. It is a no-op for
 * ANONYMOUS bitmaps. The caller should free()
 * the structure after.
 * @arg map The bitmap
 * @returns 0 on success, negative on failure.
 */
int bitmap_close(bloom_bitmap *map);

/**
 * Returns the value of the bit at index idx for the<commit_msg>Adding NEW_BITMAP flag to the bitmap_mode enum<commit_after>#ifndef BLOOM_BITMAP_H
#define BLOOM_BITMAP_H
#include <stddef.h>
#include <stdlib.h>
#include <inttypes.h>

typedef enum {
    SHARED      = 1, // MAP_SHARED mmap used, file backed.
    PERSISTENT  = 2, // MAP_ANONYMOUS used, file backed.
    ANONYMOUS   = 4, // MAP_ANONYMOUS mmap used. No file backing.
    NEW_BITMAP  = 8  // File contents not read. Used with PERSISTENT
} bitmap_mode;

typedef struct {
    bitmap_mode mode;
    int fileno;          // Underlying fileno
    uint64_t size;       // Size of bitmap in bytes
    unsigned char* mmap; // Starting address of the bitmap region
    unsigned char* dirty_pages; // Used for the PERSISTENT mode.
} bloom_bitmap;

/**
 * Returns a bloom_bitmap pointer from a file handle
 * that is already opened with read/write privileges.
 * @arg fileno The fileno
 * @arg len The length of the bitmap in bytes.
 * @arg mode The mode to use for the bitmap.
 * @arg map The output map. Will be initialized.
 * @return 0 on success. Negative on error.
 */
int bitmap_from_file(int fileno, uint64_t len, bitmap_mode mode, bloom_bitmap *map);

/**
 * Returns a bloom_bitmap pointer from a filename.
 * Opens the file with read/write privileges. If create
 * is true, then a file will be created if it does not exist.
 * If the file cannot be opened, NULL will be returned.
 * @arg fileno The fileno
 * @arg len The length of the bitmap in bytes.
 * @arg create If 1, then the file will be created if it does not exist.
 * @arg mode The mode to use for the bitmap.
 * @arg map The output map. Will be initialized.
 * @return 0 on success. Negative on error.
 */
int bitmap_from_filename(char* filename, uint64_t len, int create, bitmap_mode mode, bloom_bitmap *map);

/**
 * Flushes the bitmap back to disk. This is
 * a syncronous operation. It is a no-op for
 * ANONYMOUS bitmaps.
 * @arg map The bitmap
 * @returns 0 on success, negative failure.
 */
int bitmap_flush(bloom_bitmap *map);

/**
 * * Closes and flushes the bitmap. This is
 * a syncronous operation. It is a no-op for
 * ANONYMOUS bitmaps. The caller should free()
 * the structure after.
 * @arg map The bitmap
 * @returns 0 on success, negative on failure.
 */
int bitmap_close(bloom_bitmap *map);

/**
 * Returns the value of the bit at index idx for the
{'fraction_non_alphanumeric': 0.08203463203463203, 'fraction_numerical': 0.006060606060606061, 'mean_word_length': 3.274745605920444, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3475804', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1421, 'n_words': 646}