eliminate sbrk and make build on mac

This commit is contained in:
Anthony Minessale 2015-09-04 12:05:06 -05:00
parent 17f7afe76a
commit 9830fb63f6
3 changed files with 50 additions and 96 deletions

View File

@ -60,16 +60,10 @@
*/ */
#define MPOOL_FLAG_HEAVY_PACKING (1<<2) #define MPOOL_FLAG_HEAVY_PACKING (1<<2)
/*
* Use sbrk not mmap to allocate pages. This is not recommended for
* normal use.
*/
#define MPOOL_FLAG_USE_SBRK (1<<3)
/* /*
* Use MMAP_ANONYMOUS instead of /dev/zero * Use MMAP_ANONYMOUS instead of /dev/zero
*/ */
#define MPOOL_FLAG_ANONYMOUS (1<<4) #define MPOOL_FLAG_ANONYMOUS (1<<3)
/* /*
* Mpool error codes * Mpool error codes
@ -91,7 +85,7 @@
#define MPOOL_ERROR_IS_FREE 15 /* memory block already free */ #define MPOOL_ERROR_IS_FREE 15 /* memory block already free */
#define MPOOL_ERROR_BLOCK_STAT 16 /* invalid internal block status */ #define MPOOL_ERROR_BLOCK_STAT 16 /* invalid internal block status */
#define MPOOL_ERROR_FREE_ADDR 17 /* invalid internal free address */ #define MPOOL_ERROR_FREE_ADDR 17 /* invalid internal free address */
#define MPOOL_ERROR_SBRK_CONTIG 18 /* sbrk did not return contiguous mem*/ #define MPOOL_ERROR_UNUSED 18 /* UNUSED */
#define MPOOL_ERROR_NO_PAGES 19 /* ran out of pages in pool */ #define MPOOL_ERROR_NO_PAGES 19 /* ran out of pages in pool */
#define MPOOL_ERROR_ALLOC 20 /* calloc,malloc,free,realloc failed */ #define MPOOL_ERROR_ALLOC 20 /* calloc,malloc,free,realloc failed */
#define MPOOL_ERROR_PNT_OVER 21 /* pointer structure was overwritten */ #define MPOOL_ERROR_PNT_OVER 21 /* pointer structure was overwritten */

View File

@ -243,56 +243,32 @@ static void *alloc_pages(mpool_t *mp_p, const unsigned int page_n,
(void)printf("allocating %u pages or %lu bytes\n", page_n, size); (void)printf("allocating %u pages or %lu bytes\n", page_n, size);
#endif #endif
if (BIT_IS_SET(mp_p->mp_flags, MPOOL_FLAG_USE_SBRK)) {
#ifndef WIN32
mem = sbrk(size);
if (mem == (void *)-1) {
SET_POINTER(error_p, MPOOL_ERROR_NO_MEM);
return NULL;
}
fill = (unsigned long)mem % mp_p->mp_page_size;
if (fill > 0) { state = MAP_PRIVATE;
fill = mp_p->mp_page_size - fill;
fill_mem = sbrk(fill);
if (fill_mem == (void *)-1) {
SET_POINTER(error_p, MPOOL_ERROR_NO_MEM);
return NULL;
}
if ((char *)fill_mem != (char *)mem + size) {
SET_POINTER(error_p, MPOOL_ERROR_SBRK_CONTIG);
return NULL;
}
mem = (char *)mem + fill;
}
#endif
}
else {
state = MAP_PRIVATE;
#ifdef MAP_FILE #ifdef MAP_FILE
state |= MAP_FILE; state |= MAP_FILE;
#endif #endif
#ifdef MAP_VARIABLE #ifdef MAP_VARIABLE
state |= MAP_VARIABLE; state |= MAP_VARIABLE;
#endif #endif
/* mmap from /dev/zero */ /* mmap from /dev/zero */
mem = mmap(mp_p->mp_addr, size, mp_p->mp_mmflags, state, mem = mmap(mp_p->mp_addr, size, mp_p->mp_mmflags, state,
mp_p->mp_fd, mp_p->mp_top); mp_p->mp_fd, mp_p->mp_top);
if (mem == (void *)MAP_FAILED) { if (mem == (void *)MAP_FAILED) {
if (errno == ENOMEM) { if (errno == ENOMEM) {
SET_POINTER(error_p, MPOOL_ERROR_NO_MEM); SET_POINTER(error_p, MPOOL_ERROR_NO_MEM);
}
else {
SET_POINTER(error_p, MPOOL_ERROR_MMAP);
}
return NULL;
} }
mp_p->mp_top += size; else {
if (mp_p->mp_addr != NULL) { SET_POINTER(error_p, MPOOL_ERROR_MMAP);
mp_p->mp_addr = (char *)mp_p->mp_addr + size;
} }
return NULL;
} }
mp_p->mp_top += size;
if (mp_p->mp_addr != NULL) {
mp_p->mp_addr = (char *)mp_p->mp_addr + size;
}
mp_p->mp_page_c += page_n; mp_p->mp_page_c += page_n;
@ -321,13 +297,10 @@ static void *alloc_pages(mpool_t *mp_p, const unsigned int page_n,
* *
* sbrk_b -> Set to one if the pages were allocated with sbrk else mmap. * sbrk_b -> Set to one if the pages were allocated with sbrk else mmap.
*/ */
static int free_pages(void *pages, const unsigned long size, static int free_pages(void *pages, const unsigned long size)
const int sbrk_b)
{
if (! sbrk_b) {
(void)munmap(pages, size);
}
{
(void)munmap(pages, size);
return MPOOL_ERROR_NONE; return MPOOL_ERROR_NONE;
} }
@ -887,7 +860,6 @@ static int free_mem(mpool_t *mp_p, void *addr, const unsigned long size)
* multiple of the getpagesize() value. Set to 0 for the default. * multiple of the getpagesize() value. Set to 0 for the default.
* *
* start_addr -> Starting address to try and allocate memory pools. * start_addr -> Starting address to try and allocate memory pools.
* This is ignored if the MPOOL_FLAG_USE_SBRK is enabled.
* *
* error_p <- Pointer to integer which, if not NULL, will be set with * error_p <- Pointer to integer which, if not NULL, will be set with
* a mpool error code. * a mpool error code.
@ -943,27 +915,21 @@ mpool_t *mpool_open(const unsigned int flags, const unsigned int page_size,
mp.mp_mmflags = PROT_READ | PROT_WRITE; mp.mp_mmflags = PROT_READ | PROT_WRITE;
if (BIT_IS_SET(flags, MPOOL_FLAG_USE_SBRK)) { if (BIT_IS_SET(flags, MPOOL_FLAG_ANONYMOUS)) {
mp.mp_fd = -1; mp.mp_fd = -1;
mp.mp_addr = NULL; mp.mp_mmflags |= MAP_ANON;
mp.mp_top = 0; } else {
} /* open dev-zero for our mmaping */
else { mp.mp_fd = open("/dev/zero", O_RDWR, 0);
if (BIT_IS_SET(flags, MPOOL_FLAG_ANONYMOUS)) { if (mp.mp_fd < 0) {
mp.mp_fd = -1; SET_POINTER(error_p, MPOOL_ERROR_OPEN_ZERO);
mp.mp_mmflags |= MAP_ANONYMOUS; return NULL;
} 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 */
mp.mp_top = 0;
} }
mp.mp_addr = start_addr;
/* we start at the front of the file */
mp.mp_top = 0;
/* /*
* Find out how many pages we need for our mpool structure. * Find out how many pages we need for our mpool structure.
@ -1012,8 +978,8 @@ mpool_t *mpool_open(const unsigned int flags, const unsigned int page_size,
mp.mp_fd = -1; mp.mp_fd = -1;
} }
/* NOTE: after this line mp_p will be invalid */ /* NOTE: after this line mp_p will be invalid */
(void)free_pages(block_p, SIZE_OF_PAGES(&mp, page_n), (void)free_pages(block_p, SIZE_OF_PAGES(&mp, page_n));
BIT_IS_SET(flags, MPOOL_FLAG_USE_SBRK));
SET_POINTER(error_p, ret); SET_POINTER(error_p, ret);
return NULL; return NULL;
} }
@ -1099,8 +1065,8 @@ int mpool_close(mpool_t *mp_p)
block_p->mb_magic2 = 0; block_p->mb_magic2 = 0;
/* record the next pointer because it might be invalidated below */ /* record the next pointer because it might be invalidated below */
next_p = block_p->mb_next_p; next_p = block_p->mb_next_p;
ret = free_pages(block_p, (char *)block_p->mb_bounds_p - (char *)block_p, ret = free_pages(block_p, (char *)block_p->mb_bounds_p - (char *)block_p);
BIT_IS_SET(mp_p->mp_flags, MPOOL_FLAG_USE_SBRK));
if (ret != MPOOL_ERROR_NONE) { if (ret != MPOOL_ERROR_NONE) {
final = ret; final = ret;
} }
@ -1116,20 +1082,17 @@ int mpool_close(mpool_t *mp_p)
mp_p->mp_magic = 0; mp_p->mp_magic = 0;
mp_p->mp_magic2 = 0; mp_p->mp_magic2 = 0;
/* last we munmap the mpool pointer itself */ /* if we are heavy packing then we need to free the 1st block later */
if (! BIT_IS_SET(mp_p->mp_flags, MPOOL_FLAG_USE_SBRK)) { if (BIT_IS_SET(mp_p->mp_flags, MPOOL_FLAG_HEAVY_PACKING)) {
addr = (char *)mp_p - sizeof(mpool_block_t);
/* if we are heavy packing then we need to free the 1st block later */
if (BIT_IS_SET(mp_p->mp_flags, MPOOL_FLAG_HEAVY_PACKING)) {
addr = (char *)mp_p - sizeof(mpool_block_t);
}
else {
addr = mp_p;
}
size = SIZE_OF_PAGES(mp_p, PAGES_IN_SIZE(mp_p, sizeof(mpool_t)));
(void)munmap(addr, size);
} }
else {
addr = mp_p;
}
size = SIZE_OF_PAGES(mp_p, PAGES_IN_SIZE(mp_p, sizeof(mpool_t)));
(void)munmap(addr, size);
return final; return final;
} }
@ -1759,9 +1722,6 @@ const char *mpool_strerror(const int error)
case MPOOL_ERROR_FREE_ADDR: case MPOOL_ERROR_FREE_ADDR:
return "invalid internal free address"; return "invalid internal free address";
break; break;
case MPOOL_ERROR_SBRK_CONTIG:
return "sbrk did not return contiguous memory";
break;
case MPOOL_ERROR_NO_PAGES: case MPOOL_ERROR_NO_PAGES:
return "no available pages left in pool"; return "no available pages left in pool";
break; break;

View File

@ -34,7 +34,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifdef unix #if defined(unix) || defined(__APPLE__)
#include <unistd.h> #include <unistd.h>