Ejemplo n.º 1
0
/*
 * Splits a block of memory starting at the address of node,
 * inserting a new node header after the block of memory
 * about to be returned to the user
 * links new node to the original node
 * returns the address to the new node created after the split
 */
MM_node *split_node(MM_node *node, size_t req_size)
{
        if(req_size + NODE_HEADER_SIZE > node->size)
        {
                mm_malloc_had_a_problem();
        }

        size_t new_node_size = node->size - req_size - NODE_HEADER_SIZE;
        MM_node *new_node_addr = (MM_node *)((char *)node + NODE_HEADER_SIZE + req_size);
        MM_node *new_node = construct_node(new_node_addr);

        if(malloc_tail == node)
        {
                malloc_tail = new_node;
        }

        new_node->size = new_node_size;
        new_node->next_free = node->next_free;
        new_node->status = FREE;

        node->size = req_size;
        node->next_free = NULL;
        node->status = USED;


        return new_node;
}
int main()
{
    int i;
    bst_node* root = NULL;
    // 利用原地随机化方法将其打乱
    int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int length = sizeof(array)/sizeof(int);
    /* srand(GetTickCount()); */
    randomize_in_place(array, length);  
    print_array(array, length);  
    for (i = 0; i < length; i++)
    {
	bst_node* y = (bst_node*)malloc(sizeof(bst_node));
    	construct_node(y, array[i]);
    	root = insert_node(root, y);
	/* printf("%x\n", (unsigned int)root); */
    }
    mid_traverse_node(root);
    print_node(max_node(root));
    print_node(min_node(root));
    int s_value;
    scanf("%d", &s_value);
    fflush(stdin);
    while(s_value != -1)
    {
	bst_node* s_node = search_node(root, s_value);
	if (s_node)
	{
	    root = delete_node(s_node);
	}
	else
	{
	    printf("not in the bst tree\n");
	    fflush(stdout);
	}
	length--;
	mid_traverse_node(root);
	scanf("%d", &s_value);
	/* for(i = 0; i<length; i++) */
	/* { */
	/*     int search_key = random(0, length-1); */
	/*     bst_node* current_node = search_node(root, search_key); */
	/*     /\* bst_node* precursor = precursor_node(current_node); *\/ */
	/*     /\* bst_node* successor = successor_node(current_node); *\/ */
	/*     printf("the search key is %d\n", search_key); */
	/*     if(current_node->parent) */
	/* 	printf("the parent of %d is %d\n",current_node->key, current_node->parent->key); */
	/*     fflush(stdout); */
	/*     /\* if(precursor) *\/ */
	/*     /\*     printf("the precursor of %d is %d\n", current_node->key, precursor->key); *\/ */
	/*     /\* if(successor) *\/ */
	/*     /\*     printf("the successor of %d is %d\n", current_node->key, successor->key); *\/ */
	/* } */
    }
    return 0;
}
Ejemplo n.º 3
0
static void
handle_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{ 
    struct pack_cap *cur_packet;

    cur_packet = construct_node(header, packet);

    if (!cur_packet)
    {
        printf("Out of memory\n");
        exit(EXIT_FAILURE);
    } 
}
Ejemplo n.º 4
0
/*
 * Initializes malloc_head / tail from first call to malloc.
 * Mmaps for initial memory. Returns ERROR if mmap fails.
 */
int initialize(size_t req_mem_size)
{
        void *addr;
        req_mem_size = get_mem_size(req_mem_size);

        if ((addr = mmap(NULL, req_mem_size, MMAP_PROT, MMAP_FLAGS, -1, 0)) == MAP_FAILED)
        {
                return ERROR;
        }
        else
        {
                malloc_head = construct_node(addr);
                // the actual first node, so malloc_head will always point to a 'free node'
                malloc_tail = construct_node(addr + NODE_HEADER_SIZE);
                malloc_head->next_free = malloc_tail;
                malloc_head->size = 0;
                malloc_tail->next_free = NULL;
                malloc_tail->size = req_mem_size - 2 * NODE_HEADER_SIZE;

                return SUCCESS;
        }
}
Ejemplo n.º 5
0
// == File To Node Array ==
// This function takes a file pointer and parses it for node data, contructs the
// node structures, and adds them (if they are valid) to the node_array up to
// max_nodes possible nodes.
int file_to_node_array(FILE * input_file, node_t * node_array[], int max_nodes) {
	char line[max_nodes];
	int node_count = 0;
	while (fgets(line, MAX_LINE_SIZE, input_file)) {
		
		
		node_t * node = construct_node(line, node_count);
		if (node) {
			node_array[node_count] = node;//node_array[] points to node
			node_count++;
			
		} else { // node is null
			printf("Line [%d] did not produce a valid node.\n",node_count);
			exit(EXIT_STATUS_BAD_NODE_DATA);
		}
	}
	link_parents(node_array, node_count);
	return node_count; 
}
int main()
{
  // 测试方法:
  // 插入10个元素,遍历该元素,打印
  int i;
  prbtree root;
  nil =(prbtree)malloc(sizeof(rbtree));
  nil->color = BLACK;
  root = nil;
  int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  int length = sizeof(array)/sizeof(int);
  /* srand(GetTickCount()); */
  randomize_in_place(array, length);  
  print_array(array, length);  
  fflush(stdout);
  for (i = 0; i < length; i++)
    {
      prbtree y = (prbtree)malloc(sizeof(rbt_node));
      construct_node(y, array[i]);
      // print_node(y);
      root = rbt_insert_node(root, y);
      printf ("the %dth iteration\n",i + 1);
      mid_traverse_node(root);
      /* print_node(root); */
      /* printf("%x\n", (unsigned int)root); */
    }
  // mid_traverse_node(root);
  // 测试删除操作
  randomize_in_place(array, length);
  print_array(array, length);
  fflush(stdout);
  for (i = 0; i < length; ++i)
  {
       prbt_node x = rbt_search(root, array[i]);
       root = rbt_delete_node(root, x);
       printf ("the number %d deleted\n",array[i]);
       printf ("the %dth iteration\n",i + 1);
       mid_traverse_node(root);
  }
  return 0;
}
Ejemplo n.º 7
0
int add_new_mem(size_t size)
{
        void *addr;
        MM_node *new_node;

        size = get_mem_size(size) + NODE_HEADER_SIZE;

        if ((addr = mmap(NULL, size, MMAP_PROT, MMAP_FLAGS, -1, 0)) == MAP_FAILED)
        {
                return ERROR;
        }
        else
        {
                new_node = construct_node(addr);
                new_node->status = FREE;
                new_node->size = size;
                append_node(new_node);
                malloc_tail = new_node;

                return SUCCESS;
        }
}
Ejemplo n.º 8
0
void enque(queue *q,TYPE value){
node *x=construct_node(value);
if(q->rear) q->rear->pointer=x;
else q->front =x;
q->rear=x;
}
Ejemplo n.º 9
0
void push(stack *s,TYPE value){
node *x=construct_node(value);
x->pointer=s->top;
s->top=x;
}
Ejemplo n.º 10
0
int main()
{
	struct node *root = construct_node(-10);
	root = insert_node(root, construct_node(2));
	root = insert_node(root, construct_node(13));
	root = insert_node(root, construct_node(-13));
	root = insert_node(root, construct_node(-15));
	root = insert_node(root, construct_node(15));
	root = insert_node(root, construct_node(17));
	root = insert_node(root, construct_node(400));
	root = insert_node(root, construct_node(500));
	root = insert_node(root, construct_node(600));
	insert_node(root, construct_node(20));
	inorder_traversal(root);
	return 0;
}