Created
June 28, 2024 09:25
-
-
Save AdriDevelopsThings/db2b936a5a697731bb50469fcea1aa6e to your computer and use it in GitHub Desktop.
Check if a new memory page is zero filled
This file contains 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
#include <sys/mman.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#define PAGESIZE sysconf(_SC_PAGE_SIZE) | |
int main() { | |
uint8_t* ptr = (uint8_t*) mmap(NULL, PAGESIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); | |
if (ptr == MAP_FAILED) { | |
perror("error while allocating memory page"); | |
return EXIT_FAILURE; | |
} | |
unsigned int a = 0; | |
for (size_t i=0; i < PAGESIZE; i++) { | |
if (ptr[i] != 0) { | |
printf("Byte %lu is not null: %x\n", i, ptr[i]); | |
a = 1; | |
} | |
} | |
munmap(ptr, PAGESIZE); | |
if (a == 0) { | |
printf("All bytes are zero!\n"); | |
return EXIT_SUCCESS; | |
} else { | |
return EXIT_FAILURE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment