def ll_list2fixed(l): n = l.length olditems = l.items if n == len(olditems): return olditems else: LIST = typeOf(l).TO newitems = malloc(LIST.items.TO, n) rgc.ll_arraycopy(olditems, newitems, 0, 0, n) return newitems
def fn(): l = lltype.malloc(TP, 100) for i in range(100): l[i] = lltype.malloc(TP.OF.TO, i) l2 = lltype.malloc(TP, 50) rgc.ll_arraycopy(l, l2, 40, 0, 50) rgc.collect() for i in range(50): assert l2[i] == l[40 + i] return 0
def ll_arraycopy(source, dest, source_start, dest_start, length): SRCTYPE = typeOf(source) if isinstance(SRCTYPE, Ptr): # lltype rgc.ll_arraycopy(source.ll_items(), dest.ll_items(), source_start, dest_start, length) else: # ootype -- XXX improve the case of array->array copy? i = 0 while i < length: item = source.ll_getitem_fast(source_start + i) dest.ll_setitem_fast(dest_start + i, item) i += 1
def fn(): l = lltype.malloc(TP, 100) l2 = lltype.malloc(TP, 100) for i in range(100): l[i] = lltype.malloc(S) rgc.ll_arraycopy(l, l2, 50, 0, 50) # force nursery collect x = [] for i in range(20): x.append((1, lltype.malloc(S))) for i in range(50): assert l2[i] == l[50 + i] return 0
def fn(): l = lltype.malloc(TP, 100) for i in range(100): l[i] = i * 3 l2 = lltype.malloc(TP, 50) rgc.ll_arraycopy(l, l2, 40, 0, 50) # force a nursery collect x = [] for i in range(20): x.append((1, lltype.malloc(S))) for i in range(50): assert l2[i] == (40 + i) * 3 return 0
def test_ll_arraycopy_1(): TYPE = lltype.GcArray(lltype.Signed) a1 = lltype.malloc(TYPE, 10) a2 = lltype.malloc(TYPE, 6) for i in range(10): a1[i] = 100 + i for i in range(6): a2[i] = 200 + i rgc.ll_arraycopy(a1, a2, 4, 2, 3) for i in range(10): assert a1[i] == 100 + i for i in range(6): if 2 <= i < 5: assert a2[i] == a1[i+2] else: assert a2[i] == 200 + i
def test_ll_arraycopy_1(): TYPE = lltype.GcArray(lltype.Signed) a1 = lltype.malloc(TYPE, 10) a2 = lltype.malloc(TYPE, 6) for i in range(10): a1[i] = 100 + i for i in range(6): a2[i] = 200 + i rgc.ll_arraycopy(a1, a2, 4, 2, 3) for i in range(10): assert a1[i] == 100 + i for i in range(6): if 2 <= i < 5: assert a2[i] == a1[i + 2] else: assert a2[i] == 200 + i
def test_ll_arraycopy_4(): S = lltype.GcStruct('S') TYPE = lltype.GcArray(lltype.Ptr(S)) a1 = lltype.malloc(TYPE, 10) a2 = lltype.malloc(TYPE, 6) org1 = [None] * 10 org2 = [None] * 6 for i in range(10): a1[i] = org1[i] = lltype.malloc(S) for i in range(6): a2[i] = org2[i] = lltype.malloc(S) rgc.ll_arraycopy(a1, a2, 4, 2, 3) for i in range(10): assert a1[i] == org1[i] for i in range(6): if 2 <= i < 5: assert a2[i] == a1[i+2] else: assert a2[i] == org2[i]
def test_ll_arraycopy_3(): S = lltype.Struct('S') # non-gc TYPE = lltype.GcArray(lltype.Ptr(S)) a1 = lltype.malloc(TYPE, 10) a2 = lltype.malloc(TYPE, 6) org1 = [None] * 10 org2 = [None] * 6 for i in range(10): a1[i] = org1[i] = lltype.malloc(S, immortal=True) for i in range(6): a2[i] = org2[i] = lltype.malloc(S, immortal=True) rgc.ll_arraycopy(a1, a2, 4, 2, 3) for i in range(10): assert a1[i] == org1[i] for i in range(6): if 2 <= i < 5: assert a2[i] == a1[i + 2] else: assert a2[i] == org2[i]
def _ll_list_resize_really(l, newsize): """ Ensure l.items has room for at least newsize elements, and set l.length to newsize. Note that l.items may change, and even if newsize is less than l.length on entry. """ # This over-allocates proportional to the list size, making room # for additional growth. The over-allocation is mild, but is # enough to give linear-time amortized behavior over a long # sequence of appends() in the presence of a poorly-performing # system malloc(). # The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ... if newsize <= 0: ll_assert(newsize == 0, "negative list length") l.length = 0 l.items = _ll_new_empty_item_array(typeOf(l).TO) return else: if newsize < 9: some = 3 else: some = 6 some += newsize >> 3 try: new_allocated = ovfcheck(newsize + some) except OverflowError: raise MemoryError # XXX consider to have a real realloc # new_allocated is a bit more than newsize, enough to ensure an amortized # linear complexity for e.g. repeated usage of l.append(). items = l.items newitems = malloc(typeOf(l).TO.items.TO, new_allocated) before_len = l.length if before_len: # avoids copying GC flags from the prebuilt_empty_array if before_len < newsize: p = before_len else: p = newsize rgc.ll_arraycopy(items, newitems, 0, 0, p) l.length = newsize l.items = newitems
def test_ll_arraycopy_2(): TYPE = lltype.GcArray(lltype.Void) a1 = lltype.malloc(TYPE, 10) a2 = lltype.malloc(TYPE, 6) rgc.ll_arraycopy(a1, a2, 4, 2, 3)
def f(): a1 = lltype.malloc(TYPE, 10) a2 = lltype.malloc(TYPE, 6) rgc.ll_arraycopy(a2, a1, 0, 1, 5)
def f(a, b, c, d, e): ll_arraycopy(a, b, c, d, e)