7979#define ATB_HEAD_TO_MARK (block ) do { MP_STATE_MEM(gc_alloc_table_start)[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0)
8080#define ATB_MARK_TO_HEAD (block ) do { MP_STATE_MEM(gc_alloc_table_start)[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0)
8181
82- #define BLOCK_FROM_PTR (ptr ) (((ptr) - (mp_uint_t) MP_STATE_MEM(gc_pool_start)) / BYTES_PER_BLOCK)
83- #define PTR_FROM_BLOCK (block ) (((block) * BYTES_PER_BLOCK + (mp_uint_t )MP_STATE_MEM(gc_pool_start)))
82+ #define BLOCK_FROM_PTR (ptr ) (((byte*)( ptr) - MP_STATE_MEM(gc_pool_start)) / BYTES_PER_BLOCK)
83+ #define PTR_FROM_BLOCK (block ) (((block) * BYTES_PER_BLOCK + (uintptr_t )MP_STATE_MEM(gc_pool_start)))
8484#define ATB_FROM_BLOCK (bl ) ((bl) / BLOCKS_PER_ATB)
8585
8686#if MICROPY_ENABLE_FINALISER
9797// TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool
9898void gc_init (void * start , void * end ) {
9999 // align end pointer on block boundary
100- end = (void * )((mp_uint_t )end & (~(BYTES_PER_BLOCK - 1 )));
100+ end = (void * )((uintptr_t )end & (~(BYTES_PER_BLOCK - 1 )));
101101 DEBUG_printf ("Initializing GC heap: %p..%p = " UINT_FMT " bytes\n" , start , end , (byte * )end - (byte * )start );
102102
103103 // calculate parameters for GC (T=total, A=alloc table, F=finaliser table, P=pool; all in bytes):
@@ -120,11 +120,11 @@ void gc_init(void *start, void *end) {
120120#endif
121121
122122 mp_uint_t gc_pool_block_len = MP_STATE_MEM (gc_alloc_table_byte_len ) * BLOCKS_PER_ATB ;
123- MP_STATE_MEM (gc_pool_start ) = (mp_uint_t * )(( byte * )end - gc_pool_block_len * BYTES_PER_BLOCK ) ;
124- MP_STATE_MEM (gc_pool_end ) = ( mp_uint_t * ) end ;
123+ MP_STATE_MEM (gc_pool_start ) = (byte * )end - gc_pool_block_len * BYTES_PER_BLOCK ;
124+ MP_STATE_MEM (gc_pool_end ) = end ;
125125
126126#if MICROPY_ENABLE_FINALISER
127- assert (( byte * ) MP_STATE_MEM (gc_pool_start ) >= MP_STATE_MEM (gc_finaliser_table_start ) + gc_finaliser_table_byte_len );
127+ assert (MP_STATE_MEM (gc_pool_start ) >= MP_STATE_MEM (gc_finaliser_table_start ) + gc_finaliser_table_byte_len );
128128#endif
129129
130130 // clear ATBs
@@ -164,12 +164,14 @@ bool gc_is_locked(void) {
164164 return MP_STATE_MEM (gc_lock_depth ) != 0 ;
165165}
166166
167+ // ptr should be of type void*
167168#define VERIFY_PTR (ptr ) ( \
168- (ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
169- && ptr >= (mp_uint_t )MP_STATE_MEM(gc_pool_start) /* must be above start of pool */ \
170- && ptr < (mp_uint_t )MP_STATE_MEM(gc_pool_end) /* must be below end of pool */ \
169+ ((uintptr_t)( ptr) & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
170+ && ptr >= (void* )MP_STATE_MEM(gc_pool_start) /* must be above start of pool */ \
171+ && ptr < (void* )MP_STATE_MEM(gc_pool_end) /* must be below end of pool */ \
171172 )
172173
174+ // ptr should be of type void*
173175#define VERIFY_MARK_AND_PUSH (ptr ) \
174176 do { \
175177 if (VERIFY_PTR(ptr)) { \
@@ -189,7 +191,7 @@ bool gc_is_locked(void) {
189191STATIC void gc_drain_stack (void ) {
190192 while (MP_STATE_MEM (gc_sp ) > MP_STATE_MEM (gc_stack )) {
191193 // pop the next block off the stack
192- mp_uint_t block = * -- MP_STATE_MEM (gc_sp );
194+ size_t block = * -- MP_STATE_MEM (gc_sp );
193195
194196 // work out number of consecutive blocks in the chain starting with this one
195197 mp_uint_t n_blocks = 0 ;
@@ -198,9 +200,10 @@ STATIC void gc_drain_stack(void) {
198200 } while (ATB_GET_KIND (block + n_blocks ) == AT_TAIL );
199201
200202 // check this block's children
201- mp_uint_t * scan = (mp_uint_t * )PTR_FROM_BLOCK (block );
203+ mp_obj_t * scan = (mp_obj_t * )PTR_FROM_BLOCK (block );
202204 for (mp_uint_t i = n_blocks * WORDS_PER_BLOCK ; i > 0 ; i -- , scan ++ ) {
203- mp_uint_t ptr2 = * scan ;
205+ mp_obj_t obj = * scan ;
206+ void * ptr2 = (void * )obj ;
204207 VERIFY_MARK_AND_PUSH (ptr2 );
205208 }
206209 }
@@ -276,12 +279,12 @@ void gc_collect_start(void) {
276279 // correctly in the mp_state_ctx structure. We scan nlr_top, dict_locals,
277280 // dict_globals, then the root pointer section of mp_state_vm.
278281 void * * ptrs = (void * * )(void * )& mp_state_ctx ;
279- gc_collect_root (ptrs , offsetof(mp_state_ctx_t , vm .stack_top ) / sizeof (mp_uint_t ));
282+ gc_collect_root (ptrs , offsetof(mp_state_ctx_t , vm .stack_top ) / sizeof (void * ));
280283}
281284
282- void gc_collect_root (void * * ptrs , mp_uint_t len ) {
283- for (mp_uint_t i = 0 ; i < len ; i ++ ) {
284- mp_uint_t ptr = ( mp_uint_t ) ptrs [i ];
285+ void gc_collect_root (void * * ptrs , size_t len ) {
286+ for (size_t i = 0 ; i < len ; i ++ ) {
287+ void * ptr = ptrs [i ];
285288 VERIFY_MARK_AND_PUSH (ptr );
286289 gc_drain_stack ();
287290 }
@@ -295,7 +298,7 @@ void gc_collect_end(void) {
295298}
296299
297300void gc_info (gc_info_t * info ) {
298- info -> total = ( MP_STATE_MEM (gc_pool_end ) - MP_STATE_MEM (gc_pool_start )) * sizeof ( mp_uint_t );
301+ info -> total = MP_STATE_MEM (gc_pool_end ) - MP_STATE_MEM (gc_pool_start );
299302 info -> used = 0 ;
300303 info -> free = 0 ;
301304 info -> num_1block = 0 ;
@@ -339,7 +342,7 @@ void gc_info(gc_info_t *info) {
339342 info -> free *= BYTES_PER_BLOCK ;
340343}
341344
342- void * gc_alloc (mp_uint_t n_bytes , bool has_finaliser ) {
345+ void * gc_alloc (size_t n_bytes , bool has_finaliser ) {
343346 mp_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1 ) & (~(BYTES_PER_BLOCK - 1 ))) / BYTES_PER_BLOCK ;
344347 DEBUG_printf ("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n" , n_bytes , n_blocks );
345348
@@ -403,7 +406,7 @@ void *gc_alloc(mp_uint_t n_bytes, bool has_finaliser) {
403406 }
404407
405408 // get pointer to first block
406- void * ret_ptr = (void * )(MP_STATE_MEM (gc_pool_start ) + start_block * WORDS_PER_BLOCK );
409+ void * ret_ptr = (void * )(MP_STATE_MEM (gc_pool_start ) + start_block * BYTES_PER_BLOCK );
407410 DEBUG_printf ("gc_alloc(%p)\n" , ret_ptr );
408411
409412 // zero out the additional bytes of the newly allocated blocks
@@ -416,7 +419,7 @@ void *gc_alloc(mp_uint_t n_bytes, bool has_finaliser) {
416419 #if MICROPY_ENABLE_FINALISER
417420 if (has_finaliser ) {
418421 // clear type pointer in case it is never set
419- ((mp_obj_base_t * )ret_ptr )-> type = MP_OBJ_NULL ;
422+ ((mp_obj_base_t * )ret_ptr )-> type = NULL ;
420423 // set mp_obj flag only if it has a finaliser
421424 FTB_SET (start_block );
422425 }
@@ -443,13 +446,12 @@ void *gc_alloc_with_finaliser(mp_uint_t n_bytes) {
443446
444447// force the freeing of a piece of memory
445448// TODO: freeing here does not call finaliser
446- void gc_free (void * ptr_in ) {
449+ void gc_free (void * ptr ) {
447450 if (MP_STATE_MEM (gc_lock_depth ) > 0 ) {
448451 // TODO how to deal with this error?
449452 return ;
450453 }
451454
452- mp_uint_t ptr = (mp_uint_t )ptr_in ;
453455 DEBUG_printf ("gc_free(%p)\n" , ptr );
454456
455457 if (VERIFY_PTR (ptr )) {
@@ -475,14 +477,12 @@ void gc_free(void *ptr_in) {
475477 } else {
476478 assert (!"bad free" );
477479 }
478- } else if (ptr_in != NULL ) {
480+ } else if (ptr != NULL ) {
479481 assert (!"bad free" );
480482 }
481483}
482484
483- mp_uint_t gc_nbytes (const void * ptr_in ) {
484- mp_uint_t ptr = (mp_uint_t )ptr_in ;
485-
485+ size_t gc_nbytes (const void * ptr ) {
486486 if (VERIFY_PTR (ptr )) {
487487 mp_uint_t block = BLOCK_FROM_PTR (ptr );
488488 if (ATB_GET_KIND (block ) == AT_HEAD ) {
@@ -528,7 +528,7 @@ void *gc_realloc(void *ptr, mp_uint_t n_bytes) {
528528
529529#else // Alternative gc_realloc impl
530530
531- void * gc_realloc (void * ptr_in , mp_uint_t n_bytes , bool allow_move ) {
531+ void * gc_realloc (void * ptr_in , size_t n_bytes , bool allow_move ) {
532532 if (MP_STATE_MEM (gc_lock_depth ) > 0 ) {
533533 return NULL ;
534534 }
@@ -544,7 +544,7 @@ void *gc_realloc(void *ptr_in, mp_uint_t n_bytes, bool allow_move) {
544544 return NULL ;
545545 }
546546
547- mp_uint_t ptr = ( mp_uint_t ) ptr_in ;
547+ void * ptr = ptr_in ;
548548
549549 // sanity check the ptr
550550 if (!VERIFY_PTR (ptr )) {
@@ -730,15 +730,15 @@ void gc_dump_alloc_table(void) {
730730 */
731731 /* this prints the uPy object type of the head block */
732732 case AT_HEAD : {
733- mp_uint_t * ptr = MP_STATE_MEM (gc_pool_start ) + bl * WORDS_PER_BLOCK ;
734- if (* ptr == ( mp_uint_t ) & mp_type_tuple ) { c = 'T' ; }
735- else if (* ptr == ( mp_uint_t ) & mp_type_list ) { c = 'L' ; }
736- else if (* ptr == ( mp_uint_t ) & mp_type_dict ) { c = 'D' ; }
733+ void * * ptr = ( void * * )( MP_STATE_MEM (gc_pool_start ) + bl * BYTES_PER_BLOCK ) ;
734+ if (* ptr == & mp_type_tuple ) { c = 'T' ; }
735+ else if (* ptr == & mp_type_list ) { c = 'L' ; }
736+ else if (* ptr == & mp_type_dict ) { c = 'D' ; }
737737 #if MICROPY_PY_BUILTINS_FLOAT
738- else if (* ptr == ( mp_uint_t ) & mp_type_float ) { c = 'F' ; }
738+ else if (* ptr == & mp_type_float ) { c = 'F' ; }
739739 #endif
740- else if (* ptr == ( mp_uint_t ) & mp_type_fun_bc ) { c = 'B' ; }
741- else if (* ptr == ( mp_uint_t ) & mp_type_module ) { c = 'M' ; }
740+ else if (* ptr == & mp_type_fun_bc ) { c = 'B' ; }
741+ else if (* ptr == & mp_type_module ) { c = 'M' ; }
742742 else {
743743 c = 'h' ;
744744 #if 0
0 commit comments