From c7687ad7e6969bd72751741c5d8a5673d40fa51c Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 22 Aug 2014 21:48:30 +0100 Subject: py: Rename mp_builtin_id to mp_obj_id and make it public. --- py/obj.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'py/obj.c') diff --git a/py/obj.c b/py/obj.c index 430712058..0b57f24c5 100644 --- a/py/obj.c +++ b/py/obj.c @@ -352,6 +352,24 @@ mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, return i; } +mp_obj_t mp_obj_id(mp_obj_t o_in) { + mp_int_t id = (mp_int_t)o_in; + if (!MP_OBJ_IS_OBJ(o_in)) { + return mp_obj_new_int(id); + } else if (id >= 0) { + // Many OSes and CPUs have affinity for putting "user" memories + // into low half of address space, and "system" into upper half. + // We're going to take advantage of that and return small int + // (signed) for such "user" addresses. + return MP_OBJ_NEW_SMALL_INT(id); + } else { + // If that didn't work, well, let's return long int, just as + // a (big) positve value, so it will never clash with the range + // of small int returned in previous case. + return mp_obj_new_int_from_uint((mp_uint_t)id); + } +} + // will raise a TypeError if object has no length mp_obj_t mp_obj_len(mp_obj_t o_in) { mp_obj_t len = mp_obj_len_maybe(o_in); -- cgit v1.2.3