1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#include "ecma-alloc.h"
17425bb815Sopenharmony_ci#include "ecma-globals.h"
18425bb815Sopenharmony_ci#include "ecma-gc.h"
19425bb815Sopenharmony_ci#include "jrt.h"
20425bb815Sopenharmony_ci#include "jmem.h"
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_ciJERRY_STATIC_ASSERT (sizeof (ecma_property_value_t) == sizeof (ecma_value_t),
23425bb815Sopenharmony_ci                     size_of_ecma_property_value_t_must_be_equal_to_size_of_ecma_value_t);
24425bb815Sopenharmony_ciJERRY_STATIC_ASSERT (((sizeof (ecma_property_value_t) - 1) & sizeof (ecma_property_value_t)) == 0,
25425bb815Sopenharmony_ci                     size_of_ecma_property_value_t_must_be_power_of_2);
26425bb815Sopenharmony_ci
27425bb815Sopenharmony_ciJERRY_STATIC_ASSERT (sizeof (ecma_extended_object_t) - sizeof (ecma_object_t) <= sizeof (uint64_t),
28425bb815Sopenharmony_ci                     size_of_ecma_extended_object_part_must_be_less_than_or_equal_to_8_bytes);
29425bb815Sopenharmony_ci
30425bb815Sopenharmony_ci/** \addtogroup ecma ECMA
31425bb815Sopenharmony_ci * @{
32425bb815Sopenharmony_ci *
33425bb815Sopenharmony_ci * \addtogroup ecmaalloc Routines for allocation/freeing memory for ECMA data types
34425bb815Sopenharmony_ci * @{
35425bb815Sopenharmony_ci */
36425bb815Sopenharmony_ci
37425bb815Sopenharmony_ci/**
38425bb815Sopenharmony_ci * Implementation of routines for allocation/freeing memory for ECMA data types.
39425bb815Sopenharmony_ci *
40425bb815Sopenharmony_ci * All allocation routines from this module have the same structure:
41425bb815Sopenharmony_ci *  1. Try to allocate memory.
42425bb815Sopenharmony_ci *  2. If allocation was successful, return pointer to the allocated block.
43425bb815Sopenharmony_ci *  3. Run garbage collection.
44425bb815Sopenharmony_ci *  4. Try to allocate memory.
45425bb815Sopenharmony_ci *  5. If allocation was successful, return pointer to the allocated block;
46425bb815Sopenharmony_ci *     else - shutdown engine.
47425bb815Sopenharmony_ci */
48425bb815Sopenharmony_ci
49425bb815Sopenharmony_ci/**
50425bb815Sopenharmony_ci * Allocate memory for ecma-number
51425bb815Sopenharmony_ci *
52425bb815Sopenharmony_ci * @return pointer to allocated memory
53425bb815Sopenharmony_ci */
54425bb815Sopenharmony_ciecma_number_t *
55425bb815Sopenharmony_ciecma_alloc_number (void)
56425bb815Sopenharmony_ci{
57425bb815Sopenharmony_ci  return (ecma_number_t *) jmem_pools_alloc (sizeof (ecma_number_t));
58425bb815Sopenharmony_ci} /* ecma_alloc_number */
59425bb815Sopenharmony_ci
60425bb815Sopenharmony_ci/**
61425bb815Sopenharmony_ci * Dealloc memory from an ecma-number
62425bb815Sopenharmony_ci */
63425bb815Sopenharmony_civoid
64425bb815Sopenharmony_ciecma_dealloc_number (ecma_number_t *number_p) /**< number to be freed */
65425bb815Sopenharmony_ci{
66425bb815Sopenharmony_ci  jmem_pools_free ((uint8_t *) number_p, sizeof (ecma_number_t));
67425bb815Sopenharmony_ci} /* ecma_dealloc_number */
68425bb815Sopenharmony_ci
69425bb815Sopenharmony_ci/**
70425bb815Sopenharmony_ci * Allocate memory for ecma-object
71425bb815Sopenharmony_ci *
72425bb815Sopenharmony_ci * @return pointer to allocated memory
73425bb815Sopenharmony_ci */
74425bb815Sopenharmony_ciinline ecma_object_t * JERRY_ATTR_ALWAYS_INLINE
75425bb815Sopenharmony_ciecma_alloc_object (void)
76425bb815Sopenharmony_ci{
77425bb815Sopenharmony_ci#if ENABLED (JERRY_MEM_STATS)
78425bb815Sopenharmony_ci  jmem_stats_allocate_object_bytes (sizeof (ecma_object_t));
79425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_MEM_STATS) */
80425bb815Sopenharmony_ci
81425bb815Sopenharmony_ci  return (ecma_object_t *) jmem_pools_alloc (sizeof (ecma_object_t));
82425bb815Sopenharmony_ci} /* ecma_alloc_object */
83425bb815Sopenharmony_ci
84425bb815Sopenharmony_ci/**
85425bb815Sopenharmony_ci * Dealloc memory from an ecma-object
86425bb815Sopenharmony_ci */
87425bb815Sopenharmony_ciinline void JERRY_ATTR_ALWAYS_INLINE
88425bb815Sopenharmony_ciecma_dealloc_object (ecma_object_t *object_p) /**< object to be freed */
89425bb815Sopenharmony_ci{
90425bb815Sopenharmony_ci#if ENABLED (JERRY_MEM_STATS)
91425bb815Sopenharmony_ci  jmem_stats_free_object_bytes (sizeof (ecma_object_t));
92425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_MEM_STATS) */
93425bb815Sopenharmony_ci
94425bb815Sopenharmony_ci  jmem_pools_free (object_p, sizeof (ecma_object_t));
95425bb815Sopenharmony_ci} /* ecma_dealloc_object */
96425bb815Sopenharmony_ci
97425bb815Sopenharmony_ci/**
98425bb815Sopenharmony_ci * Allocate memory for extended object
99425bb815Sopenharmony_ci *
100425bb815Sopenharmony_ci * @return pointer to allocated memory
101425bb815Sopenharmony_ci */
102425bb815Sopenharmony_ciinline ecma_extended_object_t * JERRY_ATTR_ALWAYS_INLINE
103425bb815Sopenharmony_ciecma_alloc_extended_object (size_t size) /**< size of object */
104425bb815Sopenharmony_ci{
105425bb815Sopenharmony_ci#if ENABLED (JERRY_MEM_STATS)
106425bb815Sopenharmony_ci  jmem_stats_allocate_object_bytes (size);
107425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_MEM_STATS) */
108425bb815Sopenharmony_ci
109425bb815Sopenharmony_ci  return jmem_heap_alloc_block (size);
110425bb815Sopenharmony_ci} /* ecma_alloc_extended_object */
111425bb815Sopenharmony_ci
112425bb815Sopenharmony_ci/**
113425bb815Sopenharmony_ci * Dealloc memory of an extended object
114425bb815Sopenharmony_ci */
115425bb815Sopenharmony_ciinline void JERRY_ATTR_ALWAYS_INLINE
116425bb815Sopenharmony_ciecma_dealloc_extended_object (ecma_object_t *object_p, /**< extended object */
117425bb815Sopenharmony_ci                              size_t size) /**< size of object */
118425bb815Sopenharmony_ci{
119425bb815Sopenharmony_ci#if ENABLED (JERRY_MEM_STATS)
120425bb815Sopenharmony_ci  jmem_stats_free_object_bytes (size);
121425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_MEM_STATS) */
122425bb815Sopenharmony_ci
123425bb815Sopenharmony_ci  jmem_heap_free_block (object_p, size);
124425bb815Sopenharmony_ci} /* ecma_dealloc_extended_object */
125425bb815Sopenharmony_ci
126425bb815Sopenharmony_ci/**
127425bb815Sopenharmony_ci * Allocate memory for ecma-string descriptor
128425bb815Sopenharmony_ci *
129425bb815Sopenharmony_ci * @return pointer to allocated memory
130425bb815Sopenharmony_ci */
131425bb815Sopenharmony_ciinline ecma_string_t * JERRY_ATTR_ALWAYS_INLINE
132425bb815Sopenharmony_ciecma_alloc_string (void)
133425bb815Sopenharmony_ci{
134425bb815Sopenharmony_ci#if ENABLED (JERRY_MEM_STATS)
135425bb815Sopenharmony_ci  jmem_stats_allocate_string_bytes (sizeof (ecma_string_t));
136425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_MEM_STATS) */
137425bb815Sopenharmony_ci
138425bb815Sopenharmony_ci  return (ecma_string_t *) jmem_pools_alloc (sizeof (ecma_string_t));
139425bb815Sopenharmony_ci} /* ecma_alloc_string */
140425bb815Sopenharmony_ci
141425bb815Sopenharmony_ci/**
142425bb815Sopenharmony_ci * Dealloc memory from ecma-string descriptor
143425bb815Sopenharmony_ci */
144425bb815Sopenharmony_ciinline void JERRY_ATTR_ALWAYS_INLINE
145425bb815Sopenharmony_ciecma_dealloc_string (ecma_string_t *string_p) /**< string to be freed */
146425bb815Sopenharmony_ci{
147425bb815Sopenharmony_ci#if ENABLED (JERRY_MEM_STATS)
148425bb815Sopenharmony_ci  jmem_stats_free_string_bytes (sizeof (ecma_string_t));
149425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_MEM_STATS) */
150425bb815Sopenharmony_ci
151425bb815Sopenharmony_ci  jmem_pools_free (string_p, sizeof (ecma_string_t));
152425bb815Sopenharmony_ci} /* ecma_dealloc_string */
153425bb815Sopenharmony_ci
154425bb815Sopenharmony_ci/**
155425bb815Sopenharmony_ci * Allocate memory for extended ecma-string descriptor
156425bb815Sopenharmony_ci *
157425bb815Sopenharmony_ci * @return pointer to allocated memory
158425bb815Sopenharmony_ci */
159425bb815Sopenharmony_ciinline ecma_extended_string_t * JERRY_ATTR_ALWAYS_INLINE
160425bb815Sopenharmony_ciecma_alloc_extended_string (void)
161425bb815Sopenharmony_ci{
162425bb815Sopenharmony_ci#if ENABLED (JERRY_MEM_STATS)
163425bb815Sopenharmony_ci  jmem_stats_allocate_string_bytes (sizeof (ecma_extended_string_t));
164425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_MEM_STATS) */
165425bb815Sopenharmony_ci
166425bb815Sopenharmony_ci  return (ecma_extended_string_t *) jmem_heap_alloc_block (sizeof (ecma_extended_string_t));
167425bb815Sopenharmony_ci} /* ecma_alloc_extended_string */
168425bb815Sopenharmony_ci
169425bb815Sopenharmony_ci/**
170425bb815Sopenharmony_ci * Dealloc memory from extended ecma-string descriptor
171425bb815Sopenharmony_ci */
172425bb815Sopenharmony_ciinline void JERRY_ATTR_ALWAYS_INLINE
173425bb815Sopenharmony_ciecma_dealloc_extended_string (ecma_extended_string_t *ext_string_p) /**< extended string to be freed */
174425bb815Sopenharmony_ci{
175425bb815Sopenharmony_ci#if ENABLED (JERRY_MEM_STATS)
176425bb815Sopenharmony_ci  jmem_stats_free_string_bytes (sizeof (ecma_extended_string_t));
177425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_MEM_STATS) */
178425bb815Sopenharmony_ci
179425bb815Sopenharmony_ci  jmem_heap_free_block (ext_string_p, sizeof (ecma_extended_string_t));
180425bb815Sopenharmony_ci} /* ecma_dealloc_extended_string */
181425bb815Sopenharmony_ci
182425bb815Sopenharmony_ci/**
183425bb815Sopenharmony_ci * Allocate memory for an string with character data
184425bb815Sopenharmony_ci *
185425bb815Sopenharmony_ci * @return pointer to allocated memory
186425bb815Sopenharmony_ci */
187425bb815Sopenharmony_ciinline ecma_string_t * JERRY_ATTR_ALWAYS_INLINE
188425bb815Sopenharmony_ciecma_alloc_string_buffer (size_t size) /**< size of string */
189425bb815Sopenharmony_ci{
190425bb815Sopenharmony_ci#if ENABLED (JERRY_MEM_STATS)
191425bb815Sopenharmony_ci  jmem_stats_allocate_string_bytes (size);
192425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_MEM_STATS) */
193425bb815Sopenharmony_ci
194425bb815Sopenharmony_ci  return jmem_heap_alloc_block (size);
195425bb815Sopenharmony_ci} /* ecma_alloc_string_buffer */
196425bb815Sopenharmony_ci
197425bb815Sopenharmony_ci/**
198425bb815Sopenharmony_ci * Dealloc memory of a string with character data
199425bb815Sopenharmony_ci */
200425bb815Sopenharmony_ciinline void JERRY_ATTR_ALWAYS_INLINE
201425bb815Sopenharmony_ciecma_dealloc_string_buffer (ecma_string_t *string_p, /**< string with data */
202425bb815Sopenharmony_ci                            size_t size) /**< size of string */
203425bb815Sopenharmony_ci{
204425bb815Sopenharmony_ci#if ENABLED (JERRY_MEM_STATS)
205425bb815Sopenharmony_ci  jmem_stats_free_string_bytes (size);
206425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_MEM_STATS) */
207425bb815Sopenharmony_ci
208425bb815Sopenharmony_ci  jmem_heap_free_block (string_p, size);
209425bb815Sopenharmony_ci} /* ecma_dealloc_string_buffer */
210425bb815Sopenharmony_ci
211425bb815Sopenharmony_ci/**
212425bb815Sopenharmony_ci * Allocate memory for ecma-property pair
213425bb815Sopenharmony_ci *
214425bb815Sopenharmony_ci * @return pointer to allocated memory
215425bb815Sopenharmony_ci */
216425bb815Sopenharmony_ciinline ecma_property_pair_t * JERRY_ATTR_ALWAYS_INLINE
217425bb815Sopenharmony_ciecma_alloc_property_pair (void)
218425bb815Sopenharmony_ci{
219425bb815Sopenharmony_ci#if ENABLED (JERRY_MEM_STATS)
220425bb815Sopenharmony_ci  jmem_stats_allocate_property_bytes (sizeof (ecma_property_pair_t));
221425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_MEM_STATS) */
222425bb815Sopenharmony_ci
223425bb815Sopenharmony_ci  return jmem_heap_alloc_block (sizeof (ecma_property_pair_t));
224425bb815Sopenharmony_ci} /* ecma_alloc_property_pair */
225425bb815Sopenharmony_ci
226425bb815Sopenharmony_ci/**
227425bb815Sopenharmony_ci * Dealloc memory of an ecma-property
228425bb815Sopenharmony_ci */
229425bb815Sopenharmony_ciinline void JERRY_ATTR_ALWAYS_INLINE
230425bb815Sopenharmony_ciecma_dealloc_property_pair (ecma_property_pair_t *property_pair_p) /**< property pair to be freed */
231425bb815Sopenharmony_ci{
232425bb815Sopenharmony_ci#if ENABLED (JERRY_MEM_STATS)
233425bb815Sopenharmony_ci  jmem_stats_free_property_bytes (sizeof (ecma_property_pair_t));
234425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_MEM_STATS) */
235425bb815Sopenharmony_ci
236425bb815Sopenharmony_ci  jmem_heap_free_block (property_pair_p, sizeof (ecma_property_pair_t));
237425bb815Sopenharmony_ci} /* ecma_dealloc_property_pair */
238425bb815Sopenharmony_ci
239425bb815Sopenharmony_ci/**
240425bb815Sopenharmony_ci * @}
241425bb815Sopenharmony_ci * @}
242425bb815Sopenharmony_ci */
243