MCPcopy Create free account
hub / github.com/GJDuck/e9patch / pool_create

Function pool_create

examples/stdlib.c:1744–1778  ·  view source on GitHub ↗

* Create a malloc() pool. Here, flags are mmap() flags. */

Source from the content-addressed store, hash-verified

1742 * Create a malloc() pool. Here, flags are mmap() flags.
1743 */
1744static struct malloc_pool_s *pool_create(int flags, void *base,
1745 size_t lb, size_t ub)
1746{
1747 if ((uintptr_t)base % MA_PAGE_SIZE != 0)
1748 {
1749 errno = EINVAL;
1750 return NULL;
1751 }
1752 ub += (ub == 0? MA_PAGE_SIZE: 0);
1753 ub += (ub % MA_PAGE_SIZE != 0? MA_PAGE_SIZE - ub % MA_PAGE_SIZE: 0);
1754 lb = (lb > ub? ub: lb);
1755
1756 int fixed = (base != NULL? MAP_FIXED: 0x0);
1757 void *ptr = mmap(base, ub, PROT_NONE,
1758 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | fixed, -1, 0);
1759 if (ptr == MAP_FAILED)
1760 return NULL;
1761
1762 flags |= MAP_ANONYMOUS;
1763 struct malloc_pool_s *pool = (struct malloc_pool_s *)base;
1764 ptr = mmap(base, lb, PROT_READ | PROT_WRITE, flags | MAP_FIXED, -1, 0);
1765 if (ptr != (void *)pool)
1766 {
1767 (void)munmap(ptr, ub);
1768 errno = ENOMEM;
1769 return NULL;
1770 }
1771 mutex_init(&pool->mutex);
1772 pool->base = (uint8_t *)pool;
1773 pool->mmap = (MA_PAGE_SIZE * MA_PAGES(lb)) / MA_UNIT;
1774 pool->end = (MA_PAGE_SIZE * MA_PAGES(ub)) / MA_UNIT;
1775 pool->root = MA_NIL;
1776 pool->flags = flags;
1777 return pool;
1778}
1779
1780/*
1781 * Destroy a malloc() pool.

Callers

nothing calls this directly

Calls 3

mmapFunction · 0.85
munmapFunction · 0.85
mutex_initFunction · 0.85

Tested by

no test coverage detected