int * parse_into_shared_int_array(char *line, unsigned *array_size)
{
  // Use linked list to buffer the integers
  int_list list;
  int_list_init(&list);

  char *temp;
  char *token = strtok(line, " ");
  while (token)
  {
    int_list_add(&list, (int) strtol(token, &temp, 10));
    token = strtok(NULL, " ");
  }

  *array_size = list.size;

  // Initialize shared memory.
  void *array = 0;
  if ((array = mmap(0, list.size * sizeof(int), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0)) == 0)
  {
    printf("ERROR: failed to initailize shared memory.\n");
    return 0;
  }
  
  int *int_array = (int *) array;

  int i = 0;
  while (list.head)
  {
    int_array[i++] = list.head->data;
    int_list_remove(&list, list.head->data);
  }

  return int_array;
}
예제 #2
0
파일: test16.c 프로젝트: markchapman/Waldo
int main()
{
    int_list l1;
    /* list of ints */
    size_t i;			/* iterator */

    int_list_init( &l1 );	/* initialize the list */

    /* initialize list elements by some random numbers */
    srand((int)time(NULL));
    for( i=0; i<N; ++i )
    {
        float SH = RAND_MAX/10.0;
        int r = (int)rand()/SH;
        int_list_add_tail( &l1, &r );
    }

    /* print the lists */
    printf("List 1 (%u elements)\n", N);
    int_list_mapcar( &l1, printint );
    printf("\n");

    /* find 1, then 2 etc. */
    printf("All elements collecting from 0 to 9 as they occur:\n");
    int_list_move_point_head( &l1 );
    int_list_print_what_asked( &l1 );
    printf("\n");

    /* clean the lists */
    int_list_clean( &l1 );

    return 0;
}
예제 #3
0
파일: test13.c 프로젝트: markchapman/Waldo
int main()
{
  int_list l1, l2, l3, l4;
  /* list of ints */
  int i;			/* iterator */
  int *pa;			/* a pointer to the element */

  int_list_init( &l1 );	/* initialize the list */
  int_list_init( &l2 );	/* initialize the list */
  int_list_init( &l3 );	/* initialize the list */

  /* initialize list elements by meaning of loop index */
  for( i=0; i<7; ++i ) {
    int_list_add_tail( &l1, &i );
  }

  /* print the lists */
  printf("List 1: ");
  int_list_mapcar( &l1, printint );
  printf("\nList 2: ");
  int_list_mapcar( &l2, printint );
  printf("\nList 3: ");
  int_list_mapcar( &l3, printint );
  printf("\n");

  printf("\nWe make cut(0) of l1 for l2:\n");
  (void)int_list_cut( &l1, &l2, 0 );
  printf("List 1: ");
  int_list_mapcar( &l1, printint );
  printf("\nList 2: ");
  int_list_mapcar( &l2, printint );
  printf("\nList 3: ");
  int_list_mapcar( &l3, printint );
  printf("\n");

  printf("\nWe change the head of l1 to 9 (l2 will change as well then!):\n");
  pa = int_list_get_head( &l1 );
  *pa = 9;
  printf("List 1: ");
  int_list_mapcar( &l1, printint );
  printf("\nList 2: ");
  int_list_mapcar( &l2, printint );
  printf("\nList 3: ");
  int_list_mapcar( &l3, printint );
  printf("\n");

  printf("\nWe make cut(3) of l1 for l3:\n");
  (void)int_list_cut( &l1, &l3, 3 );
  printf("List 1: ");
  int_list_mapcar( &l1, printint );
  printf("\nList 2: ");
  int_list_mapcar( &l2, printint );
  printf("\nList 3: ");
  int_list_mapcar( &l3, printint );
  printf("\n");

  printf("\nWe change the head of l3 to 8 (l1 and l2 will change as well then!):\n");
  pa = int_list_get_head( &l3 );
  *pa = 8;
  printf("List 1: ");
  int_list_mapcar( &l1, printint );
  printf("\nList 2: ");
  int_list_mapcar( &l2, printint );
  printf("\nList 3: ");
  int_list_mapcar( &l3, printint );
  printf("\n");

  printf("\nWe make reproduce(cut(2)) to l3 of l1 and reproduce it for l2, \n\
then reproduce l2 to l3 making all lists independent:\n");
  int_list_reproduce( &l2, int_list_cut( &l1, &l3, 3 ), NULL, 0 );
  int_list_reproduce( &l3, &l2, NULL, 0 );
  printf("List 1: ");
  int_list_mapcar( &l1, printint );
  printf("\nList 2: ");
  int_list_mapcar( &l2, printint );
  printf("\nList 3: ");
  int_list_mapcar( &l3, printint );
  printf("\n");

  printf("\nl4 = cdr(l1):\n");
  int_list_cdr( &l1, &l4 );
  printf("List 1: ");
  int_list_mapcar( &l1, printint );
  printf("\nList 4: ");
  int_list_mapcar( &l4, printint );
  printf("\n");

  /* clean the lists */
  int_list_clean( &l1 );
  int_list_clean( &l2 );
  int_list_clean( &l3 );

  return 0;
}