@@ -396,15 +396,22 @@ mem_is_block_free (const mem_block_header_t *block_header_p) /**< block header *
396
396
397
397
/* *
398
398
* Startup initialization of heap
399
+ *
400
+ * Note:
401
+ * heap start and size should be aligned on MEM_HEAP_CHUNK_SIZE
399
402
*/
400
403
void
401
404
mem_heap_init (uint8_t *heap_start, /* *< first address of heap space */
402
405
size_t heap_size) /* *< heap space size */
403
406
{
404
407
JERRY_ASSERT (heap_start != NULL );
405
408
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 );
407
411
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
+
408
415
JERRY_ASSERT (heap_size <= (1u << MEM_HEAP_OFFSET_LOG));
409
416
410
417
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
800
807
801
808
/* marking the block free */
802
809
block_p->allocated_bytes = 0 ;
810
+ block_p->length_type = mem_block_length_type_t ::GENERAL;
803
811
804
812
if (next_block_p != NULL )
805
813
{
@@ -866,12 +874,12 @@ mem_heap_free_block (void *ptr) /**< pointer to beginning of data space of the b
866
874
} /* mem_heap_free_block */
867
875
868
876
/* *
869
- * Find beginning of user data in a block from pointer,
877
+ * Find beginning of user data in a one-chunked block from pointer,
870
878
* pointing into it, i.e. into [block_data_space_start; block_data_space_end) range.
871
879
*
872
880
* 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.
875
883
*
876
884
* Note:
877
885
* 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
880
888
* @return beginning of user data space of block identified by the pointer
881
889
*/
882
890
void *
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 */
884
892
{
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 );
890
895
891
896
JERRY_ASSERT (mem_heap.heap_start <= ptr
892
897
&& ptr < mem_heap.heap_start + mem_heap.heap_size );
893
898
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 ;
895
910
896
911
/* searching for corresponding block */
897
- while (block_p != NULL )
912
+ while (block_iter_p != NULL )
898
913
{
899
- VALGRIND_DEFINED_STRUCT (block_p );
914
+ VALGRIND_DEFINED_STRUCT (block_iter_p );
900
915
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 ,
902
917
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 ));
906
921
907
922
if (is_found)
908
923
{
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 ));
912
927
}
913
928
914
- VALGRIND_NOACCESS_STRUCT (block_p );
929
+ VALGRIND_NOACCESS_STRUCT (block_iter_p );
915
930
916
931
if (is_found)
917
932
{
918
- return ( void *) (block_p + 1 ) ;
933
+ break ;
919
934
}
920
935
921
- block_p = next_block_p;
936
+ block_iter_p = next_block_p;
922
937
}
923
938
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 */
926
944
927
945
/* *
928
946
* Get size of one-chunked block data space
0 commit comments