<commit_before>#ifndef MAPPEDFILE_H
#define MAPPEDFILE_H

//License: Public Domain
//Author: elf
//EMail: <EMAIL>

#include "File.h"
#include <sys/mman.h>
/*
#define PAGE_SHIFT 12
//4096
#define PAGE_SIZE (1 << PAGE_SHIFT)
#define PAGE_SIZE_MASK_L (PAGE_SIZE - 1)
#define PAGE_SIZE_MASK (~PAGE_SIZE_MASK_L)
#define PAGE_ALIGN(s) ((s + PAGE_SIZE_MASK_L) & PAGE_SIZE_MASK)
#define PAGES_TO_SIZE(pages) (pages << PAGE_SHIFT)
*/

typedef struct{
  File file;
  Byte *pBegin;
}MMFile;

static inline E8 MMFile_Adjust(MMFile *pFile, I32 iSize);

static inline E8 MMFile_Open(MMFile *pFile, const C *szPath){
  File *pfFile = &pFile->file;
  if(!File_Open(pfFile, szPath))
    return 1;

  if(!File_ReadMeta(pfFile)){
    File_Close(pfFile);
    return 2;
  }

  Byte *pBegin = (Byte*)mmap(null, pfFile->meta.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, pfFile->fd, 0);
  if(pBegin == MAP_FAILED){
    File_Close(pfFile);
    pFile->pBegin = null;
    return 3;
  }

  pFile->pBegin = pBegin;
  return 0;
}

static inline E8 MMFile_Prepare(MMFile *pFile, const C *szPath, U32 uMinSize){
  File *pfFile = &pFile->file;
  if(!File_Prepare(pfFile, szPath))
    return 1;

  if(!File_ReadMeta(pfFile)){
    File_Close(pfFile);
    return 2;
  }

  U64 uSize = File_GetSize(pfFile);
  if(uSize < uMinSize){
   E8 e = MMFile_Adjust(pFile, uMinSize - uSize);
   if(e){
    File_Close(pfFile);
    return e;
   }
  }

  Byte *pBegin = (Byte*)mmap(null, pfFile->meta.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, pfFile->fd, 0);
  if(pBegin == MAP_FAILED){
    File_Close(pfFile);
    pFile->pBegin = null;
    return 3;
  }
  pFile->pBegin = pBegin;
  return 0;
}

static inline const E8 MMFile_OpenForRead(MMFile *pFile, const C *szPath){
  File *pfFile = &pFile->file;
  if(!File_OpenForRead(pfFile, szPath))
    return 1;

  if(!File_ReadMeta(pfFile)){
    File_Close(pfFile);
    return 2;
  }

  Byte *pBegin = (Byte*)mmap(null, pfFile->meta.st_size, PROT_READ, MAP_SHARED, pfFile->fd, 0);
  if(pBegin == MAP_FAILED){
    File_Close(pfFile);
    pFile->pBegin = null;
    return 3;
  }
  pFile->pBegin = pBegin;
  return 0;
}

static inline void MMFile_Close(MMFile *pFile){
  if(pFile->pBegin){
    munmap(pFile->pBegin, pFile->file.meta.st_size);
    pFile->pBegin = null;<commit_msg>Add "MMFile_Create()"<commit_after>#ifndef MAPPEDFILE_H
#define MAPPEDFILE_H

//License: Public Domain
//Author: elf
//EMail: <EMAIL>

#include "File.h"
#include <sys/mman.h>
/*
#define PAGE_SHIFT 12
//4096
#define PAGE_SIZE (1 << PAGE_SHIFT)
#define PAGE_SIZE_MASK_L (PAGE_SIZE - 1)
#define PAGE_SIZE_MASK (~PAGE_SIZE_MASK_L)
#define PAGE_ALIGN(s) ((s + PAGE_SIZE_MASK_L) & PAGE_SIZE_MASK)
#define PAGES_TO_SIZE(pages) (pages << PAGE_SHIFT)
*/

typedef struct{
  File file;
  Byte *pBegin;
}MMFile;

static inline E8 MMFile_Adjust(MMFile *pFile, I32 iSize);

static inline E8 MMFile_Create(MMFile *pFile, const C *szPath, U32 uSize){
  File *pfFile = &pFile->file;
  if(!File_Create(pfFile, szPath))
    return 1;
  pFile->pBegin = null;

  if(!File_SetSize(pfFile, uSize)){
    File_Close(pfFile);
    return 2;
  }

  if(!File_ReadMeta(pfFile)){
    File_Close(pfFile);
    return 3;
  }

  Byte *pBegin = (Byte*)mmap(null, uSize, PROT_READ | PROT_WRITE, MAP_SHARED, pfFile->fd, 0);
  if(pBegin == MAP_FAILED){
    File_Close(pfFile);
    return 4;
  }
  pFile->pBegin = pBegin;
  return 0;
}

static inline E8 MMFile_Open(MMFile *pFile, const C *szPath){
  File *pfFile = &pFile->file;
  if(!File_Open(pfFile, szPath))
    return 1;
  pFile->pBegin = null;

  if(!File_ReadMeta(pfFile)){
    File_Close(pfFile);
    return 2;
  }

  Byte *pBegin = (Byte*)mmap(null, pfFile->meta.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, pfFile->fd, 0);
  if(pBegin == MAP_FAILED){
    File_Close(pfFile);
    return 3;
  }

  pFile->pBegin = pBegin;
  return 0;
}

static inline E8 MMFile_Prepare(MMFile *pFile, const C *szPath, U32 uMinSize){
  File *pfFile = &pFile->file;
  if(!File_Prepare(pfFile, szPath))
    return 1;
  pFile->pBegin = null;

  if(!File_ReadMeta(pfFile)){
    File_Close(pfFile);
    return 2;
  }

  U64 uSize = File_GetSize(pfFile);
  if(uSize < uMinSize){
   E8 e = MMFile_Adjust(pFile, uMinSize - uSize);
   if(e){
    File_Close(pfFile);
    return e;
   }
  }

  Byte *pBegin = (Byte*)mmap(null, pfFile->meta.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, pfFile->fd, 0);
  if(pBegin == MAP_FAILED){
    File_Close(pfFile);
    return 3;
  }
  pFile->pBegin = pBegin;
  return 0;
}

static inline const E8 MMFile_OpenForRead(MMFile *pFile, const C *szPath){
  File *pfFile = &pFile->file;
  if(!File_OpenForRead(pfFile, szPath))
    return 1;
  pFile->pBegin = null;

  if(!File_ReadMeta(pfFile)){
    File_Close(pfFile);
    return 2;
  }

  Byte *pBegin = (Byte*)mmap(null, pfFile->meta.st_size, PROT_READ, MAP_SHARED, pfFile->fd, 0);
  if(pBegin == MAP_FAILED){
    File_Close(pfFile);
    return 3;
  }
  pFile->pBegin = pBegin;
  return 0;
}

static inline void MMFile_Close(MMFile *pFile){
  if(pFile->pBegin){
    munmap(pFile->pBegin, pFile->file.meta.st_size);
    pFile->pBegin = null;
{'fraction_non_alphanumeric': 0.14403615641579878, 'fraction_numerical': 0.015130674002751032, 'mean_word_length': 4.0, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 49, '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': '5407554', 'n_tokens_mistral': 2190, 'n_tokens_neox': 2079, 'n_words': 438}