File: interp_struct.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (320 lines) | stat: -rw-r--r-- 11,941 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.rlib import clibffi
from rpython.rlib import libffi
from rpython.rlib import jit
from rpython.rlib.rgc import must_be_light_finalizer
from rpython.rlib.rarithmetic import r_uint, r_ulonglong, intmask
from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.typedef import TypeDef, interp_attrproperty, interp_attrproperty_w
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.error import OperationError, oefmt
from pypy.module._rawffi.alt.interp_ffitype import W_FFIType
from pypy.module._rawffi.alt.type_converter import FromAppLevelConverter, ToAppLevelConverter


class W_Field(W_Root):
    def __init__(self, name, w_ffitype):
        self.name = name
        self.w_ffitype = w_ffitype
        self.offset = -1

    def __repr__(self):
        return '<Field %s %s>' % (self.name, self.w_ffitype.name)

@unwrap_spec(name='text')
def descr_new_field(space, w_type, name, w_ffitype):
    w_ffitype = space.interp_w(W_FFIType, w_ffitype)
    return W_Field(name, w_ffitype)

W_Field.typedef = TypeDef(
    'Field',
    __new__ = interp2app(descr_new_field),
    name = interp_attrproperty('name', W_Field,
        wrapfn="newtext_or_none"),
    ffitype = interp_attrproperty_w('w_ffitype', W_Field),
    offset = interp_attrproperty('offset', W_Field,
        wrapfn="newint"),
    )


# ==============================================================================

class FFIStructOwner(object):
    """
    The only job of this class is to stay outside of the reference cycle
    W__StructDescr -> W_FFIType -> W__StructDescr and free the ffistruct
    """

    def __init__(self, ffistruct):
        self.ffistruct = ffistruct

    @must_be_light_finalizer
    def __del__(self):
        if self.ffistruct:
            lltype.free(self.ffistruct, flavor='raw', track_allocation=True)


class W__StructDescr(W_Root):

    def __init__(self, name):
        self.w_ffitype = W_FFIType('struct %s' % name, clibffi.FFI_TYPE_NULL,
                                   w_structdescr=self)
        self.fields_w = None
        self.name2w_field = {}
        self._ffistruct_owner = None

    def define_fields(self, space, w_fields):
        if self.fields_w is not None:
            raise oefmt(space.w_ValueError,
                        "%s's fields has already been defined",
                        self.w_ffitype.name)
        fields_w = space.fixedview(w_fields)
        # note that the fields_w returned by compute_size_and_alignement has a
        # different annotation than the original: list(W_Root) vs list(W_Field)
        size, alignment, fields_w = compute_size_and_alignement(space, fields_w)
        self.fields_w = fields_w
        field_types = [] # clibffi's types
        for w_field in fields_w:
            field_types.append(w_field.w_ffitype.get_ffitype())
            self.name2w_field[w_field.name] = w_field
        #
        # on CPython, the FFIStructOwner might go into gc.garbage and thus the
        # __del__ never be called. Thus, we don't track the allocation of the
        # malloc done inside this function, else the leakfinder might complain
        ffistruct = clibffi.make_struct_ffitype_e(size, alignment, field_types,
                                                  track_allocation=False)
        self.w_ffitype.set_ffitype(ffistruct.ffistruct)
        self._ffistruct_owner = FFIStructOwner(ffistruct)

    def check_complete(self, space):
        if self.fields_w is None:
            raise oefmt(space.w_ValueError,
                        "%s has an incomplete type", self.w_ffitype.name)

    def allocate(self, space):
        self.check_complete(space)
        return W__StructInstance(self)

    @unwrap_spec(addr=int)
    def fromaddress(self, space, addr):
        self.check_complete(space)
        rawmem = rffi.cast(rffi.VOIDP, addr)
        return W__StructInstance(self, allocate=False, autofree=True, rawmem=rawmem)

    def get_type_and_offset_for_field(self, space, w_name):
        name = space.text_w(w_name)
        try:
            return self._get_type_and_offset_for_field(space, name)
        except KeyError:
            raise OperationError(space.w_AttributeError, w_name)

    @jit.elidable_promote('0')
    def _get_type_and_offset_for_field(self, space, name):
        w_field = self.name2w_field[name]
        return w_field.w_ffitype, w_field.offset



@unwrap_spec(name='text')
def descr_new_structdescr(space, w_type, name, w_fields=None):
    descr = W__StructDescr(name)
    if not space.is_none(w_fields):
        descr.define_fields(space, w_fields)
    return descr

def round_up(size, alignment):
    return (size + alignment - 1) & -alignment

def compute_size_and_alignement(space, fields_w):
    size = 0
    alignment = 1
    fields_w2 = []
    for w_field in fields_w:
        w_field = space.interp_w(W_Field, w_field)
        fieldsize = w_field.w_ffitype.sizeof()
        fieldalignment = w_field.w_ffitype.get_alignment()
        alignment = max(alignment, fieldalignment)
        size = round_up(size, fieldalignment)
        w_field.offset = size
        size += fieldsize
        fields_w2.append(w_field)
    #
    size = round_up(size, alignment)
    return size, alignment, fields_w2



W__StructDescr.typedef = TypeDef(
    '_StructDescr',
    __new__ = interp2app(descr_new_structdescr),
    ffitype = interp_attrproperty_w('w_ffitype', W__StructDescr),
    define_fields = interp2app(W__StructDescr.define_fields),
    allocate = interp2app(W__StructDescr.allocate),
    fromaddress = interp2app(W__StructDescr.fromaddress),
    )


# ==============================================================================

NULL = lltype.nullptr(rffi.VOIDP.TO)

class W__StructInstance(W_Root):

    _immutable_fields_ = ['structdescr', 'rawmem']

    def __init__(self, structdescr, allocate=True, autofree=True, rawmem=NULL):
        self.structdescr = structdescr
        self.autofree = autofree
        if allocate:
            assert not rawmem
            assert autofree
            size = structdescr.w_ffitype.sizeof()
            self.rawmem = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw',
                                        zero=True, add_memory_pressure=True)
        else:
            self.rawmem = rawmem

    @must_be_light_finalizer
    def __del__(self):
        if self.autofree and self.rawmem:
            lltype.free(self.rawmem, flavor='raw')
            self.rawmem = lltype.nullptr(rffi.VOIDP.TO)

    def getaddr(self, space):
        addr = rffi.cast(lltype.Unsigned, self.rawmem)
        return space.newint(addr)

    def getfield(self, space, w_name):
        w_ffitype, offset = self.structdescr.get_type_and_offset_for_field(
            space, w_name)
        field_getter = GetFieldConverter(space, self.rawmem, offset)
        return field_getter.do_and_wrap(w_ffitype)

    def setfield(self, space, w_name, w_value):
        w_ffitype, offset = self.structdescr.get_type_and_offset_for_field(
            space, w_name)
        field_setter = SetFieldConverter(space, self.rawmem, offset)
        field_setter.unwrap_and_do(w_ffitype, w_value)


class GetFieldConverter(ToAppLevelConverter):
    """
    A converter used by W__StructInstance to get a field from the struct and
    wrap it to the correct app-level type.
    """

    def __init__(self, space, rawmem, offset):
        self.space = space
        self.rawmem = rawmem
        self.offset = offset

    def get_longlong(self, w_ffitype):
        return libffi.struct_getfield_longlong(libffi.types.slonglong,
                                               self.rawmem, self.offset)

    def get_ulonglong(self, w_ffitype):
        longlongval = libffi.struct_getfield_longlong(libffi.types.ulonglong,
                                                      self.rawmem, self.offset)
        return r_ulonglong(longlongval)


    def get_signed(self, w_ffitype):
        return libffi.struct_getfield_int(w_ffitype.get_ffitype(),
                                          self.rawmem, self.offset)

    def get_unsigned(self, w_ffitype):
        value = libffi.struct_getfield_int(w_ffitype.get_ffitype(),
                                           self.rawmem, self.offset)
        return r_uint(value)

    get_unsigned_which_fits_into_a_signed = get_signed
    get_pointer = get_unsigned

    def get_char(self, w_ffitype):
        intval = libffi.struct_getfield_int(w_ffitype.get_ffitype(),
                                            self.rawmem, self.offset)
        return rffi.cast(rffi.UCHAR, intval)

    def get_unichar(self, w_ffitype):
        intval = libffi.struct_getfield_int(w_ffitype.get_ffitype(),
                                            self.rawmem, self.offset)
        return rffi.cast(rffi.WCHAR_T, intval)

    def get_float(self, w_ffitype):
        return libffi.struct_getfield_float(w_ffitype.get_ffitype(),
                                            self.rawmem, self.offset)

    def get_singlefloat(self, w_ffitype):
        return libffi.struct_getfield_singlefloat(w_ffitype.get_ffitype(),
                                                  self.rawmem, self.offset)

    def get_struct(self, w_ffitype, w_structdescr):
        assert isinstance(w_structdescr, W__StructDescr)
        rawmem = rffi.cast(rffi.CCHARP, self.rawmem)
        innermem = rffi.cast(rffi.VOIDP, rffi.ptradd(rawmem, self.offset))
        # we return a reference to the inner struct, not a copy
        # autofree=False because it's still owned by the parent struct
        return W__StructInstance(w_structdescr, allocate=False, autofree=False,
                                 rawmem=innermem)

    ## def get_void(self, w_ffitype):
    ##     ...


class SetFieldConverter(FromAppLevelConverter):
    """
    A converter used by W__StructInstance to convert an app-level object to
    the corresponding low-level value and set the field of a structure.
    """

    def __init__(self, space, rawmem, offset):
        self.space = space
        self.rawmem = rawmem
        self.offset = offset

    def handle_signed(self, w_ffitype, w_obj, intval):
        libffi.struct_setfield_int(w_ffitype.get_ffitype(), self.rawmem, self.offset,
                                   intval)

    def handle_unsigned(self, w_ffitype, w_obj, uintval):
        libffi.struct_setfield_int(w_ffitype.get_ffitype(), self.rawmem, self.offset,
                                   intmask(uintval))

    handle_pointer = handle_signed
    handle_char = handle_signed
    handle_unichar = handle_signed

    def handle_longlong(self, w_ffitype, w_obj, longlongval):
        libffi.struct_setfield_longlong(w_ffitype.get_ffitype(),
                                        self.rawmem, self.offset, longlongval)

    def handle_float(self, w_ffitype, w_obj, floatval):
        libffi.struct_setfield_float(w_ffitype.get_ffitype(),
                                     self.rawmem, self.offset, floatval)

    def handle_singlefloat(self, w_ffitype, w_obj, singlefloatval):
        libffi.struct_setfield_singlefloat(w_ffitype.get_ffitype(),
                                           self.rawmem, self.offset, singlefloatval)

    def handle_struct(self, w_ffitype, w_structinstance):
        rawmem = rffi.cast(rffi.CCHARP, self.rawmem)
        dst = rffi.cast(rffi.VOIDP, rffi.ptradd(rawmem, self.offset))
        src = rffi.cast(rffi.CONST_VOIDP, w_structinstance.rawmem)
        length = w_ffitype.sizeof()
        rffi.c_memcpy(dst, src, length)

    ## def handle_char_p(self, w_ffitype, w_obj, strval):
    ##     ...

    ## def handle_unichar_p(self, w_ffitype, w_obj, unicodeval):
    ##     ...




W__StructInstance.typedef = TypeDef(
    '_StructInstance',
    getaddr  = interp2app(W__StructInstance.getaddr),
    getfield = interp2app(W__StructInstance.getfield),
    setfield = interp2app(W__StructInstance.setfield),
    )