[pypy-svn] r15913 - in pypy/dist/pypy/rpython/memory: . test
cfbolz at codespeak.net
cfbolz at codespeak.net
Wed Aug 10 16:03:00 CEST 2005
Author: cfbolz
Date: Wed Aug 10 16:02:58 2005
New Revision: 15913
Modified:
pypy/dist/pypy/rpython/memory/convertlltype.py
pypy/dist/pypy/rpython/memory/lladdress.py
pypy/dist/pypy/rpython/memory/lltypesimulation.py
pypy/dist/pypy/rpython/memory/simulator.py
pypy/dist/pypy/rpython/memory/test/test_address.py
pypy/dist/pypy/rpython/memory/test/test_simulator.py
Log:
removed the addr.attached stuff. now I have a block in the simulator
that is dedicated to functions and other Python objects.
Modified: pypy/dist/pypy/rpython/memory/convertlltype.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/convertlltype.py (original)
+++ pypy/dist/pypy/rpython/memory/convertlltype.py Wed Aug 10 16:02:58 2005
@@ -104,12 +104,12 @@
def convert_object(self, _obj, inline_to_addr, from_parent):
if inline_to_addr is not None:
- inline_to_addr.attached[0] = _obj
+ inline_to_addr.address[0] = lladdress.get_address_of_object(_obj)
return inline_to_addr
else:
addr = self.curraddress
- addr.attached[0] = _obj
- self.curraddress += struct.calcsize("i")
+ addr.address[0] = lladdress.get_address_of_object(_obj)
+ self.curraddress += struct.calcsize("P")
return addr
class FlowGraphConstantConverter(object):
Modified: pypy/dist/pypy/rpython/memory/lladdress.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/lladdress.py (original)
+++ pypy/dist/pypy/rpython/memory/lladdress.py Wed Aug 10 16:02:58 2005
@@ -79,35 +79,11 @@
convert_from = Address
convert_to = Address._getintattr
-# used to attach some real python object to the address:
-# for SomeObjects and Functions
-class _attached_accessor(_accessor):
- attached_objects = []
- object_to_number = {}
-
- format = "i"
- size = struct.calcsize("i")
-
- def convert_from(self, i):
- try:
- return self.attached_objects[i]
- except IndexError:
- raise ValueError, "trying to access invalid attached object"
-
- def convert_to(self, obj):
- try:
- return self.object_to_number[obj]
- except KeyError:
- num = len(self.attached_objects)
- self.object_to_number[obj] = num
- self.attached_objects.append(obj)
- return num
Address.signed = property(_signed_accessor)
Address.unsigned = property(_unsigned_accessor)
Address.char = property(_char_accessor)
Address.address = property(_address_accessor)
-Address.attached = property(_attached_accessor)
NULL = Address()
simulator = MemorySimulator()
@@ -121,6 +97,12 @@
def raw_memcopy(addr1, addr2, size):
simulator.memcopy(addr1.intaddress, addr2.intaddress, size)
+def get_address_of_object(obj):
+ return Address(simulator.get_address_of_object(obj))
+
+def get_py_object(address):
+ return simulator.get_py_object(address.intaddress)
+
supported_access_types = {"signed": lltype.Signed,
"unsigned": lltype.Unsigned,
Modified: pypy/dist/pypy/rpython/memory/lltypesimulation.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/lltypesimulation.py (original)
+++ pypy/dist/pypy/rpython/memory/lltypesimulation.py Wed Aug 10 16:02:58 2005
@@ -200,7 +200,7 @@
def _getobj(self):
assert isinstance(self._T, (lltype.FuncType, lltype.PyObjectType))
- return self._address.attached[0]
+ return lladdress.get_py_object(self._address.address[0])
_obj = property(_getobj)
def __call__(self, *args):
@@ -210,7 +210,7 @@
for a, ARG in zip(args, self._T.ARGS):
if lltype.typeOf(a) != ARG:
raise TypeError,"calling %r with wrong argument types: %r" % (self._T, args)
- callb = self._address.attached[0]._callable
+ callb = lladdress.get_py_object(self._address.address[0])._callable
if callb is None:
raise RuntimeError,"calling undefined function"
return callb(*args)
@@ -271,7 +271,6 @@
def nullptr(T):
return simulatorptr(lltype.Ptr(T), lladdress.NULL)
-#XXX unify attached objects with the address space as to samuele's suggestion
def functionptr(TYPE, name, **attrs):
if not isinstance(TYPE, lltype.FuncType):
raise TypeError, "functionptr() for FuncTypes only"
@@ -280,10 +279,11 @@
except TypeError:
raise TypeError("'%r' must be hashable"%attrs)
addr = lladdress.raw_malloc(get_total_size(TYPE))
- addr.attached[0] = lltype._func(TYPE, _name=name, **attrs)
+ addr.address[0] = lladdress.get_address_of_object(
+ lltype._func(TYPE, _name=name, **attrs))
return simulatorptr(lltype.Ptr(TYPE), addr)
def pyobjectptr(obj):
addr = lladdress.raw_malloc(get_total_size(lltype.PyObject))
- addr.attached[0] = lltype._pyobject(obj)
+ addr.address[0] = lladdress.get_address_of_object(lltype._pyobject(obj))
return simulatorptr(lltype.Ptr(lltype.PyObject), addr)
Modified: pypy/dist/pypy/rpython/memory/simulator.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/simulator.py (original)
+++ pypy/dist/pypy/rpython/memory/simulator.py Wed Aug 10 16:02:58 2005
@@ -58,11 +58,40 @@
other.memory[offset2:offset2+size] = self.memory[offset1:offset1+size]
other.status[offset2:offset2+size] = self.status[offset1:offset1+size]
+
+# block which stores functions and PyObects
+class ObjectBlock(object):
+ def __init__(self, baseaddress, size):
+ self.baseaddress = baseaddress
+ self.size = size
+ self.objects_to_num = {}
+ self.objects = []
+
+ def get_py_object(self, offset):
+ try:
+ return self.objects[offset]
+ except IndexError:
+ raise MemorySimulatorError, "trying to access unknown object"
+
+ def get_address_of_object(self, obj):
+ if obj in self.objects_to_num:
+ return self.objects_to_num[obj]
+ else:
+ assert len(self.objects) <= self.size
+ index = len(self.objects)
+ self.objects_to_num[obj] = index
+ self.objects.append(obj)
+ return index
+
+
+SIZE_OF_OBJECT_BLOCK = 2 ** 16 # arbitraly choosen size
+
class MemorySimulator(object):
size_of_simulated_ram = 64 * 1024 * 1024
def __init__(self, ram_size = None):
- self.blocks = []
- self.freememoryaddress = 4
+ self.objectblock = ObjectBlock(4, SIZE_OF_OBJECT_BLOCK)
+ self.blocks = [ObjectBlock(4, SIZE_OF_OBJECT_BLOCK)]
+ self.freememoryaddress = 4 + SIZE_OF_OBJECT_BLOCK
if ram_size is not None:
self.size_of_simulated_ram = ram_size
@@ -115,3 +144,13 @@
offset1 = address1 - block1.baseaddress
offset2 = address2 - block2.baseaddress
block1.memcopy(offset1, block2, offset2, size)
+
+ def get_py_object(self, address):
+ block = self.objectblock
+ offset = address - block.baseaddress
+ assert isinstance(block, ObjectBlock)
+ return block.get_py_object(offset)
+
+ def get_address_of_object(self, obj):
+ return (self.objectblock.get_address_of_object(obj) +
+ self.objectblock.baseaddress)
Modified: pypy/dist/pypy/rpython/memory/test/test_address.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/test/test_address.py (original)
+++ pypy/dist/pypy/rpython/memory/test/test_address.py Wed Aug 10 16:02:58 2005
@@ -6,6 +6,7 @@
from pypy.objspace.flow import FlowObjSpace
from pypy.rpython.memory.lladdress import Address, NULL
from pypy.rpython.memory.lladdress import raw_malloc, raw_free, raw_memcopy
+from pypy.rpython.memory.lladdress import get_py_object, get_address_of_object
from pypy.rpython.memory.simulator import MemorySimulatorError
class TestAddressAnnotation(object):
@@ -125,28 +126,28 @@
addr.char[10] = "c"
assert (addr + 10).char[0] == "c"
- def test_attached_pyobjects(self):
+ def test_pyobjects(self):
def f(x):
return x + 1
def g(x):
return x - 1
addr = raw_malloc(100)
- addr.attached[0] = f
- addr.attached[1] = g
- assert addr.attached[0] == f
- assert addr.attached[1] == g
- assert addr.attached[0](1) == 2
- assert addr.attached[1](0) == -1
+ addr.address[0] = get_address_of_object(f)
+ addr.address[1] = get_address_of_object(g)
+ assert get_py_object(addr.address[0]) == f
+ assert get_py_object(addr.address[1]) == g
+ assert get_py_object(addr.address[0])(1) == 2
+ assert get_py_object(addr.address[1])(0) == -1
def test_memcopy(self):
def f(x):
return x + 1
addr = raw_malloc(100)
- addr.attached[0] = f
+ addr.address[0] = get_address_of_object(f)
(addr + 10).signed[0] = 42
(addr + 20).char[0] = "a"
addr1 = raw_malloc(100)
raw_memcopy(addr, addr1, 100)
- assert addr1.attached[0](0) == 1
+ assert get_py_object(addr1.address[0])(0) == 1
assert (addr1 + 10).signed[0] == 42
assert (addr1 + 20).char[0] == "a"
Modified: pypy/dist/pypy/rpython/memory/test/test_simulator.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/test/test_simulator.py (original)
+++ pypy/dist/pypy/rpython/memory/test/test_simulator.py Wed Aug 10 16:02:58 2005
@@ -66,3 +66,15 @@
for i in xrange(10000000):
sim.malloc(4096)
py.test.raises(MemorySimulatorError, f)
+
+ def test_object_access(self):
+ sim = MemorySimulator()
+ def f(x):
+ return x + 1
+ def g(x):
+ return x + 2
+ a1 = sim.get_address_of_object(f)
+ a2 = sim.get_address_of_object(g)
+ assert sim.get_py_object(a1)(1) == 2
+ assert sim.get_py_object(a2)(1) == 3
+
More information about the Pypy-commit
mailing list