Skip to content

Commit d2276f0

Browse files
committed
py/dynruntime: Add mp_binary_get_size/get_val_array/set_val_array.
These are needed to read/write array.array objects, which is useful in native code to provide fast extensions that work with big arrays of data. Signed-off-by: Damien George <damien@micropython.org>
1 parent 4662a71 commit d2276f0

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

py/dynruntime.h

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
// This header file contains definitions to dynamically implement the static
3030
// MicroPython runtime API defined in py/obj.h and py/runtime.h.
3131

32+
#include "py/binary.h"
3233
#include "py/nativeglue.h"
3334
#include "py/objfun.h"
3435
#include "py/objstr.h"
@@ -184,6 +185,10 @@ static inline void *mp_obj_malloc_helper_dyn(size_t num_bytes, const mp_obj_type
184185
/******************************************************************************/
185186
// General runtime functions
186187

188+
#define mp_binary_get_size(struct_type, val_type, palign) (mp_fun_table.binary_get_size((struct_type), (val_type), (palign)))
189+
#define mp_binary_get_val_array(typecode, p, index) (mp_fun_table.binary_get_val_array((typecode), (p), (index)))
190+
#define mp_binary_set_val_array(typecode, p, index, val_in) (mp_fun_table.binary_set_val_array((typecode), (p), (index), (val_in)))
191+
187192
#define mp_load_name(qst) (mp_fun_table.load_name((qst)))
188193
#define mp_load_global(qst) (mp_fun_table.load_global((qst)))
189194
#define mp_load_attr(base, attr) (mp_fun_table.load_attr((base), (attr)))

py/nativeglue.c

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <string.h>
3030
#include <assert.h>
3131

32+
#include "py/binary.h"
3233
#include "py/runtime.h"
3334
#include "py/smallint.h"
3435
#include "py/nativeglue.h"
@@ -330,6 +331,9 @@ const mp_fun_table_t mp_fun_table = {
330331
mp_obj_get_float_to_d,
331332
mp_get_buffer,
332333
mp_get_stream_raise,
334+
mp_binary_get_size,
335+
mp_binary_get_val_array,
336+
mp_binary_set_val_array,
333337
&mp_plat_print,
334338
&mp_type_type,
335339
&mp_type_str,

py/nativeglue.h

+5
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ typedef struct _mp_fun_table_t {
156156
double (*obj_get_float_to_d)(mp_obj_t o);
157157
bool (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
158158
const mp_stream_p_t *(*get_stream_raise)(mp_obj_t self_in, int flags);
159+
size_t (*binary_get_size)(char struct_type, char val_type, size_t *palign);
160+
mp_obj_t (*binary_get_val_array)(char typecode, void *p, size_t index);
161+
void (*binary_set_val_array)(char typecode, void *p, size_t index, mp_obj_t val_in);
159162
const mp_print_t *plat_print;
163+
// The following entries start at index 70 and are referenced by tools-mpy_ld.py,
164+
// see constant MP_FUN_TABLE_MP_TYPE_TYPE_OFFSET.
160165
const mp_obj_type_t *type_type;
161166
const mp_obj_type_t *type_str;
162167
const mp_obj_type_t *type_list;

tools/mpy_ld.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
MP_SCOPE_FLAG_VIPERRODATA = 0x20
5353
MP_SCOPE_FLAG_VIPERBSS = 0x40
5454
MP_SMALL_INT_BITS = 31
55+
MP_FUN_TABLE_MP_TYPE_TYPE_OFFSET = 70
5556

5657
# ELF constants
5758
R_386_32 = 1
@@ -754,7 +755,7 @@ def link_objects(env, native_qstr_vals_len):
754755
# Resolve unknown symbols
755756
mp_fun_table_sec = Section(".external.mp_fun_table", b"", 0)
756757
fun_table = {
757-
key: 67 + idx
758+
key: MP_FUN_TABLE_MP_TYPE_TYPE_OFFSET + idx
758759
for idx, key in enumerate(
759760
[
760761
"mp_type_type",

0 commit comments

Comments
 (0)