Skip to content

Commit 3a78d30

Browse files
committed
Introducing global context.
Currently the static variables of the allocator are moved into a global structure. The future plan is supporting multiple context models with different advantages and disadvantages. Users can select the most appropriate context model for their own use case. This context model must be selected at compile time using compiler defines. Currently only one model is implemented, which will be the default context model: the context is stored in global variables, so only a single context is available which always exists. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent ca39875 commit 3a78d30

File tree

7 files changed

+360
-237
lines changed

7 files changed

+360
-237
lines changed

jerry-core/CMakeLists.txt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,16 @@ project (JerryCore C ASM)
105105
# Include directories
106106
set(INCLUDE_CORE
107107
${CMAKE_SOURCE_DIR}/jerry-core
108-
${CMAKE_SOURCE_DIR}/jerry-core/lit
109-
${CMAKE_SOURCE_DIR}/jerry-core/rcs
110-
${CMAKE_SOURCE_DIR}/jerry-core/jmem
111-
${CMAKE_SOURCE_DIR}/jerry-core/vm
112-
${CMAKE_SOURCE_DIR}/jerry-core/ecma/builtin-objects
113108
${CMAKE_SOURCE_DIR}/jerry-core/ecma/base
109+
${CMAKE_SOURCE_DIR}/jerry-core/ecma/builtin-objects
114110
${CMAKE_SOURCE_DIR}/jerry-core/ecma/operations
111+
${CMAKE_SOURCE_DIR}/jerry-core/jcontext
112+
${CMAKE_SOURCE_DIR}/jerry-core/jmem
113+
${CMAKE_SOURCE_DIR}/jerry-core/jrt
114+
${CMAKE_SOURCE_DIR}/jerry-core/lit
115115
${CMAKE_SOURCE_DIR}/jerry-core/parser/js
116116
${CMAKE_SOURCE_DIR}/jerry-core/parser/regexp
117-
${CMAKE_SOURCE_DIR}/jerry-core/jrt)
117+
${CMAKE_SOURCE_DIR}/jerry-core/vm)
118118

119119
# Third-party
120120
# Valgrind
@@ -123,29 +123,29 @@ project (JerryCore C ASM)
123123
# Sources
124124
# Jerry core
125125
file(GLOB SOURCE_CORE_API *.c)
126-
file(GLOB SOURCE_CORE_LIT lit/*.c)
127-
file(GLOB SOURCE_CORE_RCS rcs/*.c)
128-
file(GLOB SOURCE_CORE_MEM jmem/*.c)
129-
file(GLOB SOURCE_CORE_VM vm/*.c)
130-
file(GLOB SOURCE_CORE_ECMA_BUILTINS ecma/builtin-objects/*.c)
131126
file(GLOB SOURCE_CORE_ECMA_BASE ecma/base/*.c)
127+
file(GLOB SOURCE_CORE_ECMA_BUILTINS ecma/builtin-objects/*.c)
132128
file(GLOB SOURCE_CORE_ECMA_OPERATIONS ecma/operations/*.c)
129+
file(GLOB SOURCE_CORE_JCONTEXT jcontext/*.c)
130+
file(GLOB SOURCE_CORE_JMEM jmem/*.c)
131+
file(GLOB SOURCE_CORE_JRT jrt/*.c)
132+
file(GLOB SOURCE_CORE_LIT lit/*.c)
133133
file(GLOB SOURCE_CORE_PARSER_JS parser/js/*.c)
134134
file(GLOB SOURCE_CORE_PARSER_REGEXP parser/regexp/*.c)
135-
file(GLOB SOURCE_CORE_JRT jrt/*.c)
135+
file(GLOB SOURCE_CORE_VM vm/*.c)
136136

137137
set(SOURCE_CORE_FILES
138138
${SOURCE_CORE_API}
139-
${SOURCE_CORE_LIT}
140-
${SOURCE_CORE_RCS}
141-
${SOURCE_CORE_MEM}
142-
${SOURCE_CORE_VM}
143-
${SOURCE_CORE_ECMA_BUILTINS}
144139
${SOURCE_CORE_ECMA_BASE}
140+
${SOURCE_CORE_ECMA_BUILTINS}
145141
${SOURCE_CORE_ECMA_OPERATIONS}
142+
${SOURCE_CORE_JCONTEXT}
143+
${SOURCE_CORE_JMEM}
144+
${SOURCE_CORE_JRT}
145+
${SOURCE_CORE_LIT}
146146
${SOURCE_CORE_PARSER_JS}
147147
${SOURCE_CORE_PARSER_REGEXP}
148-
${SOURCE_CORE_JRT})
148+
${SOURCE_CORE_VM})
149149

150150
# Jerry port
151151
if(NOT ("${PORT_DIR}" STREQUAL ""))

jerry-core/jcontext/jcontext.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Copyright 2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2016 University of Szeged.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "jcontext.h"
18+
19+
/** \addtogroup context Jerry context
20+
* @{
21+
*
22+
* \addtogroup context Context
23+
* @{
24+
*/
25+
26+
/**
27+
* Global context.
28+
*/
29+
jerry_context_t jerry_global_context;
30+
31+
/**
32+
* Jerry global heap section attribute.
33+
*/
34+
#ifndef JERRY_HEAP_SECTION_ATTR
35+
#define JERRY_GLOBAL_HEAP_SECTION
36+
#else /* JERRY_HEAP_SECTION_ATTR */
37+
#define JERRY_GLOBAL_HEAP_SECTION __attribute__ ((section (JERRY_HEAP_SECTION_ATTR)))
38+
#endif /* !JERRY_HEAP_SECTION_ATTR */
39+
40+
/**
41+
* Global heap.
42+
*/
43+
jmem_heap_t jerry_global_heap __attribute__ ((aligned (JMEM_ALIGNMENT))) JERRY_GLOBAL_HEAP_SECTION;
44+
45+
/**
46+
* @}
47+
* @}
48+
*/

jerry-core/jcontext/jcontext.h

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/* Copyright 2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2016 University of Szeged.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Memory context for JerryScript
19+
*/
20+
#ifndef JCONTEXT_H
21+
#define JCONTEXT_H
22+
23+
#include "jrt.h"
24+
#include "jmem-allocator.h"
25+
#include "jmem-config.h"
26+
27+
/** \addtogroup context Jerry context
28+
* @{
29+
*
30+
* \addtogroup context Context
31+
* @{
32+
*/
33+
34+
/**
35+
* Calculate heap area size, leaving space for a pointer to the free list
36+
*/
37+
#define JMEM_HEAP_AREA_SIZE (JMEM_HEAP_SIZE - JMEM_ALIGNMENT)
38+
39+
/**
40+
* Heap structure
41+
*
42+
* Memory blocks returned by the allocator must not start from the
43+
* beginning of the heap area because offset 0 is reserved for
44+
* JMEM_CP_NULL. This special constant is used in several places,
45+
* e.g. it marks the end of the property chain list, so it cannot
46+
* be eliminated from the project. Although the allocator cannot
47+
* use the first 8 bytes of the heap, nothing prevents to use it
48+
* for other purposes. Currently the free region start is stored
49+
* there.
50+
*/
51+
typedef struct
52+
{
53+
jmem_heap_free_t first; /**< first node in free region list */
54+
uint8_t area[JMEM_HEAP_AREA_SIZE]; /**< heap area */
55+
} jmem_heap_t;
56+
57+
/**
58+
* JerryScript context
59+
*
60+
* The purpose of this header is storing
61+
* all global variables for Jerry
62+
*/
63+
typedef struct
64+
{
65+
/**
66+
* Memory manager part.
67+
*/
68+
size_t jmem_heap_allocated_size; /**< size of allocated regions */
69+
size_t jmem_heap_limit; /**< current limit of heap usage, that is upon being reached,
70+
* causes call of "try give memory back" callbacks */
71+
jmem_heap_free_t *jmem_heap_list_skip_p; /**< This is used to speed up deallocation. */
72+
jmem_pools_chunk_t *jmem_free_chunk_p; /**< list of free pool chunks */
73+
jmem_free_unused_memory_callback_t jmem_free_unused_memory_callback; /**< Callback for freeing up memory. */
74+
75+
#ifdef JMEM_STATS
76+
jmem_heap_stats_t jmem_heap_stats; /**< heap's memory usage statistics */
77+
jmem_pools_stats_t jmem_pools_stats; /**< pools' memory usage statistics */
78+
#endif /* MEM_STATS */
79+
80+
#ifdef JERRY_VALGRIND_FREYA
81+
bool valgrind_freya_mempool_request; /**< Tells whether a pool manager
82+
* allocator request is in progress */
83+
#endif /* JERRY_VALGRIND_FREYA */
84+
} jerry_context_t;
85+
86+
/**
87+
* Jerry global context.
88+
*/
89+
extern jerry_context_t jerry_global_context;
90+
91+
/**
92+
* Jerry global heap.
93+
*/
94+
extern jmem_heap_t jerry_global_heap;
95+
96+
/**
97+
* Provides a reference to a field in the current context.
98+
*/
99+
#define JERRY_CONTEXT(field) (jerry_global_context.field)
100+
101+
/**
102+
* Provides a reference to the area field of the heap.
103+
*/
104+
#define JERRY_HEAP_CONTEXT(field) (jerry_global_heap.field)
105+
106+
/**
107+
* @}
108+
* @}
109+
*/
110+
111+
#endif /* !JCONTEXT_H */

jerry-core/jmem/jmem-allocator.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,15 @@
1818
* Allocator implementation
1919
*/
2020

21-
#include "jrt.h"
22-
#include "jrt-libc-includes.h"
21+
#include "jcontext.h"
2322
#include "jmem-allocator.h"
2423
#include "jmem-heap.h"
2524
#include "jmem-poolman.h"
25+
#include "jrt-libc-includes.h"
2626

2727
#define JMEM_ALLOCATOR_INTERNAL
2828
#include "jmem-allocator-internal.h"
2929

30-
/**
31-
* The 'try to give memory back' callback
32-
*/
33-
static jmem_free_unused_memory_callback_t jmem_free_unused_memory_callback = NULL;
34-
3530
/**
3631
* Initialize memory allocators.
3732
*/
@@ -94,9 +89,9 @@ void
9489
jmem_register_free_unused_memory_callback (jmem_free_unused_memory_callback_t callback) /**< callback routine */
9590
{
9691
/* Currently only one callback is supported */
97-
JERRY_ASSERT (jmem_free_unused_memory_callback == NULL);
92+
JERRY_ASSERT (JERRY_CONTEXT (jmem_free_unused_memory_callback) == NULL);
9893

99-
jmem_free_unused_memory_callback = callback;
94+
JERRY_CONTEXT (jmem_free_unused_memory_callback) = callback;
10095
} /* jmem_register_free_unused_memory_callback */
10196

10297
/**
@@ -106,9 +101,9 @@ void
106101
jmem_unregister_free_unused_memory_callback (jmem_free_unused_memory_callback_t callback) /**< callback routine */
107102
{
108103
/* Currently only one callback is supported */
109-
JERRY_ASSERT (jmem_free_unused_memory_callback == callback);
104+
JERRY_ASSERT (JERRY_CONTEXT (jmem_free_unused_memory_callback) == callback);
110105

111-
jmem_free_unused_memory_callback = NULL;
106+
JERRY_CONTEXT (jmem_free_unused_memory_callback) = NULL;
112107
} /* jmem_unregister_free_unused_memory_callback */
113108

114109
/**
@@ -117,9 +112,9 @@ jmem_unregister_free_unused_memory_callback (jmem_free_unused_memory_callback_t
117112
void
118113
jmem_run_free_unused_memory_callbacks (jmem_free_unused_memory_severity_t severity) /**< severity of the request */
119114
{
120-
if (jmem_free_unused_memory_callback != NULL)
115+
if (JERRY_CONTEXT (jmem_free_unused_memory_callback) != NULL)
121116
{
122-
jmem_free_unused_memory_callback (severity);
117+
JERRY_CONTEXT (jmem_free_unused_memory_callback) (severity);
123118
}
124119

125120
jmem_pools_collect_empty ();

jerry-core/jmem/jmem-allocator.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,24 @@ typedef enum
6969
} jmem_free_unused_memory_severity_t;
7070

7171
/**
72-
* A 'try give memory back' callback routine type.
72+
* Free region node
73+
*/
74+
typedef struct
75+
{
76+
uint32_t next_offset; /* Offset of next region in list */
77+
uint32_t size; /* Size of region */
78+
} jmem_heap_free_t;
79+
80+
/**
81+
* Node for free chunk list
82+
*/
83+
typedef struct mem_pools_chunk
84+
{
85+
struct mem_pools_chunk *next_p; /* pointer to next pool chunk */
86+
} jmem_pools_chunk_t;
87+
88+
/**
89+
* A free memory callback routine type.
7390
*/
7491
typedef void (*jmem_free_unused_memory_callback_t) (jmem_free_unused_memory_severity_t);
7592

0 commit comments

Comments
 (0)