예제 #1
0
파일: mct.c 프로젝트: elominp/zappy
bool
mct(t_cli *c, t_msg *msg)
{
  int64_t	x;
  int64_t	y;
  int64_t	xmax;
  int64_t	ymax;
  t_list	*params;

  (void)msg;
  x = 0;
  y = 0;
  xmax = c->servptr->map.width;
  ymax = c->servptr->map.height;
  if ((params = new_list()) == NULL || init_list(params) == false)
    return (ERR(RED"Omg malloc failed, BAIL OUT BAIL OUT\n"WHITE), false);
  while (y <= xmax)
    {
      while (x <= ymax)
	{
	  prepare_message(params, x, y);
	  bct(c, &(t_msg){NULL, "bct", params, 0});
	  x++;
	}
      x = 0;
      y++;
    }
  return (list_destruct(&params, &free), true);
}
예제 #2
0
파일: t_opt.c 프로젝트: elominp/zappy
bool
t_opt_dest(t_opt *o)
{
  if (!o)
    return (false);
  if (o->teams)
    list_destruct(&o->teams, &delete_t_team);
  ZERO_MEM(o);
  return (false);
}
예제 #3
0
int main(void)
{
    data_t *datap = NULL;
    list_t *Lptr = NULL;
   /***** 
    Lptr = list_construct(trte_compare, trte_route_rec_cleanup);
    
    // create one item to test list_insert
    datap = (data_t *) calloc(1,sizeof(data_t));
    datap->src_ip_addr = 555;
    datap->dest_ip_addr = 555;
    list_insert(Lptr, datap, LISTPOS_HEAD);
    datap = NULL;
    
    // add a second item to head  of the list
    datap = (data_t *) calloc(1,sizeof(data_t));
    datap->src_ip_addr = 444;
    datap->dest_ip_addr = 444;
    list_insert(Lptr, datap, LISTPOS_HEAD);
    datap = NULL;
    
    // add a 3rd item to tail of the list
    datap = (data_t *) calloc(1,sizeof(data_t));
    datap->src_ip_addr = 777;
    datap->dest_ip_addr = 777;
    list_insert(Lptr, datap, LISTPOS_TAIL);
    datap = NULL;
    
    // add a 4th item
    datap = (data_t *) calloc(1,sizeof(data_t));
    datap->src_ip_addr = 500;
    datap->dest_ip_addr = 500;
    list_insert(Lptr, datap, LISTPOS_TAIL);
    datap = NULL;
    
    // add a 4th item, sorted
    datap = (data_t *) calloc(1,sizeof(data_t));
    datap->src_ip_addr = 333;
    datap->dest_ip_addr = 333;
    list_insert_sorted(Lptr, datap);
    datap = NULL;
    
    // test list_access with one item in list
    datap = list_access(Lptr, LISTPOS_HEAD);
    printf("Should find 555 and found (%d)\n\n", datap->src_ip_addr);
    datap = NULL;
    
    
    // find all three and print
    datap = list_access(Lptr, 0);
    printf("Second test\nPosition 0 should find 555 and found (%d)\n", datap->src_ip_addr);
    datap = list_access(Lptr, 1);
    printf("Position 1 should find 555 and found (%d)\n", datap->src_ip_addr);
    datap = list_access(Lptr, 2);
    printf("Position 2 should find 777  and found (%d)\n", datap->src_ip_addr);
    datap = list_access(Lptr, 3);
    printf("Position 3 should find 777  and found (%d)\n", datap->src_ip_addr);
    datap = list_access(Lptr, 3);
    printf("Position 3 should find 777  and found (%d)\n", datap->src_ip_addr);
    
    //Next try to use list_debug_print
    printf("\nTest of list print\n\n");
    list_debug_print(Lptr);
    */
    
    // Uncomment this section to test list_elem_find
    /*********************************************************************
     data_t template;
     int my_index = -999;
     template.src_ip_addr = 444;
     template.dest_ip_addr = 444;
     data_t *foundp = list_elem_find(Lptr, &template, &my_index);
     printf("\nTest of list elem find\n");
     if (foundp != NULL)
     printf("looked for %d and found %d at index %d\n",
     template.src_ip_addr, foundp->src_ip_addr, my_index);
     else
     printf("looked for %d and did not find \n", template.src_ip_addr);
     foundp = NULL;
     
     */
    
    //list_destruct(Lptr);
    // End of tests with unsorted list
    
    // Uncomment this section to try some tests on a sorted list
    /*********************************************************************/
     list_t *Lsortptr = list_construct(trte_compare, trte_route_rec_cleanup);
     //Lsortptr->list_sorted_state = -7654321;
     datap = (data_t *) calloc(1,sizeof(data_t));
     datap->src_ip_addr = 23;
     datap->dest_ip_addr = 23;
     list_insert_sorted(Lsortptr, datap);
     datap = NULL;
     
     // add a second item
     datap = (data_t *) calloc(1,sizeof(data_t));
     datap->src_ip_addr = 7;
     datap->dest_ip_addr = 7;
     list_insert_sorted(Lsortptr, datap);
     datap = NULL;
     
     // add a third item
     datap = (data_t *) calloc(1,sizeof(data_t));
     datap->src_ip_addr = 15;
     datap->dest_ip_addr = 15;
     list_insert_sorted(Lsortptr, datap);
     datap = NULL;
     
     // add a fourth item
     datap = (data_t *) calloc(1,sizeof(data_t));
     datap->src_ip_addr = 8;
     datap->dest_ip_addr = 8;
     list_insert_sorted(Lsortptr, datap);
     datap = NULL;

    // add a fifth item
    datap = (data_t *) calloc(1,sizeof(data_t));
    datap->src_ip_addr = 28;
    datap->dest_ip_addr = 28;
    list_insert_sorted(Lsortptr, datap);
    datap = NULL;
    //print
    list_debug_print(Lsortptr);
    // remove an item
    data_t *temp = list_remove(Lsortptr, 3);       
    //print
    list_debug_print(Lsortptr);
    // add a sixth  item
    datap = (data_t *) calloc(1,sizeof(data_t));
    datap->src_ip_addr = 2;
    datap->dest_ip_addr = 2;
    list_insert_sorted(Lsortptr, datap);
    datap = NULL;
    
     list_reverse(Lsortptr);

     //print
     list_debug_print(Lsortptr);
     list_destruct(Lsortptr);
     
    
    return 0;
}
예제 #4
0
void test_linked_list()
{
  char* x = "xylophone";
  char* y = "yankee";
  char* z = "zebra";

  List* l = list_create();

  printf("initial list size: %d (expects 0)\n\n", list_length(l));

  list_add(l, x);
  list_add(l, y);
  list_add(l, z);
  printf("list size after adds: %d (expects 3)\n", list_length(l));
  printf("text of first item: %s (expects xylophone)\n", (char*) (list_nth(l, 0)));
  printf("text of second item: %s (expects yankee)\n", (char*) (list_nth(l, 1)));
  printf("text of third item: %s (expects zebra)\n\n", (char*) (list_nth(l, 2)));
  printf("text of last item: %s (expects zebra)\n\n", (char*) (list_last(l)));

  list_remove(l, 1);
  printf("list size after remove: %d (expects 2)\n", list_length(l));
  printf("text of first item: %s (expects xylophone)\n", (char*) (list_nth(l, 0)));
  printf("text of second item: %s (expects zebra)\n\n", (char*) (list_nth(l, 1)));

  list_remove(l, 1);
  printf("list size after remove: %d (expects 1)\n", list_length(l));
  printf("text of first item: %s (expects xylophone)\n\n", (char*) (list_nth(l, 0)));

  list_remove(l, 0);
  printf("list size after remove: %d (expects 0)\n\n", list_length(l));

  list_add(l, x);
  list_add(l, y);
  list_add(l, z);
  printf("list: ");
  print_string_list(l);
  printf("\n");

  list_move_up(l, 0);
  printf("\nmove up 0: ");
  print_string_list(l);
  printf("\n  (expects xylophone yankee zebra)");

  list_move_up(l, 1);
  printf("\nmove up 1: ");
  print_string_list(l);
  printf("\n  (expects yankee xylophone zebra)");

  list_move_up(l, 2);
  printf("\nmove up 2: ");
  print_string_list(l);
  printf("\n  (expects yankee zebra xylophone)\n");

  list_move_down(l, 2);
  printf("\nmove down 2: ");
  print_string_list(l);
  printf("\n    (expects yankee zebra xylophone)");

  list_move_down(l, 1);
  printf("\nmove down 1: ");
  print_string_list(l);
  printf("\n    (expects yankee xylophone zebra)");

  list_move_down(l, 0);
  printf("\nmove down 0: ");
  print_string_list(l);
  printf("\n    (expects xylophone yankee zebra)\n\n");

  list_remove(l, 0);
  list_remove(l, 0);

  list_move_up(l, 0);
  list_move_down(l, 0);

  printf("one item move: ");
  print_string_list(l);
  printf("\n      (expects zebra)\n\n");

  list_destruct(l);

  printf("list destructed.\n\n");
}
예제 #5
0
/**
	Carries out simulation setup and management.
	@param argc The number of arguments
	@param argv The array of arguments
*/
int main(int argc, char *argv[]) {
	int *results, *shm_states;
	int i, op_count, proc_id;
	char *tmp_operator, *cmd;
	list *commands;
	operation *shm_operations;
	
	if(signal(SIGTERM, &stop_execution) == SIG_ERR) {
		write_to_fd(2, "Failed to register signal\n");
		exit(1);
	}
	if(argc != 3) {
		write_to_fd(2, "Usage: main.x <source file> <results file>\n");
		exit(1);
	}
	commands = parse_file(argv[1]);
	processors = atoi(list_extract(commands));
	if (processors <= 0) {
		write_to_fd(2, "Invalid number of processors\n");
		exit(1);
	}
	write_with_int(1, "Number of processors: ", processors);
	op_count = list_count(commands);
	if (op_count == 0) {
		write_to_fd(2, "No operations provided\n");
		exit(1);
	}
	write_with_int(1, "Number of operations: ", op_count);		
	results = (int *) malloc(op_count * sizeof(int));
	if (results == NULL) {
		write_to_fd(2, "Failed to allocate results array\n");
		exit(1);	
	}
	
	init_ipc(2 * processors + 2, processors * sizeof(operation), processors * sizeof(int), 0666 | IPC_CREAT | IPC_EXCL);
	write_with_int(1, "Created semaphore set with ID ", ipc_id[0]);
	write_with_int(1, "Created shm for operations with ID ", ipc_id[1]);
	write_with_int(1, "Created shm for states with ID ", ipc_id[2]);
	init_sems(processors);
	shm_operations = (operation *) shm_attach(ipc_id[1]);
	shm_states = (int *) shm_attach(ipc_id[2]);
	
	for (i = 0; i < processors; ++i)
		shm_states[i] = 0;
	
	start_processors();
	for (i = 1; list_count(commands) > 0; ++i) {
		cmd = list_extract(commands);
		write_with_int(1, "\nOperation #", i);
		proc_id = atoi(strtok(cmd, " "));
		sem_p(2 * processors + 1);
		if (proc_id-- == 0) {
			proc_id = find_proc(shm_states);
		}
		write_with_int(1, "Waiting for processor ", proc_id + 1);
		sem_p(2 * proc_id);
		write_with_int(1, "Delivering operation to processor ", proc_id + 1);
		if (shm_states[proc_id] != 0) {
			results[(shm_states[proc_id] + 1) * -1] = shm_operations[proc_id].num1;
			write_with_int(1, "Previous result: ", shm_operations[proc_id].num1);
		}
		shm_operations[proc_id].num1 = atoi(strtok(NULL, " "));
		tmp_operator = strtok(NULL, " ");
		shm_operations[proc_id].op = *tmp_operator;
		shm_operations[proc_id].num2 = atoi(strtok(NULL, " "));
		shm_states[proc_id] = i;
		write_with_int(1, "Operation delivered. Unblocking processor ", proc_id + 1);
		sem_v((2 * proc_id) + 1);
		free(cmd);
	}
	
	list_destruct(commands);
	
	for (i = 0; i < processors; ++i) {
		sem_p(2 * i);
		write_with_int(1, "\nPassing termination command to processor #", i + 1);
		if (shm_states[i] != 0) {
			results[(shm_states[i] + 1) * -1] = shm_operations[i].num1;
			write_with_int(1, "Last result: ", shm_operations[i].num1);
		}
		shm_operations[i].op = 'K';
		sem_v((2 * i) + 1);
	}

	for (i = 0; i < processors; ++i) 
		if(wait(NULL) == -1)
			write_to_fd(2, "Wait failed\n");
			
	write_to_fd(1, "\nAll processors exited. Writing output file\n");
	write_results(argv[2], results, op_count);
	free(results);
	write_to_fd(1, "Closing IPCs\n");
	shm_detach((void *) shm_operations);
	shm_detach((void *) shm_states);
	close_ipc();
	exit(0);
}
예제 #6
0
파일: alloc-list.c 프로젝트: tyru/dentaku
void
al_destroy(void)
{
    list_destruct(pointers_list);
}