• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "ecma-init-finalize.h"
17 #include "jmem.h"
18 
19 #include "test-common.h"
20 
21 #define BASIC_SIZE (64)
22 
23 int
main(void)24 main (void)
25 {
26   TEST_INIT ();
27 
28   jmem_init ();
29   ecma_init ();
30 
31   {
32     uint8_t *block1_p = (uint8_t *) jmem_heap_alloc_block (BASIC_SIZE);
33     uint8_t *block2_p = (uint8_t *) jmem_heap_alloc_block (BASIC_SIZE);
34     uint8_t *block3_p = (uint8_t *) jmem_heap_alloc_block (BASIC_SIZE);
35 
36     /* [block1 64] [block2 64] [block3 64] [...] */
37 
38     for (uint8_t i = 0; i < BASIC_SIZE; i++)
39     {
40       block2_p[i] = i;
41     }
42 
43     /* Realloc by moving */
44     block2_p = jmem_heap_realloc_block (block2_p, BASIC_SIZE, BASIC_SIZE * 2);
45 
46     /* [block1 64] [free 64] [block3 64] [block2 128] [...] */
47 
48     for (uint8_t i = 0; i < BASIC_SIZE; i++)
49     {
50       TEST_ASSERT (block2_p[i] == i);
51     }
52 
53     for (uint8_t i = BASIC_SIZE; i < BASIC_SIZE * 2; i++)
54     {
55       block2_p[i] = i;
56     }
57 
58     uint8_t *block4_p = (uint8_t *) jmem_heap_alloc_block (BASIC_SIZE * 2);
59 
60     /* [block1 64] [free 64] [block3 64] [block2 128] [block4 128] [...] */
61 
62     jmem_heap_free_block (block3_p, BASIC_SIZE);
63 
64     /* [block1 64] [free 128] [block2 128] [block4 128] [...] */
65 
66     /* Realloc by extending front */
67     block2_p = (uint8_t *) jmem_heap_realloc_block (block2_p, BASIC_SIZE * 2, BASIC_SIZE * 3);
68 
69     /* [block1 64] [free 64] [block2 192] [block4 128] [...] */
70 
71     for (uint8_t i = 0; i < BASIC_SIZE * 2; i++)
72     {
73       TEST_ASSERT (block2_p[i] == i);
74     }
75 
76     /* Shrink */
77     block2_p = (uint8_t *) jmem_heap_realloc_block (block2_p, BASIC_SIZE * 3, BASIC_SIZE);
78 
79     /* [block1 64] [free 64] [block2 64] [free 128] [block4 128] [...] */
80 
81     for (uint8_t i = 0; i < BASIC_SIZE; i++)
82     {
83       TEST_ASSERT (block2_p[i] == i);
84     }
85 
86     for (uint8_t i = 0; i < BASIC_SIZE; i++)
87     {
88       block1_p[i] = i;
89     }
90 
91     /* Grow in place */
92     block1_p = (uint8_t *) jmem_heap_realloc_block (block1_p, BASIC_SIZE, BASIC_SIZE * 2);
93 
94     /* [block1 128] [block2 64] [free 128] [block4 128] [...] */
95 
96     for (uint8_t i = 0; i < BASIC_SIZE; i++)
97     {
98       TEST_ASSERT (block1_p[i] == i);
99     }
100 
101     jmem_heap_free_block (block1_p, BASIC_SIZE * 2);
102     jmem_heap_free_block (block2_p, BASIC_SIZE);
103     jmem_heap_free_block (block4_p, BASIC_SIZE * 2);
104   }
105 
106   ecma_finalize ();
107   jmem_finalize ();
108 
109   return 0;
110 } /* main */
111