def test_arena_create(self): p = lib.qcgc_arena_create() self.assertEqual(p, lib.qcgc_arena_addr(lib.arena_cells(p))) self.assertEqual(p, lib.qcgc_arena_addr(ffi.addressof(lib.arena_cells(p)[lib.qcgc_arena_cells_count - 1]))) self.assertEqual(0, lib.qcgc_arena_cell_index(lib.arena_cells(p))) self.assertEqual(int(ffi.cast("uint64_t", p)), int(ffi.cast("uint64_t", p)) << lib.QCGC_ARENA_SIZE_EXP >> lib.QCGC_ARENA_SIZE_EXP) self.assertEqual(lib.BLOCK_FREE, self.get_blocktype(ffi.addressof(lib.arena_cells(p)[lib.qcgc_arena_first_cell_index])))
def test_many_small_allocations(self): objects = set() p = self.bump_allocate(16) arena = lib.qcgc_arena_addr(ffi.cast("cell_t *", p)) objects.add(p) for _ in range(1000): p = self.bump_allocate(16) objects.add(p) self.assertEqual(arena, lib.qcgc_arena_addr(ffi.cast("cell_t *", p))) self.assertFalse(ffi.NULL in objects) self.assertEqual(len(objects), 1001)
def test_arena_sweep_no_bump_ptr_coalescing(self): p = self.bump_allocate(16) arena = lib.qcgc_arena_addr(ffi.cast("cell_t *", p)) lib.qcgc_arena_sweep(arena) self.assertEqual(self.get_blocktype(ffi.cast("cell_t *", lib.bump_ptr())), lib.BLOCK_FREE)
def test_write_barrier(self): o = self.allocate(16) self.push_root(o) arena = lib.qcgc_arena_addr(ffi.cast("cell_t *", o)) o.hdr.flags = o.hdr.flags & ~lib.QCGC_GRAY_FLAG self.assertEqual(ffi.cast("object_t *", o).flags & lib.QCGC_GRAY_FLAG, 0) lib.qcgc_write(ffi.cast("object_t *", o)) self.assertEqual(ffi.cast("object_t *", o).flags & lib.QCGC_GRAY_FLAG, lib.QCGC_GRAY_FLAG) lib.qcgc_state.phase = lib.GC_MARK o = self.allocate(16) self.push_root(o) arena = lib.qcgc_arena_addr(ffi.cast("cell_t *", o)) o.hdr.flags = o.hdr.flags & ~lib.QCGC_GRAY_FLAG self.assertEqual(ffi.cast("object_t *", o).flags & lib.QCGC_GRAY_FLAG, 0) self.set_blocktype(ffi.cast("cell_t *", o), lib.BLOCK_BLACK) lib.qcgc_state.phase = lib.GC_MARK lib.qcgc_write(ffi.cast("object_t *", o)) self.assertEqual(ffi.cast("object_t *", o).flags & lib.QCGC_GRAY_FLAG, lib.QCGC_GRAY_FLAG) self.assertEqual(lib.arena_gray_stack(arena).count, 1) self.assertEqual(lib.arena_gray_stack(arena).items[0], o)
def set_blocktype(self, ptr, blocktype): lib.qcgc_arena_set_blocktype(lib.qcgc_arena_addr(ptr), lib.qcgc_arena_cell_index(ptr), blocktype)
def get_blocktype(self, ptr): return lib.qcgc_arena_get_blocktype(lib.qcgc_arena_addr(ptr), lib.qcgc_arena_cell_index(ptr))