Source

pypy / pypy / rpython / lltypesystem / llmemory.py

The branch 'zlib-mem-pressure' does not exist.
  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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# this file contains the definitions and most extremely faked
# implementations of things relating to the description of the layout
# of objects in memeory.

# sizeof, offsetof

import weakref
from pypy.rlib.objectmodel import Symbolic
from pypy.rpython.lltypesystem import lltype
from pypy.tool.uid import uid
from pypy.rlib.rarithmetic import is_valid_int


class AddressOffset(Symbolic):

    def annotation(self):
        from pypy.annotation import model
        return model.SomeInteger()

    def lltype(self):
        return lltype.Signed

    def __add__(self, other):
        if not isinstance(other, AddressOffset):
            return NotImplemented
        return CompositeOffset(self, other)

    # special-casing: only for '>= 0' and '< 0' and only when the
    # symbolic offset is known to be non-negative
    def __ge__(self, other):
        if self is other:
            return True
        elif (is_valid_int(other) and other == 0 and
            self.known_nonneg()):
            return True
        else:
            raise TypeError("Symbolics cannot be compared! (%r, %r)"
                            % (self, other))

    def __lt__(self, other):
        return not self.__ge__(other)

    def known_nonneg(self):
        return False

    def _raw_malloc(self, rest, zero):
        raise NotImplementedError("_raw_malloc(%r, %r)" % (self, rest))

    def raw_memcopy(self, srcadr, dstadr):
        raise NotImplementedError("raw_memcopy(%r)" % (self,))


class ItemOffset(AddressOffset):

    def __init__(self, TYPE, repeat=1):
        self.TYPE = TYPE
        self.repeat = repeat

    def __repr__(self):
        return "<ItemOffset %r %r>" % (self.TYPE, self.repeat)

    def __mul__(self, other):
        if not is_valid_int(other):
            return NotImplemented
        return ItemOffset(self.TYPE, self.repeat * other)

    __rmul__ = __mul__

    def __neg__(self):
        return ItemOffset(self.TYPE, -self.repeat)

    def known_nonneg(self):
        return self.repeat >= 0

    def ref(self, firstitemptr):
        A = lltype.typeOf(firstitemptr).TO
        if A == self.TYPE:
            # for array of containers
            parent, index = lltype.parentlink(firstitemptr._obj)
            assert parent, "%r is not within a container" % (firstitemptr,)
            assert isinstance(lltype.typeOf(parent),
                              (lltype.Array, lltype.FixedSizeArray)), (
                "%r is not within an array" % (firstitemptr,))
            if isinstance(index, str):
                assert index.startswith('item')    # itemN => N
                index = int(index[4:])
            index += self.repeat
            if index == parent.getlength():
                # for references exactly to the end of the array
                try:
                    endmarker = _end_markers[parent]
                except KeyError:
                    endmarker = _endmarker_struct(A, parent=parent,
                                                  parentindex=index)
                    _end_markers[parent] = endmarker
                return endmarker._as_ptr()
            else:
                return parent.getitem(index)._as_ptr()
        elif ((isinstance(A, lltype.FixedSizeArray)
               or (isinstance(A, lltype.Array) and A._hints.get('nolength',
                                                                False)))
              and array_item_type_match(A.OF, self.TYPE)):
            # for array of primitives or pointers
            return lltype.direct_ptradd(firstitemptr, self.repeat)
        else:
            raise TypeError('got %r, expected %r' % (A, self.TYPE))

    def _raw_malloc(self, rest, zero):
        assert not rest
        if (isinstance(self.TYPE, lltype.ContainerType)
            and self.TYPE._gckind == 'gc'):
            assert self.repeat == 1
            p = lltype.malloc(self.TYPE, flavor='raw', zero=zero,
                              track_allocation=False)
            return cast_ptr_to_adr(p)
        else:
            T = lltype.FixedSizeArray(self.TYPE, self.repeat)
            p = lltype.malloc(T, flavor='raw', zero=zero,
                              track_allocation=False)
            array_adr = cast_ptr_to_adr(p)
            return array_adr + ArrayItemsOffset(T)

    def raw_memcopy(self, srcadr, dstadr):
        repeat = self.repeat
        if repeat == 0:
            return
        if isinstance(self.TYPE, lltype.ContainerType):
            PTR = lltype.Ptr(self.TYPE)
        elif self.TYPE == GCREF:
            self._raw_memcopy_gcrefs(srcadr, dstadr)
            return
        else:
            PTR = lltype.Ptr(lltype.FixedSizeArray(self.TYPE, 1))
        while True:
            src = cast_adr_to_ptr(srcadr, PTR)
            dst = cast_adr_to_ptr(dstadr, PTR)
            _reccopy(src, dst)
            repeat -= 1
            if repeat <= 0:
                break
            srcadr += ItemOffset(self.TYPE)
            dstadr += ItemOffset(self.TYPE)

    def _raw_memcopy_gcrefs(self, srcadr, dstadr):
        # special case to handle arrays of any GC pointers
        repeat = self.repeat
        while True:
            data = srcadr.address[0]
            dstadr.address[0] = data
            repeat -= 1
            if repeat <= 0:
                break
            srcadr += ItemOffset(self.TYPE)
            dstadr += ItemOffset(self.TYPE)

_end_markers = weakref.WeakKeyDictionary()  # <array of STRUCT> -> _endmarker
class _endmarker_struct(lltype._struct):
    __slots__ = ()
    def __new__(self, *args, **kwds):
        return object.__new__(self)
    def __init__(self, *args, **kwds):
        lltype._struct.__init__(self, *args, **kwds)
        self._storage = False
    def __getattr__(self, name):
        raise AttributeError("cannot access fields in the endmarker "
                             "structure at the end of the array")
    def __setattr__(self, name, value):
        if name.startswith('_'):
            object.__setattr__(self, name, value)  # '_xxx' attributes
        elif self._storage is False:
            raise AttributeError("cannot access fields in the endmarker "
                                 "structure at the end of the array")


class FieldOffset(AddressOffset):

    def __init__(self, TYPE, fldname):
        self.TYPE = TYPE
        self.fldname = fldname

    def __repr__(self):
        return "<FieldOffset %r %r>" % (self.TYPE, self.fldname)

    def known_nonneg(self):
        return True

    def ref(self, struct):
        if lltype.typeOf(struct).TO != self.TYPE:
            struct = lltype.cast_pointer(lltype.Ptr(self.TYPE), struct)
        FIELD = getattr(self.TYPE, self.fldname)
        if isinstance(FIELD, lltype.ContainerType):
            substruct = struct._obj._getattr(self.fldname)
            return substruct._as_ptr()
        else:
            return lltype.direct_fieldptr(struct, self.fldname)

    def _raw_malloc(self, rest, parenttype=None, zero=False):
        if self.fldname != self.TYPE._arrayfld:
            # for the error msg
            return AddressOffset._raw_malloc(self, rest, zero=zero)
        assert rest
        return rest[0]._raw_malloc(rest[1:], parenttype=parenttype or self.TYPE,
                                            zero=zero)

    def raw_memcopy(self, srcadr, dstadr):
        if self.fldname != self.TYPE._arrayfld:
            return AddressOffset.raw_memcopy(srcadr, dstadr) #for the error msg
        PTR = lltype.Ptr(self.TYPE)
        src = cast_adr_to_ptr(srcadr, PTR)
        dst = cast_adr_to_ptr(dstadr, PTR)
        _reccopy(src, dst)


class CompositeOffset(AddressOffset):

    def __new__(cls, *offsets):
        lst = []
        for item in offsets:
            if isinstance(item, CompositeOffset):
                lst.extend(item.offsets)
            else:
                lst.append(item)
        for i in range(len(lst)-2, -1, -1):
            if (isinstance(lst[i], ItemOffset) and
                isinstance(lst[i+1], ItemOffset) and
                lst[i].TYPE == lst[i+1].TYPE):
                lst[i:i+2] = [ItemOffset(lst[i].TYPE,
                                         lst[i].repeat + lst[i+1].repeat)]
        if len(lst) == 1:
            return lst[0]
        else:
            self = object.__new__(cls)
            self.offsets = lst
            return self

    def __repr__(self):
        return '< %s >' % (' + '.join([repr(item) for item in self.offsets]),)

    def __neg__(self):
        ofs = [-item for item in self.offsets]
        ofs.reverse()
        return CompositeOffset(*ofs)

    def known_nonneg(self):
        for item in self.offsets:
            if not item.known_nonneg():
                return False
        return True

    def ref(self, ptr):
        for item in self.offsets:
            ptr = item.ref(ptr)
        return ptr

    def _raw_malloc(self, rest, zero):
        return self.offsets[0]._raw_malloc(self.offsets[1:] + rest, zero=zero)

    def raw_memcopy(self, srcadr, dstadr):
        for o in self.offsets[:-1]:
            o.raw_memcopy(srcadr, dstadr)
            srcadr += o
            dstadr += o
        o = self.offsets[-1]
        o.raw_memcopy(srcadr, dstadr)


class ArrayItemsOffset(AddressOffset):

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

    def __repr__(self):
        return '< ArrayItemsOffset %r >' % (self.TYPE,)

    def known_nonneg(self):
        return True

    def ref(self, arrayptr):
        assert array_type_match(lltype.typeOf(arrayptr).TO, self.TYPE)
        if isinstance(self.TYPE.OF, lltype.ContainerType):
            # XXX this doesn't support empty arrays
            # XXX it's also missing 'solid' support, probably
            o = arrayptr._obj.getitem(0)
            return o._as_ptr()
        else:
            return lltype.direct_arrayitems(arrayptr)

    def _raw_malloc(self, rest, parenttype=None, zero=False):
        if rest:
            assert len(rest) == 1
            assert isinstance(rest[0], ItemOffset)
            assert self.TYPE.OF == rest[0].TYPE
            count = rest[0].repeat
        else:
            count = 0
        p = lltype.malloc(parenttype or self.TYPE, count,
                          immortal = self.TYPE._gckind == 'raw',
                          zero = zero,
                          track_allocation = False)
        return cast_ptr_to_adr(p)

    def raw_memcopy(self, srcadr, dstadr):
        # should really copy the length field, but we can't
        pass


class ArrayLengthOffset(AddressOffset):

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

    def __repr__(self):
        return '< ArrayLengthOffset %r >' % (self.TYPE,)

    def known_nonneg(self):
        return True

    def ref(self, arrayptr):
        assert array_type_match(lltype.typeOf(arrayptr).TO, self.TYPE)
        return lltype._arraylenref._makeptr(arrayptr._obj, arrayptr._solid)


class GCHeaderOffset(AddressOffset):
    def __init__(self, gcheaderbuilder):
        self.gcheaderbuilder = gcheaderbuilder

    def __repr__(self):
        return '< GCHeaderOffset >'

    def __neg__(self):
        return GCHeaderAntiOffset(self.gcheaderbuilder)

    def known_nonneg(self):
        return True

    def ref(self, headerptr):
        gcptr = self.gcheaderbuilder.object_from_header(headerptr)
        return gcptr

    def _raw_malloc(self, rest, zero):
        assert rest
        if isinstance(rest[0], GCHeaderAntiOffset):
            return rest[1]._raw_malloc(rest[2:], zero=zero)    # just for fun
        gcobjadr = rest[0]._raw_malloc(rest[1:], zero=zero)
        headerptr = self.gcheaderbuilder.new_header(gcobjadr.ptr)
        return cast_ptr_to_adr(headerptr)

    def raw_memcopy(self, srcadr, dstadr):
        _reccopy(srcadr.ptr, dstadr.ptr)

class GCHeaderAntiOffset(AddressOffset):
    def __init__(self, gcheaderbuilder):
        self.gcheaderbuilder = gcheaderbuilder

    def __repr__(self):
        return '< GCHeaderAntiOffset >'

    def __neg__(self):
        return GCHeaderOffset(self.gcheaderbuilder)

    def ref(self, gcptr):
        headerptr = self.gcheaderbuilder.header_of_object(gcptr)
        return headerptr

    def _raw_malloc(self, rest, zero):
        assert len(rest) >= 2
        assert isinstance(rest[0], GCHeaderOffset)
        return rest[1]._raw_malloc(rest[2:], zero=zero)

# ____________________________________________________________

def _sizeof_none(TYPE):
    assert not TYPE._is_varsize()
    return ItemOffset(TYPE)
_sizeof_none._annspecialcase_ = 'specialize:memo'

def _sizeof_int(TYPE, n):
    "NOT_RPYTHON"
    if isinstance(TYPE, lltype.Struct):
        return FieldOffset(TYPE, TYPE._arrayfld) + \
               itemoffsetof(TYPE._flds[TYPE._arrayfld], n)
    else:
        raise Exception("don't know how to take the size of a %r"%TYPE)

def sizeof(TYPE, n=None):
    if n is None:
        return _sizeof_none(TYPE)
    elif isinstance(TYPE, lltype.Array):
        return itemoffsetof(TYPE) + _sizeof_none(TYPE.OF) * n
    else:
        return _sizeof_int(TYPE, n)
sizeof._annspecialcase_ = 'specialize:arg(0)'

def offsetof(TYPE, fldname):
    assert fldname in TYPE._flds
    return FieldOffset(TYPE, fldname)
offsetof._annspecialcase_ = 'specialize:memo'

def itemoffsetof(TYPE, n=0):
    result = ArrayItemsOffset(TYPE)
    if n != 0:
        result += ItemOffset(TYPE.OF) * n
    return result
itemoffsetof._annspecialcase_ = 'specialize:memo'
# -------------------------------------------------------------

class fakeaddress(object):
    __slots__ = ['ptr']
    # NOTE: the 'ptr' in the addresses must be normalized.
    # Use cast_ptr_to_adr() instead of directly fakeaddress() if unsure.
    def __init__(self, ptr):
        if ptr is not None and ptr._obj0 is None:
            ptr = None   # null ptr => None
        self.ptr = ptr

    def __repr__(self):
        if self.ptr is None:
            s = 'NULL'
        else:
            #try:
            #    s = hex(self.ptr._cast_to_int())
            #except:
            s = str(self.ptr)
        return '<fakeaddr %s>' % (s,)

    def __add__(self, other):
        if isinstance(other, AddressOffset):
            if self.ptr is None:
                raise NullAddressError("offset from NULL address")
            return fakeaddress(other.ref(self.ptr))
        if other == 0:
            return self
        return NotImplemented

    def __sub__(self, other):
        if isinstance(other, AddressOffset):
            return self + (-other)
        if isinstance(other, fakeaddress):
            if self == other:
                return 0
            else:
                raise TypeError("cannot subtract fakeaddresses in general")
        if other == 0:
            return self
        return NotImplemented

    def __nonzero__(self):
        return self.ptr is not None

    #def __hash__(self):
    #    raise TypeError("don't put addresses in a prebuilt dictionary")

    def __eq__(self, other):
        if isinstance(other, fakeaddress):
            try:
                obj1 = self._fixup().ptr
                obj2 = other._fixup().ptr
                if obj1 is not None: obj1 = obj1._obj
                if obj2 is not None: obj2 = obj2._obj
                return obj1 == obj2
            except lltype.DelayedPointer:
                return self.ptr is other.ptr
        else:
            return NotImplemented

    def __ne__(self, other):
        if isinstance(other, fakeaddress):
            return not (self == other)
        else:
            return NotImplemented

    def __lt__(self, other):
        # for the convenience of debugging the GCs, NULL compares as the
        # smallest address even when compared with a non-fakearenaaddress
        if not isinstance(other, fakeaddress):
            raise TypeError("cannot compare fakeaddress and %r" % (
                other.__class__.__name__,))
        if not other:
            return False     # self < NULL              => False
        if not self:
            return True      # NULL < non-null-other    => True
        raise TypeError("cannot compare non-NULL fakeaddresses with '<'")
    def __le__(self, other):
        return self == other or self < other
    def __gt__(self, other):
        return not (self == other or self < other)
    def __ge__(self, other):
        return not (self < other)

    def ref(self):
        if not self:
            raise NullAddressError
        return self.ptr

    def _cast_to_ptr(self, EXPECTED_TYPE):
        addr = self._fixup()
        if addr:
            return cast_any_ptr(EXPECTED_TYPE, addr.ptr)
        else:
            return lltype.nullptr(EXPECTED_TYPE.TO)

    def _cast_to_int(self, symbolic=False):
        if self:
            if isinstance(self.ptr._obj0, int):    # tagged integer
                return self.ptr._obj0
            if symbolic:
                return AddressAsInt(self)
            else:
                # This is a bit annoying. We want this method to still work
                # when the pointed-to object is dead
                return self.ptr._cast_to_int(False)
        else:
            return 0

    def _fixup(self):
        if self.ptr is not None and self.ptr._was_freed():
            # hack to support llarena.test_replace_object_with_stub()
            from pypy.rpython.lltypesystem import llarena
            return llarena.getfakearenaaddress(self)
        else:
            return self

# ____________________________________________________________

class AddressAsInt(Symbolic):
    # a symbolic, rendered as an address cast to an integer.
    def __init__(self, adr):
        self.adr = adr
    def annotation(self):
        from pypy.annotation import model
        return model.SomeInteger()
    def lltype(self):
        return lltype.Signed
    def __eq__(self, other):
        return self.adr == cast_int_to_adr(other)
    def __ne__(self, other):
        return self.adr != cast_int_to_adr(other)
    def __nonzero__(self):
        return bool(self.adr)
    def __repr__(self):
        try:
            return '<AddressAsInt %s>' % (self.adr.ptr,)
        except AttributeError:
            return '<AddressAsInt at 0x%x>' % (uid(self),)

# ____________________________________________________________

class NullAddressError(Exception):
    pass

class DanglingPointerError(Exception):
    pass

NULL = fakeaddress(None)
Address = lltype.Primitive("Address", NULL)

# GCREF is similar to Address but it is GC-aware
GCREF = lltype.Ptr(lltype.GcOpaqueType('GCREF'))

# A placeholder for any type that is a GcArray of pointers.
# This can be used in the symbolic offsets above to access such arrays
# in a generic way.
GCARRAY_OF_PTR = lltype.GcArray(GCREF, hints={'placeholder': True})
gcarrayofptr_lengthoffset = ArrayLengthOffset(GCARRAY_OF_PTR)
gcarrayofptr_itemsoffset = ArrayItemsOffset(GCARRAY_OF_PTR)
gcarrayofptr_singleitemoffset = ItemOffset(GCARRAY_OF_PTR.OF)
def array_type_match(A1, A2):
    return A1 == A2 or (A2 == GCARRAY_OF_PTR and
                        isinstance(A1, lltype.GcArray) and
                        isinstance(A1.OF, lltype.Ptr) and
                        not A1._hints.get('nolength'))
def array_item_type_match(T1, T2):
    return T1 == T2 or (T2 == GCREF and isinstance(T1, lltype.Ptr))


class _fakeaccessor(object):
    def __init__(self, addr):
        self.addr = addr
    def __getitem__(self, index):
        ptr = self.addr.ref()
        if index != 0:
            ptr = lltype.direct_ptradd(ptr, index)
        return self.read_from_ptr(ptr)

    def __setitem__(self, index, value):
        assert lltype.typeOf(value) == self.TYPE
        ptr = self.addr.ref()
        if index != 0:
            ptr = lltype.direct_ptradd(ptr, index)
        self.write_into_ptr(ptr, value)

    def read_from_ptr(self, ptr):
        value = ptr[0]
        assert lltype.typeOf(value) == self.TYPE
        return value

    def write_into_ptr(self, ptr, value):
        ptr[0] = value


class _signed_fakeaccessor(_fakeaccessor):
    TYPE = lltype.Signed

class _float_fakeaccessor(_fakeaccessor):
    TYPE = lltype.Float

class _char_fakeaccessor(_fakeaccessor):
    TYPE = lltype.Char

class _address_fakeaccessor(_fakeaccessor):
    TYPE = Address

    def read_from_ptr(self, ptr):
        value = ptr[0]
        if isinstance(value, lltype._ptr):
            return value._cast_to_adr()
        elif lltype.typeOf(value) == Address:
            return value
        else:
            raise TypeError(value)

    def write_into_ptr(self, ptr, value):
        TARGETTYPE = lltype.typeOf(ptr).TO.OF
        if TARGETTYPE == Address:
            pass
        elif isinstance(TARGETTYPE, lltype.Ptr):
            value = cast_adr_to_ptr(value, TARGETTYPE)
        else:
            raise TypeError(TARGETTYPE)
        ptr[0] = value

supported_access_types = {"signed":    lltype.Signed,
                          "unsigned":  lltype.Unsigned,
                          "char":      lltype.Char,
                          "address":   Address,
                          "float":     lltype.Float,
                          }

fakeaddress.signed = property(_signed_fakeaccessor)
fakeaddress.float = property(_float_fakeaccessor)
fakeaddress.char = property(_char_fakeaccessor)
fakeaddress.address = property(_address_fakeaccessor)
fakeaddress._TYPE = Address

# the obtained address will not keep the object alive. e.g. if the object is
# only reachable through an address, it might get collected
def cast_ptr_to_adr(obj):
    assert isinstance(lltype.typeOf(obj), lltype.Ptr)
    return obj._cast_to_adr()

def cast_adr_to_ptr(adr, EXPECTED_TYPE):
    return adr._cast_to_ptr(EXPECTED_TYPE)

def cast_adr_to_int(adr, mode="emulated"):
    # The following modes are supported before translation (after
    # translation, it's all just a cast):
    # * mode="emulated": goes via lltype.cast_ptr_to_int(), which returns some
    #     number based on id().  The difference is that it works even if the
    #     address is that of a dead object.
    # * mode="symbolic": returns an AddressAsInt instance, which can only be
    #     cast back to an address later.
    # * mode="forced": uses rffi.cast() to return a real number.
    assert mode in ("emulated", "symbolic", "forced")
    res = adr._cast_to_int(symbolic = (mode != "emulated"))
    if mode == "forced":
        from pypy.rpython.lltypesystem.rffi import cast
        res = cast(lltype.Signed, res)
    return res

_NONGCREF = lltype.Ptr(lltype.OpaqueType('NONGCREF'))
def cast_int_to_adr(int):
    if isinstance(int, AddressAsInt):
        return int.adr
    try:
        ptr = lltype.cast_int_to_ptr(_NONGCREF, int)
    except ValueError:
        from pypy.rpython.lltypesystem import ll2ctypes
        ptr = ll2ctypes._int2obj[int]._as_ptr()
    return cast_ptr_to_adr(ptr)

# ____________________________________________________________
# Weakrefs.
#
# An object of type WeakRef is a small GC-managed object that contains
# a weak reference to another GC-managed object, as in regular Python.
#

class _WeakRefType(lltype.ContainerType):
    _gckind = 'gc'
    def __str__(self):
        return "WeakRef"

WeakRef = _WeakRefType()
WeakRefPtr = lltype.Ptr(WeakRef)

def weakref_create(ptarget):
    # ptarget should not be a nullptr
    PTRTYPE = lltype.typeOf(ptarget)
    assert isinstance(PTRTYPE, lltype.Ptr)
    assert PTRTYPE.TO._gckind == 'gc'
    assert ptarget
    return _wref(ptarget)._as_ptr()

def weakref_deref(PTRTYPE, pwref):
    # pwref should not be a nullptr
    assert isinstance(PTRTYPE, lltype.Ptr)
    assert PTRTYPE.TO._gckind == 'gc'
    assert lltype.typeOf(pwref) == WeakRefPtr
    p = pwref._obj._dereference()
    if p is None:
        return lltype.nullptr(PTRTYPE.TO)
    else:
        return cast_any_ptr(PTRTYPE, p)

class _wref(lltype._container):
    _gckind = 'gc'
    _TYPE = WeakRef

    def __init__(self, ptarget):
        if ptarget is None:
            self._obref = lambda: None
        else:
            obj = lltype.normalizeptr(ptarget)._obj
            self._obref = weakref.ref(obj)

    def _dereference(self):
        obj = self._obref()
        # in combination with a GC like the SemiSpace, the 'obj' can be
        # still alive in the CPython sense but freed by the arena logic.
        if obj is None or obj._was_freed():
            return None
        else:
            return obj._as_ptr()

    def __repr__(self):
        return '<%s>' % (self,)

    def __str__(self):
        return 'wref -> %s' % (self._obref(),)

# a prebuilt pointer to a dead low-level weakref
dead_wref = _wref(None)._as_ptr()

# The rest is to support the GC transformers: they can use it to build
# an explicit weakref object with some structure and then "hide" the
# result by casting it to a WeakRefPtr, and "reveal" it again.  In other
# words, weakref_create and weakref_deref are operations that exist only
# before the GC transformation, whereas the two cast operations below
# exist only after.  They are implemented here only to allow GC
# transformers to be tested on top of the llinterpreter.
def cast_ptr_to_weakrefptr(ptr):
    if ptr:
        return _gctransformed_wref(ptr)._as_ptr()
    else:
        return lltype.nullptr(WeakRef)

def cast_weakrefptr_to_ptr(PTRTYPE, pwref):
    assert lltype.typeOf(pwref) == WeakRefPtr
    if pwref:
        assert isinstance(pwref._obj, _gctransformed_wref)
        if PTRTYPE is not None:
            assert PTRTYPE == lltype.typeOf(pwref._obj._ptr)
        return pwref._obj._ptr
    else:
        return lltype.nullptr(PTRTYPE.TO)

class _gctransformed_wref(lltype._container):
    _gckind = 'gc'
    _TYPE = WeakRef
    def __init__(self, ptr):
        self._ptr = ptr
    def __repr__(self):
        return '<%s>' % (self,)
    def __str__(self):
        return 'gctransformed_wref(%s)' % (self._ptr,)
    def _normalizedcontainer(self, check=True):
        return self._ptr._getobj(check=check)._normalizedcontainer(check=check)
    def _was_freed(self):
        return self._ptr._was_freed()

# ____________________________________________________________

def raw_malloc(size):
    if not isinstance(size, AddressOffset):
        raise NotImplementedError(size)
    return size._raw_malloc([], zero=False)

def raw_free(adr):
    # try to free the whole object if 'adr' is the address of the header
    from pypy.rpython.memory.gcheader import GCHeaderBuilder
    try:
        objectptr = GCHeaderBuilder.object_from_header(adr.ptr)
    except KeyError:
        pass
    else:
        raw_free(cast_ptr_to_adr(objectptr))
    assert isinstance(adr.ref()._obj, lltype._parentable)
    adr.ptr._as_obj()._free()

def raw_malloc_usage(size):
    if isinstance(size, AddressOffset):
        # ouah
        from pypy.rpython.memory.lltypelayout import convert_offset_to_int
        size = convert_offset_to_int(size)
    return size

def raw_memclear(adr, size):
    if not isinstance(size, AddressOffset):
        raise NotImplementedError(size)
    assert lltype.typeOf(adr) == Address
    zeroadr = size._raw_malloc([], zero=True)
    size.raw_memcopy(zeroadr, adr)

def raw_memcopy(source, dest, size):
    assert lltype.typeOf(source) == Address
    assert lltype.typeOf(dest)   == Address
    size.raw_memcopy(source, dest)

def raw_memmove(source, dest, size):
    # for now let's assume that raw_memmove is the same as raw_memcopy,
    # when run on top of fake addresses, but we _free the source object
    raw_memcopy(source, dest, size)
    source.ptr._as_obj()._free()

def cast_any_ptr(EXPECTED_TYPE, ptr):
    # this is a generalization of the various cast_xxx_ptr() functions.
    PTRTYPE = lltype.typeOf(ptr)
    if PTRTYPE == EXPECTED_TYPE:
        return ptr
    elif EXPECTED_TYPE == WeakRefPtr:
        return cast_ptr_to_weakrefptr(ptr)
    elif PTRTYPE == WeakRefPtr:
        ptr = cast_weakrefptr_to_ptr(None, ptr)
        return cast_any_ptr(EXPECTED_TYPE, ptr)
    elif (isinstance(EXPECTED_TYPE.TO, lltype.OpaqueType) or
        isinstance(PTRTYPE.TO, lltype.OpaqueType)):
        return lltype.cast_opaque_ptr(EXPECTED_TYPE, ptr)
    else:
        # regular case
        return lltype.cast_pointer(EXPECTED_TYPE, ptr)


def _reccopy(source, dest):
    # copy recursively a structure or array onto another.
    T = lltype.typeOf(source).TO
    assert T == lltype.typeOf(dest).TO
    if isinstance(T, (lltype.Array, lltype.FixedSizeArray)):
        sourcelgt = source._obj.getlength()
        destlgt = dest._obj.getlength()
        lgt = min(sourcelgt, destlgt)
        ITEMTYPE = T.OF
        for i in range(lgt):
            if isinstance(ITEMTYPE, lltype.ContainerType):
                subsrc = source._obj.getitem(i)._as_ptr()
                subdst = dest._obj.getitem(i)._as_ptr()
                _reccopy(subsrc, subdst)
            else:
                # this is a hack XXX de-hack this
                llvalue = source._obj.getitem(i, uninitialized_ok=True)
                dest._obj.setitem(i, llvalue)
    elif isinstance(T, lltype.Struct):
        for name in T._names:
            FIELDTYPE = getattr(T, name)
            if isinstance(FIELDTYPE, lltype.ContainerType):
                subsrc = source._obj._getattr(name)._as_ptr()
                subdst = dest._obj._getattr(name)._as_ptr()
                _reccopy(subsrc, subdst)
            else:
                # this is a hack XXX de-hack this
                llvalue = source._obj._getattr(name, uninitialized_ok=True)
                setattr(dest._obj, name, llvalue)
    else:
        raise TypeError(T)

from pypy.rpython.extregistry import ExtRegistryEntry

class RawMemmoveEntry(ExtRegistryEntry):
    _about_ = raw_memmove

    def compute_result_annotation(self, s_from, s_to, s_size):
        from pypy.annotation.model import SomeAddress, SomeInteger
        assert isinstance(s_from, SomeAddress)
        assert isinstance(s_to, SomeAddress)
        assert isinstance(s_size, SomeInteger)
    
    def specialize_call(self, hop):
        hop.exception_cannot_occur()
        v_list = hop.inputargs(Address, Address, lltype.Signed)
        return hop.genop('raw_memmove', v_list)