Exemplo n.º 1
0
Arquivo: w05.c Projeto: Faur/C_basics
int main(){

	int a = 5;
	struct Single_link *head = sl_create( (void**)&a);

	printf("%d\n", *((int**)head->key) );
	// printf("%d\n", (int) head->key);

	char b[] = "asdf";
	struct Single_link * head2 = sl_create( (void**)&b);
	printf("%s\n", b);
	printf("%s\n", *(char**) head2->key);

	return 0;
}
Exemplo n.º 2
0
Arquivo: load.c Projeto: adh/dfsch
char* read_file(char* fname){
  FILE* f;
  char* buf = GC_MALLOC_ATOMIC(8192);
  size_t r;
  str_list_t* sl = sl_create();

  f = fopen(fname, "r");
  DFSCH_UNWIND {
    if (!f){
      dfsch_operating_system_error(dfsch_saprintf("Cannot open file %d",
                                                  fname));
    }
    
    while (!feof(f)){
      r = fread(buf, 1, 8192, f);
      if (r != 0){
        sl_nappend(sl, buf, r);
        buf = GC_MALLOC_ATOMIC(8192);
      } else {
        if (ferror(f)){
          dfsch_operating_system_error(dfsch_saprintf("Error reading file %d",
                                                      fname));
        }
      }
    }
    
  } DFSCH_PROTECT {
    fclose(f);
  } DFSCH_PROTECT_END;
  
  return sl_value(sl);
}
Exemplo n.º 3
0
Arquivo: load.c Projeto: adh/dfsch
static char* get_module_symbol(char* name){
  str_list_t* l = sl_create();
  char* buf;
  char* i;

  i = strrchr(name, '/');
  
  if (i) {
    buf = stracpy(i + 1);
  } else {
    buf = stracpy(name);
  }
  
  i = strchr(buf, '.');
  if (i){
    *i = '\0';
  }

  i = buf;

  while(*i){
    if (!strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", *i)){
      *i = '_';
    }
    i++;
  }
  
  sl_append(l, "dfsch_module_");
  sl_append(l, buf);
  sl_append(l, "_register");

  return sl_value(l);
}
Exemplo n.º 4
0
Arquivo: ports.c Projeto: leia/dfsch
dfsch_object_t* dfsch_string_output_port(){
  string_output_port_t* port = 
    (string_output_port_t*)dfsch_make_object((dfsch_type_t*)
                                             DFSCH_STRING_OUTPUT_PORT_TYPE);

  port->mutex = create_finalized_mutex();
  port->list = sl_create();

  return (dfsch_object_t*)port;
}
Exemplo n.º 5
0
Arquivo: ports.c Projeto: leia/dfsch
dfsch_strbuf_t* dfsch_port_read_whole(dfsch_object_t* port){
  ssize_t ret;
  char* buf = GC_MALLOC_ATOMIC(1024);
  str_list_t* sl = sl_create();
  while ((ret = dfsch_port_read_buf(port, buf, 1024))){
    sl_nappend(sl, buf, ret);
    buf = GC_MALLOC_ATOMIC(1024);
  }
  return sl_value_strbuf(sl);
}
Exemplo n.º 6
0
static char *sl_test_update(void) {
    int v1 = 5;
    int v2 = 6;
    int *v;
    sl *slist = sl_create();

    sl_insert(slist, 9, &v1);
    sl_insert(slist, 9, &v2);

    v = sl_find(slist, 9);
    mu_assert("updating list with one element fails", *v == v2);
    sl_destroy(slist);
    return 0;
}
Exemplo n.º 7
0
static char* build_doc(char* name){
  str_list_t* sl = sl_create();
  name++;
  sl_append(sl, "Return ");

  while (*name){
    switch (*name){
    case 'a':
      sl_append(sl, "car of ");
      break;
    case 'd':
      sl_append(sl, "cdr of ");
      break;
    }
    name++;
  }
  sl_append(sl, "argument");

  return sl_value(sl);
}
Exemplo n.º 8
0
void test_setup(void) {
    srand(time(NULL));

    slist = sl_create();
    klist = kl_create();
    
    for(int i = 0; i < ARRAY_LEN(keys); i++) {
        sl_insert(slist, keys[i], &vals[i]);
    }
    sl_output(slist);

    printf("\n");

    for(int i = 0; i < ARRAY_LEN(keys); i++) {
        kl_insert(klist, keys[i], &vals[i]);
    }
    kl_output(klist);

    printf("\n");
}
Exemplo n.º 9
0
void setup_running(struct running *rr) {
  rr->stats_fd = -1;
  rr->have_quit = 0;
  rr->eb = event_base_new();
  rr->edb = evdns_base_new(rr->eb,1);
  rr->sq = sq_create(rr->eb);
  rr->sl = sl_create();
  rr->si = syncif_create(rr->eb);
  rr->icc = array_create(ic_done,0);
  rr->src = array_create(0,0);
  rr->src_shop = assoc_create(0,0,0,0);
  rr->ic_shop = assoc_create(0,0,0,0);
  rr->rot = rotator_create();
  ref_create(&(rr->need_loop));
  ref_create(&(rr->ic_running));
  /* Need to process syncqueue closing events before exiting mainloop */
  ref_on_free(&(rr->need_loop),do_exit,rr->eb);
  ref_until_free(&(rr->need_loop),sq_ref(rr->sq));
  ref_until_free(&(rr->need_loop),sl_ref(rr->sl));
  /* keep a ref to need_loop for all interfaces: released in ic_running */
  ref_on_release(&(rr->ic_running),interfaces_quit,rr);
}
Exemplo n.º 10
0
Arquivo: load.c Projeto: adh/dfsch
void dfsch_load(dfsch_object_t* env, char* name, 
                dfsch_object_t* path_list,
                int as_toplevel){
  struct stat st;
  dfsch_object_t* path;
  char *pathpart;
  char *fname;
  str_list_t* l;
  int i;

  for (i = 0; i < sizeof(builtin_modules) / sizeof(builtin_module_t); i++){
    if (strcmp(builtin_modules[i].name, name) == 0){
      builtin_modules[i].register_proc(env);
      return;
    }
  }

  if (path_list){
    path = path_list;
  } else {
    path = dfsch_env_get_cstr(env, "*load-path*");
    if (path == DFSCH_INVALID_OBJECT){
      path = NULL;
    }
  }

  while (DFSCH_PAIR_P(path)){
    dfsch_object_t* pp = DFSCH_FAST_CAR(path);
    if (!dfsch_string_p(pp)){
      if (dfsch_apply(pp, dfsch_list(2,
                                     env,
                                     dfsch_make_string_cstr(name)))){
        return;
      }
      path = DFSCH_FAST_CDR(path);
      continue;
    }
    l = sl_create();
    sl_append(l, dfsch_string_to_cstr(DFSCH_FAST_CAR(path)));
    sl_append(l, "/");
    sl_append(l, name);
    pathpart = sl_value(l);
    if (stat(pathpart, &st) == 0){ 
      if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)){

        for (i = 0; i < sizeof(loaders) / sizeof(module_loader_t); i++){
          if (strcmp(pathpart + strlen(pathpart) - strlen(loaders[i].path_ext),
                     loaders[i].path_ext) == 0){
            loaders[i].load(pathpart, env, as_toplevel);	      
            return;
          }
        }

        dfsch_load_scm(env, pathpart, 0);
        return;
      }
    }

    for (i = 0; i < sizeof(loaders) / sizeof(module_loader_t); i++){
      fname = stracat(pathpart, loaders[i].path_ext);
      if (stat(fname, &st) == 0 && (S_ISREG(st.st_mode) || 
                                    S_ISLNK(st.st_mode))){
        loaders[i].load(fname, env, as_toplevel);	      
        return;
      }
    }
    
    path = dfsch_cdr(path);
  }
  
  dfsch_error("Module not found", dfsch_make_string_cstr(name));
}
Exemplo n.º 11
0
int main( int argc, char **argv )
{
   sl_List       sl;
   int           count;
   int           i;

   maa_init( argv[0] );
   
   if (argc == 1) {
      count = 10;
   } else if (argc != 2 ) {
      fprintf( stderr, "usage: sltest count\n" );
      return 1;
   } else {
      count = atoi( argv[1] );
   }

   printf( "Running test for count of %d\n", count );

   sl = sl_create( compare, key, NULL );
   
   for (i = 1; i < count; i++) {
      printf( "adding %d\n", i );
      sl_insert( sl, (void *) (intptr_t) i );
#ifdef DUMP
      _sl_dump( sl );
#endif
   }

   sl_iterate( sl, print );
   printf( "\n" );

   sl_delete( sl, (void *)5 );
   sl_iterate( sl, print );
   printf( "\n" );

   sl_insert( sl, (void *)0 );
   sl_iterate( sl, print );
   printf( "\n" );
   sl_insert( sl, (void *)66 );
   sl_iterate( sl, print );
   printf( "\n" );
   sl_insert( sl, (void *)100 );
   sl_iterate( sl, print );
   printf( "\n" );
   sl_insert( sl, (void *)-1 );
   sl_iterate( sl, print );
   printf( "\n" );
   sl_insert( sl, (void *)5 );
   sl_iterate( sl, print );
   printf( "\n" );
   sl_insert( sl, (void *)67 );
   sl_iterate( sl, print );
   printf( "\n" );
   sl_insert( sl, (void *)68 );
   sl_iterate( sl,print );
   printf( "\n" );
   sl_insert( sl, (void *)65 );
   sl_iterate( sl, print );
   printf( "\n" );
   
   sl_destroy( sl );

   return 0;
}
Exemplo n.º 12
0
static char *test_speed_compare(void) {
    struct _timeb start;
    struct _timeb finish;
    unsigned long sl_time_taken;
    unsigned long kl_time_taken;
    int keys[10000];
    int vals[10000];
    int test_keys[100];
    sl *slist = sl_create();
    kl *klist = kl_create();
    int *v;

    _ftime(&start);
    /* generate long list (same for both) */
    for(int i = 0; i < 10000; i++) {
        keys[i] = rand();
        vals[i] = rand();
    }
    for(int i = 0; i < 100; i++) {
        test_keys[i] = rand() % 10000;
    }

    /* fill lists with it (time this) */
    _ftime(&start);
    for(int i = 0; i < 10000; i++) {
        kl_insert(klist, keys[i], &vals[i]);
    }
    _ftime(&finish);
    printf("kl: inserted 10,000 keys in %lums\n", ftime_diff(&start, &finish));

    _ftime(&start);
    for(int i = 0; i < 10000; i++) {
        sl_insert(slist, keys[i], &vals[i]);
    }
    _ftime(&finish);
    printf("sl: inserted 10,000 keys in %lums\n", ftime_diff(&start, &finish));

    /* find random values in it (same for both) n times, average time taken */
    _ftime(&start);
    for(int i = 0; i < 1000; i++) {
        for(int j = 0; j < 100; j++) {
            v = sl_find(slist, keys[test_keys[j]]);
        }
    }
    _ftime(&finish);
    sl_time_taken = ftime_diff(&start, &finish);

    _ftime(&start);
    for(int i = 0; i < 1000; i++) {
        for(int j = 0; j < 100; j++) {
            v = kl_find(klist, keys[test_keys[j]]);
        }
    }
    _ftime(&finish);
    kl_time_taken = ftime_diff(&start, &finish);

    printf("sl: found 100,000 keys in %fs, %.2fkeys/s\n", sl_time_taken/1000.0, 100.0*100000/sl_time_taken);
    printf("kl: found 100,000 keys in %fs, %.2fkeys/s\n\n", kl_time_taken/1000.0, 100.0*100000/kl_time_taken);

    mu_assert("average sl find speed is slower than kl", sl_time_taken < kl_time_taken);

    sl_destroy(slist);
    kl_destroy(klist);

    return 0;
}