Refactor memory management (#2954)

This PR is a general cleanup for garbage collection and memory
allocation code paths.

Changes:
  * Removed an unnecesary local variable from 'ecma_gc_mark'.
  * Refactored 'ecma_gc_run' to have an implicit list head during
    iteration, which results in one less condition in the loops,
    and changed the loops to use compressed pointers to reduce the
    overall amount of compression/decompression.
  * Renamed 'jmem_free_unused_memory_severity_t' to 'jmem_pressure_t',
    and added additional values.
  * Removed 'jmem_free_unused_memory_callback', instead
    'ecma_free_unused_memory' is now called directly.
  * Reworked 'ecma_free_unused_memory' to handle all code paths related
    to 'jmem_pressure_t', and moved all relevant code paths into this
    function. This simplifies the code paths in other places.
  * Reworked 'jmem_heap_gc_and_alloc_block' to be more streamlined.
  * Changed mem-stats to not report unused pool chunks as allocated
    memory.
  * Created an allocator internal API for allocating/freeing memory blocks
    that are not reported as used memory in mem-stats.
  * Removed iteration statistics for the jerry allocator from mem-stats,
    as they don't provide any actually useful information.

Co-authored-by: Marko Fabo <mfabo@inf.u-szeged.hu>
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
Dániel Bátyai
2019-07-17 14:12:23 +02:00
committed by GitHub
parent a44d584842
commit ff22634e27
27 changed files with 297 additions and 538 deletions
+1 -1
View File
@@ -727,7 +727,7 @@ main (void)
jerry_release_value (global_obj_val);
/* Test: run gc. */
jerry_gc (JERRY_GC_SEVERITY_LOW);
jerry_gc (JERRY_GC_PRESSURE_LOW);
/* Test: spaces */
const jerry_char_t eval_code_src2[] = "\x0a \x0b \x0c \xc2\xa0 \xe2\x80\xa8 \xe2\x80\xa9 \xef\xbb\xbf 4321";
-126
View File
@@ -1,126 +0,0 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "jmem.h"
#define JMEM_ALLOCATOR_INTERNAL
#include "jmem-allocator-internal.h"
#include "test-common.h"
/* Heap size is 32K. */
#define test_heap_size (32 * 1024)
/* Iterations count. */
#define test_iters (4 * 1024)
/* Subiterations count. */
#define test_sub_iters 32
/* Threshold size of block to allocate. */
#define test_threshold_block_size 8192
uint8_t *ptrs[test_sub_iters];
size_t sizes[test_sub_iters];
bool is_one_chunked[test_sub_iters];
static void
test_heap_give_some_memory_back (jmem_free_unused_memory_severity_t severity)
{
int p;
if (severity == JMEM_FREE_UNUSED_MEMORY_SEVERITY_LOW)
{
p = 8;
}
else
{
TEST_ASSERT (severity == JMEM_FREE_UNUSED_MEMORY_SEVERITY_HIGH);
p = 1;
}
for (int i = 0; i < test_sub_iters; i++)
{
if (rand () % p == 0)
{
if (ptrs[i] != NULL)
{
for (size_t k = 0; k < sizes[i]; k++)
{
TEST_ASSERT (ptrs[i][k] == 0);
}
jmem_heap_free_block (ptrs[i], sizes[i]);
ptrs[i] = NULL;
}
}
}
} /* test_heap_give_some_memory_back */
int
main (void)
{
TEST_INIT ();
jmem_heap_init ();
jmem_register_free_unused_memory_callback (test_heap_give_some_memory_back);
#ifdef JMEM_STATS
// TODO: uncomment it after we solve the divide zero issue.
// jmem_heap_stats_print ();
#endif /* JMEM_STATS */
for (uint32_t i = 0; i < test_iters; i++)
{
for (uint32_t j = 0; j < test_sub_iters; j++)
{
size_t size = (size_t) rand () % test_threshold_block_size;
ptrs[j] = (uint8_t *) jmem_heap_alloc_block (size);
sizes[j] = size;
TEST_ASSERT (sizes[j] == 0 || ptrs[j] != NULL);
memset (ptrs[j], 0, sizes[j]);
}
/* jmem_heap_print (true); */
for (uint32_t j = 0; j < test_sub_iters; j++)
{
if (ptrs[j] != NULL)
{
for (size_t k = 0; k < sizes[j]; k++)
{
TEST_ASSERT (ptrs[j][k] == 0);
}
jmem_heap_free_block (ptrs[j], sizes[j]);
ptrs[j] = NULL;
}
}
}
#ifdef JMEM_STATS
jmem_heap_stats_print ();
#endif /* JMEM_STATS */
return 0;
} /* main */
+2 -2
View File
@@ -99,7 +99,7 @@ main (void)
jerry_release_value (object);
/* Collect garbage. */
jerry_gc (JERRY_GC_SEVERITY_LOW);
jerry_gc (JERRY_GC_PRESSURE_LOW);
/* Attempt to retrieve the object by its native pointer again. */
TEST_ASSERT (!jerry_objects_foreach_by_native_info (&test_info, find_test_object_by_data, &found_object));
@@ -124,7 +124,7 @@ main (void)
jerry_release_value (args[1]);
/* Collect garbage. */
jerry_gc (JERRY_GC_SEVERITY_LOW);
jerry_gc (JERRY_GC_PRESSURE_LOW);
/* Attempt to retrieve the object by the presence of its property again. */
args[0] = property_name;