5555# define VALGRIND_DEFINED_SPACE (p, s )
5656#endif /* JERRY_VALGRIND */
5757
58- /* *
59- * Magic numbers for heap memory blocks
60- */
61- typedef enum
62- {
63- MEM_MAGIC_NUM_OF_FREE_BLOCK = 0xc809 ,
64- MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK = 0x5b46
65- } mem_magic_num_of_block_t ;
66-
6758/* *
6859 * State of the block to initialize (argument of mem_init_block_header)
6960 *
@@ -108,9 +99,6 @@ typedef struct __attribute__ ((aligned (MEM_ALIGNMENT))) mem_block_header_t
10899 * 0 - if the block is the first block */
109100 mem_heap_offset_t next_p : MEM_HEAP_OFFSET_LOG; /* *< next block's offset;
110101 * 0 - if the block is the last block */
111- uint16_t magic_num; /* *< magic number (mem_magic_num_of_block_t):
112- * MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK for allocated block
113- * or MEM_MAGIC_NUM_OF_FREE_BLOCK for free block */
114102} mem_block_header_t ;
115103
116104#if MEM_HEAP_OFFSET_LOG <= 16
@@ -152,6 +140,7 @@ mem_heap_state_t mem_heap;
152140static size_t mem_get_block_chunks_count (const mem_block_header_t *block_header_p);
153141static size_t mem_get_block_data_space_size (const mem_block_header_t *block_header_p);
154142static size_t mem_get_block_chunks_count_from_data_size (size_t block_allocated_size);
143+ static bool mem_is_block_free (const mem_block_header_t *block_header_p);
155144
156145static void mem_init_block_header (uint8_t *first_chunk_p,
157146 size_t size_in_chunks,
@@ -370,6 +359,18 @@ mem_get_block_chunks_count_from_data_size (size_t block_allocated_size) /**< siz
370359 return JERRY_ALIGNUP (sizeof (mem_block_header_t ) + block_allocated_size, MEM_HEAP_CHUNK_SIZE) / MEM_HEAP_CHUNK_SIZE;
371360} /* mem_get_block_chunks_count_from_data_size */
372361
362+ /* *
363+ * Check whether specified block is free.
364+ *
365+ * @return true - if block is free,
366+ * false - otherwise
367+ */
368+ static bool
369+ mem_is_block_free (const mem_block_header_t *block_header_p) /* *< block header */
370+ {
371+ return (block_header_p->allocated_bytes == 0 );
372+ } /* mem_is_block_free */
373+
373374/* *
374375 * Startup initialization of heap
375376 */
@@ -410,7 +411,7 @@ mem_heap_finalize (void)
410411 VALGRIND_DEFINED_SPACE (mem_heap.heap_start , mem_heap.heap_size );
411412
412413 JERRY_ASSERT (mem_heap.first_block_p == mem_heap.last_block_p );
413- JERRY_ASSERT ( mem_heap.first_block_p -> magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK );
414+ JERRY_ASSERT ( mem_is_block_free ( mem_heap.first_block_p ) );
414415
415416 VALGRIND_NOACCESS_SPACE (mem_heap.heap_start , mem_heap.heap_size );
416417
@@ -433,13 +434,11 @@ mem_init_block_header (uint8_t *first_chunk_p, /**< address of the first
433434
434435 if (block_state == MEM_BLOCK_FREE)
435436 {
436- block_header_p->magic_num = MEM_MAGIC_NUM_OF_FREE_BLOCK;
437-
438- JERRY_ASSERT (allocated_bytes == 0 );
437+ JERRY_ASSERT (allocated_bytes == 0 );
439438 }
440439 else
441440 {
442- block_header_p-> magic_num = MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK ;
441+ JERRY_ASSERT (allocated_bytes != 0 ) ;
443442 }
444443
445444 mem_set_block_prev (block_header_p, prev_block_p);
@@ -448,6 +447,8 @@ mem_init_block_header (uint8_t *first_chunk_p, /**< address of the first
448447
449448 JERRY_ASSERT (allocated_bytes <= mem_get_block_data_space_size (block_header_p));
450449
450+ JERRY_ASSERT (mem_is_block_free (block_header_p) == (block_state == MEM_BLOCK_FREE));
451+
451452 VALGRIND_NOACCESS_STRUCT (block_header_p);
452453} /* mem_init_block_header */
453454
@@ -489,7 +490,7 @@ void* mem_heap_alloc_block_internal (size_t size_in_bytes, /**< size
489490 {
490491 VALGRIND_DEFINED_STRUCT (block_p);
491492
492- if (block_p-> magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK )
493+ if (mem_is_block_free (block_p) )
493494 {
494495 if (mem_get_block_data_space_size (block_p) >= size_in_bytes)
495496 {
@@ -498,7 +499,7 @@ void* mem_heap_alloc_block_internal (size_t size_in_bytes, /**< size
498499 }
499500 else
500501 {
501- JERRY_ASSERT (block_p-> magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
502+ JERRY_ASSERT (! mem_is_block_free (block_p) );
502503 }
503504
504505 mem_block_header_t *next_block_p = mem_get_next_block_by_direction (block_p, direction);
@@ -715,25 +716,16 @@ mem_heap_free_block (void *ptr) /**< pointer to beginning of data space of the b
715716
716717 VALGRIND_NOACCESS_SPACE (uint8_ptr, block_p->allocated_bytes );
717718
718- /* checking magic nums that are neighbour to data space */
719- JERRY_ASSERT (block_p->magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK);
720- if (next_block_p != NULL )
721- {
722- VALGRIND_DEFINED_STRUCT (next_block_p);
723-
724- JERRY_ASSERT (next_block_p->magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK
725- || next_block_p->magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK);
726-
727- VALGRIND_NOACCESS_STRUCT (next_block_p);
728- }
719+ JERRY_ASSERT (!mem_is_block_free (block_p));
729720
730- block_p->magic_num = MEM_MAGIC_NUM_OF_FREE_BLOCK;
721+ /* marking the block free */
722+ block_p->allocated_bytes = 0 ;
731723
732724 if (next_block_p != NULL )
733725 {
734726 VALGRIND_DEFINED_STRUCT (next_block_p);
735727
736- if (next_block_p-> magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK )
728+ if (mem_is_block_free (next_block_p) )
737729 {
738730 /* merge with the next block */
739731 MEM_HEAP_STAT_FREE_BLOCK_MERGE ();
@@ -764,7 +756,7 @@ mem_heap_free_block (void *ptr) /**< pointer to beginning of data space of the b
764756 {
765757 VALGRIND_DEFINED_STRUCT (prev_block_p);
766758
767- if (prev_block_p-> magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK )
759+ if (mem_is_block_free (prev_block_p) )
768760 {
769761 /* merge with the previous block */
770762 MEM_HEAP_STAT_FREE_BLOCK_MERGE ();
@@ -834,7 +826,7 @@ mem_heap_get_block_start (void *ptr) /**< pointer into a block */
834826
835827 if (is_found)
836828 {
837- JERRY_ASSERT (block_p-> magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
829+ JERRY_ASSERT (! mem_is_block_free (block_p) );
838830 JERRY_ASSERT (block_p + 1 <= ptr);
839831 JERRY_ASSERT (ptr < ((uint8_t *) (block_p + 1 ) + block_p->allocated_bytes ));
840832 }
@@ -894,9 +886,9 @@ mem_heap_print (bool dump_block_headers, /**< print block headers */
894886 {
895887 VALGRIND_DEFINED_STRUCT (block_p);
896888
897- printf (" Block (%p): magic num=0x%08x , size in chunks=%lu, previous block->%p next block->%p\n " ,
889+ printf (" Block (%p): state=%s , size in chunks=%lu, previous block->%p next block->%p\n " ,
898890 (void *) block_p,
899- block_p-> magic_num ,
891+ mem_is_block_free ( block_p) ? " free " : " allocated " ,
900892 (unsigned long ) mem_get_block_chunks_count (block_p),
901893 (void *) mem_get_next_block_by_direction (block_p, MEM_DIRECTION_PREV),
902894 (void *) mem_get_next_block_by_direction (block_p, MEM_DIRECTION_NEXT));
@@ -973,11 +965,9 @@ mem_check_heap (void)
973965 {
974966 VALGRIND_DEFINED_STRUCT (block_p);
975967
976- JERRY_ASSERT (block_p->magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK
977- || block_p->magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK);
978968 chunk_sizes_sum += mem_get_block_chunks_count (block_p);
979969
980- if (block_p-> magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK )
970+ if (! mem_is_block_free (block_p) )
981971 {
982972 allocated_sum += block_p->allocated_bytes ;
983973 }
@@ -1011,8 +1001,6 @@ mem_check_heap (void)
10111001 {
10121002 VALGRIND_DEFINED_STRUCT (block_p);
10131003
1014- JERRY_ASSERT (block_p->magic_num == MEM_MAGIC_NUM_OF_FREE_BLOCK
1015- || block_p->magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK);
10161004 chunk_sizes_sum += mem_get_block_chunks_count (block_p);
10171005
10181006 prev_block_p = mem_get_next_block_by_direction (block_p, MEM_DIRECTION_PREV);
@@ -1076,7 +1064,7 @@ mem_heap_stat_init ()
10761064static void
10771065mem_heap_stat_alloc_block (mem_block_header_t *block_header_p) /* *< allocated block */
10781066{
1079- JERRY_ASSERT (block_header_p-> magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
1067+ JERRY_ASSERT (! mem_is_block_free (block_header_p) );
10801068
10811069 const size_t chunks = mem_get_block_chunks_count (block_header_p);
10821070 const size_t bytes = block_header_p->allocated_bytes ;
@@ -1134,7 +1122,7 @@ mem_heap_stat_alloc_block (mem_block_header_t *block_header_p) /**< allocated bl
11341122static void
11351123mem_heap_stat_free_block (mem_block_header_t *block_header_p) /* *< block to be freed */
11361124{
1137- JERRY_ASSERT (block_header_p-> magic_num == MEM_MAGIC_NUM_OF_ALLOCATED_BLOCK );
1125+ JERRY_ASSERT (! mem_is_block_free (block_header_p) );
11381126
11391127 const size_t chunks = mem_get_block_chunks_count (block_header_p);
11401128 const size_t bytes = block_header_p->allocated_bytes ;
0 commit comments