def free_nonmovingbuffer(data, buf): """ Either free a non-moving buffer or keep the original storage alive. """ # We cannot rely on rgc.can_move(data) here, because its result # might have changed since get_nonmovingbuffer(). Instead we check # if 'buf' points inside 'data'. This is only possible if we # followed the 2nd case in get_nonmovingbuffer(); in the first case, # 'buf' points to its own raw-malloced memory. data = llstrtype(data) data_start = cast_ptr_to_adr(data) + offsetof(STRTYPE, "chars") + itemoffsetof(STRTYPE.chars, 0) followed_2nd_path = buf == cast(TYPEP, data_start) keepalive_until_here(data) if not followed_2nd_path: lltype.free(buf, flavor="raw")
def free_nonmovingbuffer(data, buf): """ Either free a non-moving buffer or keep the original storage alive. """ # We cannot rely on rgc.can_move(data) here, because its result # might have changed since get_nonmovingbuffer(). Instead we check # if 'buf' points inside 'data'. This is only possible if we # followed the 2nd case in get_nonmovingbuffer(); in the first case, # 'buf' points to its own raw-malloced memory. data = llstrtype(data) data_start = cast_ptr_to_adr(data) + \ offsetof(STRTYPE, 'chars') + itemoffsetof(STRTYPE.chars, 0) followed_2nd_path = (buf == cast(TYPEP, data_start)) keepalive_until_here(data) if not followed_2nd_path: lltype.free(buf, flavor='raw')
def get_nonmovingbuffer(data): """ Either returns a non-moving copy or performs neccessary pointer arithmetic to return a pointer to the characters of a string if the string is already nonmovable. Must be followed by a free_nonmovingbuffer call. """ if rgc.can_move(data): count = len(data) buf = lltype.malloc(TYPEP.TO, count, flavor="raw") for i in range(count): buf[i] = data[i] return buf else: data_start = cast_ptr_to_adr(llstrtype(data)) + offsetof(STRTYPE, "chars") + itemoffsetof(STRTYPE.chars, 0) return cast(TYPEP, data_start)
def get_nonmovingbuffer(data): """ Either returns a non-moving copy or performs neccessary pointer arithmetic to return a pointer to the characters of a string if the string is already nonmovable. Must be followed by a free_nonmovingbuffer call. """ if rgc.can_move(data): count = len(data) buf = lltype.malloc(TYPEP.TO, count, flavor='raw') for i in range(count): buf[i] = data[i] return buf else: data_start = cast_ptr_to_adr(llstrtype(data)) + \ offsetof(STRTYPE, 'chars') + itemoffsetof(STRTYPE.chars, 0) return cast(TYPEP, data_start)