Select Git revision
-
Paul Sokolovsky authoredPaul Sokolovsky authored
objarray.c 8.70 KiB
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "map.h"
#include "runtime0.h"
#include "runtime.h"
// Use special typecode to differentiate repr() of bytearray vs array.array('B')
// (underlyingly they're same).
#define BYTEARRAY_TYPECODE 0
typedef struct _mp_obj_array_t {
mp_obj_base_t base;
struct {
machine_uint_t typecode : 8;
// free is number of unused elements after len used elements
// alloc size = len + free
machine_uint_t free : (8 * sizeof(machine_uint_t) - 8);
};
machine_uint_t len; // in elements
void *items;
} mp_obj_array_t;
static mp_obj_t array_iterator_new(mp_obj_t array_in);
static mp_obj_array_t *array_new(char typecode, uint n);
static mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
/******************************************************************************/
/* array */
static machine_int_t array_get_el_size(char typecode) {
// This assumes that unsigned and signed types are of the same type,
// which is invariant for [u]intN_t.
switch (typecode) {
case BYTEARRAY_TYPECODE:
case 'b':
case 'B':
return sizeof(int8_t);
case 'h':
case 'H':
return sizeof(int16_t);
case 'i':
case 'I':
return sizeof(int32_t);
case 'l':
case 'L':
return sizeof(int32_t);
}
return -1;
}
static machine_int_t array_get_el(mp_obj_array_t *o, int index) {
machine_int_t val = 0;
switch (o->typecode) {
case 'b':
val = ((int8_t*)o->items)[index];
break;
case BYTEARRAY_TYPECODE:
case 'B':
val = ((uint8_t*)o->items)[index];
break;
case 'h':