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

Function free_impl

examples/stdlib.c:2265–2297  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2263}
2264
2265static void free_impl(struct malloc_pool_s *pool, void *ptr, bool lock)
2266{
2267 if (ptr == NULL || ptr == MA_ZERO)
2268 return;
2269
2270 pool = pool_init(pool);
2271 if ((uint8_t *)ptr < pool->base ||
2272 (uint8_t *)ptr >= pool->base + MA_UNIT * (size_t)pool->end)
2273 panic("bad free() detected");
2274 if ((uintptr_t)ptr % MA_UNIT != 0)
2275 panic("bad free() detected");
2276 off_t diff = (uint8_t *)ptr - pool->base - sizeof(struct malloc_node_s);
2277 uint32_t i = (uint32_t)(diff / MA_UNIT);
2278
2279 if (lock && mutex_lock(&pool->mutex) < 0)
2280 panic("failed to acquire malloc() lock");
2281
2282 uint32_t n = pool->root;
2283 while (true)
2284 {
2285 if (n == MA_NIL)
2286 panic("bad free() detected");
2287 if (n == i)
2288 {
2289 if (MA_MAGIC(pool, n) != MA_MAGIC_NUMBER)
2290 panic("bad free() detected");
2291 malloc_remove(pool, n);
2292 if (lock) mutex_unlock(&pool->mutex);
2293 return;
2294 }
2295 n = (i < n? MA_LEFT(pool, n): MA_RIGHT(pool, n));
2296 }
2297}
2298
2299static void *calloc_impl(struct malloc_pool_s *pool, size_t nmemb,
2300 size_t size, bool lock)

Callers 4

realloc_implFunction · 0.85
freeFunction · 0.85
free_unlockedFunction · 0.85
pool_freeFunction · 0.85

Calls 4

pool_initFunction · 0.85
mutex_lockFunction · 0.85
malloc_removeFunction · 0.85
mutex_unlockFunction · 0.85

Tested by

no test coverage detected