EXTERN void wn_mksklist ( wn_sklist *psklist, double threshold, int (*pcompare_keys_func)(ptr key1,ptr key2), void (*palloc_copy_key_func)(ptr *pkey,ptr key), void (*pfree_key_func)(ptr key) ) { *psklist = (wn_sklist) wn_alloc(sizeof(struct wn_sklist_struct)); wn_assert(1.0 > threshold); wn_assert(0.0 < threshold); (*psklist)->threshold = (int) (threshold * (1<<LO_SKIP_RAND_BITS)); (*psklist)->threshold = wn_max((*psklist)->threshold, 0); (*psklist)->threshold = wn_min((*psklist)->threshold, (1<<LO_SKIP_RAND_BITS)-2); (*psklist)->max_levels = 1000; /* for now */ (*psklist)->pcompare_keys_func = pcompare_keys_func; (*psklist)->palloc_copy_key_func = palloc_copy_key_func; (*psklist)->pfree_key_func = pfree_key_func; (*psklist)->handle = (wn_skhandle) wn_alloc(WN_SKHANDLE_SIZE(1)); (*psklist)->ptr_count = 1; (*psklist)->handle->key = NULL; WN_SKIP_NEXTS((*psklist)->handle)[0] = NULL; (*psklist)->group = wn_curgp(); } /* wn_mksklist */
local void lo_calculate_handle_level_from_children( wn_mbhandle _handle2 ) { int _left_level,_right_level; wn_mbhandle _left_child,_right_child; _left_child = (_handle2)->left_child; _right_child = (_handle2)->right_child; _left_level = lo_handle_tree_level(_left_child); _right_level = lo_handle_tree_level(_right_child); (_handle2)->level = wn_max(_left_level,_right_level) + 1; }
local void lo_verify_this_handle_tree ( wn_mbhandle handle_tree, wn_mbhandle parent, int (*pcompare_keys_func)(ptr key1,ptr key2) ) { int left_count,right_count,left_level,right_level; wn_assert(handle_tree->parent == parent); wn_assert(handle_tree->level >= 1); wn_assert(handle_tree->count >= 1); left_count = lo_handle_tree_count(handle_tree->left_child); right_count = lo_handle_tree_count(handle_tree->right_child); left_level = lo_handle_tree_level(handle_tree->left_child); right_level = lo_handle_tree_level(handle_tree->right_child); wn_assert(handle_tree->count == left_count+right_count+1); wn_assert(handle_tree->level == wn_max(left_level,right_level)+1); wn_assert(wn_abs(left_level-right_level) <= 2); if(handle_tree->left_child != NULL) { wn_assert( (*pcompare_keys_func)(handle_tree->left_child->key, handle_tree->key) <= 0 ); wn_assert(handle_tree->left_child->parent == handle_tree); } if(handle_tree->right_child != NULL) { wn_assert( (*pcompare_keys_func)(handle_tree->key, handle_tree->right_child->key) <= 0 ); wn_assert(handle_tree->right_child->parent == handle_tree); } } /* lo_verify_this_handle_tree */
local void get_more_memory(int size,wn_memgp group) { wn_mem_block block; int size_to_get,size_to_alloc; if(group->current_block != NULL) { group->current_block->leftover += group->block_mem_left; } size_to_get = wn_max(size, group->block_size); /* we add in 4 here in case we have to realign the block_ptr */ size_to_alloc = size_to_get + 4 + sizeof(struct wn_mem_block_struct); block = (wn_mem_block)wn_system_alloc(size_to_alloc); wn_assert(sizeof(ptr) < 8 || !(((long unsigned) block) & 7)); block->next = group->current_block; group->current_block = block; block->leftover = 0; block->size = size_to_alloc; group->block_ptr = WN_MEM_BLOCK_MEMORY(block); group->block_end_ptr = (ptr)((char*)block + size_to_alloc); group->block_mem_left = (char *) group->block_end_ptr - /**/ (char *) group->block_ptr; /* make sure block_ptr is 8 bit aligned. (Note under valgrind on linux, ** malloc sometimes returns non 8-byte aligned segments.) */ if ((unsigned long)group->block_ptr & 0x7) { group->block_ptr = (ptr) ((char *)group->block_ptr + 4); group->block_mem_left -= 4; block->leftover += 4; } wn_assert(!((unsigned long)group->block_ptr & 0x7)); wn_assert(!((unsigned long)group->block_end_ptr & 0x3)); wn_assert(group->block_mem_left >= size_to_get); } /* get_more_memory */