コード例 #1
0
ファイル: finalizer.c プロジェクト: kstephens/smal
static void _initialize()
{
  {
    smal_type_descriptor desc = { 0 };
    desc.object_size = sizeof(smal_finalized);
    desc.mark_func = (void*) smal_finalized_mark;
    desc.free_func = (void*) smal_finalized_free;
    smal_finalized_type_ = smal_type_for_desc(&desc);
  }

  {
    smal_type_descriptor desc = { 0 };
    desc.object_size = sizeof(smal_finalizer);
    desc.mark_func = (void*) smal_finalizer_mark;
    // desc.free_func = (void*) smal_finalizer_free;
    smal_finalizer_type_ = smal_type_for_desc(&desc);
  }

  voidP_voidP_TableInit(&referred_table, 8);
  smal_thread_mutex_init(&referred_table_mutex);

  smal_thread_mutex_init(&finalized_queue_mutex);

  initialized = 1;
}
コード例 #2
0
ファイル: gc.c プロジェクト: jbulow/tort
static void *_type_for_size(size_t size)
{
  smal_type_descriptor desc = { 0 };
  desc.object_size = size;
  desc.object_alignment = sizeof(tort_v);
  desc.mark_func = mark_obj;
  desc.free_func = free_obj;
  return smal_type_for_desc(&desc);
}
コード例 #3
0
ファイル: mutation_test_1.c プロジェクト: kstephens/smal
int main(int argc, char **argv)
{
  int i = 0;
  int ncollect = 2;
  int alloc_id = 0;
  int alloc_n = 100;
  my_cons *x = 0, *y = 0;
  my_cons *xp = 0, *yp = 0;
  smal_type *my_cons_type_mu; /* mostly_unchanging */
  smal_roots_4(x, y, xp, yp);

  smal_debug_set_level(smal_debug_mprotect, 9);
  smal_debug_set_level(smal_debug_mmap, 9);
  
  my_cons_type = smal_type_for(sizeof(my_cons), my_cons_mark, 0);
  {
    smal_type_descriptor desc;
    memset(&desc, 0, sizeof(desc));
    desc.object_size = sizeof(my_cons);
    desc.mark_func = my_cons_mark;
    desc.mostly_unchanging = 1;
    my_cons_type_mu = smal_type_for_desc(&desc);
  }

  fprintf(stderr, "allocing for x list\n");
  for ( alloc_id = 0; alloc_id < alloc_n; ++ alloc_id ) {
    my_cons *c = smal_alloc(my_cons_type);
    c->car = (void*) 1;
    c->cdr = x;
    x = c;
  }
  my_print_stats();
  {
    smal_stats stats = { 0 };
    smal_global_stats(&stats);
    assert(stats.buffer_mutations == 0);
  }

  fprintf(stderr, "collecting x after list\n");
  smal_collect();
  my_print_stats();
  {
    smal_stats stats = { 0 };
    smal_global_stats(&stats);
    assert(stats.buffer_mutations == 0);
  }

  fprintf(stderr, "allocing for y list (mostly_unchanging)\n");
  for ( alloc_id = 0; alloc_id < alloc_n; ++ alloc_id ) {
    my_cons *c = smal_alloc(my_cons_type_mu);
    fprintf(stderr, "  c = %p\n", c);
    c->car = (void*) 2;
    c->cdr = y;
    y = c;
  }
  my_print_stats();

  fprintf(stderr, "collecting after x and y list\n");
  smal_collect();
  my_print_stats();

  fprintf(stderr, "mutating y list\n");
  xp = x; yp = y;
  while ( yp ) {
    yp->car = xp;
    yp = yp->cdr;
    xp = xp->cdr;
  }

  {
    smal_stats stats = { 0 };
    smal_global_stats(&stats);
    assert(stats.buffer_mutations == 1);
  }

  fprintf(stderr, "collecting after mutating y list\n");
  smal_collect();
  my_print_stats();
  {
    smal_stats stats = { 0 };
    smal_global_stats(&stats);
    assert(stats.buffer_mutations == 1);
  }

  fprintf(stderr, "mutating y list, again\n");
  xp = x; yp = y;
  while ( yp ) {
    yp->car = xp;
    yp = yp->cdr;
    xp = xp->cdr;
  }

  {
    smal_stats stats = { 0 };
    smal_global_stats(&stats);
    assert(stats.buffer_mutations == 2);
  }

  fprintf(stderr, "collecting after mutating y list, again\n");
  smal_collect();
  my_print_stats();
  {
    smal_stats stats = { 0 };
    smal_global_stats(&stats);
    assert(stats.buffer_mutations == 2);
  }

#if 0
  fprintf(stderr, "dropping some of x\n");
  {
    my_cons *c;
    for ( c = x; c; c = c->cdr ) {
      if ( rand() % 10 > 5 ) {
	c->cdr = c->cdr ? ((my_cons*) c->cdr)->cdr : 0;
      }
    }
  }
  smal_collect();
  my_print_stats();

  fprintf(stderr, "allocing more for y list\n");
  for ( alloc_id = 0; alloc_id < alloc_n; ++ alloc_id ) {
    my_cons *c = smal_alloc(my_cons_type);
    c->car = (void*) 2;
    c->cdr = y;
    y = c;
  }
  smal_collect();
  my_print_stats();
  
#endif

  x = y = 0;
  // smal_debug_level = 9;
  for ( i = 0; i < ncollect; ++ i ) {
    smal_collect();
    fprintf(stderr, "dereference all %d\n", i);
  }
  my_print_stats();
  {
    smal_stats stats = { 0 };
    smal_global_stats(&stats);
    assert(stats.alloc_id == alloc_n * 2);
    assert(stats.free_id == stats.alloc_id);
    assert(stats.capacity_n == 0);
    assert(stats.buffer_n == 0);
    assert(stats.buffer_mutations == 2);
  }

  smal_roots_end();
  
  fprintf(stderr, "\n%s OK\n", argv[0]);
  return 0;
}