Skip to content

Commit ad4656b

Browse files
committed
all: Rename BYTES_PER_WORD to MP_BYTES_PER_OBJ_WORD.
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 <damien@micropython.org>
1 parent 7e956fa commit ad4656b

11 files changed

Lines changed: 16 additions & 19 deletions

File tree

examples/embedding/hello-embed.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mp_obj_t execute_from_str(const char *str) {
5353

5454
int main() {
5555
// Initialized stack limit
56-
mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4));
56+
mp_stack_set_limit(40000 * (sizeof(void *) / 4));
5757
// Initialize heap
5858
gc_init(heap, heap + sizeof(heap));
5959
// Initialize interpreter

mpy-cross/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ STATIC void pre_process_options(int argc, char **argv) {
170170
heap_size *= 1024 * 1024;
171171
}
172172
if (word_adjust) {
173-
heap_size = heap_size * BYTES_PER_WORD / 4;
173+
heap_size = heap_size * MP_BYTES_PER_OBJ_WORD / 4;
174174
}
175175
} else {
176176
exit(usage(argv));
@@ -182,7 +182,7 @@ STATIC void pre_process_options(int argc, char **argv) {
182182
}
183183

184184
MP_NOINLINE int main_(int argc, char **argv) {
185-
mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4));
185+
mp_stack_set_limit(40000 * (sizeof(void *) / 4));
186186

187187
pre_process_options(argc, argv);
188188

ports/esp32/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ struct mp_bluetooth_nimble_root_pointers_t;
221221

222222
// type definitions for the specific machine
223223

224-
#define BYTES_PER_WORD (4)
225224
#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p)))
226225
void *esp_native_code_commit(void *, size_t, void *);
227226
#define MP_PLAT_COMMIT_EXEC(buf, len, reloc) esp_native_code_commit(buf, len, reloc)

ports/nrf/mpconfigport.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,6 @@
201201

202202
// type definitions for the specific machine
203203

204-
#define BYTES_PER_WORD (4)
205-
206204
#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1))
207205

208206
#define MP_SSIZE_MAX (0x7fffffff)

ports/unix/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ STATIC void pre_process_options(int argc, char **argv) {
387387
goto invalid_arg;
388388
}
389389
if (word_adjust) {
390-
heap_size = heap_size * BYTES_PER_WORD / 4;
390+
heap_size = heap_size * MP_BYTES_PER_OBJ_WORD / 4;
391391
}
392392
// If requested size too small, we'll crash anyway
393393
if (heap_size < 700) {
@@ -446,7 +446,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
446446
signal(SIGPIPE, SIG_IGN);
447447
#endif
448448

449-
mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4));
449+
mp_stack_set_limit(40000 * (sizeof(void *) / 4));
450450

451451
pre_process_options(argc, argv);
452452

ports/unix/mpthreadport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void mp_thread_start(void) {
206206
void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) {
207207
// default stack size is 8k machine-words
208208
if (*stack_size == 0) {
209-
*stack_size = 8192 * BYTES_PER_WORD;
209+
*stack_size = 8192 * sizeof(void *);
210210
}
211211

212212
// minimum stack size is set by pthreads

py/binary.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p
321321
double f;
322322
} fp_dp;
323323
fp_dp.f = mp_obj_get_float_to_d(val_in);
324-
if (BYTES_PER_WORD == 8) {
324+
if (MP_BYTES_PER_OBJ_WORD == 8) {
325325
val = fp_dp.i64;
326326
} else {
327327
int be = struct_type == '>';
@@ -342,7 +342,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p
342342

343343
val = mp_obj_get_int(val_in);
344344
// zero/sign extend if needed
345-
if (BYTES_PER_WORD < 8 && size > sizeof(val)) {
345+
if (MP_BYTES_PER_OBJ_WORD < 8 && size > sizeof(val)) {
346346
int c = (mp_int_t)val < 0 ? 0xff : 0x00;
347347
memset(p, c, size);
348348
if (struct_type == '>') {

py/emitbc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
#if MICROPY_ENABLE_COMPILER
3838

39-
#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
39+
#define BYTES_FOR_INT ((MP_BYTES_PER_OBJ_WORD * 8 + 6) / 7)
4040
#define DUMMY_DATA_SIZE (BYTES_FOR_INT)
4141

4242
struct _emit_t {

py/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
// detect untraced object still in use
5050
#define CLEAR_ON_SWEEP (0)
5151

52-
#define WORDS_PER_BLOCK ((MICROPY_BYTES_PER_GC_BLOCK) / BYTES_PER_WORD)
52+
#define WORDS_PER_BLOCK ((MICROPY_BYTES_PER_GC_BLOCK) / MP_BYTES_PER_OBJ_WORD)
5353
#define BYTES_PER_BLOCK (MICROPY_BYTES_PER_GC_BLOCK)
5454

5555
// ATB = allocation table byte

py/mpconfig.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
// Number of bytes in memory allocation/GC block. Any size allocated will be
127127
// rounded up to be multiples of this.
128128
#ifndef MICROPY_BYTES_PER_GC_BLOCK
129-
#define MICROPY_BYTES_PER_GC_BLOCK (4 * BYTES_PER_WORD)
129+
#define MICROPY_BYTES_PER_GC_BLOCK (4 * MP_BYTES_PER_OBJ_WORD)
130130
#endif
131131

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

1533-
// Number of bytes in a word
1534-
#ifndef BYTES_PER_WORD
1535-
#define BYTES_PER_WORD (sizeof(mp_uint_t))
1533+
// Number of bytes in an object word: mp_obj_t, mp_uint_t, mp_uint_t
1534+
#ifndef MP_BYTES_PER_OBJ_WORD
1535+
#define MP_BYTES_PER_OBJ_WORD (sizeof(mp_uint_t))
15361536
#endif
15371537

15381538
// Number of bits in a byte
15391539
#ifndef MP_BITS_PER_BYTE
15401540
#define MP_BITS_PER_BYTE (8)
15411541
#endif
15421542
// mp_int_t value with most significant bit set
1543-
#define WORD_MSBIT_HIGH (((mp_uint_t)1) << (BYTES_PER_WORD * 8 - 1))
1543+
#define WORD_MSBIT_HIGH (((mp_uint_t)1) << (MP_BYTES_PER_OBJ_WORD * MP_BITS_PER_BYTE - 1))
15441544

15451545
// Make sure both MP_ENDIANNESS_LITTLE and MP_ENDIANNESS_BIG are
15461546
// defined and that they are the opposite of each other.

0 commit comments

Comments
 (0)