@@ -91,6 +91,7 @@ jmem_heap_init (void)
91
91
92
92
JERRY_CONTEXT (jmem_heap_list_skip_p ) = & JERRY_HEAP_CONTEXT (first );
93
93
94
+ JMEM_VALGRIND_NOACCESS_SPACE (& JERRY_HEAP_CONTEXT (first ), sizeof (jmem_heap_free_t ));
94
95
JMEM_VALGRIND_NOACCESS_SPACE (JERRY_HEAP_CONTEXT (area ), JMEM_HEAP_AREA_SIZE );
95
96
96
97
#endif /* !ENABLED (JERRY_SYSTEM_ALLOCATOR) */
@@ -162,7 +163,7 @@ jmem_heap_alloc (const size_t size) /**< size of requested block */
162
163
JERRY_HEAP_CONTEXT (first ).next_offset = JMEM_HEAP_GET_OFFSET_FROM_ADDR (remaining_p );
163
164
}
164
165
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 ));
166
167
167
168
if (JERRY_UNLIKELY (data_space_p == JERRY_CONTEXT (jmem_heap_list_skip_p )))
168
169
{
@@ -239,12 +240,7 @@ jmem_heap_alloc (const size_t size) /**< size of requested block */
239
240
JMEM_VALGRIND_NOACCESS_SPACE (& JERRY_HEAP_CONTEXT (first ), sizeof (jmem_heap_free_t ));
240
241
241
242
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 );
248
244
249
245
return (void * ) data_space_p ;
250
246
#else /* ENABLED (JERRY_SYSTEM_ALLOCATOR) */
@@ -299,7 +295,6 @@ jmem_heap_gc_and_alloc_block (const size_t size, /**< required memory size */
299
295
data_space_p = jmem_heap_alloc (size );
300
296
}
301
297
302
- JMEM_VALGRIND_MALLOCLIKE_SPACE (data_space_p , size );
303
298
return data_space_p ;
304
299
} /* jmem_heap_gc_and_alloc_block */
305
300
@@ -365,8 +360,6 @@ jmem_heap_find_prev (const jmem_heap_free_t * const block_p) /**< which memory b
365
360
{
366
361
const jmem_heap_free_t * prev_p ;
367
362
368
- JMEM_VALGRIND_DEFINED_SPACE (& JERRY_HEAP_CONTEXT (first ), sizeof (jmem_heap_free_t ));
369
-
370
363
if (block_p > JERRY_CONTEXT (jmem_heap_list_skip_p ))
371
364
{
372
365
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
391
384
prev_p = next_p ;
392
385
}
393
386
387
+ JMEM_VALGRIND_NOACCESS_SPACE (prev_p , sizeof (jmem_heap_free_t ));
394
388
return (jmem_heap_free_t * ) prev_p ;
395
389
} /* jmem_heap_find_prev */
396
390
@@ -408,11 +402,13 @@ jmem_heap_insert_block (jmem_heap_free_t *block_p, /**< block to insert */
408
402
JERRY_ASSERT ((uintptr_t ) block_p % JMEM_ALIGNMENT == 0 );
409
403
JERRY_ASSERT (size % JMEM_ALIGNMENT == 0 );
410
404
405
+ JMEM_VALGRIND_NOACCESS_SPACE (block_p , size );
406
+
411
407
JMEM_VALGRIND_DEFINED_SPACE (prev_p , sizeof (jmem_heap_free_t ));
412
408
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 ));
415
409
JMEM_VALGRIND_DEFINED_SPACE (block_p , sizeof (jmem_heap_free_t ));
410
+ JMEM_VALGRIND_DEFINED_SPACE (next_p , sizeof (jmem_heap_free_t ));
411
+
416
412
417
413
const uint32_t block_offset = JMEM_HEAP_GET_OFFSET_FROM_ADDR (block_p );
418
414
@@ -445,7 +441,7 @@ jmem_heap_insert_block (jmem_heap_free_t *block_p, /**< block to insert */
445
441
JERRY_CONTEXT (jmem_heap_list_skip_p ) = prev_p ;
446
442
447
443
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 ) );
449
445
JMEM_VALGRIND_NOACCESS_SPACE (next_p , sizeof (jmem_heap_free_t ));
450
446
} /* jmem_heap_insert_block */
451
447
#endif /* !ENABLED (JERRY_SYSTEM_ALLOCATOR) */
@@ -468,13 +464,12 @@ jmem_heap_free_block_internal (void *ptr, /**< pointer to beginning of data spac
468
464
469
465
const size_t aligned_size = (size + JMEM_ALIGNMENT - 1 ) / JMEM_ALIGNMENT * JMEM_ALIGNMENT ;
470
466
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 );
473
469
jmem_heap_insert_block (block_p , prev_p , aligned_size );
474
470
475
471
JERRY_CONTEXT (jmem_heap_allocated_size ) -= aligned_size ;
476
472
477
- JMEM_VALGRIND_NOACCESS_SPACE (ptr , size );
478
473
JMEM_VALGRIND_FREELIKE_SPACE (ptr );
479
474
#else /* ENABLED (JERRY_SYSTEM_ALLOCATOR) */
480
475
JERRY_CONTEXT (jmem_heap_allocated_size ) -= size ;
@@ -511,8 +506,6 @@ jmem_heap_realloc_block (void *ptr, /**< memory region to reallocate */
511
506
if (aligned_old_size == aligned_new_size )
512
507
{
513
508
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 );
516
509
JMEM_HEAP_STAT_FREE (old_size );
517
510
JMEM_HEAP_STAT_ALLOC (new_size );
518
511
return block_p ;
@@ -521,8 +514,6 @@ jmem_heap_realloc_block (void *ptr, /**< memory region to reallocate */
521
514
if (aligned_new_size < aligned_old_size )
522
515
{
523
516
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 );
526
517
JMEM_HEAP_STAT_FREE (old_size );
527
518
JMEM_HEAP_STAT_ALLOC (new_size );
528
519
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 */
551
542
jmem_heap_free_t * prev_p = jmem_heap_find_prev (block_p );
552
543
JMEM_VALGRIND_DEFINED_SPACE (prev_p , sizeof (jmem_heap_free_t ));
553
544
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 ));
555
545
556
546
/* Check if block can be extended at the end */
557
547
if (((jmem_heap_free_t * ) ((uint8_t * ) block_p + aligned_old_size )) == next_p )
558
548
{
549
+ JMEM_VALGRIND_DEFINED_SPACE (next_p , sizeof (jmem_heap_free_t ));
550
+
559
551
if (required_size <= next_p -> size )
560
552
{
561
553
/* Block can be extended, update the list. */
@@ -573,9 +565,16 @@ jmem_heap_realloc_block (void *ptr, /**< memory region to reallocate */
573
565
prev_p -> next_offset = JMEM_HEAP_GET_OFFSET_FROM_ADDR (new_next_p );
574
566
}
575
567
568
+ /* next_p will be marked as undefined space. */
576
569
JMEM_VALGRIND_RESIZE_SPACE (block_p , old_size , new_size );
577
570
ret_block_p = block_p ;
578
571
}
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 ));
579
578
}
580
579
/*
581
580
* Check if block can be extended at the front.
@@ -597,18 +596,26 @@ jmem_heap_realloc_block (void *ptr, /**< memory region to reallocate */
597
596
prev_p -> size = (uint32_t ) (prev_p -> size - required_size );
598
597
}
599
598
599
+ JMEM_VALGRIND_NOACCESS_SPACE (prev_p , sizeof (jmem_heap_free_t ));
600
+
600
601
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 );
602
607
memmove (ret_block_p , block_p , old_size );
608
+
603
609
JMEM_VALGRIND_FREELIKE_SPACE (block_p );
604
- JMEM_VALGRIND_NOACCESS_SPACE (block_p , old_size );
605
610
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 ));
606
616
}
607
617
}
608
618
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
-
612
619
if (ret_block_p != NULL )
613
620
{
614
621
/* 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 */
619
626
{
620
627
JERRY_CONTEXT (jmem_heap_limit ) += CONFIG_GC_LIMIT ;
621
628
}
622
-
623
- JMEM_VALGRIND_UNDEFINED_SPACE (ret_block_p , new_size );
624
- JMEM_VALGRIND_DEFINED_SPACE (ret_block_p , old_size );
625
629
}
626
630
else
627
631
{
0 commit comments