Skip to content

Reorganize heap context related elements #2500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion jerry-core/jcontext/jcontext.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@
*/

#ifndef JERRY_ENABLE_EXTERNAL_CONTEXT

/**
* Global context.
*/
jerry_context_t jerry_global_context;

#ifndef JERRY_SYSTEM_ALLOCATOR

/**
* Check size of heap is corresponding to configuration
*/
JERRY_STATIC_ASSERT (sizeof (jmem_heap_t) <= JMEM_HEAP_SIZE,
size_of_mem_heap_must_be_less_than_or_equal_to_JMEM_HEAP_SIZE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we use lowercase everywhere.


/**
* Jerry global heap section attribute.
*/
Expand All @@ -34,11 +43,11 @@ jerry_context_t jerry_global_context;
#define JERRY_GLOBAL_HEAP_SECTION JERRY_ATTR_SECTION (JERRY_HEAP_SECTION_ATTR)
#endif /* !JERRY_HEAP_SECTION_ATTR */

#ifndef JERRY_SYSTEM_ALLOCATOR
/**
* Global heap.
*/
jmem_heap_t jerry_global_heap JERRY_ATTR_ALIGNED (JMEM_ALIGNMENT) JERRY_GLOBAL_HEAP_SECTION;

#endif /* !JERRY_SYSTEM_ALLOCATOR */

#endif /* !JERRY_ENABLE_EXTERNAL_CONTEXT */
Expand Down
85 changes: 39 additions & 46 deletions jerry-core/jcontext/jcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

/**
/*
* Memory context for JerryScript
*/
#ifndef JCONTEXT_H
Expand All @@ -33,11 +33,6 @@
* @{
*/

/**
* First member of the jerry context
*/
#define JERRY_CONTEXT_FIRST_MEMBER ecma_builtin_objects

/**
* User context item
*/
Expand All @@ -50,6 +45,11 @@ typedef struct jerry_context_data_header
#define JERRY_CONTEXT_DATA_HEADER_USER_DATA(item_p) \
((uint8_t *) (item_p + 1))

/**
* First member of the jerry context
*/
#define JERRY_CONTEXT_FIRST_MEMBER ecma_builtin_objects

/**
* JerryScript context
*
Expand Down Expand Up @@ -141,24 +141,35 @@ typedef struct

#ifdef JERRY_ENABLE_EXTERNAL_CONTEXT

#ifndef JERRY_GET_CURRENT_INSTANCE
/*
* This part is for JerryScript which uses external context.
*/

#ifndef JERRY_GET_CURRENT_INSTANCE
/**
* Default function if JERRY_GET_CURRENT_INSTANCE is not defined.
*/
#define JERRY_GET_CURRENT_INSTANCE() (jerry_port_get_current_instance ())

#endif /* !JERRY_GET_CURRENT_INSTANCE */

/**
* This part is for Jerry which enable external context.
*/
#define JERRY_CONTEXT(field) (JERRY_GET_CURRENT_INSTANCE ()->context.field)

#ifndef JERRY_SYSTEM_ALLOCATOR

#define JMEM_HEAP_SIZE (JERRY_GET_CURRENT_INSTANCE ()->heap_size)

#define JMEM_HEAP_AREA_SIZE (JMEM_HEAP_SIZE - JMEM_ALIGNMENT)

typedef struct
{
jmem_heap_free_t first; /**< first node in free region list */
uint8_t area[]; /**< heap area */
} jmem_heap_t;

#define JERRY_HEAP_CONTEXT(field) (JERRY_GET_CURRENT_INSTANCE ()->heap_p->field)

#endif /* !JERRY_SYSTEM_ALLOCATOR */

/**
* Description of jerry instance which is the header of the context space.
*/
Expand All @@ -171,33 +182,28 @@ struct jerry_instance_t
#endif /* !JERRY_SYSTEM_ALLOCATOR */
};

#define JERRY_CONTEXT(field) (JERRY_GET_CURRENT_INSTANCE ()->context.field)

#ifndef JERRY_SYSTEM_ALLOCATOR

static inline jmem_heap_t * JERRY_ATTR_ALWAYS_INLINE
jerry_context_get_current_heap (void)
{
return JERRY_GET_CURRENT_INSTANCE ()->heap_p;
} /* jerry_context_get_current_heap */

#define JERRY_HEAP_CONTEXT(field) (jerry_context_get_current_heap ()->field)

#ifdef JMEM_HEAP_SIZE
#error "JMEM_HEAP_SIZE must not be defined if JERRY_ENABLE_EXTERNAL_CONTEXT is defined"
#endif /* JMEM_HEAP_SIZE */
#else /* !JERRY_ENABLE_EXTERNAL_CONTEXT */

#define JMEM_HEAP_SIZE (JERRY_GET_CURRENT_INSTANCE ()->heap_size)
/*
* This part is for JerryScript which uses default context.
*/

#define JMEM_HEAP_AREA_SIZE (JERRY_GET_CURRENT_INSTANCE ()->heap_size - JMEM_ALIGNMENT)
/**
* Global context.
*/
extern jerry_context_t jerry_global_context;

#endif /* !JERRY_SYSTEM_ALLOCATOR */
/**
* Provides a reference to a field in the current context.
*/
#define JERRY_CONTEXT(field) (jerry_global_context.field)

#else /* !JERRY_ENABLE_EXTERNAL_CONTEXT */
#ifndef JERRY_SYSTEM_ALLOCATOR

/**
* This part is for Jerry which use default context.
*/
* Size of heap
*/
#define JMEM_HEAP_SIZE ((size_t) (CONFIG_MEM_HEAP_AREA_SIZE))

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

/**
* Global context.
*/
extern jerry_context_t jerry_global_context;

#ifndef JERRY_SYSTEM_ALLOCATOR
/**
* Global heap.
*/
extern jmem_heap_t jerry_global_heap;
#endif /* !JERRY_SYSTEM_ALLOCATOR */

/**
* Provides a reference to a field in the current context.
*/
#define JERRY_CONTEXT(field) (jerry_global_context.field)

#ifndef JERRY_SYSTEM_ALLOCATOR
/**
* Provides a reference to the area field of the heap.
* Provides a reference to a field of the heap.
*/
#define JERRY_HEAP_CONTEXT(field) (jerry_global_heap.field)

Expand Down
25 changes: 10 additions & 15 deletions jerry-core/jmem/jmem-heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* @{
*/

#ifndef JERRY_SYSTEM_ALLOCATOR
/**
* End of list marker.
*/
Expand All @@ -52,7 +53,6 @@
* @}
*/

#ifndef JERRY_SYSTEM_ALLOCATOR
/**
* Get end of region
*
Expand All @@ -65,14 +65,6 @@ jmem_heap_get_region_end (jmem_heap_free_t *curr_p) /**< current region */
} /* jmem_heap_get_region_end */
#endif /* !JERRY_SYSTEM_ALLOCATOR */

#ifndef JERRY_ENABLE_EXTERNAL_CONTEXT
/**
* Check size of heap is corresponding to configuration
*/
JERRY_STATIC_ASSERT (sizeof (jmem_heap_t) <= JMEM_HEAP_SIZE,
size_of_mem_heap_must_be_less_than_or_equal_to_MEM_HEAP_SIZE);
#endif /* !JERRY_ENABLE_EXTERNAL_CONTEXT */

#ifdef JMEM_STATS
static void jmem_heap_stat_init (void);
static void jmem_heap_stat_alloc (size_t num);
Expand Down Expand Up @@ -117,12 +109,11 @@ static void jmem_heap_stat_free_iter (void);
void
jmem_heap_init (void)
{
#ifndef JERRY_SYSTEM_ALLOCATOR
#ifndef JERRY_CPOINTER_32_BIT
/* the maximum heap size for 16bit compressed pointers should be 512K */
JERRY_ASSERT (((UINT16_MAX + 1) << JMEM_ALIGNMENT_LOG) >= JMEM_HEAP_SIZE);
#endif /* !JERRY_CPOINTER_32_BIT */

#ifndef JERRY_SYSTEM_ALLOCATOR
JERRY_ASSERT ((uintptr_t) JERRY_HEAP_CONTEXT (area) % JMEM_ALIGNMENT == 0);

JERRY_CONTEXT (jmem_heap_limit) = CONFIG_MEM_HEAP_DESIRED_LIMIT;
Expand Down Expand Up @@ -547,9 +538,12 @@ jmem_heap_stats_print (void)
{
jmem_heap_stats_t *heap_stats = &JERRY_CONTEXT (jmem_heap_stats);

JERRY_DEBUG_MSG ("Heap stats:\n"
" Heap size = %zu bytes\n"
" Allocated = %zu bytes\n"
JERRY_DEBUG_MSG ("Heap stats:\n");
#ifndef JERRY_SYSTEM_ALLOCATOR
JERRY_DEBUG_MSG (" Heap size = %zu bytes\n",
heap_stats->size);
#endif /* !JERRY_SYSTEM_ALLOCATOR */
JERRY_DEBUG_MSG (" Allocated = %zu bytes\n"
" Peak allocated = %zu bytes\n"
" Waste = %zu bytes\n"
" Peak waste = %zu bytes\n"
Expand All @@ -561,7 +555,6 @@ jmem_heap_stats_print (void)
" Peak allocated object data = %zu bytes\n"
" Allocated property data = %zu bytes\n"
" Peak allocated property data = %zu bytes\n",
heap_stats->size,
heap_stats->allocated_bytes,
heap_stats->peak_allocated_bytes,
heap_stats->waste_bytes,
Expand Down Expand Up @@ -593,7 +586,9 @@ jmem_heap_stats_print (void)
static void
jmem_heap_stat_init (void)
{
#ifndef JERRY_SYSTEM_ALLOCATOR
JERRY_CONTEXT (jmem_heap_stats).size = JMEM_HEAP_AREA_SIZE;
#endif /* !JERRY_SYSTEM_ALLOCATOR */
} /* jmem_heap_stat_init */

/**
Expand Down
7 changes: 0 additions & 7 deletions jerry-core/jmem/jmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
* @{
*/

#ifndef JERRY_ENABLE_EXTERNAL_CONTEXT
/**
* Size of heap
*/
#define JMEM_HEAP_SIZE ((size_t) (CONFIG_MEM_HEAP_AREA_SIZE))
#endif /* !JERRY_ENABLE_EXTERNAL_CONTEXT */

/**
* Logarithm of required alignment for allocated units/blocks
*/
Expand Down