py: Replace stream_p with *stream_p in mp_obj_type_t.

This is to reduce ROM usage.  stream_p is used in file and socket types
only (at the moment), so seems a good idea to make the protocol
functions a pointer instead of the actual structure.

It saves 308 bytes of ROM in the stmhal/ port, 928 in unix/.
pull/439/head
Damien George 2014-04-05 23:02:23 +01:00
rodzic 17520224fa
commit 27e735fd18
10 zmienionych plików z 30 dodań i 26 usunięć

Wyświetl plik

@ -229,7 +229,7 @@ struct _mp_obj_type_t {
// in mp_obj_type_t at the expense of extra pointer and extra dereference
// when actually used.
mp_buffer_p_t buffer_p;
mp_stream_p_t stream_p;
const mp_stream_p_t *stream_p;
// these are for dynamically created types (classes)
mp_obj_t bases_tuple;

Wyświetl plik

@ -111,6 +111,7 @@ Q(max)
Q(min)
Q(namedtuple)
Q(next)
Q(open)
Q(ord)
Q(path)
Q(pow)

Wyświetl plik

@ -14,7 +14,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in);
STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
if (o->type->stream_p.read == NULL) {
if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
// CPython: io.UnsupportedOperation, OSError subclass
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
}
@ -25,7 +25,7 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
}
byte *buf = m_new(byte, sz);
int error;
machine_int_t out_sz = o->type->stream_p.read(o, buf, sz, &error);
machine_int_t out_sz = o->type->stream_p->read(o, buf, sz, &error);
if (out_sz == -1) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
} else {
@ -37,7 +37,7 @@ 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) {
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
if (o->type->stream_p.write == NULL) {
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"));
}
@ -45,7 +45,7 @@ STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
uint sz;
const char *buf = mp_obj_str_get_data(arg, &sz);
int error;
machine_int_t out_sz = o->type->stream_p.write(self_in, buf, sz, &error);
machine_int_t out_sz = o->type->stream_p->write(self_in, buf, sz, &error);
if (out_sz == -1) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
} else {
@ -60,7 +60,7 @@ STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
#define READ_SIZE 256
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.read == NULL) {
if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
// CPython: io.UnsupportedOperation, OSError subclass
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
}
@ -72,7 +72,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
int error;
int current_read = READ_SIZE;
while (true) {
machine_int_t out_sz = o->type->stream_p.read(self_in, p, current_read, &error);
machine_int_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error);
if (out_sz == -1) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
}
@ -101,7 +101,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
// Unbuffered, inefficient implementation of readline() for raw I/O files.
STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
if (o->type->stream_p.read == NULL) {
if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
// CPython: io.UnsupportedOperation, OSError subclass
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
}
@ -126,7 +126,7 @@ STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
}
machine_int_t out_sz = o->type->stream_p.read(o, p, 1, &error);
machine_int_t out_sz = o->type->stream_p->read(o, p, 1, &error);
if (out_sz == -1) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
}

Wyświetl plik

@ -36,7 +36,6 @@ Q(Usart)
Q(ADC)
Q(ADC_all)
Q(Audio)
Q(open)
Q(File)
// Entries for sys.path
Q(0:/)

Wyświetl plik

@ -61,17 +61,20 @@ STATIC const mp_map_elem_t file_locals_dict_table[] = {
STATIC MP_DEFINE_CONST_DICT(file_locals_dict, file_locals_dict_table);
STATIC mp_obj_t file_obj_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args);
static const mp_obj_type_t file_obj_type = {
STATIC const mp_stream_p_t file_obj_stream_p = {
.read = file_read,
.write = file_write,
};
STATIC const mp_obj_type_t file_obj_type = {
{ &mp_type_type },
.name = MP_QSTR_File,
.make_new = file_obj_make_new,
.print = file_obj_print,
.getiter = mp_identity,
.iternext = mp_stream_unbuffered_iter,
.stream_p = {
.read = file_read,
.write = file_write,
},
.stream_p = &file_obj_stream_p,
.locals_dict = (mp_obj_t)&file_locals_dict,
};

Wyświetl plik

@ -33,7 +33,6 @@ Q(SDcard)
Q(gpio)
Q(gpio_in)
Q(gpio_out)
Q(open)
Q(File)
// Entries for sys.path
Q(0:/)

Wyświetl plik

@ -123,6 +123,11 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
STATIC MP_DEFINE_CONST_DICT(rawfile_locals_dict, rawfile_locals_dict_table);
STATIC const mp_stream_p_t rawfile_stream_p = {
.read = fdfile_read,
.write = fdfile_write,
};
STATIC const mp_obj_type_t rawfile_type = {
{ &mp_type_type },
.name = MP_QSTR_io_dot_FileIO,
@ -130,10 +135,7 @@ STATIC const mp_obj_type_t rawfile_type = {
.make_new = fdfile_make_new,
.getiter = mp_identity,
.iternext = mp_stream_unbuffered_iter,
.stream_p = {
.read = fdfile_read,
.write = fdfile_write,
},
.stream_p = &rawfile_stream_p,
.locals_dict = (mp_obj_t)&rawfile_locals_dict,
};

Wyświetl plik

@ -240,6 +240,11 @@ STATIC const mp_map_elem_t microsocket_locals_dict_table[] = {
STATIC MP_DEFINE_CONST_DICT(microsocket_locals_dict, microsocket_locals_dict_table);
STATIC const mp_stream_p_t microsocket_stream_p = {
.read = socket_read,
.write = socket_write,
};
STATIC const mp_obj_type_t microsocket_type = {
{ &mp_type_type },
.name = MP_QSTR_socket,
@ -247,10 +252,7 @@ STATIC const mp_obj_type_t microsocket_type = {
.make_new = socket_make_new,
.getiter = NULL,
.iternext = NULL,
.stream_p = {
.read = socket_read,
.write = socket_write,
},
.stream_p = &microsocket_stream_p,
.locals_dict = (mp_obj_t)&microsocket_locals_dict,
};

Wyświetl plik

@ -31,7 +31,6 @@ typedef unsigned int machine_uint_t; // must be pointer size
typedef void *machine_ptr_t; // must be of pointer size
typedef const void *machine_const_ptr_t; // must be of pointer size
struct _mp_obj_fun_native_t;
extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
#define MICROPY_EXTRA_BUILTINS \
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },

Wyświetl plik

@ -3,7 +3,6 @@
Q(Test)
Q(argv)
Q(open)
Q(stdin)
Q(stdout)
Q(stderr)