/*....................................................................... * Delete a HashTable free-list. An error will be displayed if the list is * still in use and the deletion will be aborted. * * Input: * mem HashMemory * The free-list container to be deleted. * force int If force==0 then _del_HashMemory() will complain * and refuse to delete the free-list if any * of nodes have not been returned to the free-list. * If force!=0 then _del_HashMemory() will not check * whether any nodes are still in use and will * always delete the list. * Output: * return HashMemory * Always NULL (even if the memory could not be * deleted). */ HashMemory *_del_HashMemory(HashMemory *mem, int force) { if(mem) { if(!force && (_busy_FreeListNodes(mem->hash_memory) > 0 || _busy_FreeListNodes(mem->node_memory) > 0)) { errno = EBUSY; return NULL; }; mem->hash_memory = _del_FreeList(mem->hash_memory, force); mem->node_memory = _del_FreeList(mem->node_memory, force); mem->string_memory = _del_StringMem(mem->string_memory, force); free(mem); }; return NULL; }
/*....................................................................... * Create a string free-list container and the first block of its free-list. * * Input: * blocking_factor int The blocking_factor argument specifies how * many strings of length SM_STRLEN * bytes (see stringmem.h) are allocated in each * free-list block. * For example if blocking_factor=64 and * SM_STRLEN=16, then each new * free-list block will take 1K of memory. * Output: * return StringMem * The new free-list container, or NULL on * error. */ StringMem *_new_StringMem(unsigned blocking_factor) { StringMem *sm; /* The container to be returned. */ /* * Check arguments. */ if(blocking_factor < 1) { errno = EINVAL; return NULL; }; /* * Allocate the container. */ sm = (StringMem *) malloc(sizeof(StringMem)); if(!sm) { errno = ENOMEM; return NULL; }; /* * Before attempting any operation that might fail, initialize * the container at least up to the point at which it can safely * be passed to _del_StringMem(). */ sm->nmalloc = 0; sm->fl = NULL; /* * Allocate the free-list. */ sm->fl = _new_FreeList(SM_STRLEN, blocking_factor); if(!sm->fl) return _del_StringMem(sm, 1); /* * Return the free-list container. */ return sm; }