Beispiel #1
0
Test(data, initializer_type_to_array) {
    struct type **types;
    gc_t gc = gc_init();
    cr_assert_eq(initializer_type_to_array, itta);

    types = itta(stt{
        construct_type(tid_integral, NULL, &gc),
        construct_type(tid_real, NULL, &gc),
    }, 2, &gc);
    cr_assert_eq(types[0]->single, tid_integral);
    cr_assert_null(types[0]->multiple); 
    cr_assert_eq(types[1]->single, tid_real);
    cr_assert_null(types[1]->multiple);
    cr_assert_null(types[2]);

    types = itta(stt{
        construct_type(tid_real, NULL, &gc),
        construct_type(tid_integral, NULL, &gc),
        construct_type(tid_tuple, itta(
            stt{construct_type(tid_string, NULL, &gc)}, 1, &gc
        ), &gc),
    }, 3, &gc);
    cr_assert_eq(types[0]->single, tid_real);
    cr_assert_null(types[0]->multiple); 
    cr_assert_eq(types[1]->single, tid_integral);
    cr_assert_null(types[1]->multiple);
    cr_assert_eq(types[2]->single, tid_tuple);
    cr_assert_eq(types[2]->multiple[0]->single, tid_string);
    cr_assert_null(types[2]->multiple[0]->multiple);
    cr_assert_null(types[2]->multiple[1]);
    cr_assert_null(types[3]);

    gc_clean(&gc);
}
Beispiel #2
0
int gc_settable(int exponent, int flags)
{
  if (exponent==0) {
    gc_clean();         /* delete all "live" objects first */
    if (SharedGC.table!=NULL) {
      free(SharedGC.table);
      SharedGC.table=NULL;
    } /* if */
    SharedGC.exponent=0;
    SharedGC.flags=0;
    SharedGC.count=0;
  } else {
    int size,oldsize;
    GCPAIR *table,*oldtable;

    if (exponent<7 || (1L<<exponent)>INT_MAX)
      return GC_ERR_PARAMS;
    size=(1<<exponent);
    /* the hash table should not hold more elements than the new size */
    if (SharedGC.count>size)
      return GC_ERR_PARAMS;
    /* allocate the new table */
    table=malloc(size*sizeof(*table));
    if (table==NULL)
      return GC_ERR_MEMORY;
    /* save the statistics of the old table */
    oldtable=SharedGC.table;
    oldsize=(1<<SharedGC.exponent);
    /* clear and set the new table */
    memset(table,0,size*sizeof(*table));
    SharedGC.table=table;
    SharedGC.exponent=exponent;
    SharedGC.count=flags;
    SharedGC.count=0;           /* old table in initially empty */
    /* re-mark all objects in the old table */
    if (oldtable!=NULL) {
      int index;
      for (index=0; index<oldsize; index++)
        if (oldtable[index].value!=0)
          gc_mark(oldtable[index].value);
      free(oldtable);
    } /* if */
  } /* if */
  return GC_ERR_NONE;
}
Beispiel #3
0
static
void garbagecollect(AMX amx[], int number)
{
  int exp, usage, n;

  /* see whether it may be a good time to increase the table size */
  gc_tablestat(&exp, &usage);
  if (usage > 50) {
    if (gc_settable(exp+1, GC_AUTOGROW) != GC_ERR_NONE)
      fprintf(stderr, "Warning, memory low\n");
  } /* if */

  /* scan all abstract machines */
  for (n = 0; n < number; n++)
    gc_scan(&amx[n]);

  /* clean up unused references */
  gc_clean();
}
Beispiel #4
0
Test(data, construct_type_basic)
{
    gc_t gc = gc_init();
    struct type *type;
    struct type *multiple[10] = {NULL};
    
    type = construct_type(tid_integral, NULL, &gc);
    cr_assert_eq(type->single, tid_integral);
    cr_assert_null(type->multiple);

    type = construct_type(tid_string, NULL, &gc);
    cr_assert_eq(type->single, tid_string);
    cr_assert_null(type->multiple);

    multiple[0] = construct_type(tid_atom, NULL, &gc);
    type = construct_type(tid_list, multiple, &gc);
    cr_assert_eq(type->single, tid_list);
    cr_assert_eq(type->multiple[0]->single, tid_atom);
    cr_assert_null(type->multiple[0]->multiple);
    cr_assert_null(type->multiple[1]);

    multiple[0] = construct_type(tid_integral, NULL, &gc);
    multiple[1] = construct_type(tid_real, NULL, &gc);
    multiple[2] = construct_type(tid_string, NULL, &gc);
    type = construct_type(tid_tuple, multiple, &gc);
    cr_assert_eq(type->single, tid_tuple);
    cr_assert_eq(type->multiple[0]->single, tid_integral);
    cr_assert_null(type->multiple[0]->multiple);
    cr_assert_eq(type->multiple[1]->single, tid_real);
    cr_assert_null(type->multiple[1]->multiple);
    cr_assert_eq(type->multiple[2]->single, tid_string);
    cr_assert_null(type->multiple[2]->multiple);
    cr_assert_null(type->multiple[3]);

    gc_clean(&gc);
}