void mem_put (struct mem_pool *pool, void *ptr) { struct list_head *list = NULL; int *in_use = NULL; void *head = NULL; if (!pool || !ptr) { gf_log ("mem-pool", GF_LOG_ERROR, "invalid argument"); return; } LOCK (&pool->lock); { switch (__is_member (pool, ptr)) { case 1: list = head = mem_pool_ptr2chunkhead (ptr); in_use = (head + GF_MEM_POOL_LIST_BOUNDARY); if (!is_mem_chunk_in_use(in_use)) { gf_log_callingfn ("mem-pool", GF_LOG_CRITICAL, "mem_put called on freed ptr %p of mem " "pool %p", ptr, pool); break; } pool->hot_count--; pool->cold_count++; *in_use = 0; list_add (list, &pool->list); break; case -1: /* For some reason, the address given is within * the address range of the mem-pool but does not align * with the expected start of a chunk that includes * the list headers also. Sounds like a problem in * layers of clouds up above us. ;) */ abort (); break; case 0: /* The address is outside the range of the mem-pool. We * assume here that this address was allocated at a * point when the mem-pool was out of chunks in mem_get * or the programmer has made a mistake by calling the * wrong de-allocation interface. We do * not have enough info to distinguish between the two * situations. */ FREE (ptr); break; default: /* log error */ break; } } UNLOCK (&pool->lock); }
void mem_put (void *ptr) { struct list_head *list = NULL; int *in_use = NULL; void *head = NULL; struct mem_pool **tmp = NULL; struct mem_pool *pool = NULL; if (!ptr) { gf_msg_callingfn ("mem-pool", GF_LOG_ERROR, EINVAL, LG_MSG_INVALID_ARG, "invalid argument"); return; } list = head = mem_pool_ptr2chunkhead (ptr); tmp = mem_pool_from_ptr (head); if (!tmp) { gf_msg_callingfn ("mem-pool", GF_LOG_ERROR, 0, LG_MSG_PTR_HEADER_CORRUPTED, "ptr header is corrupted"); return; } pool = *tmp; if (!pool) { gf_msg_callingfn ("mem-pool", GF_LOG_ERROR, 0, LG_MSG_MEMPOOL_PTR_NULL, "mem-pool ptr is NULL"); return; } LOCK (&pool->lock); { switch (__is_member (pool, ptr)) { case 1: in_use = (head + GF_MEM_POOL_LIST_BOUNDARY + GF_MEM_POOL_PTR); if (!is_mem_chunk_in_use(in_use)) { gf_msg_callingfn ("mem-pool", GF_LOG_CRITICAL, 0, LG_MSG_MEMPOOL_INVALID_FREE, "mem_put called on freed ptr" " %p of mem pool %p", ptr, pool); break; } pool->hot_count--; pool->cold_count++; *in_use = 0; list_add (list, &pool->list); break; case -1: /* For some reason, the address given is within * the address range of the mem-pool but does not align * with the expected start of a chunk that includes * the list headers also. Sounds like a problem in * layers of clouds up above us. ;) */ abort (); break; case 0: /* The address is outside the range of the mem-pool. We * assume here that this address was allocated at a * point when the mem-pool was out of chunks in mem_get * or the programmer has made a mistake by calling the * wrong de-allocation interface. We do * not have enough info to distinguish between the two * situations. */ pool->curr_stdalloc--; GF_FREE (list); break; default: /* log error */ break; } } UNLOCK (&pool->lock); }