Created
October 11, 2025 05:58
-
-
Save MurageKibicho/544ee5838a9a1b573577ecf535bb1084 to your computer and use it in GitHub Desktop.
Memroy mapping Hutter Prize Enwik9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Full guide : https://leetarxiv.substack.com/p/hutter-prize-beginners-guide-to-writing | |
| #include <assert.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <stdint.h> | |
| #include <string.h> | |
| #include <math.h> | |
| #include <sys/mman.h> | |
| #include <stdbool.h> | |
| #define INDEX(x, y, cols) ((x) * (cols) + (y)) | |
| //clear && gcc main.c -lm -o m.o && ./m.o | |
| size_t GetFileSize(char *fileName) | |
| { | |
| FILE *fp = fopen(fileName, "rb"); | |
| assert(fp != NULL); | |
| fseek(fp, 0L, SEEK_END); | |
| size_t currentFileSize = ftell(fp);rewind(fp); | |
| fclose(fp); | |
| return currentFileSize; | |
| } | |
| unsigned char *LoadData(char *fileName, size_t *fileSizeHolder) | |
| { | |
| size_t fileSize = GetFileSize(fileName); | |
| FILE *fp = fopen(fileName, "rb");assert(fp != NULL); | |
| int fileNumber = fileno(fp); | |
| unsigned char *fileData = mmap(NULL,fileSize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileNumber, 0); | |
| assert(fileData != MAP_FAILED); | |
| fclose(fp); | |
| *fileSizeHolder = fileSize; | |
| return fileData; | |
| } | |
| int main() | |
| { | |
| char *fileName = "enwik9"; | |
| size_t fileSize = 0; | |
| unsigned char *data = LoadData(fileName, &fileSize); | |
| assert(munmap(data, fileSize) != -1); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment