add map_anonymoys support

This commit is contained in:
Anthony Minessale 2015-09-04 11:38:16 -05:00
parent aebc6467fb
commit b434e2b7fe
3 changed files with 19 additions and 6 deletions

View File

@ -66,6 +66,11 @@
*/
#define MPOOL_FLAG_USE_SBRK (1<<3)
/*
* Use MMAP_ANONYMOUS instead of /dev/zero
*/
#define MPOOL_FLAG_ANONYMOUS (1<<4)
/*
* Mpool error codes
*/

View File

@ -73,6 +73,7 @@
typedef struct {
unsigned int mp_magic; /* magic number for struct */
unsigned int mp_flags; /* flags for the struct */
unsigned int mp_mmflags; /* flags for mmap */
unsigned long mp_alloc_c; /* number of allocations */
unsigned long mp_user_alloc; /* user bytes allocated */
unsigned long mp_max_alloc; /* maximum user bytes allocated */

View File

@ -273,7 +273,7 @@ static void *alloc_pages(mpool_t *mp_p, const unsigned int page_n,
#endif
/* mmap from /dev/zero */
mem = mmap((caddr_t)mp_p->mp_addr, size, PROT_READ | PROT_WRITE, state,
mem = mmap((caddr_t)mp_p->mp_addr, size, mp_p->mp_mmflags, state,
mp_p->mp_fd, mp_p->mp_top);
if (mem == (void *)MAP_FAILED) {
if (errno == ENOMEM) {
@ -937,17 +937,24 @@ mpool_t *mpool_open(const unsigned int flags, const unsigned int page_size,
}
}
mp.mp_mmflags = PROT_READ | PROT_WRITE;
if (BIT_IS_SET(flags, MPOOL_FLAG_USE_SBRK)) {
mp.mp_fd = -1;
mp.mp_addr = NULL;
mp.mp_top = 0;
}
else {
/* open dev-zero for our mmaping */
mp.mp_fd = open("/dev/zero", O_RDWR, 0);
if (mp.mp_fd < 0) {
SET_POINTER(error_p, MPOOL_ERROR_OPEN_ZERO);
return NULL;
if (BIT_IS_SET(flags, MPOOL_FLAG_ANONYMOUS)) {
mp.mp_fd = -1;
mp.mp_mmflags |= MAP_ANONYMOUS;
} else {
/* open dev-zero for our mmaping */
mp.mp_fd = open("/dev/zero", O_RDWR, 0);
if (mp.mp_fd < 0) {
SET_POINTER(error_p, MPOOL_ERROR_OPEN_ZERO);
return NULL;
}
}
mp.mp_addr = start_addr;
/* we start at the front of the file */