Skip to content

Commit 10a1bb5

Browse files
author
François Baldassari
committed
Allow the port to allocate/deallocate the heap
This is a first step towards pushing the Jerryscript state into objects the port can allocate and clear as opposed to static memory. JerryScript-DCO-1.0-Signed-off-by: François Baldassari [email protected]
1 parent 5d4c09e commit 10a1bb5

File tree

7 files changed

+218
-134
lines changed

7 files changed

+218
-134
lines changed

jerry-core/jerry-port.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ extern "C"
2424
{
2525
#endif /* !__cplusplus */
2626

27+
/**
28+
* Forward definition of heap data structure
29+
*/
30+
struct mem_heap;
31+
typedef struct mem_heap mem_heap_t;
32+
2733
/** \addtogroup jerry_port Jerry engine port
2834
* @{
2935
*/
@@ -37,6 +43,16 @@ int jerry_port_putchar (int c);
3743

3844
void jerry_port_abort (void);
3945

46+
47+
/**
48+
* Target port functions for memory management
49+
*/
50+
mem_heap_t *jerry_port_init_heap (void);
51+
52+
void jerry_port_finalize_heap (mem_heap_t *mem_heap);
53+
54+
mem_heap_t *jerry_port_get_heap (void);
55+
4056
/**
4157
* @}
4258
*/

jerry-core/jerry.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,15 +2444,3 @@ jerry_api_get_string_size (const jerry_api_string_t *str_p) /**< input string */
24442444
return ecma_string_get_size ((ecma_string_t *) str_p);
24452445
} /* jerry_api_get_string_size */
24462446

2447-
/**
2448-
* Get length of Jerry string
2449-
*
2450-
* @return number of characters in the string
2451-
*/
2452-
jerry_api_length_t
2453-
jerry_api_get_string_length (const jerry_api_string_t *str_p) /**< input string */
2454-
{
2455-
jerry_assert_api_available ();
2456-
2457-
return ecma_string_get_length ((ecma_string_t *) str_p);
2458-
} /* jerry_api_get_string_length */

jerry-core/mem/mem-heap-internal.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* Copyright 2014 Samsung Electronics Co., Ltd.
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+
#ifndef MEM_HEAP_INTERNAL_H
17+
#define MEM_HEAP_INTERNAL_H
18+
19+
#ifndef MEM_HEAP_INTERNAL
20+
# error "The header is for internal routines of memory heap component. Please, don't use the routines directly."
21+
#endif /* !MEM_HEAP_INTERNAL */
22+
23+
#include "mem-allocator.h"
24+
#include "mem-config.h"
25+
26+
/** \addtogroup mem Memory allocation
27+
* @{
28+
*/
29+
30+
/* Calculate heap area size, leaving space for a pointer to the free list */
31+
#define MEM_HEAP_AREA_SIZE (MEM_HEAP_SIZE - MEM_ALIGNMENT)
32+
#define MEM_HEAP_END_OF_LIST ((mem_heap_free_t *const) ~((uint32_t) 0x0))
33+
34+
/**
35+
* Free region node
36+
*/
37+
typedef struct
38+
{
39+
uint32_t next_offset; /* Offset of next region in list */
40+
uint32_t size; /* Size of region */
41+
} mem_heap_free_t;
42+
43+
#ifdef MEM_HEAP_PTR_64
44+
#define MEM_HEAP_GET_OFFSET_FROM_ADDR(h, p) ((uint32_t) ((uint8_t *) (p) - (uint8_t *) (h)->area))
45+
#define MEM_HEAP_GET_ADDR_FROM_OFFSET(h, u) ((mem_heap_free_t *) &(h)->area[u])
46+
#else
47+
/* In this case we simply store the pointer, since it fits anyway. */
48+
#define MEM_HEAP_GET_OFFSET_FROM_ADDR(h, p) ((uint32_t) (p))
49+
#define MEM_HEAP_GET_ADDR_FROM_OFFSET(h, u) ((mem_heap_free_t *)(u))
50+
#endif
51+
52+
/**
53+
* Heap structure
54+
*/
55+
struct mem_heap
56+
{
57+
mem_heap_free_t first;
58+
/* This is used to speed up deallocation. */
59+
mem_heap_free_t *list_skip_p;
60+
/**
61+
* Size of allocated regions
62+
*/
63+
size_t allocated_size;
64+
65+
/**
66+
* Current limit of heap usage, that is upon being reached, causes call of "try give memory back" callbacks
67+
*/
68+
size_t limit;
69+
70+
/**
71+
* Heap area
72+
*/
73+
uint8_t area[MEM_HEAP_AREA_SIZE] __attribute__ ((aligned (MEM_ALIGNMENT)));
74+
};
75+
76+
77+
/**
78+
* @}
79+
*/
80+
81+
#endif /* MEM_HEAP_INTERNAL_H */

0 commit comments

Comments
 (0)