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 */
void wn_grow_2array ( ptr *parray1,ptr *parray2, int *pmemsize, int blocksize1,int blocksize2 ) { ptr new_array; int new_memsize; if((*pmemsize) <= 0) { wn_assert((*pmemsize) == 0); *pmemsize = 1; *parray1 = (ptr)wn_alloc(blocksize1); *parray2 = (ptr)wn_alloc(blocksize2); } else { new_memsize = 2*(*pmemsize); new_array = (ptr)wn_alloc(new_memsize*blocksize1); wn_memcpy(new_array,*parray1,(*pmemsize)*blocksize1); wn_free(*parray1); *parray1 = new_array; new_array = (ptr)wn_alloc(new_memsize*blocksize2); wn_memcpy(new_array,*parray2,(*pmemsize)*blocksize2); wn_free(*parray2); *parray2 = new_array; *pmemsize = new_memsize; } }
void lo_alloc_a_block() { static int sizes[] = { 5200, 5300, 5400, 5500, 5600, /**/ 2500, 3000, 3100, 3252, 5140, /**/ 500, 700, 1000, 2000, 2400, /**/ 100, 200, 260, 301, 405, /**/ 96, 92, 88, 84, 80, /**/ 76, 72, 68, 64, 60 }; int size; int r; int i; if (lo_alloc_count == LO_BLOCK_COUNT) { return; } do { r = (unsigned) rand() >> 1; size = sizes[r % lo_sizes_to_alloc]; } while (!size); do { r = (unsigned) rand() >> 1; i = r % LO_BLOCK_COUNT; } while (lo_blocks[i]); lo_blocks[i] = wn_alloc(size); lo_allocated_block_sizes[i] = size; ++ lo_alloc_count; } /* lo_alloc_a_block */
local void test_radix_pint_list_sort(void) { int i,r,*r_copy; wn_sll list,list2,list3; wn_gpmake("no_free"); wn_gplabel("test_radix_pint_list_sort_group"); list = NULL; for(i=0;i<1000;i++) /* sort verification is O(n**2), this number should ** ** not be TOO big. */ { r = wn_random_int_between(0,3000) - 1500; /* choose a number low ** enough that there will be some duplicates, make sure there are ** some -ve numbers */ r_copy = (int *) wn_alloc(sizeof(int)); *r_copy = r; wn_sllins(&list,(ptr)r_copy); } wn_sllcpy(&list2, list); wn_sllcpy(&list3, list); wn_sort_sll(&list2,(int (*)(ptr,ptr))(pintcmp)); wn_verify_pint_list_is_sorted(list, list2); /* degenerate case: hand it a sorted list */ wn_sort_sll(&list2,(int (*)(ptr,ptr))(pintcmp)); wn_verify_pint_list_is_sorted(list, list2); /* degenerate case: hand it a reverse sorted list */ wn_sllrev(&list2); wn_sort_sll(&list2,(int (*)(ptr,ptr))(pintcmp)); wn_verify_pint_list_is_sorted(list, list2); wn_radix_sort_sll(&list3, (char (*)(ptr,int))(int_index), /**/ (int (*)(ptr))(int_len)); wn_verify_pint_list_is_sorted(list, list3); /* degenerate case: hand it a sorted list */ wn_radix_sort_sll(&list3, (char (*)(ptr,int))(int_index), /**/ (int (*)(ptr))(int_len)); wn_verify_pint_list_is_sorted(list, list3); /* degenerate case: hand it a reverse sorted list */ wn_sllrev(&list2); wn_radix_sort_sll(&list3, (char (*)(ptr,int))(int_index), /**/ (int (*)(ptr))(int_len)); wn_verify_pint_list_is_sorted(list, list3); wn_gpfree(); } /* test_radix_pint_list_sort */
void wn_skins(wn_skhandle *phandle, wn_sklist sklist, ptr key) { wn_skhandle prev_handle; /* wn_skhandle new_handle, new_head_handle; ** unused - bchapman 041111 */ int new_ptr_count; int i; for (new_ptr_count = 1; new_ptr_count < sklist->max_levels; ++new_ptr_count) { if (wn_random_n_bits(LO_SKIP_RAND_BITS) > sklist->threshold) { break; } } wn_gppush(sklist->group); *phandle = (wn_skhandle) wn_alloc(WN_SKHANDLE_SIZE(new_ptr_count)); (*(sklist->palloc_copy_key_func))(&(*phandle)->key, key); if (new_ptr_count > sklist->ptr_count) { /* we need a new headnode */ wn_realloc((ptr *) &sklist->handle, WN_SKHANDLE_SIZE(sklist->ptr_count), /**/ WN_SKHANDLE_SIZE( new_ptr_count)); for (i = sklist->ptr_count; i < new_ptr_count; ++i) { WN_SKIP_NEXTS(sklist->handle)[i] = NULL; } sklist->ptr_count = new_ptr_count; } prev_handle = sklist->handle; for (i = sklist->ptr_count - 1; i >= new_ptr_count; --i) { prev_handle = lo_find_prev(prev_handle, sklist, key, i); wn_assert(prev_handle); } for ( ; i >= 0; --i) { prev_handle = lo_find_prev(prev_handle, sklist, key, i); wn_assert(prev_handle); WN_SKIP_NEXTS( *phandle)[i] = WN_SKIP_NEXTS(prev_handle)[i]; WN_SKIP_NEXTS(prev_handle)[i] = *phandle; } /* this is useful for some debugging, but may be overridden by user */ (*phandle)->contents = (ptr) new_ptr_count; wn_gppop(); } /* wn_skins */
void wn_make_mat(double ***pmat,int len_i,int len_j) { int i; *pmat = (double **)wn_alloc(len_i*sizeof(double *)); for(i=0;i<len_i;i++) { wn_make_vect(&((*pmat)[i]),len_j); } }
void wn_grow_array(ptr *parray,int *pmemsize,int blocksize) { ptr new_array; int new_memsize; if((*pmemsize) <= 0) { wn_assert((*pmemsize) == 0); *pmemsize = 1; *parray = (ptr)wn_alloc(blocksize); } else { new_memsize = 2*(*pmemsize); new_array = (ptr)wn_alloc(new_memsize*blocksize); wn_memcpy(new_array,*parray,(*pmemsize)*blocksize); wn_free(*parray); *pmemsize = new_memsize; *parray = new_array; } }
void wn_stracat(char *pout[],char s1[],char s2[]) { int l1,l2; char *out; l1 = strlen(s1); l2 = strlen(s2); *pout = out = (char *)wn_alloc(l1+l2+1); wn_memcpy(out,s1,l1); out += l1; wn_memcpy(out,s2,l2); out += l2; *out = '\0'; }
int main(int argc, char *argv[]) { int i; bool arg_d_dump_segments = FALSE; ptr arg_trap_address_address = NULL; ptr arg_mem_in_group_address = (ptr) 1; bool arg_p_print_all_groups_mem = FALSE; ptr arg_r_mem_to_read_from = (ptr) 1; ptr arg_w_mem_to_write_to = (ptr) 1; ptr arg_array_address = (ptr) 1; int arg_array_index; bool arg_speed_test = FALSE; void *p; char c = '5', d, e; int sts; for (i = 1; i < argc; ++i) { if (!strcmp("-d", argv[i])) { arg_d_dump_segments = TRUE; } else if (!strcmp("-t", argv[i])) { ++i; sts = sscanf(argv[i], "%lx", (long *) &arg_trap_address_address); wn_assert(sts == 1); wn_gp_trap_address(arg_trap_address_address); } else if (!strcmp("-m", argv[i])) { ++i; sts = sscanf(argv[i], "%lx", (long *) &arg_mem_in_group_address); wn_assert(sts == 1); } else if (!strcmp("-p", argv[i])) { arg_p_print_all_groups_mem = TRUE; } else if (!strcmp("-r", argv[i])) { ++i; sts = sscanf(argv[i], "%lx", (long *) &arg_r_mem_to_read_from); wn_assert(sts == 1); } else if (!strcmp("-w", argv[i])) { ++i; sts = sscanf(argv[i], "%lx", (long *) &arg_w_mem_to_write_to); wn_assert(sts == 1); } else if (!strcmp("-arrayread", argv[i])) { ++i; sts = sscanf(argv[i], "%lx", (long *) &arg_array_address); wn_assert(sts == 1); ++i; sts = sscanf(argv[i], "%d", &arg_array_index); wn_assert(sts == 1); } else if (!strcmp("-speedtest", argv[i])) { arg_speed_test = TRUE; if (argc > i+1) { ++i; sts = sscanf(argv[i], "%d", &lo_sizes_to_alloc); wn_assert(sts == 1); wn_assert(lo_sizes_to_alloc > 0); wn_assert(lo_sizes_to_alloc <= 30); } } else { fprintf(stderr, "Unrecognized arg \"%s\"\n", argv[i]); fprintf(stderr, " Recognized args are:\n"); fprintf(stderr, " -d : dump segments\n"); fprintf(stderr, " -t <0x address> : trap address\n"); fprintf(stderr, " -m <0x address> : is address in group?\n"); fprintf(stderr, " -p : print all groups memory\n"); fprintf(stderr, " -r <0x address> : read from location\n"); fprintf(stderr, " -w <0x address> : write to location\n"); fprintf(stderr, " -arrayread <0x array start> <0x array index> :\n"); fprintf(stderr, " array read\n"); fprintf(stderr, " -speedtest <# sizes to alloc>\n"); return 1; } } wn_assert(i == argc); p = malloc(31); /* make a lost segment for leak detection to notice */ p = NULL; wn_gpmake("no_free"); wn_gplabel("Example group"); if (arg_d_dump_segments) { printf("ptr = %8x, size = %d\n", (long) wn_alloc(40), 40); printf("ptr = %8x, size = %d\n", (long) wn_alloc(100), 100); printf("ptr = %8x, size = %d\n", (long) wn_alloc(50), 50); printf("ptr = %8x, size = %d\n", (long) wn_alloc(7), 7); printf("ptr = %8x, size = %d\n", (long) wn_alloc(9), 9); } if (arg_mem_in_group_address != (ptr) 1) { printf("Testing if address 0x%x is in group\n", arg_mem_in_group_address); if (wn_mem_in_group(arg_mem_in_group_address, wn_curgp())) { printf(" It is.\n"); } else { printf(" It isn't.\n"); } } if (arg_p_print_all_groups_mem) { wn_print_all_groups_mem_used(); } if ((ptr) 1 != arg_r_mem_to_read_from) { c = * (char *) arg_r_mem_to_read_from; d = c + 5; e = d - 5; if (e) { printf("e is not zero\n"); } else { printf("e is zero\n"); } } if ((ptr) 1 != arg_w_mem_to_write_to) { * (char *) arg_w_mem_to_write_to = c; } if ((ptr) 1 != arg_array_address) { c = ((char *) arg_array_address)[arg_array_index]; d = c + 5; e = d - 5; if (e) { printf("e is not zero\n"); } else { printf("e is zero\n"); } } if (arg_speed_test) { lo_memory_speed_trial(); } wn_gpfree(); return(0); } /* main */
void wn_make_char_type(char **pchar_type,char chars[]) { *pchar_type = (char *)wn_alloc(NUMBER_OF_CHARS); wn_load_char_type(*pchar_type,chars); }
void wn_make_vect(double **pvect,int len) { *pvect = (double *)wn_alloc(len*sizeof(double)); wn_zero_vect(*pvect,len); }