Skip to content

Commit f7391a9

Browse files
dbatyairerobika
authored andcommitted
Cleanup Valgrind macros (#3180)
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai [email protected]
1 parent 2662694 commit f7391a9

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

jerry-core/jmem/jmem-heap.c

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ jmem_heap_init (void)
9191

9292
JERRY_CONTEXT (jmem_heap_list_skip_p) = &JERRY_HEAP_CONTEXT (first);
9393

94+
JMEM_VALGRIND_NOACCESS_SPACE (&JERRY_HEAP_CONTEXT (first), sizeof (jmem_heap_free_t));
9495
JMEM_VALGRIND_NOACCESS_SPACE (JERRY_HEAP_CONTEXT (area), JMEM_HEAP_AREA_SIZE);
9596

9697
#endif /* !ENABLED (JERRY_SYSTEM_ALLOCATOR) */
@@ -162,7 +163,7 @@ jmem_heap_alloc (const size_t size) /**< size of requested block */
162163
JERRY_HEAP_CONTEXT (first).next_offset = JMEM_HEAP_GET_OFFSET_FROM_ADDR (remaining_p);
163164
}
164165

165-
JMEM_VALGRIND_UNDEFINED_SPACE (data_space_p, sizeof (jmem_heap_free_t));
166+
JMEM_VALGRIND_NOACCESS_SPACE (data_space_p, sizeof (jmem_heap_free_t));
166167

167168
if (JERRY_UNLIKELY (data_space_p == JERRY_CONTEXT (jmem_heap_list_skip_p)))
168169
{
@@ -239,12 +240,7 @@ jmem_heap_alloc (const size_t size) /**< size of requested block */
239240
JMEM_VALGRIND_NOACCESS_SPACE (&JERRY_HEAP_CONTEXT (first), sizeof (jmem_heap_free_t));
240241

241242
JERRY_ASSERT ((uintptr_t) data_space_p % JMEM_ALIGNMENT == 0);
242-
#if ENABLED (JERRY_VALGRIND)
243-
if (data_space_p != NULL)
244-
{
245-
JMEM_VALGRIND_UNDEFINED_SPACE (data_space_p, size);
246-
}
247-
#endif /* ENABLED (JERRY_VALGRIND) */
243+
JMEM_VALGRIND_MALLOCLIKE_SPACE (data_space_p, size);
248244

249245
return (void *) data_space_p;
250246
#else /* ENABLED (JERRY_SYSTEM_ALLOCATOR) */
@@ -299,7 +295,6 @@ jmem_heap_gc_and_alloc_block (const size_t size, /**< required memory size */
299295
data_space_p = jmem_heap_alloc (size);
300296
}
301297

302-
JMEM_VALGRIND_MALLOCLIKE_SPACE (data_space_p, size);
303298
return data_space_p;
304299
} /* jmem_heap_gc_and_alloc_block */
305300

@@ -365,8 +360,6 @@ jmem_heap_find_prev (const jmem_heap_free_t * const block_p) /**< which memory b
365360
{
366361
const jmem_heap_free_t *prev_p;
367362

368-
JMEM_VALGRIND_DEFINED_SPACE (&JERRY_HEAP_CONTEXT (first), sizeof (jmem_heap_free_t));
369-
370363
if (block_p > JERRY_CONTEXT (jmem_heap_list_skip_p))
371364
{
372365
prev_p = JERRY_CONTEXT (jmem_heap_list_skip_p);
@@ -391,6 +384,7 @@ jmem_heap_find_prev (const jmem_heap_free_t * const block_p) /**< which memory b
391384
prev_p = next_p;
392385
}
393386

387+
JMEM_VALGRIND_NOACCESS_SPACE (prev_p, sizeof (jmem_heap_free_t));
394388
return (jmem_heap_free_t *) prev_p;
395389
} /* jmem_heap_find_prev */
396390

@@ -408,11 +402,13 @@ jmem_heap_insert_block (jmem_heap_free_t *block_p, /**< block to insert */
408402
JERRY_ASSERT ((uintptr_t) block_p % JMEM_ALIGNMENT == 0);
409403
JERRY_ASSERT (size % JMEM_ALIGNMENT == 0);
410404

405+
JMEM_VALGRIND_NOACCESS_SPACE (block_p, size);
406+
411407
JMEM_VALGRIND_DEFINED_SPACE (prev_p, sizeof (jmem_heap_free_t));
412408
jmem_heap_free_t *next_p = JMEM_HEAP_GET_ADDR_FROM_OFFSET (prev_p->next_offset);
413-
414-
JMEM_VALGRIND_DEFINED_SPACE (next_p, sizeof (jmem_heap_free_t));
415409
JMEM_VALGRIND_DEFINED_SPACE (block_p, sizeof (jmem_heap_free_t));
410+
JMEM_VALGRIND_DEFINED_SPACE (next_p, sizeof (jmem_heap_free_t));
411+
416412

417413
const uint32_t block_offset = JMEM_HEAP_GET_OFFSET_FROM_ADDR (block_p);
418414

@@ -445,7 +441,7 @@ jmem_heap_insert_block (jmem_heap_free_t *block_p, /**< block to insert */
445441
JERRY_CONTEXT (jmem_heap_list_skip_p) = prev_p;
446442

447443
JMEM_VALGRIND_NOACCESS_SPACE (prev_p, sizeof (jmem_heap_free_t));
448-
JMEM_VALGRIND_NOACCESS_SPACE (block_p, size);
444+
JMEM_VALGRIND_NOACCESS_SPACE (block_p, sizeof (jmem_heap_free_t));
449445
JMEM_VALGRIND_NOACCESS_SPACE (next_p, sizeof (jmem_heap_free_t));
450446
} /* jmem_heap_insert_block */
451447
#endif /* !ENABLED (JERRY_SYSTEM_ALLOCATOR) */
@@ -468,13 +464,12 @@ jmem_heap_free_block_internal (void *ptr, /**< pointer to beginning of data spac
468464

469465
const size_t aligned_size = (size + JMEM_ALIGNMENT - 1) / JMEM_ALIGNMENT * JMEM_ALIGNMENT;
470466

471-
jmem_heap_free_t * const block_p = (jmem_heap_free_t *) ptr;
472-
jmem_heap_free_t * const prev_p = jmem_heap_find_prev (block_p);
467+
jmem_heap_free_t *const block_p = (jmem_heap_free_t *) ptr;
468+
jmem_heap_free_t *const prev_p = jmem_heap_find_prev (block_p);
473469
jmem_heap_insert_block (block_p, prev_p, aligned_size);
474470

475471
JERRY_CONTEXT (jmem_heap_allocated_size) -= aligned_size;
476472

477-
JMEM_VALGRIND_NOACCESS_SPACE (ptr, size);
478473
JMEM_VALGRIND_FREELIKE_SPACE (ptr);
479474
#else /* ENABLED (JERRY_SYSTEM_ALLOCATOR) */
480475
JERRY_CONTEXT (jmem_heap_allocated_size) -= size;
@@ -511,8 +506,6 @@ jmem_heap_realloc_block (void *ptr, /**< memory region to reallocate */
511506
if (aligned_old_size == aligned_new_size)
512507
{
513508
JMEM_VALGRIND_RESIZE_SPACE (block_p, old_size, new_size);
514-
JMEM_VALGRIND_UNDEFINED_SPACE (block_p, new_size);
515-
JMEM_VALGRIND_DEFINED_SPACE (block_p, old_size);
516509
JMEM_HEAP_STAT_FREE (old_size);
517510
JMEM_HEAP_STAT_ALLOC (new_size);
518511
return block_p;
@@ -521,8 +514,6 @@ jmem_heap_realloc_block (void *ptr, /**< memory region to reallocate */
521514
if (aligned_new_size < aligned_old_size)
522515
{
523516
JMEM_VALGRIND_RESIZE_SPACE (block_p, old_size, new_size);
524-
JMEM_VALGRIND_NOACCESS_SPACE (block_p, old_size);
525-
JMEM_VALGRIND_DEFINED_SPACE (block_p, new_size);
526517
JMEM_HEAP_STAT_FREE (old_size);
527518
JMEM_HEAP_STAT_ALLOC (new_size);
528519
jmem_heap_insert_block ((jmem_heap_free_t *)((uint8_t *) block_p + aligned_new_size),
@@ -551,11 +542,12 @@ jmem_heap_realloc_block (void *ptr, /**< memory region to reallocate */
551542
jmem_heap_free_t *prev_p = jmem_heap_find_prev (block_p);
552543
JMEM_VALGRIND_DEFINED_SPACE (prev_p, sizeof (jmem_heap_free_t));
553544
jmem_heap_free_t * const next_p = JMEM_HEAP_GET_ADDR_FROM_OFFSET (prev_p->next_offset);
554-
JMEM_VALGRIND_DEFINED_SPACE (next_p, sizeof (jmem_heap_free_t));
555545

556546
/* Check if block can be extended at the end */
557547
if (((jmem_heap_free_t *) ((uint8_t *) block_p + aligned_old_size)) == next_p)
558548
{
549+
JMEM_VALGRIND_DEFINED_SPACE (next_p, sizeof (jmem_heap_free_t));
550+
559551
if (required_size <= next_p->size)
560552
{
561553
/* Block can be extended, update the list. */
@@ -573,9 +565,16 @@ jmem_heap_realloc_block (void *ptr, /**< memory region to reallocate */
573565
prev_p->next_offset = JMEM_HEAP_GET_OFFSET_FROM_ADDR (new_next_p);
574566
}
575567

568+
/* next_p will be marked as undefined space. */
576569
JMEM_VALGRIND_RESIZE_SPACE (block_p, old_size, new_size);
577570
ret_block_p = block_p;
578571
}
572+
else
573+
{
574+
JMEM_VALGRIND_NOACCESS_SPACE (next_p, sizeof (jmem_heap_free_t));
575+
}
576+
577+
JMEM_VALGRIND_NOACCESS_SPACE (prev_p, sizeof (jmem_heap_free_t));
579578
}
580579
/*
581580
* Check if block can be extended at the front.
@@ -597,18 +596,26 @@ jmem_heap_realloc_block (void *ptr, /**< memory region to reallocate */
597596
prev_p->size = (uint32_t) (prev_p->size - required_size);
598597
}
599598

599+
JMEM_VALGRIND_NOACCESS_SPACE (prev_p, sizeof (jmem_heap_free_t));
600+
600601
ret_block_p = (uint8_t *) block_p - required_size;
601-
JMEM_VALGRIND_UNDEFINED_SPACE (ret_block_p, new_size);
602+
603+
/* Mark the the new block as undefined so that we are able to write to it. */
604+
JMEM_VALGRIND_UNDEFINED_SPACE (ret_block_p, old_size);
605+
/* The blocks are likely to overlap, so mark the old block as defined memory again. */
606+
JMEM_VALGRIND_DEFINED_SPACE (block_p, old_size);
602607
memmove (ret_block_p, block_p, old_size);
608+
603609
JMEM_VALGRIND_FREELIKE_SPACE (block_p);
604-
JMEM_VALGRIND_NOACCESS_SPACE (block_p, old_size);
605610
JMEM_VALGRIND_MALLOCLIKE_SPACE (ret_block_p, new_size);
611+
JMEM_VALGRIND_DEFINED_SPACE (ret_block_p, old_size);
612+
}
613+
else
614+
{
615+
JMEM_VALGRIND_NOACCESS_SPACE (prev_p, sizeof (jmem_heap_free_t));
606616
}
607617
}
608618

609-
JMEM_VALGRIND_NOACCESS_SPACE (prev_p, sizeof (jmem_heap_free_t));
610-
JMEM_VALGRIND_NOACCESS_SPACE (next_p, sizeof (jmem_heap_free_t));
611-
612619
if (ret_block_p != NULL)
613620
{
614621
/* Managed to extend the block. Update memory usage and the skip pointer. */
@@ -619,9 +626,6 @@ jmem_heap_realloc_block (void *ptr, /**< memory region to reallocate */
619626
{
620627
JERRY_CONTEXT (jmem_heap_limit) += CONFIG_GC_LIMIT;
621628
}
622-
623-
JMEM_VALGRIND_UNDEFINED_SPACE (ret_block_p, new_size);
624-
JMEM_VALGRIND_DEFINED_SPACE (ret_block_p, old_size);
625629
}
626630
else
627631
{

0 commit comments

Comments
 (0)