8000 minix/minix/net/lwip/mempool.c at master · tonnyprado/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
821 lines (689 loc) · 27.5 KB

mempool.c

File metadata and controls

821 lines (689 loc) · 27.5 KB
if (is_static)
TAILQ_INSERT_HEAD(&mempool_small_static_freelist, msb,
msb_next);
else
TAILQ_INSERT_HEAD(&mempool_small_dynamic_freelist, msb,
msb_next);
}
}
/*
* Allocate a new slab for large buffers, if allowed by policy and possible.
*/
static void
mempool_new_slab(void)
{
struct mempool_large_slab *mls;
struct mempool_large_buf *mlb;
unsigned int count;
/*
* See if allocating a new slab would result in overrunning the
* configured maximum number of large buffers. Round the maximum,
* which is probably what the user intended.
*/
if (mempool_cur_buffers() + MEMPOOL_LARGE_COUNT / 2 >
mempool_max_buffers()) {
assert(mempool_nr_slabs > 0);
return;
}
/*
* If a previous allocation failed before during this timer interval,
* do not try again now.
*/
if (mempool_defer_alloc)
return;
/*
* Allocate the slab. Preallocate the memory, or we might crash later
* during low-memory conditions. If allocation fails, simply do
* nothing further. The caller will check the free lists.
*/
mls = (struct mempool_large_slab *)mmap(NULL,
sizeof(struct mempool_large_slab), PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE | MAP_PREALLOC, -1, 0);
if (mls == MAP_FAILED) {
if (mempool_nr_slabs == 0)
panic("unable to allocate initial memory pool");
/*
* Do not keep hammering VM with mmap requests when the system
* is out of memory. Try again after the next timer tick.
*/
mempool_defer_alloc = TRUE;
return;
}
/* Initialize the new slab. */
mls->mls_header.mh_flags = 0;
mls->mls_header.mh_inuse = 0;
mlb = mls->mls_buf;
LIST_INIT(&mls->mls_free);
for (count = 0; count < MEMPOOL_LARGE_COUNT; count++, mlb++) {
mlb->mlb_header = NULL;
mlb->mlb_header2 = &mls->mls_header;
LIST_INSERT_HEAD(&mls->mls_free, mlb, mlb_next);
}
LIST_INSERT_HEAD(&mempool_empty_slabs, mls, mls_next);
mempool_nr_slabs++;
mempool_nr_large += MEMPOOL_LARGE_COUNT;
}
/*
* Deallocate a slab for large buffers, if allowed.
*/
static void
mempool_destroy_slab(struct mempool_large_slab * mls)
{
assert(mempool_nr_slabs > 0);
assert(!(mls->mls_header.mh_flags & MHF_SMALL));
assert(mls->mls_header.mh_inuse == 0);
/* Never deallocate the last large slab. */
if (mempool_nr_slabs == 1)
return;
LIST_REMOVE(mls, mls_next);
if (munmap(mls, sizeof(*mls)) != 0)
panic("munmap failed: %d", -errno);
assert(mempool_nr_large > MEMPOOL_LARGE_COUNT);
mempool_nr_large -= MEMPOOL_LARGE_COUNT;
mempool_nr_slabs--;
}
/*
* Regular timer. Deallocate empty slabs already marked for deallocation, and
* mark any other empty slabs for deallocation.
*/
static void
mempool_tick(int arg __unused)
{
struct mempool_large_slab *mls, *tmls;
/*
* Go through all the empty slabs, destroying marked slabs and marking
* unmarked slabs.
*/
LIST_FOREACH_SAFE(mls, &mempool_empty_slabs, mls_next, tmls) {
if (mls->mls_header.mh_flags & MHF_MARKED)
mempool_destroy_slab(mls);
else
mls->mls_header.mh_flags |= MHF_MARKED;
}
/*
* If allocation failed during the last interval, allow a new attempt
* during the next.
*/
mempool_defer_alloc = FALSE;
/* Set the next timer. */
set_timer(&mempool_timer, MEMPOOL_TIMER_TICKS, mempool_tick, 0);
}
/*
* Initialize the memory pool module.
*/
void
mempool_init(void)
{
unsigned int slot;
/* These checks are for absolutely essential points. */
assert(sizeof(void *) == MEM_ALIGNMENT);
assert(sizeof(struct mempool_small_slab) <= MEMPOOL_LARGE_SIZE);
assert(offsetof(struct mempool_small_buf, msb_data) == sizeof(void *));
assert(offsetof(struct mempool_large_buf, mlb_data) == sizeof(void *));
/* Initialize module-local variables. */
LIST_INIT(&mempool_empty_slabs);
LIST_INIT(&mempool_partial_slabs);
LIST_INIT(&mempool_full_slabs);
TAILQ_INIT(&mempool_small_static_freelist);
TAILQ_INIT(&mempool_small_dynamic_freelist);
mempool_max_slabs = MEMPOOL_DEFAULT_MAX_SLABS;
mempool_nr_slabs = 0;
mempool_nr_large = 0;
mempool_used_large = 0;
mempool_used_small = 0;
mempool_defer_alloc = FALSE;
/* Initialize the static pool of small buffers. */
for (slot = 0; slot < __arraycount(mempool_small_pool); slot++)
mempool_prepare_small(&mempool_small_pool[slot],
TRUE /*is_static*/);
/*
* Allocate one large slab. The service needs at least one large slab
* for basic operation, and therefore will never deallocate the last.
*/
mempool_new_slab();
/* Set a regular low-frequency timer to deallocate unused slabs. */
set_timer(&mempool_timer, MEMPOOL_TIMER_TICKS, mempool_tick, 0);
/* Register the minix.lwip.mempool subtree. */
mibtree_register_lwip(&minix_lwip_mempool_node);
}
/*
* Return the total number of large buffers currently in the system, regardless
* of allocation status.
*/
unsigned int
mempool_cur_buffers(void)
{
return mempool_nr_large;
}
/*
* Return the maximum number of large buffers that the system has been allowed
* to allocate. Note that due to low-memory conditions, this maximum may not
* be allocated in practice even when desired.
*/
unsigned int
mempool_max_buffers(void)
{
if (mempool_max_slabs <= 1)
return MEMPOOL_LARGE_COUNT;
if ((size_t)mempool_max_slabs >
INT_MAX / sizeof(struct mempool_large_slab))
return INT_MAX / sizeof(struct mempool_large_slab);
return (size_t)mempool_max_slabs * MEMPOOL_LARGE_COUNT;
}
/*
* Allocate a large buffer, either by taking one off a free list or by
* allocating a new large slab. On success, return a pointer to the data area
* of the large buffer. This data area is exactly MEMPOOL_LARGE_SIZE bytes in
* size. If no large buffer could be allocated, return NULL.
*/
static void *
mempool_alloc_large(void)
{
struct mempool_large_slab *mls;
struct mempool_large_buf *mlb;
/*
* Find a large slab that has free large blocks. As is standard for
* slab allocation, favor partially used slabs over empty slabs for
* eventual consolidation. If both lists are empty, try allocating a
* new slab. If that fails, we are out of memory, and return NULL.
*/
if (!LIST_EMPTY(&mempool_partial_slabs))
mls = LIST_FIRST(&mempool_partial_slabs);
else {
if (LIST_EMPTY(&mempool_empty_slabs)) {
mempool_new_slab();
if (LIST_EMPTY(&mempool_empty_slabs))
return NULL; /* out of memory */
}
mls = LIST_FIRST(&mempool_empty_slabs);
}
/* Allocate a block from the slab that we picked. */
assert(mls != NULL);
assert(!LIST_EMPTY(&mls->mls_free));
mlb = LIST_FIRST(&mls->mls_free);
LIST_REMOVE(mlb, mlb_next);
assert(mlb->mlb_header == NULL);
assert(mlb->mlb_header2 == &mls->mls_header);
mlb->mlb_header = &mls->mls_header;
/*
* Adjust accounting for the large slab, which may involve moving it
* to another list.
*/
assert(mls->mls_header.mh_inuse < MEMPOOL_LARGE_COUNT);
mls->mls_header.mh_inuse++;
if (mls->mls_header.mh_inuse == MEMPOOL_LARGE_COUNT) {
LIST_REMOVE(mls, mls_next);
LIST_INSERT_HEAD(&mempool_full_slabs, mls, mls_next);
} else if (mls->mls_header.mh_inuse == 1) {
LIST_REMOVE(mls, mls_next);
LIST_INSERT_HEAD(&mempool_partial_slabs, mls, mls_next);
< 10000 div id="LC577" class="react-file-line html-div" data-testid="code-cell" data-line-number="577" style="position:relative"> }
assert(mempool_used_large < mempool_nr_large);
mempool_used_large++;
/* Return the block's data area. */
return (void *)mlb->mlb_data;
}
/*
* Allocate a small buffer, either by taking one off a free list or by
* allocating a large buffer and splitting it up in new free small buffers. On
* success, return a pointer to the data area of the small buffer. This data
* area is exactly MEMPOOL_SMALL_SIZE bytes in size. If no small buffer could
* be allocated, return NULL.
*/
static void *
mempool_alloc_small(void)
{
struct mempool_small_slab *mss;
struct mempool_small_buf *msb;
struct mempool_header *mh;
/*
* Find a free small block and take it off the free list. Try the
* static free list before the dynamic one, so that after a peak in
* buffer usage we are likely to be able to free up the dynamic slabs
* quickly. If both lists are empty, try allocating a large block to
* divvy up into small blocks. If that fails, we are out of memory.
*/
if (!TAILQ_EMPTY(&mempool_small_static_freelist)) {
msb = TAILQ_FIRST(&mempool_small_static_freelist);
TAILQ_REMOVE(&mempool_small_static_freelist, msb, msb_next);
} else {
if (TAILQ_EMPTY(&mempool_small_dynamic_freelist)) {
mss =
(struct mempool_small_slab *)mempool_alloc_large();
if (mss == NULL)
return NULL; /* out of memory */
/* Initialize the small slab, including its blocks. */
mempool_prepare_small(mss, FALSE /*is_static*/);
}
msb = TAILQ_FIRST(&mempool_small_dynamic_freelist);
assert(msb != NULL);
TAILQ_REMOVE(&mempool_small_dynamic_freelist, msb, msb_next);
}
/* Mark the small block as allocated, and return its data area. */
assert(msb != NULL);
assert(msb->msb_header == NULL);
assert(msb->msb_header2 != NULL);
mh = msb->msb_header2;
msb->msb_header = mh;
assert(mh->mh_inuse < MEMPOOL_SMALL_COUNT);
mh->mh_inuse++;
mempool_used_small++;
return (void *)msb->msb_data;
}
/*
* Memory pool wrapper function for malloc() calls from lwIP.
*/
void *
mempool_malloc(size_t size)
{
/*
* It is currently expected that there will be allocation attempts for
* sizes larger than our large size, in particular for ICMP ping
* replies as described elsewhere. As such, we cannot print any
* warnings here. For now, refusing these excessive allocations should
* not be a problem in practice.
*/
if (size > MEMPOOL_LARGE_SIZE)
return NULL;
if (size <= MEMPOOL_SMALL_SIZE)
return mempool_alloc_small();
else
return mempool_alloc_large();
}
/*
* Memory pool wrapper function for free() calls from lwIP.
*/
void
mempool_free(void * ptr)
{
struct mempool_large_slab *mls;
struct mempool_large_buf *mlb;
struct mempool_small_slab *mss;
struct mempool_small_buf *msb;
struct mempool_header *mh;
unsigned int count;
/*
* Get a pointer to the slab header, which is right before the data
* area for both large and small buffers. This pointer is NULL if the
* buffer is free, which would indicate that something is very wrong.
*/
ptr = (void *)((char *)ptr - sizeof(mh));
memcpy(&mh, ptr, sizeof(mh));
if (mh == NULL)
panic("mempool_free called on unallocated object!");
/*
* If the slab header says that the slab is for small buffers, deal
* with that case first. If we free up the last small buffer of a
* dynamically allocated small slab, we also free up the entire small
* slab, which is in fact the data area of a large buffer.
*/
if (mh->mh_flags & MHF_SMALL) {
/*
* Move the small buffer onto the appropriate small free list.
*/
msb = (struct mempool_small_buf *)ptr;
msb->msb_header2 = mh;
msb->msb_header = NULL;
/*
* Simple heuristic, unless the buffer is static: favor reuse
* of small buffers in containers that are already in use
* for other small buffers as well, for consolidation.
*/
if (mh->mh_flags & MHF_STATIC)
TAILQ_INSERT_HEAD(&mempool_small_static_freelist, msb,
msb_next);
else if (mh->mh_inuse > 1)
TAILQ_INSERT_HEAD(&mempool_small_dynamic_freelist, msb,
msb_next);
else
TAILQ_INSERT_TAIL(&mempool_small_dynamic_freelist, msb,
msb_next);
assert(mh->mh_inuse > 0);
mh->mh_inuse--;
assert(mempool_used_small > 0);
mempool_used_small--;
/*
* If the small buffer is statically allocated, or it was not
* the last allocated small buffer in its containing large
* buffer, then we are done.
*/
if (mh->mh_inuse > 0 || (mh->mh_flags & MHF_STATIC))
return;
/*
* Otherwise, free the containing large buffer as well. First,
* remove all its small buffers from the free list.
*/
mss = (struct mempool_small_slab *)mh;
msb = mss->mss_buf;
for (count = 0; count < MEMPOOL_SMALL_COUNT; count++, msb++) {
assert(msb->msb_header == NULL);
assert(msb->msb_header2 == mh);
TAILQ_REMOVE(&mempool_small_dynamic_freelist, msb,
msb_next);
}
/* Then, fall through to the large-buffer free code. */
ptr = (void *)((char *)mh - sizeof(mh));
memcpy(&mh, ptr, sizeof(mh));
assert(mh != NULL);
assert(!(mh->mh_flags & MHF_SMALL));
}
/*
* Move the large buffer onto the free list of the large slab to which
* it belongs.
*/
mls = (struct mempool_large_slab *)mh;
mlb = (struct mempool_large_buf *)ptr;
mlb->mlb_header2 = &mls->mls_header;
mlb->mlb_header = NULL;
LIST_INSERT_HEAD(&mls->mls_free, mlb, mlb_next);
/*
* Adjust accounting for the large slab, which may involve moving it
* to another list.
*/
assert(mls->mls_header.mh_inuse > 0);
mls->mls_header.mh_inuse--;
if (mls->mls_header.mh_inuse == 0) {
LIST_REMOVE(mls, mls_next);
LIST_INSERT_HEAD(&mempool_empty_slabs, mls, mls_next);
mls->mls_header.mh_flags &= ~MHF_MARKED;
} else if (mls->mls_header.mh_inuse == MEMPOOL_LARGE_COUNT - 1) {
LIST_REMOVE(mls, mls_next);
LIST_INSERT_HEAD(&mempool_partial_slabs, mls, mls_next);
}
assert(mempool_used_large > 0);
mempool_used_large--;
}
/*
* Memory pool wrapper function for calloc() calls from lwIP.
*/
void *
mempool_calloc(size_t num, size_t size)
{
void *ptr;
size_t total;
/*
* Standard overflow check. This can be improved, but it doesn't have
* to be, because in practice lwIP never calls calloc() anyway.
*/
if (num > 0 && size > 0 && (size_t)-1 / size < num)
return NULL;
total = num * size;
if ((ptr = mempool_malloc(total)) == NULL)
return NULL;
memset(ptr, 0, total);
return ptr;
}
0