Beispiel #1
0
void test_managed_alloc(void) {
    char *tmp_rc;
    char *tmp_gc;
    char *tmp_rc2;
    char *tmp_gc2;

    Managed mem_rc = (Managed) iMalloc(1 Mb, REFCOUNT + ASCENDING_SIZE);
    Managed mem_gc = (Managed) iMalloc(1 Mb, GCD + ASCENDING_SIZE);
    Priv_mem priv_mem_rc = style_to_priv((Memory) mem_rc);
    Priv_mem priv_mem_gc = style_to_priv((Memory) mem_gc);

    // Test allocating 1 Kb
    tmp_rc = mem_rc->alloc((Memory) mem_rc, 1 Kb);
    CU_ASSERT(alloclist(priv_mem_rc) != NULL);
    CU_ASSERT(alloclist(priv_mem_rc)->size == 1 Kb);
    CU_ASSERT(freelist(priv_mem_rc) != NULL);
    CU_ASSERT(freelist(priv_mem_rc)->size == 1 Mb - 1 Kb);
    tmp_gc = mem_gc->alloc((Memory) mem_gc, 1 Kb);
    CU_ASSERT(alloclist(priv_mem_gc) != NULL);
    CU_ASSERT(alloclist(priv_mem_gc)->size == 1 Kb);
    CU_ASSERT(freelist(priv_mem_gc) != NULL);
    CU_ASSERT(freelist(priv_mem_gc)->size == 1 Mb - 1 Kb);

    // Allocate all remeaining space.
    tmp_rc = mem_rc->alloc((Memory) mem_rc, 1 Mb - 1 Kb);
    CU_ASSERT(alloclist(priv_mem_rc) != NULL);
    CU_ASSERT(alloclist(priv_mem_rc)->size == 1 Mb - 1 Kb);
    CU_ASSERT(freelist(priv_mem_rc) == NULL);
    tmp_gc = mem_gc->alloc((Memory) mem_gc, 1 Mb - 1 Kb);
    CU_ASSERT(alloclist(priv_mem_gc) != NULL);
    CU_ASSERT(alloclist(priv_mem_gc)->size == 1 Mb - 1 Kb);
    CU_ASSERT(freelist(priv_mem_gc) == NULL);

    // This allocation should fail using refcount, but succeed using gc.
    // Note: tmp no longer points to the 1 Kb block allocated in the beginning.
    tmp_rc2 = mem_rc->alloc((Memory) mem_rc, 1 Kb);
    CU_ASSERT(tmp_rc2 == NULL);
    zero_stack_above();
    tmp_gc2 = mem_gc->alloc((Memory) mem_gc, 1 Kb);
    CU_ASSERT(tmp_gc2 != NULL);
    CU_ASSERT(alloclist(priv_mem_gc)->size == 1 Kb);
    CU_ASSERT(freelist(priv_mem_gc) == NULL);
}
Beispiel #2
0
int main() {
  SET_STACK_BOTTOM CURRENT_SP(__g_stack_bottom__);
  Manual manMem = (Manual) iMalloc(10 Kb, MANUAL + ADDRESS);
  printf("%d\n", manMem->avail((Memory) manMem));
  char *point = manMem->alloc((Memory) manMem, sizeof(char*)*4);
  strcpy(point, "hej\0");
  printf("%s\n", point);
  char *point2 = manMem->alloc((Memory) manMem, sizeof(void*));
  strcpy(point2, "hejsan\0");
  printf("%s\n", point2);
  printf("%d\n", manMem->avail((Memory) manMem));
  manMem->free((Memory) manMem, point);
  manMem->free((Memory) manMem, point2);
  printf("%d\n", manMem->avail((Memory) manMem));
  
  Managed mgrMem = (Managed) iMalloc(10 Kb, GCD + REFCOUNT + ADDRESS);
  Tree t = mgrMem->alloc((Memory) mgrMem, sizeof(tree));
  Tree t1 = mgrMem->alloc((Memory) mgrMem, sizeof(tree));
  Tree t2 = mgrMem->alloc((Memory) mgrMem, sizeof(tree));
  Tree t3 = mgrMem->alloc((Memory) mgrMem, sizeof(tree));
  Tree t4 = mgrMem->alloc((Memory) mgrMem, sizeof(tree));
 
  t->current = 5;
  t1->current = 3;
  t2->current = 6;
  t->l = t1;
  t->r = t2;
  t2->l = t4;
  t4 = NULL;
  t3 = NULL;
  mgrMem->gc.collect((Memory) mgrMem);
  void *p  = mgrMem->gc.alloc((Memory) mgrMem,"i\0");
  mgrMem->rc.retain(p);
  printf("%d\n", mgrMem->rc.count(p));
  mgrMem->rc.release((Memory) mgrMem, p);
  mgrMem->rc.release((Memory) mgrMem, p);
  
    return 0;
    }
Beispiel #3
0
void test_typed_alloc(void) {
    char *tmp_gc;

    Managed mem_gc = (Managed) iMalloc(1 Mb, GCD + ASCENDING_SIZE);
    Priv_mem priv_mem_gc = style_to_priv((Memory) mem_gc);

    // Test allocating 10 pointers, 5 ints and 8 chars
    tmp_gc = mem_gc->gc.alloc((Memory) mem_gc, "10*5i8c");
    CU_ASSERT(alloclist(priv_mem_gc) != NULL);
    CU_ASSERT(alloclist(priv_mem_gc)->size == 10 * sizeof(void*) + 5 * sizeof(int) + 8 * sizeof(char));
    CU_ASSERT(freelist(priv_mem_gc) != NULL);
    CU_ASSERT(freelist(priv_mem_gc)->size == (1 Mb - (10 * sizeof(void*) + 5 * sizeof(int) + 8 * sizeof(char))));

    free_lists(priv_mem_gc->lists);
    free(priv_mem_gc->as->start);
    free(priv_mem_gc);
}