FS-10167 WIP getting rid of mmap

This commit is contained in:
Anthony Minessale 2017-03-27 14:52:01 -05:00
parent 020f80b8d2
commit e195d5a294
3 changed files with 9 additions and 46 deletions

View File

@ -99,7 +99,6 @@ KS_BEGIN_EXTERN_C
KS_STATUS_PAGE_SIZE, /* could not get system page-size */ KS_STATUS_PAGE_SIZE, /* could not get system page-size */
KS_STATUS_OPEN_ZERO, /* could not open /dev/zero */ KS_STATUS_OPEN_ZERO, /* could not open /dev/zero */
KS_STATUS_NO_MEM, /* no memory available */ KS_STATUS_NO_MEM, /* no memory available */
KS_STATUS_MMAP, /* problems with mmap */
KS_STATUS_SIZE, /* error processing requested size */ KS_STATUS_SIZE, /* error processing requested size */
KS_STATUS_TOO_BIG, /* allocation exceeded max size */ KS_STATUS_TOO_BIG, /* allocation exceeded max size */
KS_STATUS_MEM, /* invalid memory address */ KS_STATUS_MEM, /* invalid memory address */
@ -125,6 +124,7 @@ KS_BEGIN_EXTERN_C
"GENERR",\ "GENERR",\
"INACTIVE",\ "INACTIVE",\
"TIMEOUT",\ "TIMEOUT",\
"DUPLICATE_OPERATION",\
"ARG_NULL",\ "ARG_NULL",\
"ARG_INVALID",\ "ARG_INVALID",\
"PNT",\ "PNT",\
@ -132,7 +132,6 @@ KS_BEGIN_EXTERN_C
"PAGE_SIZE",\ "PAGE_SIZE",\
"OPEN_ZERO",\ "OPEN_ZERO",\
"NO_MEM",\ "NO_MEM",\
"MMAP",\
"SIZE",\ "SIZE",\
"TOO_BIG",\ "TOO_BIG",\
"MEM",\ "MEM",\

View File

@ -26,14 +26,9 @@
* library which was close to what we needed but did not exactly do * library which was close to what we needed but did not exactly do
* what I wanted. * what I wanted.
* *
* The following uses mmap from /dev/zero. It allows a number of
* allocations to be made inside of a memory pool then with a clear or
* close the pool can be reset without any memory fragmentation and
* growth problems.
*/ */
#include "ks.h" #include "ks.h"
#include <sys/mman.h>
#define KS_POOL_MAGIC 0xABACABA /* magic for struct */ #define KS_POOL_MAGIC 0xABACABA /* magic for struct */
#define BLOCK_MAGIC 0xB1B1007 /* magic for blocks */ #define BLOCK_MAGIC 0xB1B1007 /* magic for blocks */
@ -113,7 +108,6 @@ struct ks_pool_s {
unsigned int mp_page_size; /* page-size of our system */ unsigned int mp_page_size; /* page-size of our system */
off_t mp_top; /* top of our allocations in fd */ off_t mp_top; /* top of our allocations in fd */
ks_pool_log_func_t mp_log_func; /* log callback function */ ks_pool_log_func_t mp_log_func; /* log callback function */
void *mp_addr; /* current address for mmaping */
void *mp_min_p; /* min address in pool for checks */ void *mp_min_p; /* min address in pool for checks */
void *mp_bounds_p; /* max address in pool for checks */ void *mp_bounds_p; /* max address in pool for checks */
struct ks_pool_block_st *mp_first_p; /* first memory block we are using */ struct ks_pool_block_st *mp_first_p; /* first memory block we are using */
@ -453,7 +447,6 @@ static void *alloc_pages(ks_pool_t *mp_p, const unsigned int page_n, ks_status_t
{ {
void *mem; void *mem;
unsigned long size; unsigned long size;
int state;
/* are we over our max-pages? */ /* are we over our max-pages? */
if (mp_p->mp_max_pages > 0 && mp_p->mp_page_c >= mp_p->mp_max_pages) { if (mp_p->mp_max_pages > 0 && mp_p->mp_page_c >= mp_p->mp_max_pages) {
@ -468,34 +461,10 @@ static void *alloc_pages(ks_pool_t *mp_p, const unsigned int page_n, ks_status_t
#endif #endif
state = MAP_PRIVATE | MAP_ANON; mem = malloc(size);
ks_assert(mem);
#if defined(MAP_FILE)
state |= MAP_FILE;
#endif
#if defined(MAP_VARIABLE)
state |= MAP_VARIABLE;
#endif
/* mmap from /dev/zero */
mem = mmap(mp_p->mp_addr, size, PROT_READ | PROT_WRITE, state, -1, mp_p->mp_top);
if (mem == (void *) MAP_FAILED) {
if (errno == ENOMEM) {
SET_POINTER(error_p, KS_STATUS_NO_MEM);
} else {
SET_POINTER(error_p, KS_STATUS_MMAP);
}
return NULL;
}
mp_p->mp_top += size; 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;
SET_POINTER(error_p, KS_STATUS_SUCCESS); SET_POINTER(error_p, KS_STATUS_SUCCESS);
@ -521,11 +490,10 @@ static void *alloc_pages(ks_pool_t *mp_p, const unsigned int page_n, ks_status_t
* *
* size -> Size of the block that we are freeing. * size -> Size of the block that we are freeing.
* *
* 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)
{ {
(void) munmap(pages, size); free(pages);
return KS_STATUS_SUCCESS; return KS_STATUS_SUCCESS;
} }
@ -1123,7 +1091,6 @@ static ks_pool_t *ks_pool_raw_open(const unsigned int flags, const unsigned int
/* mp.mp_page_size set below */ /* mp.mp_page_size set below */
/* mp.mp_blocks_bit_n set below */ /* mp.mp_blocks_bit_n set below */
/* mp.mp_top set below */ /* mp.mp_top set below */
/* mp.mp_addr set below */
mp.mp_log_func = NULL; mp.mp_log_func = NULL;
mp.mp_min_p = NULL; mp.mp_min_p = NULL;
mp.mp_bounds_p = NULL; mp.mp_bounds_p = NULL;
@ -1146,7 +1113,6 @@ static ks_pool_t *ks_pool_raw_open(const unsigned int flags, const unsigned int
} }
} }
mp.mp_addr = start_addr;
/* we start at the front of the file */ /* we start at the front of the file */
mp.mp_top = 0; mp.mp_top = 0;
@ -1276,7 +1242,7 @@ static ks_status_t ks_pool_raw_close(ks_pool_t *mp_p)
{ {
ks_pool_block_t *block_p, *next_p; ks_pool_block_t *block_p, *next_p;
void *addr; void *addr;
unsigned long size; //unsigned long size;
int ret, final = KS_STATUS_SUCCESS; int ret, final = KS_STATUS_SUCCESS;
/* special case, just return no-error */ /* special case, just return no-error */
@ -1336,9 +1302,10 @@ static ks_status_t ks_pool_raw_close(ks_pool_t *mp_p)
} else { } else {
addr = mp_p; addr = mp_p;
} }
size = SIZE_OF_PAGES(mp_p, PAGES_IN_SIZE(mp_p, sizeof(ks_pool_t)));
(void) munmap(addr, size); //size = SIZE_OF_PAGES(mp_p, PAGES_IN_SIZE(mp_p, sizeof(ks_pool_t)));
free(addr);
ks_mutex_unlock(mutex); ks_mutex_unlock(mutex);
ks_mutex_destroy(&mutex); ks_mutex_destroy(&mutex);
@ -2190,9 +2157,6 @@ KS_DECLARE(const char *) ks_pool_strerror(const ks_status_t error)
case KS_STATUS_NO_MEM: case KS_STATUS_NO_MEM:
return "no memory available"; return "no memory available";
break; break;
case KS_STATUS_MMAP:
return "problems with mmap";
break;
case KS_STATUS_SIZE: case KS_STATUS_SIZE:
return "error processing requested size"; return "error processing requested size";
break; break;