Skip to content

Commit c3b6bfe

Browse files
authored
Reorganize heap context related elements (#2500)
- Moved global context's `JMEM_HEAP_SIZE` to jcontext.h as external context's heap size is declared there, too. - Moved the assert on `jmem_heap_t` vs `JMEM_HEAP_SIZE` to jcontext.c, as both entities are declared in the corresponding header. - Removed superfluous checks on `JMEM_HEAP_SIZE` as it is not a publicly configurable macro (opposed to `CONFIG_MEM_HEAP_AREA_SIZE`). - Ensured that all definitions of and references to `jmem_heap_t`, `JERRY_HEAP_CONTEXT`, `JERRY_HEAP_SIZE`, and `JERRY_HEAP_AREA_SIZE` are guarded by `#ifndef JERRY_SYSTEM_ALLOCATOR`, as they are meaningless if the system allocator is used. The commit also contains some stylistic changes in jcontext.h JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
1 parent 6049c03 commit c3b6bfe

File tree

4 files changed

+59
-69
lines changed

4 files changed

+59
-69
lines changed

jerry-core/jcontext/jcontext.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@
2020
*/
2121

2222
#ifndef JERRY_ENABLE_EXTERNAL_CONTEXT
23+
2324
/**
2425
* Global context.
2526
*/
2627
jerry_context_t jerry_global_context;
2728

29+
#ifndef JERRY_SYSTEM_ALLOCATOR
30+
31+
/**
32+
* Check size of heap is corresponding to configuration
33+
*/
34+
JERRY_STATIC_ASSERT (sizeof (jmem_heap_t) <= JMEM_HEAP_SIZE,
35+
size_of_mem_heap_must_be_less_than_or_equal_to_JMEM_HEAP_SIZE);
36+
2837
/**
2938
* Jerry global heap section attribute.
3039
*/
@@ -34,11 +43,11 @@ jerry_context_t jerry_global_context;
3443
#define JERRY_GLOBAL_HEAP_SECTION JERRY_ATTR_SECTION (JERRY_HEAP_SECTION_ATTR)
3544
#endif /* !JERRY_HEAP_SECTION_ATTR */
3645

37-
#ifndef JERRY_SYSTEM_ALLOCATOR
3846
/**
3947
* Global heap.
4048
*/
4149
jmem_heap_t jerry_global_heap JERRY_ATTR_ALIGNED (JMEM_ALIGNMENT) JERRY_GLOBAL_HEAP_SECTION;
50+
4251
#endif /* !JERRY_SYSTEM_ALLOCATOR */
4352

4453
#endif /* !JERRY_ENABLE_EXTERNAL_CONTEXT */

jerry-core/jcontext/jcontext.h

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
/**
16+
/*
1717
* Memory context for JerryScript
1818
*/
1919
#ifndef JCONTEXT_H
@@ -33,11 +33,6 @@
3333
* @{
3434
*/
3535

36-
/**
37-
* First member of the jerry context
38-
*/
39-
#define JERRY_CONTEXT_FIRST_MEMBER ecma_builtin_objects
40-
4136
/**
4237
* User context item
4338
*/
@@ -50,6 +45,11 @@ typedef struct jerry_context_data_header
5045
#define JERRY_CONTEXT_DATA_HEADER_USER_DATA(item_p) \
5146
((uint8_t *) (item_p + 1))
5247

48+
/**
49+
* First member of the jerry context
50+
*/
51+
#define JERRY_CONTEXT_FIRST_MEMBER ecma_builtin_objects
52+
5353
/**
5454
* JerryScript context
5555
*
@@ -141,24 +141,35 @@ typedef struct
141141

142142
#ifdef JERRY_ENABLE_EXTERNAL_CONTEXT
143143

144-
#ifndef JERRY_GET_CURRENT_INSTANCE
144+
/*
145+
* This part is for JerryScript which uses external context.
146+
*/
145147

148+
#ifndef JERRY_GET_CURRENT_INSTANCE
146149
/**
147150
* Default function if JERRY_GET_CURRENT_INSTANCE is not defined.
148151
*/
149152
#define JERRY_GET_CURRENT_INSTANCE() (jerry_port_get_current_instance ())
150-
151153
#endif /* !JERRY_GET_CURRENT_INSTANCE */
152154

153-
/**
154-
* This part is for Jerry which enable external context.
155-
*/
155+
#define JERRY_CONTEXT(field) (JERRY_GET_CURRENT_INSTANCE ()->context.field)
156+
157+
#ifndef JERRY_SYSTEM_ALLOCATOR
158+
159+
#define JMEM_HEAP_SIZE (JERRY_GET_CURRENT_INSTANCE ()->heap_size)
160+
161+
#define JMEM_HEAP_AREA_SIZE (JMEM_HEAP_SIZE - JMEM_ALIGNMENT)
162+
156163
typedef struct
157164
{
158165
jmem_heap_free_t first; /**< first node in free region list */
159166
uint8_t area[]; /**< heap area */
160167
} jmem_heap_t;
161168

169+
#define JERRY_HEAP_CONTEXT(field) (JERRY_GET_CURRENT_INSTANCE ()->heap_p->field)
170+
171+
#endif /* !JERRY_SYSTEM_ALLOCATOR */
172+
162173
/**
163174
* Description of jerry instance which is the header of the context space.
164175
*/
@@ -171,33 +182,28 @@ struct jerry_instance_t
171182
#endif /* !JERRY_SYSTEM_ALLOCATOR */
172183
};
173184

174-
#define JERRY_CONTEXT(field) (JERRY_GET_CURRENT_INSTANCE ()->context.field)
175-
176-
#ifndef JERRY_SYSTEM_ALLOCATOR
177-
178-
static inline jmem_heap_t * JERRY_ATTR_ALWAYS_INLINE
179-
jerry_context_get_current_heap (void)
180-
{
181-
return JERRY_GET_CURRENT_INSTANCE ()->heap_p;
182-
} /* jerry_context_get_current_heap */
183-
184-
#define JERRY_HEAP_CONTEXT(field) (jerry_context_get_current_heap ()->field)
185-
186-
#ifdef JMEM_HEAP_SIZE
187-
#error "JMEM_HEAP_SIZE must not be defined if JERRY_ENABLE_EXTERNAL_CONTEXT is defined"
188-
#endif /* JMEM_HEAP_SIZE */
185+
#else /* !JERRY_ENABLE_EXTERNAL_CONTEXT */
189186

190-
#define JMEM_HEAP_SIZE (JERRY_GET_CURRENT_INSTANCE ()->heap_size)
187+
/*
188+
* This part is for JerryScript which uses default context.
189+
*/
191190

192-
#define JMEM_HEAP_AREA_SIZE (JERRY_GET_CURRENT_INSTANCE ()->heap_size - JMEM_ALIGNMENT)
191+
/**
192+
* Global context.
193+
*/
194+
extern jerry_context_t jerry_global_context;
193195

194-
#endif /* !JERRY_SYSTEM_ALLOCATOR */
196+
/**
197+
* Provides a reference to a field in the current context.
198+
*/
199+
#define JERRY_CONTEXT(field) (jerry_global_context.field)
195200

196-
#else /* !JERRY_ENABLE_EXTERNAL_CONTEXT */
201+
#ifndef JERRY_SYSTEM_ALLOCATOR
197202

198203
/**
199-
* This part is for Jerry which use default context.
200-
*/
204+
* Size of heap
205+
*/
206+
#define JMEM_HEAP_SIZE ((size_t) (CONFIG_MEM_HEAP_AREA_SIZE))
201207

202208
/**
203209
* Calculate heap area size, leaving space for a pointer to the free list
@@ -222,26 +228,13 @@ typedef struct
222228
uint8_t area[JMEM_HEAP_AREA_SIZE]; /**< heap area */
223229
} jmem_heap_t;
224230

225-
/**
226-
* Global context.
227-
*/
228-
extern jerry_context_t jerry_global_context;
229-
230-
#ifndef JERRY_SYSTEM_ALLOCATOR
231231
/**
232232
* Global heap.
233233
*/
234234
extern jmem_heap_t jerry_global_heap;
235-
#endif /* !JERRY_SYSTEM_ALLOCATOR */
236235

237236
/**
238-
* Provides a reference to a field in the current context.
239-
*/
240-
#define JERRY_CONTEXT(field) (jerry_global_context.field)
241-
242-
#ifndef JERRY_SYSTEM_ALLOCATOR
243-
/**
244-
* Provides a reference to the area field of the heap.
237+
* Provides a reference to a field of the heap.
245238
*/
246239
#define JERRY_HEAP_CONTEXT(field) (jerry_global_heap.field)
247240

jerry-core/jmem/jmem-heap.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @{
3333
*/
3434

35+
#ifndef JERRY_SYSTEM_ALLOCATOR
3536
/**
3637
* End of list marker.
3738
*/
@@ -52,7 +53,6 @@
5253
* @}
5354
*/
5455

55-
#ifndef JERRY_SYSTEM_ALLOCATOR
5656
/**
5757
* Get end of region
5858
*
@@ -65,14 +65,6 @@ jmem_heap_get_region_end (jmem_heap_free_t *curr_p) /**< current region */
6565
} /* jmem_heap_get_region_end */
6666
#endif /* !JERRY_SYSTEM_ALLOCATOR */
6767

68-
#ifndef JERRY_ENABLE_EXTERNAL_CONTEXT
69-
/**
70-
* Check size of heap is corresponding to configuration
71-
*/
72-
JERRY_STATIC_ASSERT (sizeof (jmem_heap_t) <= JMEM_HEAP_SIZE,
73-
size_of_mem_heap_must_be_less_than_or_equal_to_MEM_HEAP_SIZE);
74-
#endif /* !JERRY_ENABLE_EXTERNAL_CONTEXT */
75-
7668
#ifdef JMEM_STATS
7769
static void jmem_heap_stat_init (void);
7870
static void jmem_heap_stat_alloc (size_t num);
@@ -117,12 +109,11 @@ static void jmem_heap_stat_free_iter (void);
117109
void
118110
jmem_heap_init (void)
119111
{
112+
#ifndef JERRY_SYSTEM_ALLOCATOR
120113
#ifndef JERRY_CPOINTER_32_BIT
121114
/* the maximum heap size for 16bit compressed pointers should be 512K */
122115
JERRY_ASSERT (((UINT16_MAX + 1) << JMEM_ALIGNMENT_LOG) >= JMEM_HEAP_SIZE);
123116
#endif /* !JERRY_CPOINTER_32_BIT */
124-
125-
#ifndef JERRY_SYSTEM_ALLOCATOR
126117
JERRY_ASSERT ((uintptr_t) JERRY_HEAP_CONTEXT (area) % JMEM_ALIGNMENT == 0);
127118

128119
JERRY_CONTEXT (jmem_heap_limit) = CONFIG_MEM_HEAP_DESIRED_LIMIT;
@@ -547,9 +538,12 @@ jmem_heap_stats_print (void)
547538
{
548539
jmem_heap_stats_t *heap_stats = &JERRY_CONTEXT (jmem_heap_stats);
549540

550-
JERRY_DEBUG_MSG ("Heap stats:\n"
551-
" Heap size = %zu bytes\n"
552-
" Allocated = %zu bytes\n"
541+
JERRY_DEBUG_MSG ("Heap stats:\n");
542+
#ifndef JERRY_SYSTEM_ALLOCATOR
543+
JERRY_DEBUG_MSG (" Heap size = %zu bytes\n",
544+
heap_stats->size);
545+
#endif /* !JERRY_SYSTEM_ALLOCATOR */
546+
JERRY_DEBUG_MSG (" Allocated = %zu bytes\n"
553547
" Peak allocated = %zu bytes\n"
554548
" Waste = %zu bytes\n"
555549
" Peak waste = %zu bytes\n"
@@ -561,7 +555,6 @@ jmem_heap_stats_print (void)
561555
" Peak allocated object data = %zu bytes\n"
562556
" Allocated property data = %zu bytes\n"
563557
" Peak allocated property data = %zu bytes\n",
564-
heap_stats->size,
565558
heap_stats->allocated_bytes,
566559
heap_stats->peak_allocated_bytes,
567560
heap_stats->waste_bytes,
@@ -593,7 +586,9 @@ jmem_heap_stats_print (void)
593586
static void
594587
jmem_heap_stat_init (void)
595588
{
589+
#ifndef JERRY_SYSTEM_ALLOCATOR
596590
JERRY_CONTEXT (jmem_heap_stats).size = JMEM_HEAP_AREA_SIZE;
591+
#endif /* !JERRY_SYSTEM_ALLOCATOR */
597592
} /* jmem_heap_stat_init */
598593

599594
/**

jerry-core/jmem/jmem.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525
* @{
2626
*/
2727

28-
#ifndef JERRY_ENABLE_EXTERNAL_CONTEXT
29-
/**
30-
* Size of heap
31-
*/
32-
#define JMEM_HEAP_SIZE ((size_t) (CONFIG_MEM_HEAP_AREA_SIZE))
33-
#endif /* !JERRY_ENABLE_EXTERNAL_CONTEXT */
34-
3528
/**
3629
* Logarithm of required alignment for allocated units/blocks
3730
*/

0 commit comments

Comments
 (0)