@@ -396,15 +396,22 @@ mem_is_block_free (const mem_block_header_t *block_header_p) /**< block header *
396396
397397/* *
398398 * Startup initialization of heap
399+ *
400+ * Note:
401+ * heap start and size should be aligned on MEM_HEAP_CHUNK_SIZE
399402 */
400403void
401404mem_heap_init (uint8_t *heap_start, /* *< first address of heap space */
402405 size_t heap_size) /* *< heap space size */
403406{
404407 JERRY_ASSERT (heap_start != NULL );
405408 JERRY_ASSERT (heap_size != 0 );
406- JERRY_ASSERT (heap_size % MEM_HEAP_CHUNK_SIZE == 0 );
409+
410+ JERRY_STATIC_ASSERT ((MEM_HEAP_CHUNK_SIZE & (MEM_HEAP_CHUNK_SIZE - 1u )) == 0 );
407411 JERRY_ASSERT ((uintptr_t ) heap_start % MEM_ALIGNMENT == 0 );
412+ JERRY_ASSERT ((uintptr_t ) heap_start % MEM_HEAP_CHUNK_SIZE == 0 );
413+ JERRY_ASSERT (heap_size % MEM_HEAP_CHUNK_SIZE == 0 );
414+
408415 JERRY_ASSERT (heap_size <= (1u << MEM_HEAP_OFFSET_LOG));
409416
410417 mem_heap.heap_start = heap_start;
@@ -800,6 +807,7 @@ mem_heap_free_block (void *ptr) /**< pointer to beginning of data space of the b
800807
801808 /* marking the block free */
802809 block_p->allocated_bytes = 0 ;
810+ block_p->length_type = mem_block_length_type_t ::GENERAL;
803811
804812 if (next_block_p != NULL )
805813 {
@@ -866,12 +874,12 @@ mem_heap_free_block (void *ptr) /**< pointer to beginning of data space of the b
866874} /* mem_heap_free_block */
867875
868876/* *
869- * Find beginning of user data in a block from pointer,
877+ * Find beginning of user data in a one-chunked block from pointer,
870878 * pointing into it, i.e. into [block_data_space_start; block_data_space_end) range.
871879 *
872880 * Note:
873- * Pointer must point to the memory region which was previously allocated
874- * with mem_heap_alloc_block and is currently valid.
881+ * Pointer must point to the one-chunked memory region which was previously allocated
882+ * with mem_heap_alloc_chunked_block and is currently valid.
875883 *
876884 * Note:
877885 * The interface should only be used for determining where the user space of heap-allocated block begins.
@@ -880,49 +888,59 @@ mem_heap_free_block (void *ptr) /**< pointer to beginning of data space of the b
880888 * @return beginning of user data space of block identified by the pointer
881889 */
882890void *
883- mem_heap_get_block_start (void *ptr) /* *< pointer into a block */
891+ mem_heap_get_chunked_block_start (void *ptr) /* *< pointer into a block */
884892{
885- mem_check_heap ();
886-
887- /*
888- * PERF: consider introducing bitmap of block beginnings
889- */
893+ JERRY_STATIC_ASSERT ((MEM_HEAP_CHUNK_SIZE & (MEM_HEAP_CHUNK_SIZE - 1u )) == 0 );
894+ JERRY_ASSERT (((uintptr_t ) mem_heap.heap_start % MEM_HEAP_CHUNK_SIZE) == 0 );
890895
891896 JERRY_ASSERT (mem_heap.heap_start <= ptr
892897 && ptr < mem_heap.heap_start + mem_heap.heap_size );
893898
894- const mem_block_header_t *block_p = mem_heap.first_block_p ;
899+ uintptr_t uintptr = (uintptr_t ) ptr;
900+ uintptr_t uintptr_chunk_aligned = JERRY_ALIGNDOWN (uintptr, MEM_HEAP_CHUNK_SIZE);
901+
902+ JERRY_ASSERT (uintptr > uintptr_chunk_aligned);
903+
904+ mem_block_header_t *block_p = (mem_block_header_t *) uintptr_chunk_aligned;
905+ JERRY_ASSERT (block_p->length_type == mem_block_length_type_t ::ONE_CHUNKED);
906+
907+ #ifndef JERRY_NDEBUG
908+ const mem_block_header_t *block_iter_p = mem_heap.first_block_p ;
909+ bool is_found = false ;
895910
896911 /* searching for corresponding block */
897- while (block_p != NULL )
912+ while (block_iter_p != NULL )
898913 {
899- VALGRIND_DEFINED_STRUCT (block_p );
914+ VALGRIND_DEFINED_STRUCT (block_iter_p );
900915
901- const mem_block_header_t *next_block_p = mem_get_next_block_by_direction (block_p ,
916+ const mem_block_header_t *next_block_p = mem_get_next_block_by_direction (block_iter_p ,
902917 MEM_DIRECTION_NEXT);
903- bool is_found = (ptr > block_p
904- && (ptr < next_block_p
905- || next_block_p == NULL ));
918+ is_found = (ptr > block_iter_p
919+ && (ptr < next_block_p
920+ || next_block_p == NULL ));
906921
907922 if (is_found)
908923 {
909- JERRY_ASSERT (!mem_is_block_free (block_p ));
910- JERRY_ASSERT (block_p + 1 <= ptr);
911- JERRY_ASSERT (ptr < ((uint8_t *) (block_p + 1 ) + block_p ->allocated_bytes ));
924+ JERRY_ASSERT (!mem_is_block_free (block_iter_p ));
925+ JERRY_ASSERT (block_iter_p + 1 <= ptr);
926+ JERRY_ASSERT (ptr < ((uint8_t *) (block_iter_p + 1 ) + block_iter_p ->allocated_bytes ));
912927 }
913928
914- VALGRIND_NOACCESS_STRUCT (block_p );
929+ VALGRIND_NOACCESS_STRUCT (block_iter_p );
915930
916931 if (is_found)
917932 {
918- return ( void *) (block_p + 1 ) ;
933+ break ;
919934 }
920935
921- block_p = next_block_p;
936+ block_iter_p = next_block_p;
922937 }
923938
924- JERRY_UNREACHABLE ();
925- } /* mem_heap_get_block_start */
939+ JERRY_ASSERT (is_found && block_p == block_iter_p);
940+ #endif /* !JERRY_NDEBUG */
941+
942+ return (void *) (block_p + 1 );
943+ } /* mem_heap_get_chunked_block_start */
926944
927945/* *
928946 * Get size of one-chunked block data space
0 commit comments