本文整理汇总了Python中pypy.rpython.lltypesystem.lltype.malloc函数的典型用法代码示例。如果您正苦于以下问题:Python malloc函数的具体用法?Python malloc怎么用?Python malloc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了malloc函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_carray_to_ll
def test_carray_to_ll():
A = lltype.Array(lltype.Signed, hints={'nolength': True})
a = lltype.malloc(A, 10, flavor='raw')
a2 = lltype.malloc(A, 10, flavor='raw')
a[0] = 100
a[1] = 101
a[2] = 110
ac = lltype2ctypes(a)
b = ctypes2lltype(lltype.Ptr(A), ac)
assert lltype.typeOf(b) == lltype.Ptr(A)
assert b == a
assert not (b != a)
assert a == b
assert not (a != b)
assert b != lltype.nullptr(A)
assert not (b == lltype.nullptr(A))
assert lltype.nullptr(A) != b
assert not (lltype.nullptr(A) == b)
assert b != a2
assert not (b == a2)
assert a2 != b
assert not (a2 == b)
assert b[2] == 110
b[2] *= 2
assert a[2] == 220
a[2] *= 3
assert b[2] == 660
lltype.free(a, flavor='raw')
lltype.free(a2, flavor='raw')
开发者ID:TheDunn,项目名称:flex-pypy,代码行数:29,代码来源:test_ll2ctypes.py
示例2: f
def f(n):
while n > 0:
myjitdriver.can_enter_jit(n=n)
myjitdriver.jit_merge_point(n=n)
xy = XY()
xy.next1 = lltype.malloc(A, 0)
xy.next2 = lltype.malloc(A, 0)
xy.next3 = lltype.malloc(A, 0)
xy.next4 = lltype.malloc(A, 0)
xy.next5 = lltype.malloc(A, 0)
xy.n = n
exctx.topframeref = vref = virtual_ref(xy)
if n % 6 == 0:
xy.next1 = lltype.nullptr(A)
xy.next2 = lltype.nullptr(A)
xy.next3 = lltype.nullptr(A)
externalfn(n)
n -= 1
exctx.topframeref = vref_None
xy.next1 = lltype.nullptr(A)
xy.next2 = lltype.nullptr(A)
xy.next3 = lltype.nullptr(A)
xy.next4 = lltype.nullptr(A)
xy.next5 = lltype.nullptr(A)
virtual_ref_finish(vref, xy)
return exctx.m
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:26,代码来源:test_virtualref.py
示例3: test_fakeadr_eq
def test_fakeadr_eq():
S = lltype.GcStruct("S", ("x", lltype.Signed), ("y", lltype.Signed))
s = lltype.malloc(S)
assert cast_ptr_to_adr(s) == cast_ptr_to_adr(s)
adr1 = cast_ptr_to_adr(s) + FieldOffset(S, "x")
adr2 = cast_ptr_to_adr(s) + FieldOffset(S, "y")
adr3 = cast_ptr_to_adr(s) + FieldOffset(S, "y")
assert adr1 != adr2
assert adr2 == adr3
A = lltype.GcArray(lltype.Char)
a = lltype.malloc(A, 5)
adr1 = cast_ptr_to_adr(a) + ArrayLengthOffset(A)
adr2 = cast_ptr_to_adr(a) + ArrayLengthOffset(A)
assert adr1 == adr2
adr1 = cast_ptr_to_adr(a) + ArrayItemsOffset(A)
adr2 = cast_ptr_to_adr(a) + ArrayItemsOffset(A)
assert adr1 == adr2
adr2 += ItemOffset(lltype.Char, 0)
assert adr1 == adr2
adr1 += ItemOffset(lltype.Char, 2)
adr2 += ItemOffset(lltype.Char, 3)
assert adr1 != adr2
adr2 += ItemOffset(lltype.Char, -1)
assert adr1 == adr2
开发者ID:ieure,项目名称:pypy,代码行数:29,代码来源:test_llmemory.py
示例4: f
def f():
s1 = lltype.malloc(S)
llop.keepalive(lltype.Void, s1)
s2 = lltype.malloc(S)
llop.keepalive(lltype.Void, s1)
llop.keepalive(lltype.Void, s2)
return lltype.cast_ptr_to_int(s1) + lltype.cast_ptr_to_int(s2)
开发者ID:AishwaryaKM,项目名称:python-tutorial,代码行数:7,代码来源:test_removenoops.py
示例5: f
def f(n):
xy2 = self.setup2()
xy2.inst_x = 10
xy2.inst_l1 = lltype.malloc(ARRAY, 1)
xy2.inst_l1[0] = 1982731
xy2.inst_l2 = lltype.malloc(ARRAY, 1)
xy2.inst_l2[0] = 10000
other = self.setup2()
other.inst_x = 15
other.inst_l1 = lltype.malloc(ARRAY, 2)
other.inst_l1[0] = 189182
other.inst_l1[1] = 58421
other.inst_l2 = lltype.malloc(ARRAY, 2)
other.inst_l2[0] = 181
other.inst_l2[1] = 189
while n > 0:
myjitdriver.can_enter_jit(xy2=xy2, n=n, other=other)
myjitdriver.jit_merge_point(xy2=xy2, n=n, other=other)
promote_virtualizable(other, 'inst_l2')
length = len(other.inst_l2) # getfield_gc/arraylen_gc
value = other.inst_l2[0] # getfield_gc/getarrayitem_gc
other.inst_l2[0] = value + length # getfield_gc/setarrayitem_gc
promote_virtualizable(xy2, 'inst_l2')
xy2.inst_l2[0] = value + 100 # virtualized away
n -= 1
promote_virtualizable(xy2, 'inst_l2')
return xy2.inst_l2[0]
开发者ID:enyst,项目名称:plexnet,代码行数:27,代码来源:test_virtualizable.py
示例6: get_type_id
def get_type_id(self, TYPE):
try:
return self.id_of_type[TYPE]
except KeyError:
assert self.can_add_new_types
assert isinstance(TYPE, (lltype.GcStruct, lltype.GcArray))
# Record the new type_id description as a TYPE_INFO structure.
# build the TYPE_INFO structure
if not TYPE._is_varsize():
fullinfo = lltype.malloc(GCData.TYPE_INFO,
immortal=True, zero=True)
info = fullinfo
else:
fullinfo = lltype.malloc(GCData.VARSIZE_TYPE_INFO,
immortal=True, zero=True)
info = fullinfo.header
type_id = self.type_info_group.add_member(fullinfo)
if self.can_encode_type_shape:
encode_type_shape(self, info, TYPE, type_id.index)
else:
self._pending_type_shapes.append((info, TYPE, type_id.index))
# store it
self.id_of_type[TYPE] = type_id
self.add_vtable_after_typeinfo(TYPE)
return type_id
开发者ID:ieure,项目名称:pypy,代码行数:25,代码来源:gctypelayout.py
示例7: detect_floatformat
def detect_floatformat():
from pypy.rpython.lltypesystem import rffi, lltype
buf = lltype.malloc(rffi.CCHARP.TO, 8, flavor='raw')
rffi.cast(rffi.DOUBLEP, buf)[0] = 9006104071832581.0
packed = rffi.charpsize2str(buf, 8)
if packed == "\x43\x3f\xff\x01\x02\x03\x04\x05":
double_format = 'IEEE, big-endian'
elif packed == "\x05\x04\x03\x02\x01\xff\x3f\x43":
double_format = 'IEEE, little-endian'
else:
double_format = 'unknown'
lltype.free(buf, flavor='raw')
#
buf = lltype.malloc(rffi.CCHARP.TO, 4, flavor='raw')
rffi.cast(rffi.FLOATP, buf)[0] = rarithmetic.r_singlefloat(16711938.0)
packed = rffi.charpsize2str(buf, 4)
if packed == "\x4b\x7f\x01\x02":
float_format = 'IEEE, big-endian'
elif packed == "\x02\x01\x7f\x4b":
float_format = 'IEEE, little-endian'
else:
float_format = 'unknown'
lltype.free(buf, flavor='raw')
return double_format, float_format
开发者ID:gorakhargosh,项目名称:pypy,代码行数:25,代码来源:floattype.py
示例8: strxfrm
def strxfrm(space, s):
"string -> string. Returns a string that behaves for cmp locale-aware."
n1 = len(s) + 1
buf = lltype.malloc(rffi.CCHARP.TO, n1, flavor="raw", zero=True)
s_c = rffi.str2charp(s)
try:
n2 = _strxfrm(buf, s_c, n1) + 1
finally:
rffi.free_charp(s_c)
if n2 > n1:
# more space needed
lltype.free(buf, flavor="raw")
buf = lltype.malloc(rffi.CCHARP.TO, intmask(n2),
flavor="raw", zero=True)
s_c = rffi.str2charp(s)
try:
_strxfrm(buf, s_c, n2)
finally:
rffi.free_charp(s_c)
val = rffi.charp2str(buf)
lltype.free(buf, flavor="raw")
return space.wrap(val)
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:25,代码来源:interp_locale.py
示例9: test_immutable_to_old_promotion
def test_immutable_to_old_promotion(self):
T_CHILD = lltype.Ptr(lltype.GcStruct('Child', ('field', lltype.Signed)))
T_PARENT = lltype.Ptr(lltype.GcStruct('Parent', ('sub', T_CHILD)))
child = lltype.malloc(T_CHILD.TO)
child2 = lltype.malloc(T_CHILD.TO)
parent = lltype.malloc(T_PARENT.TO)
parent2 = lltype.malloc(T_PARENT.TO)
parent.sub = child
child.field = 3
parent2.sub = child2
child2.field = 8
T_ALL = lltype.Ptr(lltype.GcArray(T_PARENT))
all = lltype.malloc(T_ALL.TO, 2)
all[0] = parent
all[1] = parent2
def f(x, y):
res = all[x]
#all[x] = lltype.nullptr(T_PARENT.TO)
return res.sub.field
run, transformer = self.runner(f, nbargs=2, transformer=True)
run([1, 4])
assert len(transformer.layoutbuilder.addresses_of_static_ptrs) == 0
assert transformer.layoutbuilder.additional_roots_sources >= 4
开发者ID:antoine1fr,项目名称:pygirl,代码行数:26,代码来源:test_transformed_gc.py
示例10: test_multiple_incoming_links
def test_multiple_incoming_links():
S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
s1 = lltype.malloc(S1)
s1.x = 123
s2 = lltype.malloc(S1)
s2.x = 60
s3 = lltype.malloc(S1)
s3.x = 15
def fn(x):
y = x * 10
if x == 1:
x = s1.x
elif x == 2:
x = s2.x
elif x == 3:
x = s3.x
y = s1.x
return (x+1) + y
graph, t = get_graph(fn, [int])
constant_fold_graph(graph)
assert summary(graph) == {'int_mul': 1, 'int_eq': 3, 'int_add': 2}
for link in graph.iterlinks():
if Constant(139) in link.args:
break
else:
raise AssertionError("139 not found in the graph as a constant")
for i in range(4):
check_graph(graph, [i], fn(i), t)
开发者ID:antoine1fr,项目名称:pygirl,代码行数:29,代码来源:test_constfold.py
示例11: xxx_test_later_along_link
def xxx_test_later_along_link():
S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
s1 = lltype.malloc(S1)
s1.x = 123
s2 = lltype.malloc(S1)
s2.x = 60
def fn(x, y):
if x:
x = s1.x
else:
x = s2.x
y *= 2
return (x+1) - y
graph, t = get_graph(fn, [int, int])
assert summary(graph) == {'int_is_true': 1,
'getfield': 2,
'int_mul': 1,
'int_add': 1,
'int_sub': 1}
constant_fold_graph(graph)
assert summary(graph) == {'int_is_true': 1,
'int_mul': 1,
'int_sub': 1}
check_graph(graph, [-1], 124, t)
check_graph(graph, [0], 61, t)
开发者ID:antoine1fr,项目名称:pygirl,代码行数:26,代码来源:test_constfold.py
示例12: test_iterkeys
def test_iterkeys(self, space, api):
w_dict = space.sys.getdict(space)
py_dict = make_ref(space, w_dict)
ppos = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw')
pkey = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
pvalue = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
keys_w = []
values_w = []
try:
ppos[0] = 0
while api.PyDict_Next(w_dict, ppos, pkey, None):
w_key = from_ref(space, pkey[0])
keys_w.append(w_key)
ppos[0] = 0
while api.PyDict_Next(w_dict, ppos, None, pvalue):
w_value = from_ref(space, pvalue[0])
values_w.append(w_value)
finally:
lltype.free(ppos, flavor='raw')
lltype.free(pkey, flavor='raw')
lltype.free(pvalue, flavor='raw')
api.Py_DecRef(py_dict) # release borrowed references
assert space.eq_w(space.newlist(keys_w),
space.call_method(w_dict, "keys"))
assert space.eq_w(space.newlist(values_w),
space.call_method(w_dict, "values"))
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:30,代码来源:test_dictobject.py
示例13: time_time_llimpl
def time_time_llimpl():
void = lltype.nullptr(rffi.VOIDP.TO)
result = -1.0
if self.HAVE_GETTIMEOFDAY:
t = lltype.malloc(self.TIMEVAL, flavor='raw')
errcode = -1
if self.GETTIMEOFDAY_NO_TZ:
errcode = c_gettimeofday(t)
else:
errcode = c_gettimeofday(t, void)
if rffi.cast(rffi.LONG, errcode) == 0:
result = decode_timeval(t)
lltype.free(t, flavor='raw')
if result != -1:
return result
if self.HAVE_FTIME:
t = lltype.malloc(self.TIMEB, flavor='raw')
c_ftime(t)
result = (float(intmask(t.c_time)) +
float(intmask(t.c_millitm)) * 0.001)
lltype.free(t, flavor='raw')
return result
return float(c_time(void))
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:25,代码来源:ll_time.py
示例14: test_getfield_pure
def test_getfield_pure():
S1 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed))
S2 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
hints={'immutable': True})
accessor = rclass.FieldListAccessor()
#
s1 = lltype.malloc(S1); s1.x = 45
py.test.raises(TypeError, llop.getfield, lltype.Signed, s1, 'x')
s2 = lltype.malloc(S2); s2.x = 45
assert llop.getfield(lltype.Signed, s2, 'x') == 45
#
py.test.raises(TypeError, llop.getinteriorfield, lltype.Signed, s1, 'x')
assert llop.getinteriorfield(lltype.Signed, s2, 'x') == 45
#
for kind in [rclass.IR_MUTABLE, rclass.IR_IMMUTABLE,
rclass.IR_IMMUTABLE_ARRAY, rclass.IR_QUASIIMMUTABLE,
rclass.IR_QUASIIMMUTABLE_ARRAY]:
#
S3 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
hints={'immutable_fields': accessor})
accessor.initialize(S3, {'x': kind})
s3 = lltype.malloc(S3); s3.x = 46; s3.y = 47
if kind in [rclass.IR_IMMUTABLE, rclass.IR_IMMUTABLE_ARRAY]:
assert llop.getfield(lltype.Signed, s3, 'x') == 46
assert llop.getinteriorfield(lltype.Signed, s3, 'x') == 46
else:
py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'x')
py.test.raises(TypeError, llop.getinteriorfield,
lltype.Signed, s3, 'x')
py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'y')
py.test.raises(TypeError, llop.getinteriorfield,
lltype.Signed, s3, 'y')
开发者ID:ieure,项目名称:pypy,代码行数:32,代码来源:test_lloperation.py
示例15: setlen
def setlen(self, size, zero=False, overallocate=True):
if size > 0:
if size > self.allocated or size < self.allocated / 2:
if overallocate:
if size < 9:
some = 3
else:
some = 6
some += size >> 3
else:
some = 0
self.allocated = size + some
if zero:
new_buffer = lltype.malloc(mytype.arraytype,
self.allocated, flavor='raw',
add_memory_pressure=True,
zero=True)
else:
new_buffer = lltype.malloc(mytype.arraytype,
self.allocated, flavor='raw',
add_memory_pressure=True)
for i in range(min(size, self.len)):
new_buffer[i] = self.buffer[i]
else:
self.len = size
return
else:
assert size == 0
self.allocated = 0
new_buffer = lltype.nullptr(mytype.arraytype)
if self.buffer:
lltype.free(self.buffer, flavor='raw')
self.buffer = new_buffer
self.len = size
开发者ID:MichaelBlume,项目名称:pypy,代码行数:35,代码来源:interp_array.py
示例16: test_array_type_bug
def test_array_type_bug(self):
A = lltype.Array(lltype.Signed)
a1 = lltype.malloc(A, 0, flavor='raw')
a2 = lltype.malloc(A, 0, flavor='raw')
c1 = lltype2ctypes(a1)
c2 = lltype2ctypes(a2)
assert type(c1) is type(c2)
开发者ID:antoine1fr,项目名称:pygirl,代码行数:7,代码来源:test_ll2ctypes.py
示例17: test_gc_pointers_inside
def test_gc_pointers_inside():
from pypy.rpython import rclass
PT = lltype.Ptr(lltype.GcStruct('T'))
S1 = lltype.GcStruct('S', ('x', PT), ('y', PT))
S2 = lltype.GcStruct('S', ('x', PT), ('y', PT),
hints={'immutable': True})
accessor = rclass.FieldListAccessor()
S3 = lltype.GcStruct('S', ('x', PT), ('y', PT),
hints={'immutable_fields': accessor})
accessor.initialize(S3, {'x': IR_IMMUTABLE})
#
s1 = lltype.malloc(S1)
adr = llmemory.cast_ptr_to_adr(s1)
lst = list(gc_pointers_inside(s1._obj, adr, mutable_only=True))
expected = [adr + llmemory.offsetof(S1, 'x'),
adr + llmemory.offsetof(S1, 'y')]
assert lst == expected or lst == expected[::-1]
#
s2 = lltype.malloc(S2)
adr = llmemory.cast_ptr_to_adr(s2)
lst = list(gc_pointers_inside(s2._obj, adr, mutable_only=True))
assert lst == []
#
s3 = lltype.malloc(S3)
adr = llmemory.cast_ptr_to_adr(s3)
lst = list(gc_pointers_inside(s3._obj, adr, mutable_only=True))
assert lst == [adr + llmemory.offsetof(S3, 'y')]
开发者ID:ieure,项目名称:pypy,代码行数:27,代码来源:test_gctypelayout.py
示例18: estimate_best_nursery_size
def estimate_best_nursery_size():
"""Try to estimate the best nursery size at run-time, depending
on the machine we are running on.
"""
L2cache = 0
l2cache_p = lltype.malloc(rffi.LONGLONGP.TO, 1, flavor='raw')
try:
len_p = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw')
try:
size = rffi.sizeof(rffi.LONGLONG)
l2cache_p[0] = rffi.cast(rffi.LONGLONG, 0)
len_p[0] = rffi.cast(rffi.SIZE_T, size)
result = sysctlbyname("hw.l2cachesize",
rffi.cast(rffi.VOIDP, l2cache_p),
len_p,
lltype.nullptr(rffi.VOIDP.TO),
rffi.cast(rffi.SIZE_T, 0))
if (rffi.cast(lltype.Signed, result) == 0 and
rffi.cast(lltype.Signed, len_p[0]) == size):
L2cache = rffi.cast(lltype.Signed, l2cache_p[0])
if rffi.cast(rffi.LONGLONG, L2cache) != l2cache_p[0]:
L2cache = 0 # overflow!
finally:
lltype.free(len_p, flavor='raw')
finally:
lltype.free(l2cache_p, flavor='raw')
if L2cache > 0:
return best_nursery_size_for_L2cache(L2cache)
else:
# Print a warning even in non-debug builds
llop.debug_print(lltype.Void,
"Warning: cannot find your CPU L2 cache size with sysctl()")
return -1
开发者ID:antoine1fr,项目名称:pygirl,代码行数:33,代码来源:generation.py
示例19: get_darwin_sysctl_signed
def get_darwin_sysctl_signed(sysctl_name):
rval_p = lltype.malloc(rffi.LONGLONGP.TO, 1, flavor='raw')
try:
len_p = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw')
try:
size = rffi.sizeof(rffi.LONGLONG)
rval_p[0] = rffi.cast(rffi.LONGLONG, 0)
len_p[0] = rffi.cast(rffi.SIZE_T, size)
# XXX a hack for llhelper not being robust-enough
result = sysctlbyname(sysctl_name,
rffi.cast(rffi.VOIDP, rval_p),
len_p,
lltype.nullptr(rffi.VOIDP.TO),
rffi.cast(rffi.SIZE_T, 0))
rval = 0
if (rffi.cast(lltype.Signed, result) == 0 and
rffi.cast(lltype.Signed, len_p[0]) == size):
rval = rffi.cast(lltype.Signed, rval_p[0])
if rffi.cast(rffi.LONGLONG, rval) != rval_p[0]:
rval = 0 # overflow!
return rval
finally:
lltype.free(len_p, flavor='raw')
finally:
lltype.free(rval_p, flavor='raw')
开发者ID:ieure,项目名称:pypy,代码行数:25,代码来源:env.py
示例20: convert_const
def convert_const(self, value):
if self.ctypecheck(value):
key = "by_id", id(value)
keepalive = value
else:
if self.ownsmemory:
raise TyperError("convert_const(%r) but repr owns memory" % (
value,))
key = "by_value", value
keepalive = None
try:
return self.const_cache[key][0]
except KeyError:
self.setup()
p = lltype.malloc(self.r_memoryowner.lowleveltype.TO, zero=True)
self.initialize_const(p, value)
if self.ownsmemory:
result = p
else:
# we must return a non-memory-owning box that keeps the
# memory-owning box alive
result = lltype.malloc(self.lowleveltype.TO, zero=True)
result.c_data = p.c_data # initialize c_data pointer
result.c_data_owner_keepalive = p
self.const_cache[key] = result, keepalive
return result
开发者ID:TheDunn,项目名称:flex-pypy,代码行数:26,代码来源:rmodel.py
注:本文中的pypy.rpython.lltypesystem.lltype.malloc函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论