Пример #1
0
def gc_pointers_inside(v, adr, mutable_only=False):
    t = lltype.typeOf(v)
    if isinstance(t, lltype.Struct):
        if mutable_only and t._hints.get('immutable'):
            return
        for n, t2 in t._flds.iteritems():
            if isinstance(t2, lltype.Ptr) and t2.TO._gckind == 'gc':
                yield adr + llmemory.offsetof(t, n)
            elif isinstance(t2, (lltype.Array, lltype.Struct)):
                for a in gc_pointers_inside(getattr(v, n),
                                            adr + llmemory.offsetof(t, n),
                                            mutable_only):
                    yield a
    elif isinstance(t, lltype.Array):
        if mutable_only and t._hints.get('immutable'):
            return
        if isinstance(t.OF, lltype.Ptr) and t.OF.TO._gckind == 'gc':
            for i in range(len(v.items)):
                yield adr + llmemory.itemoffsetof(t, i)
        elif isinstance(t.OF, lltype.Struct):
            for i in range(len(v.items)):
                for a in gc_pointers_inside(v.items[i],
                                            adr + llmemory.itemoffsetof(t, i),
                                            mutable_only):
                    yield a
Пример #2
0
def encode_type_shape(builder, info, TYPE):
    """Encode the shape of the TYPE into the TYPE_INFO structure 'info'."""
    offsets = offsets_to_gc_pointers(TYPE)
    info.ofstoptrs = builder.offsets2table(offsets, TYPE)
    info.finalizer = builder.make_finalizer_funcptr_for_type(TYPE)
    info.weakptrofs = weakpointer_offset(TYPE)
    if not TYPE._is_varsize():
        #info.isvarsize = False
        #info.gcptrinvarsize = False
        info.fixedsize = llarena.round_up_for_allocation(
            llmemory.sizeof(TYPE))
        info.ofstolength = -1
        # note about round_up_for_allocation(): in the 'info' table
        # we put a rounded-up size only for fixed-size objects.  For
        # varsize ones, the GC must anyway compute the size at run-time
        # and round up that result.
    else:
        #info.isvarsize = True
        info.fixedsize = llmemory.sizeof(TYPE, 0)
        if isinstance(TYPE, lltype.Struct):
            ARRAY = TYPE._flds[TYPE._arrayfld]
            ofs1 = llmemory.offsetof(TYPE, TYPE._arrayfld)
            info.ofstolength = ofs1 + llmemory.ArrayLengthOffset(ARRAY)
            info.ofstovar = ofs1 + llmemory.itemoffsetof(ARRAY, 0)
        else:
            ARRAY = TYPE
            info.ofstolength = llmemory.ArrayLengthOffset(ARRAY)
            info.ofstovar = llmemory.itemoffsetof(TYPE, 0)
        assert isinstance(ARRAY, lltype.Array)
        if ARRAY.OF != lltype.Void:
            offsets = offsets_to_gc_pointers(ARRAY.OF)
        else:
            offsets = ()
        info.varofstoptrs = builder.offsets2table(offsets, ARRAY.OF)
        info.varitemsize = llmemory.sizeof(ARRAY.OF)
Пример #3
0
def ll_arraycopy(source, dest, source_start, dest_start, length):
    from pypy.rpython.lltypesystem.lloperation import llop
    from pypy.rpython.lltypesystem import lltype, llmemory
    from pypy.rlib.objectmodel import keepalive_until_here

    # supports non-overlapping copies only
    if not we_are_translated():
        if source == dest:
            assert (source_start + length <= dest_start
                    or dest_start + length <= source_start)

    TP = lltype.typeOf(source).TO
    assert TP == lltype.typeOf(dest).TO
    if isinstance(TP.OF, lltype.Ptr) and TP.OF.TO._gckind == 'gc':
        # perform a write barrier that copies necessary flags from
        # source to dest
        if not llop.gc_writebarrier_before_copy(lltype.Bool, source, dest):
            # if the write barrier is not supported, copy by hand
            for i in range(length):
                dest[i + dest_start] = source[i + source_start]
            return
    source_addr = llmemory.cast_ptr_to_adr(source)
    dest_addr = llmemory.cast_ptr_to_adr(dest)
    cp_source_addr = (source_addr + llmemory.itemoffsetof(TP, 0) +
                      llmemory.sizeof(TP.OF) * source_start)
    cp_dest_addr = (dest_addr + llmemory.itemoffsetof(TP, 0) +
                    llmemory.sizeof(TP.OF) * dest_start)

    llmemory.raw_memcopy(cp_source_addr, cp_dest_addr,
                         llmemory.sizeof(TP.OF) * length)
    keepalive_until_here(source)
    keepalive_until_here(dest)
Пример #4
0
Файл: rgc.py Проект: ieure/pypy
def ll_arraycopy(source, dest, source_start, dest_start, length):
    from pypy.rpython.lltypesystem.lloperation import llop
    from pypy.rlib.objectmodel import keepalive_until_here

    # supports non-overlapping copies only
    if not we_are_translated():
        if source == dest:
            assert (source_start + length <= dest_start or
                    dest_start + length <= source_start)

    TP = lltype.typeOf(source).TO
    assert TP == lltype.typeOf(dest).TO
    if isinstance(TP.OF, lltype.Ptr) and TP.OF.TO._gckind == 'gc':
        # perform a write barrier that copies necessary flags from
        # source to dest
        if not llop.gc_writebarrier_before_copy(lltype.Bool, source, dest):
            # if the write barrier is not supported, copy by hand
            for i in range(length):
                dest[i + dest_start] = source[i + source_start]
            return
    source_addr = llmemory.cast_ptr_to_adr(source)
    dest_addr   = llmemory.cast_ptr_to_adr(dest)
    cp_source_addr = (source_addr + llmemory.itemoffsetof(TP, 0) +
                      llmemory.sizeof(TP.OF) * source_start)
    cp_dest_addr = (dest_addr + llmemory.itemoffsetof(TP, 0) +
                    llmemory.sizeof(TP.OF) * dest_start)
    
    llmemory.raw_memcopy(cp_source_addr, cp_dest_addr,
                         llmemory.sizeof(TP.OF) * length)
    keepalive_until_here(source)
    keepalive_until_here(dest)
Пример #5
0
def gc_pointers_inside(v, adr, mutable_only=False):
    t = lltype.typeOf(v)
    if isinstance(t, lltype.Struct):
        skip = ()
        if mutable_only:
            if t._hints.get("immutable"):
                return
            if "immutable_fields" in t._hints:
                skip = t._hints["immutable_fields"].all_immutable_fields()
        for n, t2 in t._flds.iteritems():
            if isinstance(t2, lltype.Ptr) and t2.TO._gckind == "gc":
                if n not in skip:
                    yield adr + llmemory.offsetof(t, n)
            elif isinstance(t2, (lltype.Array, lltype.Struct)):
                for a in gc_pointers_inside(getattr(v, n), adr + llmemory.offsetof(t, n), mutable_only):
                    yield a
    elif isinstance(t, lltype.Array):
        if mutable_only and t._hints.get("immutable"):
            return
        if isinstance(t.OF, lltype.Ptr) and t.OF.TO._gckind == "gc":
            for i in range(len(v.items)):
                yield adr + llmemory.itemoffsetof(t, i)
        elif isinstance(t.OF, lltype.Struct):
            for i in range(len(v.items)):
                for a in gc_pointers_inside(v.items[i], adr + llmemory.itemoffsetof(t, i), mutable_only):
                    yield a
Пример #6
0
    def str_from_buffer(raw_buf, gc_buf, allocated_size, needed_size):
        """
        Converts from a pair returned by alloc_buffer to a high-level string.
        The returned string will be truncated to needed_size.
        """
        assert allocated_size >= needed_size

        if gc_buf and (allocated_size == needed_size):
            return hlstrtype(gc_buf)

        new_buf = lltype.malloc(STRTYPE, needed_size)
        try:
            str_chars_offset = (offsetof(STRTYPE, 'chars') + \
                                itemoffsetof(STRTYPE.chars, 0))
            if gc_buf:
                src = cast_ptr_to_adr(gc_buf) + str_chars_offset
            else:
                src = cast_ptr_to_adr(raw_buf) + itemoffsetof(TYPEP.TO, 0)
            dest = cast_ptr_to_adr(new_buf) + str_chars_offset
            ## FIXME: This is bad, because dest could potentially move
            ## if there are threads involved.
            raw_memcopy(src, dest, llmemory.sizeof(ll_char_type) * needed_size)
            return hlstrtype(new_buf)
        finally:
            keepalive_until_here(new_buf)
Пример #7
0
def encode_type_shape(builder, info, TYPE):
    """Encode the shape of the TYPE into the TYPE_INFO structure 'info'."""
    offsets = offsets_to_gc_pointers(TYPE)
    info.ofstoptrs = builder.offsets2table(offsets, TYPE)
    info.finalizer = builder.make_finalizer_funcptr_for_type(TYPE)
    info.weakptrofs = weakpointer_offset(TYPE)
    if not TYPE._is_varsize():
        #info.isvarsize = False
        #info.gcptrinvarsize = False
        info.fixedsize = llarena.round_up_for_allocation(llmemory.sizeof(TYPE))
        info.ofstolength = -1
        # note about round_up_for_allocation(): in the 'info' table
        # we put a rounded-up size only for fixed-size objects.  For
        # varsize ones, the GC must anyway compute the size at run-time
        # and round up that result.
    else:
        #info.isvarsize = True
        info.fixedsize = llmemory.sizeof(TYPE, 0)
        if isinstance(TYPE, lltype.Struct):
            ARRAY = TYPE._flds[TYPE._arrayfld]
            ofs1 = llmemory.offsetof(TYPE, TYPE._arrayfld)
            info.ofstolength = ofs1 + llmemory.ArrayLengthOffset(ARRAY)
            info.ofstovar = ofs1 + llmemory.itemoffsetof(ARRAY, 0)
        else:
            ARRAY = TYPE
            info.ofstolength = llmemory.ArrayLengthOffset(ARRAY)
            info.ofstovar = llmemory.itemoffsetof(TYPE, 0)
        assert isinstance(ARRAY, lltype.Array)
        if ARRAY.OF != lltype.Void:
            offsets = offsets_to_gc_pointers(ARRAY.OF)
        else:
            offsets = ()
        info.varofstoptrs = builder.offsets2table(offsets, ARRAY.OF)
        info.varitemsize = llmemory.sizeof(ARRAY.OF)
Пример #8
0
def gc_pointers_inside(v, adr, mutable_only=False):
    t = lltype.typeOf(v)
    if isinstance(t, lltype.Struct):
        if mutable_only and t._hints.get('immutable'):
            return
        for n, t2 in t._flds.iteritems():
            if isinstance(t2, lltype.Ptr) and t2.TO._gckind == 'gc':
                yield adr + llmemory.offsetof(t, n)
            elif isinstance(t2, (lltype.Array, lltype.Struct)):
                for a in gc_pointers_inside(getattr(v, n),
                                            adr + llmemory.offsetof(t, n),
                                            mutable_only):
                    yield a
    elif isinstance(t, lltype.Array):
        if mutable_only and t._hints.get('immutable'):
            return
        if isinstance(t.OF, lltype.Ptr) and t.OF.TO._gckind == 'gc':
            for i in range(len(v.items)):
                yield adr + llmemory.itemoffsetof(t, i)
        elif isinstance(t.OF, lltype.Struct):
            for i in range(len(v.items)):
                for a in gc_pointers_inside(v.items[i],
                                            adr + llmemory.itemoffsetof(t, i),
                                            mutable_only):
                    yield a
Пример #9
0
    def str_from_buffer(raw_buf, gc_buf, allocated_size, needed_size):
        """
        Converts from a pair returned by alloc_buffer to a high-level string.
        The returned string will be truncated to needed_size.
        """
        assert allocated_size >= needed_size

        if gc_buf and (allocated_size == needed_size):
            return hlstrtype(gc_buf)

        new_buf = lltype.malloc(STRTYPE, needed_size)
        try:
            str_chars_offset = (offsetof(STRTYPE, 'chars') + \
                                itemoffsetof(STRTYPE.chars, 0))
            if gc_buf:
                src = cast_ptr_to_adr(gc_buf) + str_chars_offset
            else:
                src = cast_ptr_to_adr(raw_buf) + itemoffsetof(TYPEP.TO, 0)
            dest = cast_ptr_to_adr(new_buf) + str_chars_offset
            ## FIXME: This is bad, because dest could potentially move
            ## if there are threads involved.
            raw_memcopy(src, dest,
                        llmemory.sizeof(ll_char_type) * needed_size)
            return hlstrtype(new_buf)
        finally:
            keepalive_until_here(new_buf)
Пример #10
0
def encode_type_shape(builder, info, TYPE, index):
    """Encode the shape of the TYPE into the TYPE_INFO structure 'info'."""
    offsets = offsets_to_gc_pointers(TYPE)
    infobits = index
    info.ofstoptrs = builder.offsets2table(offsets, TYPE)
    #
    kind_and_fptr = builder.special_funcptr_for_type(TYPE)
    if kind_and_fptr is not None:
        kind, fptr = kind_and_fptr
        info.finalizer_or_customtrace = fptr
        if kind == "finalizer":
            infobits |= T_HAS_FINALIZER
        elif kind == 'light_finalizer':
            infobits |= T_HAS_FINALIZER | T_HAS_LIGHTWEIGHT_FINALIZER
        elif kind == "custom_trace":
            infobits |= T_HAS_CUSTOM_TRACE
        else:
            assert 0, kind
    #
    if not TYPE._is_varsize():
        info.fixedsize = llarena.round_up_for_allocation(
            llmemory.sizeof(TYPE), builder.GCClass.object_minimal_size)
        # note about round_up_for_allocation(): in the 'info' table
        # we put a rounded-up size only for fixed-size objects.  For
        # varsize ones, the GC must anyway compute the size at run-time
        # and round up that result.
    else:
        infobits |= T_IS_VARSIZE
        varinfo = lltype.cast_pointer(GCData.VARSIZE_TYPE_INFO_PTR, info)
        info.fixedsize = llmemory.sizeof(TYPE, 0)
        if isinstance(TYPE, lltype.Struct):
            ARRAY = TYPE._flds[TYPE._arrayfld]
            ofs1 = llmemory.offsetof(TYPE, TYPE._arrayfld)
            varinfo.ofstolength = ofs1 + llmemory.ArrayLengthOffset(ARRAY)
            varinfo.ofstovar = ofs1 + llmemory.itemoffsetof(ARRAY, 0)
        else:
            assert isinstance(TYPE, lltype.GcArray)
            ARRAY = TYPE
            if (isinstance(ARRAY.OF, lltype.Ptr)
                    and ARRAY.OF.TO._gckind == 'gc'):
                infobits |= T_IS_GCARRAY_OF_GCPTR
            varinfo.ofstolength = llmemory.ArrayLengthOffset(ARRAY)
            varinfo.ofstovar = llmemory.itemoffsetof(TYPE, 0)
        assert isinstance(ARRAY, lltype.Array)
        if ARRAY.OF != lltype.Void:
            offsets = offsets_to_gc_pointers(ARRAY.OF)
        else:
            offsets = ()
        if len(offsets) > 0:
            infobits |= T_HAS_GCPTR_IN_VARSIZE
        varinfo.varofstoptrs = builder.offsets2table(offsets, ARRAY.OF)
        varinfo.varitemsize = llmemory.sizeof(ARRAY.OF)
    if builder.is_weakref_type(TYPE):
        infobits |= T_IS_WEAKREF
    if is_subclass_of_object(TYPE):
        infobits |= T_IS_RPYTHON_INSTANCE
    info.infobits = infobits | T_KEY_VALUE
Пример #11
0
def encode_type_shape(builder, info, TYPE, index):
    """Encode the shape of the TYPE into the TYPE_INFO structure 'info'."""
    offsets = offsets_to_gc_pointers(TYPE)
    infobits = index
    info.ofstoptrs = builder.offsets2table(offsets, TYPE)
    #
    kind_and_fptr = builder.special_funcptr_for_type(TYPE)
    if kind_and_fptr is not None:
        kind, fptr = kind_and_fptr
        info.finalizer_or_customtrace = fptr
        if kind == "finalizer":
            infobits |= T_HAS_FINALIZER
        elif kind == "light_finalizer":
            infobits |= T_HAS_FINALIZER | T_HAS_LIGHTWEIGHT_FINALIZER
        elif kind == "custom_trace":
            infobits |= T_HAS_CUSTOM_TRACE
        else:
            assert 0, kind
    #
    if not TYPE._is_varsize():
        info.fixedsize = llarena.round_up_for_allocation(llmemory.sizeof(TYPE), builder.GCClass.object_minimal_size)
        # note about round_up_for_allocation(): in the 'info' table
        # we put a rounded-up size only for fixed-size objects.  For
        # varsize ones, the GC must anyway compute the size at run-time
        # and round up that result.
    else:
        infobits |= T_IS_VARSIZE
        varinfo = lltype.cast_pointer(GCData.VARSIZE_TYPE_INFO_PTR, info)
        info.fixedsize = llmemory.sizeof(TYPE, 0)
        if isinstance(TYPE, lltype.Struct):
            ARRAY = TYPE._flds[TYPE._arrayfld]
            ofs1 = llmemory.offsetof(TYPE, TYPE._arrayfld)
            varinfo.ofstolength = ofs1 + llmemory.ArrayLengthOffset(ARRAY)
            varinfo.ofstovar = ofs1 + llmemory.itemoffsetof(ARRAY, 0)
        else:
            assert isinstance(TYPE, lltype.GcArray)
            ARRAY = TYPE
            if isinstance(ARRAY.OF, lltype.Ptr) and ARRAY.OF.TO._gckind == "gc":
                infobits |= T_IS_GCARRAY_OF_GCPTR
            varinfo.ofstolength = llmemory.ArrayLengthOffset(ARRAY)
            varinfo.ofstovar = llmemory.itemoffsetof(TYPE, 0)
        assert isinstance(ARRAY, lltype.Array)
        if ARRAY.OF != lltype.Void:
            offsets = offsets_to_gc_pointers(ARRAY.OF)
        else:
            offsets = ()
        if len(offsets) > 0:
            infobits |= T_HAS_GCPTR_IN_VARSIZE
        varinfo.varofstoptrs = builder.offsets2table(offsets, ARRAY.OF)
        varinfo.varitemsize = llmemory.sizeof(ARRAY.OF)
    if builder.is_weakref_type(TYPE):
        infobits |= T_IS_WEAKREF
    if is_subclass_of_object(TYPE):
        infobits |= T_IS_RPYTHON_INSTANCE
    info.infobits = infobits | T_KEY_VALUE
Пример #12
0
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.
    """
    allocated = len(l.items)

    # 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
    items = l.items
    newitems = malloc(typeOf(l).TO.items.TO, new_allocated)
    before_len = l.length
    if before_len < new_allocated:
        p = before_len - 1
    else:
        p = new_allocated - 1
    ITEM = typeOf(l).TO.ITEM
    if isinstance(ITEM, Ptr):
        while p >= 0:
            newitems[p] = items[p]
            items[p] = nullptr(ITEM.TO)
            p -= 1
    else:
        source = cast_ptr_to_adr(items) + itemoffsetof(typeOf(l.items).TO, 0)
        dest = cast_ptr_to_adr(newitems) + itemoffsetof(typeOf(l.items).TO, 0)
        s = p + 1
        raw_memcopy(source, dest, sizeof(ITEM) * s)
        keepalive_until_here(items)
    l.length = newsize
    l.items = newitems
Пример #13
0
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.
    """
    allocated = len(l.items)

    # 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
    items = l.items
    newitems = malloc(typeOf(l).TO.items.TO, new_allocated)
    before_len = l.length
    if before_len < new_allocated:
        p = before_len - 1
    else:
        p = new_allocated - 1
    ITEM = typeOf(l).TO.ITEM
    if isinstance(ITEM, Ptr):
        while p >= 0:
            newitems[p] = items[p]
            items[p] = nullptr(ITEM.TO)
            p -= 1
    else:
        source = cast_ptr_to_adr(items) + itemoffsetof(typeOf(l.items).TO, 0)
        dest = cast_ptr_to_adr(newitems) + itemoffsetof(typeOf(l.items).TO, 0)
        s = p + 1
        raw_memcopy(source, dest, sizeof(ITEM) * s)
        keepalive_until_here(items)
    l.length = newsize
    l.items = newitems
Пример #14
0
 def get_type_id(self, TYPE):
     try:
         return self.id_of_type[TYPE]
     except KeyError:
         assert not self.finished_tables
         assert isinstance(TYPE, (lltype.GcStruct, lltype.GcArray))
         # Record the new type_id description as a small dict for now.
         # It will be turned into a Struct("type_info") in finish()
         type_id = len(self.type_info_list)
         info = {}
         self.type_info_list.append(info)
         self.id_of_type[TYPE] = type_id
         offsets = offsets_to_gc_pointers(TYPE)
         info["ofstoptrs"] = self.offsets2table(offsets, TYPE)
         info["finalyzer"] = self.finalizer_funcptr_for_type(TYPE)
         if not TYPE._is_varsize():
             info["isvarsize"] = False
             info["fixedsize"] = llmemory.sizeof(TYPE)
             info["ofstolength"] = -1
         else:
             info["isvarsize"] = True
             info["fixedsize"] = llmemory.sizeof(TYPE, 0)
             if isinstance(TYPE, lltype.Struct):
                 ARRAY = TYPE._flds[TYPE._arrayfld]
                 ofs1 = llmemory.offsetof(TYPE, TYPE._arrayfld)
                 info["ofstolength"] = ofs1 + llmemory.ArrayLengthOffset(ARRAY)
                 if ARRAY.OF != lltype.Void:
                     info["ofstovar"] = ofs1 + llmemory.itemoffsetof(ARRAY, 0)
                 else:
                     info["fixedsize"] = ofs1 + llmemory.sizeof(lltype.Signed)
                 if ARRAY._hints.get('isrpystring'):
                     info["fixedsize"] = llmemory.sizeof(TYPE, 1)
             else:
                 ARRAY = TYPE
                 info["ofstolength"] = llmemory.ArrayLengthOffset(ARRAY)
                 if ARRAY.OF != lltype.Void:
                     info["ofstovar"] = llmemory.itemoffsetof(TYPE, 0)
                 else:
                     info["fixedsize"] = llmemory.ArrayLengthOffset(ARRAY) + llmemory.sizeof(lltype.Signed)
             assert isinstance(ARRAY, lltype.Array)
             if ARRAY.OF != lltype.Void:
                 offsets = offsets_to_gc_pointers(ARRAY.OF)
                 info["varofstoptrs"] = self.offsets2table(offsets, ARRAY.OF)
                 info["varitemsize"] = llmemory.sizeof(ARRAY.OF)
             else:
                 info["varofstoptrs"] = self.offsets2table((), lltype.Void)
                 info["varitemsize"] = llmemory.sizeof(ARRAY.OF)
         return type_id
Пример #15
0
def ll_shrink_array(p, smallerlength):
    from pypy.rpython.lltypesystem.lloperation import llop
    from pypy.rlib.objectmodel import keepalive_until_here

    if llop.shrink_array(lltype.Bool, p, smallerlength):
        return p  # done by the GC
    # XXX we assume for now that the type of p is GcStruct containing a
    # variable array, with no further pointers anywhere, and exactly one
    # field in the fixed part -- like STR and UNICODE.

    TP = lltype.typeOf(p).TO
    newp = lltype.malloc(TP, smallerlength)

    assert len(TP._names) == 2
    field = getattr(p, TP._names[0])
    setattr(newp, TP._names[0], field)

    ARRAY = getattr(TP, TP._arrayfld)
    offset = (llmemory.offsetof(TP, TP._arrayfld) +
              llmemory.itemoffsetof(ARRAY, 0))
    source_addr = llmemory.cast_ptr_to_adr(p) + offset
    dest_addr = llmemory.cast_ptr_to_adr(newp) + offset
    llmemory.raw_memcopy(source_addr, dest_addr,
                         llmemory.sizeof(ARRAY.OF) * smallerlength)

    keepalive_until_here(p)
    keepalive_until_here(newp)
    return newp
Пример #16
0
Файл: rgc.py Проект: ieure/pypy
def ll_shrink_array(p, smallerlength):
    from pypy.rpython.lltypesystem.lloperation import llop
    from pypy.rlib.objectmodel import keepalive_until_here

    if llop.shrink_array(lltype.Bool, p, smallerlength):
        return p    # done by the GC
    # XXX we assume for now that the type of p is GcStruct containing a
    # variable array, with no further pointers anywhere, and exactly one
    # field in the fixed part -- like STR and UNICODE.

    TP = lltype.typeOf(p).TO
    newp = lltype.malloc(TP, smallerlength)

    assert len(TP._names) == 2
    field = getattr(p, TP._names[0])
    setattr(newp, TP._names[0], field)

    ARRAY = getattr(TP, TP._arrayfld)
    offset = (llmemory.offsetof(TP, TP._arrayfld) +
              llmemory.itemoffsetof(ARRAY, 0))
    source_addr = llmemory.cast_ptr_to_adr(p)    + offset
    dest_addr   = llmemory.cast_ptr_to_adr(newp) + offset
    llmemory.raw_memcopy(source_addr, dest_addr, 
                         llmemory.sizeof(ARRAY.OF) * smallerlength)

    keepalive_until_here(p)
    keepalive_until_here(newp)
    return newp
Пример #17
0
 def _gct_resize_buffer_no_realloc(self, hop, v_lgt):
     op = hop.spaceop
     meth = self.gct_fv_gc_malloc_varsize
     flags = {'flavor':'gc', 'varsize': True, 'keep_current_args': True}
     self.varsize_malloc_helper(hop, flags, meth, [])
     # fish resvar
     v_newbuf = hop.llops[-1].result
     v_src = op.args[0]
     TYPE = v_src.concretetype.TO
     c_fldname = rmodel.inputconst(lltype.Void, TYPE._arrayfld)
     v_adrsrc = hop.genop('cast_ptr_to_adr', [v_src],
                          resulttype=llmemory.Address)
     v_adrnewbuf = hop.genop('cast_ptr_to_adr', [v_newbuf],
                             resulttype=llmemory.Address)
     ofs = (llmemory.offsetof(TYPE, TYPE._arrayfld) +
            llmemory.itemoffsetof(getattr(TYPE, TYPE._arrayfld), 0))
     v_ofs = rmodel.inputconst(lltype.Signed, ofs)
     v_adrsrc = hop.genop('adr_add', [v_adrsrc, v_ofs],
                          resulttype=llmemory.Address)
     v_adrnewbuf = hop.genop('adr_add', [v_adrnewbuf, v_ofs],
                             resulttype=llmemory.Address)
     size = llmemory.sizeof(getattr(TYPE, TYPE._arrayfld).OF)
     c_size = rmodel.inputconst(lltype.Signed, size)
     v_lgtsym = hop.genop('int_mul', [c_size, v_lgt],
                          resulttype=lltype.Signed) 
     vlist = [v_adrsrc, v_adrnewbuf, v_lgtsym]
     hop.genop('raw_memcopy', vlist)
Пример #18
0
 def _gct_resize_buffer_no_realloc(self, hop, v_lgt):
     op = hop.spaceop
     meth = self.gct_fv_gc_malloc_varsize
     flags = {'flavor': 'gc', 'varsize': True, 'keep_current_args': True}
     self.varsize_malloc_helper(hop, flags, meth, [])
     # fish resvar
     v_newbuf = hop.llops[-1].result
     v_src = op.args[0]
     TYPE = v_src.concretetype.TO
     c_fldname = rmodel.inputconst(lltype.Void, TYPE._arrayfld)
     v_adrsrc = hop.genop('cast_ptr_to_adr', [v_src],
                          resulttype=llmemory.Address)
     v_adrnewbuf = hop.genop('cast_ptr_to_adr', [v_newbuf],
                             resulttype=llmemory.Address)
     ofs = (llmemory.offsetof(TYPE, TYPE._arrayfld) +
            llmemory.itemoffsetof(getattr(TYPE, TYPE._arrayfld), 0))
     v_ofs = rmodel.inputconst(lltype.Signed, ofs)
     v_adrsrc = hop.genop('adr_add', [v_adrsrc, v_ofs],
                          resulttype=llmemory.Address)
     v_adrnewbuf = hop.genop('adr_add', [v_adrnewbuf, v_ofs],
                             resulttype=llmemory.Address)
     size = llmemory.sizeof(getattr(TYPE, TYPE._arrayfld).OF)
     c_size = rmodel.inputconst(lltype.Signed, size)
     v_lgtsym = hop.genop('int_mul', [c_size, v_lgt],
                          resulttype=lltype.Signed)
     vlist = [v_adrsrc, v_adrnewbuf, v_lgtsym]
     hop.genop('raw_memcopy', vlist)
Пример #19
0
 def f():
     a = llstr("xyz")
     b = (llmemory.cast_ptr_to_adr(a) +
          llmemory.offsetof(STR, 'chars') +
          llmemory.itemoffsetof(STR.chars, 0))
     buf = rffi.cast(rffi.VOIDP, b)
     return buf[2]
Пример #20
0
def gc_pointers_inside(v, adr):
    t = lltype.typeOf(v)
    if isinstance(t, lltype.Struct):
        for n, t2 in t._flds.iteritems():
            if isinstance(t2, lltype.Ptr) and t2.TO._gckind == 'gc':
                yield adr + llmemory.offsetof(t, n)
            elif isinstance(t2, (lltype.Array, lltype.Struct)):
                for a in gc_pointers_inside(getattr(v, n), adr + llmemory.offsetof(t, n)):
                    yield a
    elif isinstance(t, lltype.Array):
        if isinstance(t.OF, lltype.Ptr) and t2._needsgc():
            for i in range(len(v.items)):
                yield adr + llmemory.itemoffsetof(t, i)
        elif isinstance(t.OF, lltype.Struct):
            for i in range(len(v.items)):
                for a in gc_pointers_inside(v.items[i], adr + llmemory.itemoffsetof(t, i)):
                    yield a
Пример #21
0
def encode_type_shape(builder, info, TYPE):
    """Encode the shape of the TYPE into the TYPE_INFO structure 'info'."""
    offsets = offsets_to_gc_pointers(TYPE)
    infobits = 0
    info.ofstoptrs = builder.offsets2table(offsets, TYPE)
    info.finalizer = builder.make_finalizer_funcptr_for_type(TYPE)
    if not TYPE._is_varsize():
        info.fixedsize = llarena.round_up_for_allocation(
            llmemory.sizeof(TYPE), builder.GCClass.object_minimal_size)
        # note about round_up_for_allocation(): in the 'info' table
        # we put a rounded-up size only for fixed-size objects.  For
        # varsize ones, the GC must anyway compute the size at run-time
        # and round up that result.
    else:
        infobits |= T_IS_VARSIZE
        varinfo = lltype.cast_pointer(GCData.VARSIZE_TYPE_INFO_PTR, info)
        info.fixedsize = llmemory.sizeof(TYPE, 0)
        if isinstance(TYPE, lltype.Struct):
            ARRAY = TYPE._flds[TYPE._arrayfld]
            ofs1 = llmemory.offsetof(TYPE, TYPE._arrayfld)
            varinfo.ofstolength = ofs1 + llmemory.ArrayLengthOffset(ARRAY)
            varinfo.ofstovar = ofs1 + llmemory.itemoffsetof(ARRAY, 0)
        else:
            assert isinstance(TYPE, lltype.GcArray)
            ARRAY = TYPE
            if (isinstance(ARRAY.OF, lltype.Ptr)
                    and ARRAY.OF.TO._gckind == 'gc'):
                infobits |= T_IS_GCARRAY_OF_GCPTR
            varinfo.ofstolength = llmemory.ArrayLengthOffset(ARRAY)
            varinfo.ofstovar = llmemory.itemoffsetof(TYPE, 0)
        assert isinstance(ARRAY, lltype.Array)
        if ARRAY.OF != lltype.Void:
            offsets = offsets_to_gc_pointers(ARRAY.OF)
        else:
            offsets = ()
        if len(offsets) > 0:
            infobits |= T_HAS_GCPTR_IN_VARSIZE
        varinfo.varofstoptrs = builder.offsets2table(offsets, ARRAY.OF)
        varinfo.varitemsize = llmemory.sizeof(ARRAY.OF)
    if TYPE == WEAKREF:
        infobits |= T_IS_WEAKREF
    info.infobits = infobits
Пример #22
0
def encode_type_shape(builder, info, TYPE, index):
    """Encode the shape of the TYPE into the TYPE_INFO structure 'info'."""
    offsets = offsets_to_gc_pointers(TYPE)
    infobits = index
    info.ofstoptrs = builder.offsets2table(offsets, TYPE)
    info.finalizer = builder.make_finalizer_funcptr_for_type(TYPE)
    if not TYPE._is_varsize():
        info.fixedsize = llarena.round_up_for_allocation(
            llmemory.sizeof(TYPE), builder.GCClass.object_minimal_size)
        # note about round_up_for_allocation(): in the 'info' table
        # we put a rounded-up size only for fixed-size objects.  For
        # varsize ones, the GC must anyway compute the size at run-time
        # and round up that result.
    else:
        infobits |= T_IS_VARSIZE
        varinfo = lltype.cast_pointer(GCData.VARSIZE_TYPE_INFO_PTR, info)
        info.fixedsize = llmemory.sizeof(TYPE, 0)
        if isinstance(TYPE, lltype.Struct):
            ARRAY = TYPE._flds[TYPE._arrayfld]
            ofs1 = llmemory.offsetof(TYPE, TYPE._arrayfld)
            varinfo.ofstolength = ofs1 + llmemory.ArrayLengthOffset(ARRAY)
            varinfo.ofstovar = ofs1 + llmemory.itemoffsetof(ARRAY, 0)
        else:
            assert isinstance(TYPE, lltype.GcArray)
            ARRAY = TYPE
            if (isinstance(ARRAY.OF, lltype.Ptr)
                and ARRAY.OF.TO._gckind == 'gc'):
                infobits |= T_IS_GCARRAY_OF_GCPTR
            varinfo.ofstolength = llmemory.ArrayLengthOffset(ARRAY)
            varinfo.ofstovar = llmemory.itemoffsetof(TYPE, 0)
        assert isinstance(ARRAY, lltype.Array)
        if ARRAY.OF != lltype.Void:
            offsets = offsets_to_gc_pointers(ARRAY.OF)
        else:
            offsets = ()
        if len(offsets) > 0:
            infobits |= T_HAS_GCPTR_IN_VARSIZE
        varinfo.varofstoptrs = builder.offsets2table(offsets, ARRAY.OF)
        varinfo.varitemsize = llmemory.sizeof(ARRAY.OF)
    if TYPE == WEAKREF:
        infobits |= T_IS_WEAKREF
    info.infobits = infobits
Пример #23
0
 def getinneraddr(self, obj, *offsets):
     TYPE = lltype.typeOf(obj).TO
     addr = llmemory.cast_ptr_to_adr(obj)
     for o in offsets:
         if isinstance(o, str):
             addr += llmemory.offsetof(TYPE, o)
             TYPE = getattr(TYPE, o)
         else:
             addr += llmemory.itemoffsetof(TYPE, o)
             TYPE = TYPE.OF
     return addr, TYPE
Пример #24
0
def longername(a, b, size):
    if 1:
        baseofs = itemoffsetof(TP, 0)
        onesize = sizeof(TP.OF)
        size = baseofs + onesize*(size - 1)
        raw_memcopy(cast_ptr_to_adr(b)+baseofs, cast_ptr_to_adr(a)+baseofs, size)
    else:
        a = []
        for i in range(x):
            a.append(i)
    return 0
Пример #25
0
 def getinneraddr(self, obj, *offsets):
     TYPE = lltype.typeOf(obj).TO
     addr = llmemory.cast_ptr_to_adr(obj)
     for o in offsets:
         if isinstance(o, str):
             addr += llmemory.offsetof(TYPE, o)
             TYPE = getattr(TYPE, o)
         else:
             addr += llmemory.itemoffsetof(TYPE, o)
             TYPE = TYPE.OF
     return addr, TYPE
Пример #26
0
def test_sizeof_array_with_no_length():
    py.test.skip("inprogress")
    A = lltype.GcArray(lltype.Signed, hints={'nolength': True})
    a = lltype.malloc(A, 5, zero=True)
    
    arraysize = llmemory.itemoffsetof(A, 10)
    signedsize = llmemory.sizeof(lltype.Signed)
    def f():
        return a[0] + arraysize-signedsize*10
    fn = compile_function(f, [])
    res = fn()
    assert res == 0
Пример #27
0
    def str_from_buffer(raw_buf, gc_buf, allocated_size, needed_size):
        """
        Converts from a pair returned by alloc_buffer to a high-level string.
        The returned string will be truncated to needed_size.
        """
        assert allocated_size >= needed_size

        if gc_buf and (allocated_size == needed_size):
            return hlstrtype(gc_buf)

        new_buf = lltype.malloc(STRTYPE, needed_size)
        str_chars_offset = offsetof(STRTYPE, "chars") + itemoffsetof(STRTYPE.chars, 0)
        if gc_buf:
            src = cast_ptr_to_adr(gc_buf) + str_chars_offset
        else:
            src = cast_ptr_to_adr(raw_buf) + itemoffsetof(TYPEP.TO, 0)
        dest = cast_ptr_to_adr(new_buf) + str_chars_offset
        raw_memcopy(src, dest, llmemory.sizeof(ll_char_type) * needed_size)
        keepalive_until_here(gc_buf)
        keepalive_until_here(new_buf)
        return hlstrtype(new_buf)
Пример #28
0
def longername(a, b, size):
    if 1:
        baseofs = itemoffsetof(TP, 0)
        onesize = sizeof(TP.OF)
        size = baseofs + onesize * (size - 1)
        raw_memcopy(
            cast_ptr_to_adr(b) + baseofs,
            cast_ptr_to_adr(a) + baseofs, size)
    else:
        a = []
        for i in range(x):
            a.append(i)
    return 0
Пример #29
0
def test_sizeof_array_with_no_length():
    A = lltype.GcArray(lltype.Signed, hints={'nolength': True})
    B = lltype.Array(lltype.Signed, hints={'nolength': True})
    a = lltype.malloc(A, 5, zero=True)
    
    arraysize = llmemory.itemoffsetof(A, 10)
    signedsize = llmemory.sizeof(lltype.Signed)
    b_items = llmemory.ArrayItemsOffset(B)
    def f():
        return (a[0] + arraysize-signedsize*10) * 1000 + b_items
    fn = compile_function(f, [])
    res = fn()
    assert res == 0
Пример #30
0
    def str_from_buffer(raw_buf, gc_buf, allocated_size, needed_size):
        """
        Converts from a pair returned by alloc_buffer to a high-level string.
        The returned string will be truncated to needed_size.
        """
        assert allocated_size >= needed_size

        if gc_buf and (allocated_size == needed_size):
            return hlstrtype(gc_buf)

        new_buf = lltype.malloc(STRTYPE, needed_size)
        str_chars_offset = (offsetof(STRTYPE, 'chars') + \
                            itemoffsetof(STRTYPE.chars, 0))
        if gc_buf:
            src = cast_ptr_to_adr(gc_buf) + str_chars_offset
        else:
            src = cast_ptr_to_adr(raw_buf) + itemoffsetof(TYPEP.TO, 0)
        dest = cast_ptr_to_adr(new_buf) + str_chars_offset
        raw_memcopy(src, dest, llmemory.sizeof(ll_char_type) * needed_size)
        keepalive_until_here(gc_buf)
        keepalive_until_here(new_buf)
        return hlstrtype(new_buf)
Пример #31
0
def test_sizeof_array_with_no_length():
    A = lltype.GcArray(lltype.Signed, hints={'nolength': True})
    B = lltype.Array(lltype.Signed, hints={'nolength': True})
    a = lltype.malloc(A, 5, zero=True)

    arraysize = llmemory.itemoffsetof(A, 10)
    signedsize = llmemory.sizeof(lltype.Signed)
    b_items = llmemory.ArrayItemsOffset(B)

    def f():
        return (a[0] + arraysize - signedsize * 10) * 1000 + b_items

    fn = compile_function(f, [])
    res = fn()
    assert res == 0
Пример #32
0
 def free_nonmovingbuffer(data, buf):
     """
     Either free a non-moving buffer or keep the original storage alive.
     """
     # We cannot rely on rgc.can_move(data) here, because its result
     # might have changed since get_nonmovingbuffer().  Instead we check
     # if 'buf' points inside 'data'.  This is only possible if we
     # followed the 2nd case in get_nonmovingbuffer(); in the first case,
     # 'buf' points to its own raw-malloced memory.
     data = llstrtype(data)
     data_start = cast_ptr_to_adr(data) + offsetof(STRTYPE, "chars") + itemoffsetof(STRTYPE.chars, 0)
     followed_2nd_path = buf == cast(TYPEP, data_start)
     keepalive_until_here(data)
     if not followed_2nd_path:
         lltype.free(buf, flavor="raw")
Пример #33
0
 def free_nonmovingbuffer(data, buf):
     """
     Either free a non-moving buffer or keep the original storage alive.
     """
     # We cannot rely on rgc.can_move(data) here, because its result
     # might have changed since get_nonmovingbuffer().  Instead we check
     # if 'buf' points inside 'data'.  This is only possible if we
     # followed the 2nd case in get_nonmovingbuffer(); in the first case,
     # 'buf' points to its own raw-malloced memory.
     data = llstrtype(data)
     data_start = cast_ptr_to_adr(data) + \
         offsetof(STRTYPE, 'chars') + itemoffsetof(STRTYPE.chars, 0)
     followed_2nd_path = (buf == cast(TYPEP, data_start))
     keepalive_until_here(data)
     if not followed_2nd_path:
         lltype.free(buf, flavor='raw')
Пример #34
0
 def get_nonmovingbuffer(data):
     """
     Either returns a non-moving copy or performs neccessary pointer
     arithmetic to return a pointer to the characters of a string if the
     string is already nonmovable.  Must be followed by a
     free_nonmovingbuffer call.
     """
     if rgc.can_move(data):
         count = len(data)
         buf = lltype.malloc(TYPEP.TO, count, flavor='raw')
         for i in range(count):
             buf[i] = data[i]
         return buf
     else:
         data_start = cast_ptr_to_adr(llstrtype(data)) + \
             offsetof(STRTYPE, 'chars') + itemoffsetof(STRTYPE.chars, 0)
         return cast(TYPEP, data_start)
Пример #35
0
 def alloc_buffer(count):
     """
     Returns a (raw_buffer, gc_buffer) pair, allocated with count bytes.
     The raw_buffer can be safely passed to a native function which expects
     it to not move. Call str_from_buffer with the returned values to get a
     safe high-level string. When the garbage collector cooperates, this
     allows for the process to be performed without an extra copy.
     Make sure to call keep_buffer_alive_until_here on the returned values.
     """
     str_chars_offset = offsetof(STRTYPE, "chars") + itemoffsetof(STRTYPE.chars, 0)
     gc_buf = rgc.malloc_nonmovable(STRTYPE, count)
     if gc_buf:
         realbuf = cast_ptr_to_adr(gc_buf) + str_chars_offset
         raw_buf = cast(TYPEP, realbuf)
         return raw_buf, gc_buf
     else:
         raw_buf = lltype.malloc(TYPEP.TO, count, flavor="raw")
         return raw_buf, lltype.nullptr(STRTYPE)
Пример #36
0
 def alloc_buffer(count):
     """
     Returns a (raw_buffer, gc_buffer) pair, allocated with count bytes.
     The raw_buffer can be safely passed to a native function which expects
     it to not move. Call str_from_buffer with the returned values to get a
     safe high-level string. When the garbage collector cooperates, this
     allows for the process to be performed without an extra copy.
     Make sure to call keep_buffer_alive_until_here on the returned values.
     """
     str_chars_offset = (offsetof(STRTYPE, 'chars') + \
                         itemoffsetof(STRTYPE.chars, 0))
     gc_buf = rgc.malloc_nonmovable(STRTYPE, count)
     if gc_buf:
         realbuf = cast_ptr_to_adr(gc_buf) + str_chars_offset
         raw_buf = cast(TYPEP, realbuf)
         return raw_buf, gc_buf
     else:
         raw_buf = lltype.malloc(TYPEP.TO, count, flavor='raw')
         return raw_buf, lltype.nullptr(STRTYPE)
Пример #37
0
def test_itemoffsetof_fixedsizearray():
    ARRAY = lltype.FixedSizeArray(lltype.Signed, 5)
    itemoffsets = [llmemory.itemoffsetof(ARRAY, i) for i in range(5)]
    a = lltype.malloc(ARRAY, immortal=True)
    def f():
        adr = llmemory.cast_ptr_to_adr(a)
        result = 0
        for i in range(5):
            a[i] = i + 1
        for i in range(5):
            result = result * 10 + (adr + itemoffsets[i]).signed[0]
        for i in range(5):
            (adr + itemoffsets[i]).signed[0] = i
        for i in range(5):
            result = 10 * result + a[i]
        return result
    fn, t = getcompiled(f, [])
    res = fn()
    assert res == 1234501234
Пример #38
0
def test_itemoffsetof_fixedsizearray():
    ARRAY = lltype.FixedSizeArray(lltype.Signed, 5)
    itemoffsets = [llmemory.itemoffsetof(ARRAY, i) for i in range(5)]
    a = lltype.malloc(ARRAY, immortal=True)
    def f():
        adr = llmemory.cast_ptr_to_adr(a)
        result = 0
        for i in range(5):
            a[i] = i + 1
        for i in range(5):
            result = result * 10 + (adr + itemoffsets[i]).signed[0]
        for i in range(5):
            (adr + itemoffsets[i]).signed[0] = i
        for i in range(5):
            result = 10 * result + a[i]
        return result
    fn = compile_function(f, [])
    res = fn()
    assert res == 1234501234
Пример #39
0
def test_itemoffsetof():
    ARRAY = lltype.GcArray(lltype.Signed)
    itemoffsets = [llmemory.itemoffsetof(ARRAY, i) for i in range(5)]
    def f():
        a = lltype.malloc(ARRAY, 5)
        adr = llmemory.cast_ptr_to_adr(a)
        result = 0
        for i in range(5):
            a[i] = i + 1
        for i in range(5):
            result = result * 10 + (adr + itemoffsets[i]).signed[0]
        for i in range(5):
            (adr + itemoffsets[i]).signed[0] = i
        for i in range(5):
            result = 10 * result + a[i]
        return result
    fn, t = getcompiled(f, [])
    res = fn()
    assert res == 1234501234
Пример #40
0
def test_structarray_add():
    from pypy.rpython.lltypesystem import llmemory
    S = Struct("S", ("x", Signed))
    PS = Ptr(S)
    size = llmemory.sizeof(S)
    A = GcArray(S)
    itemoffset = llmemory.itemoffsetof(A, 0)
    def llf(n):
        a = malloc(A, 5)
        a[0].x = 1
        a[1].x = 2
        a[2].x = 3
        a[3].x = 42
        a[4].x = 4
        adr_s = llmemory.cast_ptr_to_adr(a)
        adr_s += itemoffset + size * n
        s = llmemory.cast_adr_to_ptr(adr_s, PS)
        return s.x
    fn = compile_function(llf, [int])
    res = fn(3)
    assert res == 42
Пример #41
0
def test_itemoffsetof():
    ARRAY = lltype.GcArray(lltype.Signed)
    itemoffsets = [llmemory.itemoffsetof(ARRAY, i) for i in range(5)]

    def f():
        a = lltype.malloc(ARRAY, 5)
        adr = llmemory.cast_ptr_to_adr(a)
        result = 0
        for i in range(5):
            a[i] = i + 1
        for i in range(5):
            result = result * 10 + (adr + itemoffsets[i]).signed[0]
        for i in range(5):
            (adr + itemoffsets[i]).signed[0] = i
        for i in range(5):
            result = 10 * result + a[i]
        return result

    fn = compile_function(f, [])
    res = fn()
    assert res == 1234501234
Пример #42
0
 def setarrayitem(self, array, index, newitem):
     ARRAY = lltype.typeOf(array).TO
     addr = llmemory.cast_ptr_to_adr(array)
     addr += llmemory.itemoffsetof(ARRAY, index)
     self.setinterior(array, addr, ARRAY.OF, newitem, (index,))
Пример #43
0
 def _str_ofs(item):
     return (llmemory.offsetof(TP, 'chars') +
             llmemory.itemoffsetof(TP.chars, 0) +
             llmemory.sizeof(CHAR_TP) * item)
Пример #44
0
 def setarrayitem(self, array, index, newitem):
     ARRAY = lltype.typeOf(array).TO
     addr = llmemory.cast_ptr_to_adr(array)
     addr += llmemory.itemoffsetof(ARRAY, index)
     self.setinterior(array, addr, ARRAY.OF, newitem)
Пример #45
0
 def _str_ofs(item):
     return (llmemory.offsetof(TP, 'chars') +
             llmemory.itemoffsetof(TP.chars, 0) +
             llmemory.sizeof(CHAR_TP) * item)
Пример #46
0
 def f():
     a = llstr("xyz")
     b = (llmemory.cast_ptr_to_adr(a) + llmemory.offsetof(STR, 'chars')
          + llmemory.itemoffsetof(STR.chars, 0))
     buf = rffi.cast(rffi.VOIDP, b)
     return buf[2]