Skip to content

Commit 85f12de

Browse files
Introduce parser-specific managed memory allocator with interface jsp_mm_free_all that, upon call, frees all memory allocated with the allocator.
JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan [email protected]
1 parent 9b0f5d4 commit 85f12de

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed

jerry-core/parser/js/jsp-mm.cpp

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/* Copyright 2015 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+
#include "jrt.h"
17+
#include "jsp-mm.h"
18+
#include "mem-allocator.h"
19+
#include "mem-heap.h"
20+
21+
/** \addtogroup jsparser ECMAScript parser
22+
* @{
23+
*
24+
* \addtogroup managedmem Managed memory allocation
25+
* @{
26+
*/
27+
28+
/**
29+
* Header of a managed block, allocated by parser
30+
*/
31+
typedef struct __attribute__ ((alignment (MEM_ALIGNMENT)))
32+
{
33+
mem_cpointer_t prev_block_cp; /**< previous managed block */
34+
mem_cpointer_t next_block_cp; /**< next managed block */
35+
} jsp_mm_header_t;
36+
37+
/**
38+
* List used for tracking memory blocks
39+
*/
40+
jsp_mm_header_t *jsp_mm_blocks_p = NULL;
41+
42+
/**
43+
* Initialize managed memory allocator
44+
*/
45+
void
46+
jsp_mm_init (void)
47+
{
48+
JERRY_ASSERT (jsp_mm_blocks_p == NULL);
49+
} /* jsp_mm_init */
50+
51+
/**
52+
* Finalize managed memory allocator
53+
*/
54+
void
55+
jsp_mm_finalize (void)
56+
{
57+
JERRY_ASSERT (jsp_mm_blocks_p == NULL);
58+
} /* jsp_mm_finalize */
59+
60+
/**
61+
* Recommend allocation size
62+
*
63+
* Note:
64+
* The interface is used by collection allocators
65+
* for storage of data that takes the specified
66+
* amount of bytes upon allocation, but probably
67+
* would require more.
68+
*
69+
* To reduce probability of reallocation in future,
70+
* the allocators can request more space in first
71+
* allocation request.
72+
*
73+
* The interface helps to choose appropriate space
74+
* to allocate, considering amount of heap space,
75+
* that would be waste if allocation size
76+
* would not be increased.
77+
*
78+
* @return recommended allocation size
79+
*/
80+
size_t
81+
jsp_mm_recommend_size (size_t minimum_size) /**< minimum required size */
82+
{
83+
size_t block_and_header_size = mem_heap_recommend_allocation_size (minimum_size + sizeof (jsp_mm_header_t));
84+
return block_and_header_size - sizeof (jsp_mm_header_t);
85+
} /* jsp_mm_recommend_size */
86+
87+
/**
88+
* Allocate a managed memory block of specified size
89+
*
90+
* @return pointer to data space of allocated block
91+
*/
92+
void*
93+
jsp_mm_alloc (size_t size) /**< size of block to allocate */
94+
{
95+
void *ptr_p = mem_heap_alloc_block (size, MEM_HEAP_ALLOC_SHORT_TERM);
96+
97+
jsp_mm_header_t *tmem_header_p = (jsp_mm_header_t*) ptr_p;
98+
99+
tmem_header_p->prev_block_cp = MEM_CP_NULL;
100+
MEM_CP_SET_POINTER (tmem_header_p->next_block_cp, jsp_mm_blocks_p);
101+
102+
if (jsp_mm_blocks_p != NULL)
103+
{
104+
MEM_CP_SET_POINTER (jsp_mm_blocks_p->prev_block_cp, tmem_header_p);
105+
}
106+
107+
jsp_mm_blocks_p = tmem_header_p;
108+
109+
return (void *) (tmem_header_p + 1);
110+
} /* jsp_mm_alloc */
111+
112+
/**
113+
* Free a managed memory block
114+
*/
115+
void
116+
jsp_mm_free (void *ptr) /**< pointer to data space of allocated block */
117+
{
118+
jsp_mm_header_t *tmem_header_p = ((jsp_mm_header_t *) ptr) - 1;
119+
120+
jsp_mm_header_t *prev_block_p = MEM_CP_GET_POINTER (jsp_mm_header_t,
121+
tmem_header_p->prev_block_cp);
122+
jsp_mm_header_t *next_block_p = MEM_CP_GET_POINTER (jsp_mm_header_t,
123+
tmem_header_p->next_block_cp);
124+
125+
if (prev_block_p != NULL)
126+
{
127+
prev_block_p->next_block_cp = tmem_header_p->next_block_cp;
128+
}
129+
else
130+
{
131+
JERRY_ASSERT (jsp_mm_blocks_p == tmem_header_p);
132+
jsp_mm_blocks_p = next_block_p;
133+
}
134+
135+
if (next_block_p != NULL)
136+
{
137+
next_block_p->prev_block_cp = tmem_header_p->prev_block_cp;
138+
}
139+
140+
mem_heap_free_block (tmem_header_p);
141+
} /* jsp_mm_free */
142+
143+
/**
144+
* Free all currently allocated managed memory blocks
145+
*/
146+
void
147+
jsp_mm_free_all (void)
148+
{
149+
while (jsp_mm_blocks_p != NULL)
150+
{
151+
jsp_mm_header_t *next_block_p = MEM_CP_GET_POINTER (jsp_mm_header_t,
152+
jsp_mm_blocks_p->next_block_cp);
153+
154+
mem_heap_free_block (jsp_mm_blocks_p);
155+
156+
jsp_mm_blocks_p = next_block_p;
157+
}
158+
} /* jsp_mm_free_all */
159+
160+
/**
161+
* @}
162+
* @}
163+
*/

jerry-core/parser/js/jsp-mm.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Copyright 2015 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 JSP_MM_H
17+
#define JSP_MM_H
18+
19+
#include "jrt-libc-includes.h"
20+
21+
/** \addtogroup jsparser ECMAScript parser
22+
* @{
23+
*
24+
* \addtogroup managedmem Managed memory allocation
25+
* @{
26+
*/
27+
28+
extern void jsp_mm_init (void);
29+
extern void jsp_mm_finalize (void);
30+
extern size_t jsp_mm_recommend_size (size_t);
31+
extern void * jsp_mm_alloc (size_t);
32+
extern void jsp_mm_free (void *);
33+
extern void jsp_mm_free_all (void);
34+
35+
/**
36+
* @}
37+
* @}
38+
*/
39+
40+
#endif /* !JSP_MM_H */

0 commit comments

Comments
 (0)