1/* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16extern "C" 17{ 18 #include "ecma-init-finalize.h" 19 #include "jmem.h" 20} 21 22#include "jerryscript-port.h" 23#include "jerryscript-port-default.h" 24#include "jerryscript.h" 25#include "test-common.h" 26#include <gtest/gtest.h> 27 28#define BASIC_SIZE (64) 29 30class JmemTest : public testing::Test{ 31public: 32 static void SetUpTestCase() 33 { 34 GTEST_LOG_(INFO) << "JmemTest SetUpTestCase"; 35 } 36 37 static void TearDownTestCase() 38 { 39 GTEST_LOG_(INFO) << "JmemTest TearDownTestCase"; 40 } 41 42 void SetUp() override {} 43 void TearDown() override {} 44 45}; 46static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024; 47static void* context_alloc_fn(size_t size, void* cb_data) 48{ 49 (void)cb_data; 50 size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size; 51 return malloc(newSize); 52} 53 54HWTEST_F(JmemTest, Test001, testing::ext::TestSize.Level1) 55{ 56 TEST_INIT (); 57 jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL); 58 jerry_port_default_set_current_context (ctx_p); 59 jmem_init (); 60 ecma_init (); 61 62 { 63 uint8_t *block1_p = (uint8_t *) jmem_heap_alloc_block (BASIC_SIZE); 64 uint8_t *block2_p = (uint8_t *) jmem_heap_alloc_block (BASIC_SIZE); 65 uint8_t *block3_p = (uint8_t *) jmem_heap_alloc_block (BASIC_SIZE); 66 67 /* [block1 64] [block2 64] [block3 64] [...] */ 68 69 for (uint8_t i = 0; i < BASIC_SIZE; i++) 70 { 71 block2_p[i] = i; 72 } 73 74 /* Realloc by moving */ 75 block2_p = (uint8_t *)jmem_heap_realloc_block (block2_p, BASIC_SIZE, BASIC_SIZE * 2); 76 77 /* [block1 64] [free 64] [block3 64] [block2 128] [...] */ 78 79 for (uint8_t i = 0; i < BASIC_SIZE; i++) 80 { 81 TEST_ASSERT (block2_p[i] == i); 82 } 83 84 for (uint8_t i = BASIC_SIZE; i < BASIC_SIZE * 2; i++) 85 { 86 block2_p[i] = i; 87 } 88 89 uint8_t *block4_p = (uint8_t *) jmem_heap_alloc_block (BASIC_SIZE * 2); 90 91 /* [block1 64] [free 64] [block3 64] [block2 128] [block4 128] [...] */ 92 93 jmem_heap_free_block (block3_p, BASIC_SIZE); 94 95 /* [block1 64] [free 128] [block2 128] [block4 128] [...] */ 96 97 /* Realloc by extending front */ 98 block2_p = (uint8_t *) jmem_heap_realloc_block (block2_p, BASIC_SIZE * 2, BASIC_SIZE * 3); 99 100 /* [block1 64] [free 64] [block2 192] [block4 128] [...] */ 101 102 for (uint8_t i = 0; i < BASIC_SIZE * 2; i++) 103 { 104 TEST_ASSERT (block2_p[i] == i); 105 } 106 107 /* Shrink */ 108 block2_p = (uint8_t *) jmem_heap_realloc_block (block2_p, BASIC_SIZE * 3, BASIC_SIZE); 109 110 /* [block1 64] [free 64] [block2 64] [free 128] [block4 128] [...] */ 111 112 for (uint8_t i = 0; i < BASIC_SIZE; i++) 113 { 114 TEST_ASSERT (block2_p[i] == i); 115 } 116 117 for (uint8_t i = 0; i < BASIC_SIZE; i++) 118 { 119 block1_p[i] = i; 120 } 121 122 /* Grow in place */ 123 block1_p = (uint8_t *) jmem_heap_realloc_block (block1_p, BASIC_SIZE, BASIC_SIZE * 2); 124 125 /* [block1 128] [block2 64] [free 128] [block4 128] [...] */ 126 127 for (uint8_t i = 0; i < BASIC_SIZE; i++) 128 { 129 TEST_ASSERT (block1_p[i] == i); 130 } 131 132 jmem_heap_free_block (block1_p, BASIC_SIZE * 2); 133 jmem_heap_free_block (block2_p, BASIC_SIZE); 134 jmem_heap_free_block (block4_p, BASIC_SIZE * 2); 135 } 136 137 ecma_finalize (); 138 jmem_finalize (); 139 free (ctx_p); 140 return; 141} 142