summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/emitbc.c5
-rw-r--r--py/misc.h3
-rw-r--r--py/stream.c15
-rw-r--r--py/stream.h2
4 files changed, 18 insertions, 7 deletions
diff --git a/py/emitbc.c b/py/emitbc.c
index ebc2ba5003..365ec458a2 100644
--- a/py/emitbc.c
+++ b/py/emitbc.c
@@ -50,7 +50,6 @@
struct _emit_t {
pass_kind_t pass : 8;
uint last_emit_was_return_value : 8;
- byte dummy_data[DUMMY_DATA_SIZE];
int stack_size;
@@ -67,6 +66,8 @@ struct _emit_t {
uint bytecode_offset;
uint bytecode_size;
byte *code_base; // stores both byte code and code info
+ // Accessed as uint, so must be aligned as such
+ byte dummy_data[DUMMY_DATA_SIZE];
};
STATIC void emit_bc_rot_two(emit_t *emit);
@@ -207,6 +208,8 @@ STATIC void emit_write_bytecode_byte_ptr(emit_t* emit, byte b, void *ptr) {
emit_write_bytecode_byte(emit, b);
emit_align_bytecode_to_machine_word(emit);
mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t));
+ // Verify thar c is already uint-aligned
+ assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
*c = (mp_uint_t)ptr;
}
diff --git a/py/misc.h b/py/misc.h
index c8ccdaa02e..94a3ffaaf8 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -84,6 +84,9 @@ int m_get_peak_bytes_allocated(void);
// get the number of elements in a fixed-size array
#define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+// align ptr to the nearest multiple of "alignment"
+#define MP_ALIGN(ptr, alignment) (void*)(((mp_uint_t)(ptr) + ((alignment) - 1)) & ~((alignment) - 1))
+
/** unichar / UTF-8 *********************************************/
typedef int unichar; // TODO
diff --git a/py/stream.c b/py/stream.c
index 98f62518a5..2b4410728f 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -98,18 +98,15 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
}
}
-STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
+mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len) {
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
if (o->type->stream_p == NULL || o->type->stream_p->write == NULL) {
// CPython: io.UnsupportedOperation, OSError subclass
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
}
- mp_buffer_info_t bufinfo;
- mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
-
int error;
- mp_int_t out_sz = o->type->stream_p->write(self_in, bufinfo.buf, bufinfo.len, &error);
+ mp_int_t out_sz = o->type->stream_p->write(self_in, buf, len, &error);
if (out_sz == -1) {
if (is_nonblocking_error(error)) {
// http://docs.python.org/3/library/io.html#io.RawIOBase.write
@@ -125,6 +122,12 @@ STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
}
}
+STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) {
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
+ return mp_stream_write(self_in, bufinfo.buf, bufinfo.len);
+}
+
STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
@@ -248,4 +251,4 @@ mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
-MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write);
+MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method);
diff --git a/py/stream.h b/py/stream.h
index e52508daae..4cdc1b4dc5 100644
--- a/py/stream.h
+++ b/py/stream.h
@@ -32,3 +32,5 @@ MP_DECLARE_CONST_FUN_OBJ(mp_stream_write_obj);
// Iterator which uses mp_stream_unbuffered_readline_obj
mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self);
+
+mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len);