| 1 |
import ctypes |
| 2 |
from rpython.rtyper.lltypesystem import lltype, ll2ctypes, llmemory, rffi |
| 3 |
from rpython.rlib.objectmodel import specialize |
| 4 |
from rpython.rlib.unroll import unrolling_iterable |
| 5 |
|
| 6 |
@specialize.memo() |
| 7 |
def get_field_token(STRUCT, fieldname, translate_support_code): |
| 8 |
if translate_support_code: |
| 9 |
return (llmemory.offsetof(STRUCT, fieldname), |
| 10 |
get_size(getattr(STRUCT, fieldname), True)) |
| 11 |
cstruct = ll2ctypes.get_ctypes_type(STRUCT) |
| 12 |
cfield = getattr(cstruct, fieldname) |
| 13 |
return (cfield.offset, cfield.size) |
| 14 |
|
| 15 |
@specialize.memo() |
| 16 |
def get_size(TYPE, translate_support_code): |
| 17 |
if translate_support_code: |
| 18 |
if TYPE._is_varsize(): |
| 19 |
return llmemory.sizeof(TYPE, 0) |
| 20 |
return llmemory.sizeof(TYPE) |
| 21 |
ctype = ll2ctypes.get_ctypes_type(TYPE) |
| 22 |
return ctypes.sizeof(ctype) |
| 23 |
|
| 24 |
@specialize.memo() |
| 25 |
def get_size_of_ptr(translate_support_code): |
| 26 |
return get_size(llmemory.GCREF, translate_support_code) |
| 27 |
|
| 28 |
@specialize.memo() |
| 29 |
def get_array_token(T, translate_support_code): |
| 30 |
|
| 31 |
if translate_support_code: |
| 32 |
basesize = llmemory.sizeof(T, 0) |
| 33 |
if isinstance(T, lltype.Struct): |
| 34 |
SUBARRAY = getattr(T, T._arrayfld) |
| 35 |
itemsize = llmemory.sizeof(SUBARRAY.OF) |
| 36 |
ofs_length = (llmemory.offsetof(T, T._arrayfld) + |
| 37 |
llmemory.ArrayLengthOffset(SUBARRAY)) |
| 38 |
else: |
| 39 |
if T._hints.get('nolength', None): |
| 40 |
ofs_length = -1 |
| 41 |
else: |
| 42 |
ofs_length = llmemory.ArrayLengthOffset(T) |
| 43 |
itemsize = llmemory.sizeof(T.OF) |
| 44 |
else: |
| 45 |
if isinstance(T, lltype.Struct): |
| 46 |
assert T._arrayfld is not None, "%r is not variable-sized" % (T,) |
| 47 |
cstruct = ll2ctypes.get_ctypes_type(T) |
| 48 |
cfield = getattr(cstruct, T._arrayfld) |
| 49 |
before_array_part = cfield.offset |
| 50 |
T = getattr(T, T._arrayfld) |
| 51 |
else: |
| 52 |
before_array_part = 0 |
| 53 |
carray = ll2ctypes.get_ctypes_type(T) |
| 54 |
if T._hints.get('nolength', None): |
| 55 |
ofs_length = -1 |
| 56 |
else: |
| 57 |
assert carray.length.size == WORD |
| 58 |
ofs_length = before_array_part + carray.length.offset |
| 59 |
basesize = before_array_part + carray.items.offset |
| 60 |
basesize += T._hints.get('extra_item_after_alloc', 0) |
| 61 |
carrayitem = ll2ctypes.get_ctypes_type(T.OF) |
| 62 |
itemsize = ctypes.sizeof(carrayitem) |
| 63 |
return basesize, itemsize, ofs_length |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
WORD = get_size(lltype.Signed, False) |
| 68 |
SIZEOF_CHAR = get_size(lltype.Char, False) |
| 69 |
SIZEOF_SHORT = get_size(rffi.SHORT, False) |
| 70 |
SIZEOF_INT = get_size(rffi.INT, False) |
| 71 |
SIZEOF_FLOAT = get_size(lltype.Float, False) |
| 72 |
|
| 73 |
unroll_basic_sizes = unrolling_iterable([ |
| 74 |
(lltype.Signed, lltype.Unsigned, WORD), |
| 75 |
(rffi.SIGNEDCHAR, lltype.Char, SIZEOF_CHAR), |
| 76 |
(rffi.SHORT, rffi.USHORT, SIZEOF_SHORT), |
| 77 |
(rffi.INT, rffi.UINT, SIZEOF_INT)]) |
| 78 |
|