Пример #1
0
int main (int argc, char* argv[]) 
{
  int objs[2*N+1];
  int i;

  objs[0]=N;
  for(i=1; i<=N;i++){
    objs[2*i-1]=N+i;
    objs[2*i]=N-i;
  }
  random_permutation(objs, 2*N+1);

  printf("Test de heap_remove_min\n");

  /********************************/
  heap h=prof_heap_create(f);

  for(i=0; i<=2*N; i++)
    h=prof_heap_insert(h, &objs[i]);

  heap_dump(h);

  for(i=0; i<=2*N; i++){
    h=heap_remove_min(h);
    heap_dump(h);
  }

  prof_heap_destroy(h);

  return 0;  
}
Пример #2
0
void main()
{
    char *p;
    heap_dump();   p = (char *) malloc( 80 );
    heap_dump();   free( p );
    heap_dump();
}
Пример #3
0
static void
dump_process_info(int to, void *to_arg, Process *p)
{
    Eterm* sp;
    ErlMessage* mp;
    int yreg = -1;

    ERTS_SMP_MSGQ_MV_INQ2PRIVQ(p);

    if ((p->trace_flags & F_SENSITIVE) == 0 && p->msg.first) {
	erts_print(to, to_arg, "=proc_messages:%T\n", p->id);
	for (mp = p->msg.first; mp != NULL; mp = mp->next) {
	    Eterm mesg = ERL_MESSAGE_TERM(mp);
	    if (is_value(mesg))
		dump_element(to, to_arg, mesg);
	    else
		dump_dist_ext(to, to_arg, mp->data.dist_ext);
	    mesg = ERL_MESSAGE_TOKEN(mp);
	    erts_print(to, to_arg, ":");
	    dump_element(to, to_arg, mesg);
	    erts_print(to, to_arg, "\n");
	}
    }

    if ((p->trace_flags & F_SENSITIVE) == 0) {
	if (p->dictionary) {
	    erts_print(to, to_arg, "=proc_dictionary:%T\n", p->id);
	    erts_deep_dictionary_dump(to, to_arg,
				      p->dictionary, dump_element_nl);
	}
    }

    if ((p->trace_flags & F_SENSITIVE) == 0) {
	erts_print(to, to_arg, "=proc_stack:%T\n", p->id);
	for (sp = p->stop; sp < STACK_START(p); sp++) {
	    yreg = stack_element_dump(to, to_arg, p, sp, yreg);
	}

	erts_print(to, to_arg, "=proc_heap:%T\n", p->id);
	for (sp = p->stop; sp < STACK_START(p); sp++) {
	    Eterm term = *sp;
	    
	    if (!is_catch(term) && !is_CP(term)) {
		heap_dump(to, to_arg, term);
	    }
	}
	for (mp = p->msg.first; mp != NULL; mp = mp->next) {
	    Eterm mesg = ERL_MESSAGE_TERM(mp);
	    if (is_value(mesg))
		heap_dump(to, to_arg, mesg);
	    mesg = ERL_MESSAGE_TOKEN(mp);
	    heap_dump(to, to_arg, mesg);
	}
	if (p->dictionary) {
	    erts_deep_dictionary_dump(to, to_arg, p->dictionary, heap_dump);
	}
    }
}
Пример #4
0
static void heap_test(void)
{
	void *ptr[16];

	ptr[0] = heap_alloc(8, 0);
	ptr[1] = heap_alloc(32, 0);
	ptr[2] = heap_alloc(7, 0);
	ptr[3] = heap_alloc(0, 0);
	ptr[4] = heap_alloc(98713, 0);
	ptr[5] = heap_alloc(16, 0);

	heap_free(ptr[5]);
	heap_free(ptr[1]);
	heap_free(ptr[3]);
	heap_free(ptr[0]);
	heap_free(ptr[4]);
	heap_free(ptr[2]);

	heap_dump();

	int i;
	for (i=0; i < 16; i++)
		ptr[i] = 0;

	for (i=0; i < 32768; i++) {
		unsigned int index = (unsigned int)rand() % 16;

		if ((i % (16*1024)) == 0)
			printf("pass %d\n", i);

//		printf("index 0x%x\n", index);
		if (ptr[index]) {
//			printf("freeing ptr[0x%x] = %p\n", index, ptr[index]);
			heap_free(ptr[index]);
			ptr[index] = 0;
		}
		unsigned int align = 1 << ((unsigned int)rand() % 8);
		ptr[index] = heap_alloc((unsigned int)rand() % 32768, align);
//		printf("ptr[0x%x] = %p, align 0x%x\n", index, ptr[index], align);

		DEBUG_ASSERT(((addr_t)ptr[index] % align) == 0);
//		heap_dump();
	}

	for (i=0; i < 16; i++) {
		if (ptr[i])
			heap_free(ptr[i]);
	}

	heap_dump();
}
Пример #5
0
int
main(int argc, char *argv[])
{
	heap_t * h;
	double v = 12.34;

	h = heap_alloc(8, (heap_free_elt_fn_t) NULL);
	heap_dump(h);
	heap_insert(h, 10.0, &v);
	heap_dump(h);
	heap_free(h);

	return 0;
}
Пример #6
0
static void
heap_dump(heapnode_t * top, int32 level)
{
    int32 i;

    if (!top)
        return;

    for (i = 0; i < level; i++)
        printf("  ");
    /* print top info */
    heap_dump(top->l, level + 1);
    heap_dump(top->r, level + 1);
}
Пример #7
0
static int cmd_heap(int argc, const cmd_args *argv)
{
	if (argc < 2) {
notenoughargs:
		printf("not enough arguments\n");
usage:
		printf("usage:\n");
		printf("\t%s info\n", argv[0].str);
		printf("\t%s alloc <size> [alignment]\n", argv[0].str);
		printf("\t%s free <address>\n", argv[0].str);
		return -1;
	}

	if (strcmp(argv[1].str, "info") == 0) {
		heap_dump();
	} else if (strcmp(argv[1].str, "alloc") == 0) {
		if (argc < 3) goto notenoughargs;

		void *ptr = heap_alloc(argv[2].u, (argc >= 3) ? argv[3].u : 0);
		printf("heap_alloc returns %p\n", ptr);
	} else if (strcmp(argv[1].str, "free") == 0) {
		if (argc < 2) goto notenoughargs;

		heap_free((void *)argv[2].u);
	} else {
		printf("unrecognized command\n");
		goto usage;
	}

	return 0;
}
Пример #8
0
void test_gc_with_cycle()
{
  Node *a, *b;
  char *result, *expected;

  GC_SAVE_RP;
  gc();

  a = new_Node("a", NULL);
  b = new_Node("b", a);
  a->neighbor = b;

  ADD_ROOT(a);
  ADD_ROOT(b);

  ASSERT(align(2 * Node_type.size), heap_allocated());

  gc();

  expected = "heap2[64,1000]\n"
             "0000:Node[32]->[32]\n"
             "0032:Node[32]->[0]\n";
  result = calloc(1000, sizeof(char));
  heap_dump(result);
  ASSERT_STR(expected, result);
//  printf("%s\n", result);
  free(result);

  ASSERT(align(2 * Node_type.size), heap_allocated());
  ASSERT_STR("a", b->neighbor->payload);
  ASSERT_STR("b", a->neighbor->payload);
  GC_RESTORE_RP;
}
Пример #9
0
void test_heap_dump()
{
  char *expected, *result;
  User *u;
  String *s;
  Array *a;
  GC_SAVE_RP;

  gc();

  s = new_String("tim");
  u = new_User(103, s);
  a = (Array *) alloc_Array(2, ARRAY_POINTER);
  ((void **) a->elements)[0] = s;
  ((void **) a->elements)[1] = u;

  ADD_ROOT(s);
  ADD_ROOT(u);
  ADD_ROOT(a);

  expected = "heap2[116,1000]\n"
             "0000:String[32+4]=\"tim\"\n"
             "0036:User[32]->[0]\n"
             "0068:Array[32+2]->[0, 36]\n";

  result = calloc(1000, sizeof(char));
  heap_dump(result);

//  printf("\n%s", result);
  ASSERT_STR(expected, result);

  GC_RESTORE_RP;
  free(result);
}
Пример #10
0
static int execute_built_in(const unsigned char *word, int word_len) {
  int i, j;

  if(counted_string_equal(word, word_len, "macro", -1)) {
    ctx.is_macro=1;
  } else if(counted_string_equal(word, word_len, "forth", -1)) {
    ctx.is_macro=0;
  } else if(counted_string_equal(word, word_len, "unsmudge", -1)) {
    ctx.dictionary->smudged=0;
  } else if(counted_string_equal(word, word_len, "smudge", -1)) {
    ctx.dictionary->smudged=1;
  } else if(counted_string_equal(word, word_len, "heap-dump", -1)) {
    heap_dump();
  } else if(counted_string_equal(word, word_len, "word-dump", -1)) {
    word_dump();
  } else if(counted_string_equal(word, word_len, "b,", -1)) {
    (*ctx.code_here)=dstack_pop();
    ctx.code_here++;
  } else if(counted_string_equal(word, word_len, "windows?", -1)) {
#ifdef _WIN32
    dstack_push(1);
#else
    dstack_push(0);
#endif
  } else if(counted_string_equal(word, word_len, "load", -1)) {
    load(dstack_pop());
  } else if(counted_string_equal(word, word_len, "thru", -1)) {
    j=dstack_pop();
    i=dstack_pop();
    for(;i<=j;i++) load(i);
  } else {
    return 0;
  }
  return 1;
}
Пример #11
0
int main( )
{
    int rc = 0;
    //heap_dump();
    int original_count = heap_count( );
    
    try {
        if( !construct_test( )  || !heap_ok( "t1" ) ) rc = 1;
        if( !access_test( )     || !heap_ok( "t2" ) ) rc = 1;
        //if( !string_test( )     || !heap_ok( "t3" ) ) rc = 1;
        //if( !torture_test( )    || !heap_ok( "t4" ) ) rc = 1;
        //if( !clear_test( )      || !heap_ok( "t5" ) ) rc = 1;
        if( !iterator_test( )   || !heap_ok( "t6" ) ) rc = 1;
        if( !copy_test( )       || !heap_ok( "t7" ) ) rc = 1;
        if( !allocator_test( )  || !heap_ok( "t8" ) ) rc = 1;
        if( !bounds_test( )     || !heap_ok( "t9" ) ) rc = 1;
        if( !hint_ins_test( )   || !heap_ok( "tA" ) ) rc = 1;
    }
    catch( ... ) {
        std::cout << "Unexpected exception of unexpected type.\n";
        rc = 1;
    }
    int heap_diff = heap_count( ) - original_count;
    if( heap_diff ) {
        heap_dump();
        std::cout << "Possible memory leak! " << heap_diff << " " << original_count << "\n";
        rc = 1;
    }
    
    return( rc );
}
Пример #12
0
void heap_validate(const char* file, const int lineno) {
    //	consout("%04x !\n", context->programCounter);
    size_t used_m = 0;
    size_t free_m = 0;

    size_t free_l = 0;

    // Traverse through heap memory:
    size_t offset = 0;
    while (offset < heap_size) {
        header_t* h = offset_header(heap, offset);

        if (is_type(h, HT_FREE)) {
            free_m += h->e.size;
        } else if (is_type(h, HT_USED) || is_type(h, HT_PROTECTED)) {
            used_m += h->e.size;
        } else {
            consout("Heap corrupted (magic)\n");
            heap_dump();
            heap_exit(file, lineno);
        }

        // Advance pointer using size of element:
        offset += h->e.size;
    }

    if (used_m + free_m != heap_size) {
        consout("Heap corrupted (size)\n");
        heap_exit(file, lineno);
    }

    // Validate free list:
    if (free_list != NULL) {
        header_t* h = free_list;
        header_t* prev = NULL;

        free_l = 0;
        while (h != NULL) {
            if (is_type(h, HT_FREE)) {
                free_l += h->e.size;
            } else {
                consout("Heap corrupted (free magic)\n");
                heap_exit(file, lineno);
            }
            if (prev != NULL && h < prev) {
                consout("Heap corrupted (free list order)\n");
                heap_exit(file, lineno);
            }
            prev = h;
            h = h->e.next;
        }
    }

    if (free_l != free_m) {
        consout("Heap corrupted (free size)\n");
        heap_exit(file, lineno);
    }
}
Пример #13
0
static int merge(struct w_heap* heap){

	uint32 brk = 0;
	struct w_listnode* it1 = FIRST_NODE(heap->block_list_head);
	struct w_listnode* it2 = it1;

	while(it1->next){

		struct w_block* b1 = LIST_ENTRY(it1, struct w_block, block_node);
		it2 = it1->next;

		while(it2){

			struct w_block* b2 = LIST_ENTRY(it2, struct w_block, block_node);

			/* List is unsorted... figure out block order */

			if(b2->start > b1->start){

				/* block 1 then block 2 */

				if(b1->end + HEAP_OVERHEAD + 1 >= b2->start){
					printf("HEAP MERGE\n");
					b1->end = b2->end;
					list_remove( &heap->block_list_head, it2);
					zero(b2, sizeof(struct w_block));
					brk = 1;
					break;
				}
			}
			else{

				/* block 2 then block 1 */

				if(b2->end + HEAP_OVERHEAD + 1 >= b1->start){
					printf("HEAP MERGE\n");
					b2->end = b1->end;
					list_remove( &heap->block_list_head, it1);
					zero(b1, sizeof(struct w_block));
					brk = 1;
					break;
				}
			}

			it2 = it2->next;
		}

		if(brk){
			heap_dump(heap);
			return 1;
		}

		it1 = NEXT_NODE(it1);
	}

	return 0;
}
Пример #14
0
void test_gc_with_pointer_array()
{
  User *u, *t;
  String *s;
  Array *a;
  char *expected, *result_before, *result_after;

  gc();
  GC_SAVE_RP;
  s = new_String("tim");
  u = new_User(102, s);
  a = new_Array(2, ARRAY_POINTER);
  add_to(a, 0, s);
  add_to(a, 1, u);

  ADD_ROOT(a);

  expected = "heap1[116,1000]\n"
             "0068:Array[32+2]->[0, 36]\n"
             "0000:String[32+4]=\"tim\"\n"
             "0036:User[32]->[0]\n";
  result_before = calloc(1000, sizeof(char));
  heap_dump(result_before);
  ASSERT_STR(expected, result_before);
  free(result_before);

  gc();

  t = new_User(102, NULL);
  ADD_ROOT(t);

  expected = "heap2[148,1000]\n"
             "0000:Array[32+2]->[48, 84]\n"
             "0048:String[32+4]=\"tim\"\n"
             "0084:User[32]->[48]\n"
             "0116:User[32]->[-1]\n";
  result_after = calloc(1000, sizeof(char));
  heap_dump(result_after);
  ASSERT_STR(expected, result_after);
//  printf("\n%s", result_after);
  free(result_after);

  GC_RESTORE_RP;
}
Пример #15
0
void heap_dump(const heap* h, const unsigned int from) {
    if(from == 0) {
        printf("digraph g {\nnode [shape = record,height=.1];\n");
    }
    if(from < h->count) {
        printf("node%d[ label = \"<f0> | <f1> %s | <f2>\"];\n", from, h->g->nodes[h->nodes[from]].value);

        if(HEAP_LEFT(from) < h->count) {
            printf("\"node%d\":f0 -> \"node%d\":f1\n", from, HEAP_LEFT(from));
            heap_dump(h, HEAP_LEFT(from));
        }
    
        if(HEAP_RIGHT(from) < h->count) {
            printf("\"node%d\":f2 -> \"node%d\":f1\n", from, HEAP_RIGHT(from));
            heap_dump(h, HEAP_RIGHT(from));
        }
    }
    
    if(from == 0) { 
        printf("}\n");
    }
}
Пример #16
0
int main (int argc, char* argv[]) 
{
  printf("Test de heap_destroy\n");

  heap h=prof_heap_create(f);

  int a=1;
  h=prof_heap_insert(h, &a);

  heap_dump(h);

  heap_destroy(h);
  return 0;  
}
Пример #17
0
static int cmd_heap(int argc, const cmd_args *argv)
{
	if (argc < 2) {
		printf("not enough arguments\n");
		return -1;
	}

	if (strcmp(argv[1].str, "info") == 0) {
		heap_dump();
	} else {
		printf("unrecognized command\n");
		return -1;
	}

	return 0;
}
Пример #18
0
void test_gc_user()
{
  char *expected, *result;
  void *old_a;
  User *a; 
  String *b;
  int userlen, strlen;

  GC_SAVE_RP;

  b = new_String("tim");
  a = new_User(103, b);
  old_a = &*a;

  ADD_ROOT(a);
  ADD_ROOT(b);

  ASSERT_NE(User_type.size, heap_allocated());

  gc();

  expected = "heap1[68,1000]\n"
             "0000:User[32]->[32]\n"
             "0032:String[32+4]=\"tim\"\n";
  result = calloc(1000, sizeof(char));
  heap_dump(result);
  ASSERT_STR(expected, result);
//  printf("%s\n", result);
  free(result);

  userlen = align(User_type.size);
  strlen = String_type.size + 3 + 1;
 
  ASSERT(align(userlen + strlen), heap_allocated());
  ASSERT_NE(old_a, &*a);
  ASSERT(103, a->id);
  ASSERT(&User_type, a->type);
  ASSERT_STR("tim", a->name->elements);
  ASSERT(NULL, a->forward);
  GC_RESTORE_RP;
}
Пример #19
0
void test_gc_string()
{
  char *expected, *result;
  void *old_a, *old_b;
  String *a, *b;
  int a_len;
  GC_SAVE_RP;
  gc();

  a = new_String("abcd");
  old_a = &*a; 

  b = alloc_String(5);
  old_b = &*b;

  ADD_ROOT(a);

  a_len = sizeof_array(4, ARRAY_CHAR);

  ASSERT_NE(a_len, heap_allocated());

  gc();

  expected = "heap2[40,1000]\n"
             "0000:String[32+5]=\"abcd\"\n";
  result = calloc(1000, sizeof(char));
  heap_dump(result);
  ASSERT_STR(expected, result);
//  printf("%s\n", result);
  free(result);

  ASSERT(align(a_len), heap_allocated());
  ASSERT_NE(old_a, &*a);
  ASSERT(old_b, &*b);
  ASSERT_STR("Array", a->type->name);
  ASSERT(4, a->length);
  ASSERT_STR("abcd", a->elements);
  GC_RESTORE_RP;
}
Пример #20
0
void* kalloc(uint32 size){

	struct w_heap* heap = &kern_heap;
	struct w_listnode* it = FIRST_NODE(heap->block_list_head);
	struct w_block* blk;

	/* We need a new list node which holds a block pointer.
	 * This will allow us to add to free list once freed
	 */

	uint32 eff_size = size + HEAP_OVERHEAD;


	while( it != NULL){

		blk = LIST_ENTRY(it, struct w_block, block_node);

		if((blk->end - blk->start) > eff_size){

			/* Found a big enough block! */

			break;
		}

		it = it->next;
	}

	if(it == NULL){

		/* Not enough contiguous memory available */

        return NULL;
	}

	/* Remove this node from the free list */

	struct w_listnode* alloc_node = list_remove( &heap->block_list_head, it);

	uint32 start = blk->start;
	blk->start += eff_size;

	/* First initizlize the block */

	struct w_block* nblock = (struct w_block*)(start + size);
	nblock->start = start;
	nblock->end = start + size;

	/* swap the two nodes to keep descriptors in order */

	//struct w_listnode temp = nblock->block_node;
	//nblock->block_node = *alloc_node;
	//*alloc_node = temp;

	uint32 t1,t2;
	t1 = nblock->start;
	t2 = nblock->end;

	nblock->start = blk->start;
	nblock->end = blk->end;
	blk->start = t1;
	blk->end = t2;

    /* Is there still enough room for another allocation? */

	if((blk->end - blk->start) > 0)
		list_add( &heap->block_list_head, &nblock->block_node);

	//list_remove(&heap->block_list_head, &nblock->block_node);
	//list_add(&heap->block_list_head, alloc_node);

	heap_dump(heap);

	return (void*)start;
}
Пример #21
0
static void
semi_greedy(data *d, int start, int *tour, int *tour_length)
{
    int i, j, k;
    int dim = d->numnodes;
    int current, len, dist;
    int *visited = MALLOC(dim, int);
    /* RCL. */
    struct heapelm *candidates = MALLOC(dim, struct heapelm);
    int rclsize, rclused;
    double total_bias, rprob, racc, rpick;

    memset(visited, 0, dim * sizeof(int));

    len = 0;
    current = start;
    for (i = 0; i < dim - 1; i++) {
        visited[current] = 1;
        tour[i] = current;

        /* Define RCL size. */
        rclused = 0;
        if (RANDOM_UNIT() < 0.05) rclsize = dim - i - 2;
        else rclsize = RANDOM(((dim - i - 2) / 4) + 1);
        rclsize++;

        DEBUG("RCL size: %d\n", rclsize);

        /* Define RCL. */
        for (j = 0; j < dim; j++) {
            if (!visited[j]) {
                dist = d->dist(d, current, j);
                if (rclused < rclsize) {
                    candidates[rclused].num = j;
                    candidates[rclused].cost = dist;
                    rclused++;

                    if (rclused == rclsize) {
                        heap_build_max(candidates, rclsize);
                    }
                } else if (dist < candidates[0].cost) {
                    heap_replace_root(candidates, rclsize, j, dist);
                }
            }
        }

        DEBUG("RCL used: %d\n", rclused);
        heap_dump(candidates, rclused);

        /* Pick RCL element based on logarithmic bias. */
#define BIAS_LOG(r) (1/log((r)+1))
#define BIAS_LINEAR(r) (1./(r))
#define BIAS_EXP(r) (1/exp(r))
#define BIAS_RANDOM(r) 1
#define BIAS(r) BIAS_LOG(r)
        total_bias = 0;
        for (j = 1; j <= rclused; j++) {
            total_bias += BIAS(j);
        }
        racc = 0;
        rpick = RANDOM_UNIT();
#if DEBUGGING
        for (j = rclused; j >= 1; j--) {
            DEBUG("%f ", BIAS(j) / total_bias);
        }
        DEBUG("\nR: %f\n", rpick);
#endif
        for (j = rclused, k = 1; j >= 0; j--, k++) {
            rprob = BIAS(k) / total_bias;
            if (rprob + racc > rpick) {
                break;
            }
            racc += rprob;
        }
        DEBUG("Picked: j = %d, %d %d\n\n", j,
              candidates[j-1].num, candidates[j-1].cost);


        current = candidates[j - 1].num;
        len += candidates[j - 1].cost;
    }
    tour[i] = current;
    len += d->dist(d, current, start);
    *tour_length = len;

    free(visited);
    free(candidates);

#undef BIAS
#undef BIAS_LOG
}
Пример #22
0
int main( int argc, char **argv ){
    FILE                *in;
    char                buff[140];
    char                *cmdline;
    struct heap_stat    before;
    struct heap_stat    after;
    IDEDllHdl   hdl;
    IDEBool     fatal;
    IDEBool     retn;               // - return code
    IDEInitInfo info;

    if( argc < 2 ) {
        printf( "Useage wcc386f fn\n" );
        exit( 1 );
    }
    if( (in = fopen( argv[1], "r"))== NULL ){
        printf( "Coundn't open %s\n", argv[1]  );
        exit( 1 );
    }


//  setmode( STDOUT_FILENO, O_BINARY );
    fatal = FALSE;

    if(  IDE_CUR_DLL_VER != IDEGetVersion() ){
         printf( "invalid DLL version\n" );
    }
    IDEInitDLL( NULL, &callbacks, &hdl );
    info.ver = IDE_CUR_INFO_VER;
    info.ignore_env = FALSE;
    info.cmd_line_has_files = TRUE;
    IDEPassInitInfo( NULL, &info );
    while( fgets( buff, sizeof( buff ), in ) != NULL ){
        char *tmp;
        cmdline = buff;
        printf( "%s", cmdline );
        while( isspace( *cmdline ) )++cmdline; // skip wcc386 whatever
        while( isalnum( *cmdline ) )++cmdline;
        tmp = cmdline;
        while( *tmp != '\0' ){
            if( *tmp == '\n' ){
                *tmp = '\0';
                break;
            }
            ++tmp;
        }
        if( *cmdline == '\0' )continue; // skip blank lines
        heap_size( &before );
        retn = IDERunYourSelf( hdl, cmdline, &fatal );
        heap_size( &after );
        if( before.free != after.free ){
            printf( "Free different\n" );
        }
        if( before.used != after.used ){
            printf( "Used different\n" );
        }
#ifdef HEAP_DUMP
        heap_dump();
#endif
    }
    IDEFiniDLL( NULL );
    fclose( in );
    return( 0 );
}