I'm getting a segmentation fault the first time I call malloc() after I protect a memory region with mprotect(). This is a code sniplet that does the memory allocation the the protection:
#define PAGESIZE 4096
void* paalloc(int size){ // Allocates and aligns memory
int type_size = sizeof(double);
void* p;
p = malloc(type_size*size+PAGESIZE-1);
p = (void*)(((long) p + PAGESIZE-1) & ~(PAGESIZE-1));
return p;
}
void aprotect(int size, void* array){ // Protects memory after values are set
int type_size = sizeof(double);
if (mprotect(array, type_size*size, PROT_READ)) {
perror("Couldn't mprotect");
}
}
I want to use mprotect to avoid anything writing into my arrays (which are pre-calculated sine/cosine values). Is this a stupid idea?