Skip to content

Commit

Permalink
all: Rename BYTES_PER_WORD to MP_BYTES_PER_OBJ_WORD.
Browse files Browse the repository at this point in the history
The "word" referred to by BYTES_PER_WORD is actually the size of mp_obj_t
which is not always the same as the size of a pointer on the target
architecture.  So rename this config value to better reflect what it
measures, and also prefix it with MP_.

For uses of BYTES_PER_WORD in setting the stack limit this has been
changed to sizeof(void *), because the stack usually grows with
machine-word sized values (eg an nlr_buf_t has many machine words in it).

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Feb 4, 2021
1 parent 7e956fa commit ad4656b
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/embedding/hello-embed.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mp_obj_t execute_from_str(const char *str) {

int main() {
// Initialized stack limit
mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4));
mp_stack_set_limit(40000 * (sizeof(void *) / 4));
// Initialize heap
gc_init(heap, heap + sizeof(heap));
// Initialize interpreter
Expand Down
4 changes: 2 additions & 2 deletions mpy-cross/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ STATIC void pre_process_options(int argc, char **argv) {
heap_size *= 1024 * 1024;
}
if (word_adjust) {
heap_size = heap_size * BYTES_PER_WORD / 4;
heap_size = heap_size * MP_BYTES_PER_OBJ_WORD / 4;
}
} else {
exit(usage(argv));
Expand All @@ -182,7 +182,7 @@ STATIC void pre_process_options(int argc, char **argv) {
}

MP_NOINLINE int main_(int argc, char **argv) {
mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4));
mp_stack_set_limit(40000 * (sizeof(void *) / 4));

pre_process_options(argc, argv);

Expand Down
1 change: 0 additions & 1 deletion ports/esp32/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ struct mp_bluetooth_nimble_root_pointers_t;

// type definitions for the specific machine

#define BYTES_PER_WORD (4)
#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p)))
void *esp_native_code_commit(void *, size_t, void *);
#define MP_PLAT_COMMIT_EXEC(buf, len, reloc) esp_native_code_commit(buf, len, reloc)
Expand Down
2 changes: 0 additions & 2 deletions ports/nrf/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@

// type definitions for the specific machine

#define BYTES_PER_WORD (4)

#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1))

#define MP_SSIZE_MAX (0x7fffffff)
Expand Down
4 changes: 2 additions & 2 deletions ports/unix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ STATIC void pre_process_options(int argc, char **argv) {
goto invalid_arg;
}
if (word_adjust) {
heap_size = heap_size * BYTES_PER_WORD / 4;
heap_size = heap_size * MP_BYTES_PER_OBJ_WORD / 4;
}
// If requested size too small, we'll crash anyway
if (heap_size < 700) {
Expand Down Expand Up @@ -446,7 +446,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
signal(SIGPIPE, SIG_IGN);
#endif

mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4));
mp_stack_set_limit(40000 * (sizeof(void *) / 4));

pre_process_options(argc, argv);

Expand Down
2 changes: 1 addition & 1 deletion ports/unix/mpthreadport.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void mp_thread_start(void) {
void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) {
// default stack size is 8k machine-words
if (*stack_size == 0) {
*stack_size = 8192 * BYTES_PER_WORD;
*stack_size = 8192 * sizeof(void *);
}

// minimum stack size is set by pthreads
Expand Down
4 changes: 2 additions & 2 deletions py/binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p
double f;
} fp_dp;
fp_dp.f = mp_obj_get_float_to_d(val_in);
if (BYTES_PER_WORD == 8) {
if (MP_BYTES_PER_OBJ_WORD == 8) {
val = fp_dp.i64;
} else {
int be = struct_type == '>';
Expand All @@ -342,7 +342,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p

val = mp_obj_get_int(val_in);
// zero/sign extend if needed
if (BYTES_PER_WORD < 8 && size > sizeof(val)) {
if (MP_BYTES_PER_OBJ_WORD < 8 && size > sizeof(val)) {
int c = (mp_int_t)val < 0 ? 0xff : 0x00;
memset(p, c, size);
if (struct_type == '>') {
Expand Down
2 changes: 1 addition & 1 deletion py/emitbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#if MICROPY_ENABLE_COMPILER

#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
#define BYTES_FOR_INT ((MP_BYTES_PER_OBJ_WORD * 8 + 6) / 7)
#define DUMMY_DATA_SIZE (BYTES_FOR_INT)

struct _emit_t {
Expand Down
2 changes: 1 addition & 1 deletion py/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
// detect untraced object still in use
#define CLEAR_ON_SWEEP (0)

#define WORDS_PER_BLOCK ((MICROPY_BYTES_PER_GC_BLOCK) / BYTES_PER_WORD)
#define WORDS_PER_BLOCK ((MICROPY_BYTES_PER_GC_BLOCK) / MP_BYTES_PER_OBJ_WORD)
#define BYTES_PER_BLOCK (MICROPY_BYTES_PER_GC_BLOCK)

// ATB = allocation table byte
Expand Down
10 changes: 5 additions & 5 deletions py/mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
// Number of bytes in memory allocation/GC block. Any size allocated will be
// rounded up to be multiples of this.
#ifndef MICROPY_BYTES_PER_GC_BLOCK
#define MICROPY_BYTES_PER_GC_BLOCK (4 * BYTES_PER_WORD)
#define MICROPY_BYTES_PER_GC_BLOCK (4 * MP_BYTES_PER_OBJ_WORD)
#endif

// Number of words allocated (in BSS) to the GC stack (minimum is 1)
Expand Down Expand Up @@ -1530,17 +1530,17 @@ typedef double mp_float_t;
#define STATIC static
#endif

// Number of bytes in a word
#ifndef BYTES_PER_WORD
#define BYTES_PER_WORD (sizeof(mp_uint_t))
// Number of bytes in an object word: mp_obj_t, mp_uint_t, mp_uint_t
#ifndef MP_BYTES_PER_OBJ_WORD
#define MP_BYTES_PER_OBJ_WORD (sizeof(mp_uint_t))
#endif

// Number of bits in a byte
#ifndef MP_BITS_PER_BYTE
#define MP_BITS_PER_BYTE (8)
#endif
// mp_int_t value with most significant bit set
#define WORD_MSBIT_HIGH (((mp_uint_t)1) << (BYTES_PER_WORD * 8 - 1))
#define WORD_MSBIT_HIGH (((mp_uint_t)1) << (MP_BYTES_PER_OBJ_WORD * MP_BITS_PER_BYTE - 1))

// Make sure both MP_ENDIANNESS_LITTLE and MP_ENDIANNESS_BIG are
// defined and that they are the opposite of each other.
Expand Down
2 changes: 1 addition & 1 deletion py/persistentcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ STATIC void mp_print_bytes(mp_print_t *print, const byte *data, size_t len) {
print->print_strn(print->data, (const char *)data, len);
}

#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
#define BYTES_FOR_INT ((MP_BYTES_PER_OBJ_WORD * 8 + 6) / 7)
STATIC void mp_print_uint(mp_print_t *print, size_t n) {
byte buf[BYTES_FOR_INT];
byte *p = buf + sizeof(buf);
Expand Down

0 comments on commit ad4656b

Please sign in to comment.