- map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
- if (map == MAP_FAILED) return EAGAIN;
- if (guard) mprotect(map, guard, PROT_NONE);
+ if (guard) {
+ map = mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
+ if (map == MAP_FAILED) return EAGAIN;
+ if (mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)) {
+ munmap(map, size);
+ return EAGAIN;
+ }
+ } else {
+ map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
+ if (map == MAP_FAILED) return EAGAIN;
+ }