• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright JS Foundation and other contributors, http://js.foundation
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 "ecma-alloc.h"
17 #include "ecma-globals.h"
18 #include "ecma-gc.h"
19 #include "jrt.h"
20 #include "jmem.h"
21 
22 JERRY_STATIC_ASSERT (sizeof (ecma_property_value_t) == sizeof (ecma_value_t),
23                      size_of_ecma_property_value_t_must_be_equal_to_size_of_ecma_value_t);
24 JERRY_STATIC_ASSERT (((sizeof (ecma_property_value_t) - 1) & sizeof (ecma_property_value_t)) == 0,
25                      size_of_ecma_property_value_t_must_be_power_of_2);
26 
27 JERRY_STATIC_ASSERT (sizeof (ecma_extended_object_t) - sizeof (ecma_object_t) <= sizeof (uint64_t),
28                      size_of_ecma_extended_object_part_must_be_less_than_or_equal_to_8_bytes);
29 
30 /** \addtogroup ecma ECMA
31  * @{
32  *
33  * \addtogroup ecmaalloc Routines for allocation/freeing memory for ECMA data types
34  * @{
35  */
36 
37 /**
38  * Implementation of routines for allocation/freeing memory for ECMA data types.
39  *
40  * All allocation routines from this module have the same structure:
41  *  1. Try to allocate memory.
42  *  2. If allocation was successful, return pointer to the allocated block.
43  *  3. Run garbage collection.
44  *  4. Try to allocate memory.
45  *  5. If allocation was successful, return pointer to the allocated block;
46  *     else - shutdown engine.
47  */
48 
49 /**
50  * Allocate memory for ecma-number
51  *
52  * @return pointer to allocated memory
53  */
54 ecma_number_t *
ecma_alloc_number(void)55 ecma_alloc_number (void)
56 {
57   return (ecma_number_t *) jmem_pools_alloc (sizeof (ecma_number_t));
58 } /* ecma_alloc_number */
59 
60 /**
61  * Dealloc memory from an ecma-number
62  */
63 void
ecma_dealloc_number(ecma_number_t * number_p)64 ecma_dealloc_number (ecma_number_t *number_p) /**< number to be freed */
65 {
66   jmem_pools_free ((uint8_t *) number_p, sizeof (ecma_number_t));
67 } /* ecma_dealloc_number */
68 
69 /**
70  * Allocate memory for ecma-object
71  *
72  * @return pointer to allocated memory
73  */
74 inline ecma_object_t * JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_object(void)75 ecma_alloc_object (void)
76 {
77 #if ENABLED (JERRY_MEM_STATS)
78   jmem_stats_allocate_object_bytes (sizeof (ecma_object_t));
79 #endif /* ENABLED (JERRY_MEM_STATS) */
80 
81   return (ecma_object_t *) jmem_pools_alloc (sizeof (ecma_object_t));
82 } /* ecma_alloc_object */
83 
84 /**
85  * Dealloc memory from an ecma-object
86  */
87 inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_object(ecma_object_t * object_p)88 ecma_dealloc_object (ecma_object_t *object_p) /**< object to be freed */
89 {
90 #if ENABLED (JERRY_MEM_STATS)
91   jmem_stats_free_object_bytes (sizeof (ecma_object_t));
92 #endif /* ENABLED (JERRY_MEM_STATS) */
93 
94   jmem_pools_free (object_p, sizeof (ecma_object_t));
95 } /* ecma_dealloc_object */
96 
97 /**
98  * Allocate memory for extended object
99  *
100  * @return pointer to allocated memory
101  */
102 inline ecma_extended_object_t * JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_extended_object(size_t size)103 ecma_alloc_extended_object (size_t size) /**< size of object */
104 {
105 #if ENABLED (JERRY_MEM_STATS)
106   jmem_stats_allocate_object_bytes (size);
107 #endif /* ENABLED (JERRY_MEM_STATS) */
108 
109   return jmem_heap_alloc_block (size);
110 } /* ecma_alloc_extended_object */
111 
112 /**
113  * Dealloc memory of an extended object
114  */
115 inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_extended_object(ecma_object_t * object_p,size_t size)116 ecma_dealloc_extended_object (ecma_object_t *object_p, /**< extended object */
117                               size_t size) /**< size of object */
118 {
119 #if ENABLED (JERRY_MEM_STATS)
120   jmem_stats_free_object_bytes (size);
121 #endif /* ENABLED (JERRY_MEM_STATS) */
122 
123   jmem_heap_free_block (object_p, size);
124 } /* ecma_dealloc_extended_object */
125 
126 /**
127  * Allocate memory for ecma-string descriptor
128  *
129  * @return pointer to allocated memory
130  */
131 inline ecma_string_t * JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_string(void)132 ecma_alloc_string (void)
133 {
134 #if ENABLED (JERRY_MEM_STATS)
135   jmem_stats_allocate_string_bytes (sizeof (ecma_string_t));
136 #endif /* ENABLED (JERRY_MEM_STATS) */
137 
138   return (ecma_string_t *) jmem_pools_alloc (sizeof (ecma_string_t));
139 } /* ecma_alloc_string */
140 
141 /**
142  * Dealloc memory from ecma-string descriptor
143  */
144 inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_string(ecma_string_t * string_p)145 ecma_dealloc_string (ecma_string_t *string_p) /**< string to be freed */
146 {
147 #if ENABLED (JERRY_MEM_STATS)
148   jmem_stats_free_string_bytes (sizeof (ecma_string_t));
149 #endif /* ENABLED (JERRY_MEM_STATS) */
150 
151   jmem_pools_free (string_p, sizeof (ecma_string_t));
152 } /* ecma_dealloc_string */
153 
154 /**
155  * Allocate memory for extended ecma-string descriptor
156  *
157  * @return pointer to allocated memory
158  */
159 inline ecma_extended_string_t * JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_extended_string(void)160 ecma_alloc_extended_string (void)
161 {
162 #if ENABLED (JERRY_MEM_STATS)
163   jmem_stats_allocate_string_bytes (sizeof (ecma_extended_string_t));
164 #endif /* ENABLED (JERRY_MEM_STATS) */
165 
166   return (ecma_extended_string_t *) jmem_heap_alloc_block (sizeof (ecma_extended_string_t));
167 } /* ecma_alloc_extended_string */
168 
169 /**
170  * Dealloc memory from extended ecma-string descriptor
171  */
172 inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_extended_string(ecma_extended_string_t * ext_string_p)173 ecma_dealloc_extended_string (ecma_extended_string_t *ext_string_p) /**< extended string to be freed */
174 {
175 #if ENABLED (JERRY_MEM_STATS)
176   jmem_stats_free_string_bytes (sizeof (ecma_extended_string_t));
177 #endif /* ENABLED (JERRY_MEM_STATS) */
178 
179   jmem_heap_free_block (ext_string_p, sizeof (ecma_extended_string_t));
180 } /* ecma_dealloc_extended_string */
181 
182 /**
183  * Allocate memory for an string with character data
184  *
185  * @return pointer to allocated memory
186  */
187 inline ecma_string_t * JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_string_buffer(size_t size)188 ecma_alloc_string_buffer (size_t size) /**< size of string */
189 {
190 #if ENABLED (JERRY_MEM_STATS)
191   jmem_stats_allocate_string_bytes (size);
192 #endif /* ENABLED (JERRY_MEM_STATS) */
193 
194   return jmem_heap_alloc_block (size);
195 } /* ecma_alloc_string_buffer */
196 
197 /**
198  * Dealloc memory of a string with character data
199  */
200 inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_string_buffer(ecma_string_t * string_p,size_t size)201 ecma_dealloc_string_buffer (ecma_string_t *string_p, /**< string with data */
202                             size_t size) /**< size of string */
203 {
204 #if ENABLED (JERRY_MEM_STATS)
205   jmem_stats_free_string_bytes (size);
206 #endif /* ENABLED (JERRY_MEM_STATS) */
207 
208   jmem_heap_free_block (string_p, size);
209 } /* ecma_dealloc_string_buffer */
210 
211 /**
212  * Allocate memory for ecma-property pair
213  *
214  * @return pointer to allocated memory
215  */
216 inline ecma_property_pair_t * JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_property_pair(void)217 ecma_alloc_property_pair (void)
218 {
219 #if ENABLED (JERRY_MEM_STATS)
220   jmem_stats_allocate_property_bytes (sizeof (ecma_property_pair_t));
221 #endif /* ENABLED (JERRY_MEM_STATS) */
222 
223   return jmem_heap_alloc_block (sizeof (ecma_property_pair_t));
224 } /* ecma_alloc_property_pair */
225 
226 /**
227  * Dealloc memory of an ecma-property
228  */
229 inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_property_pair(ecma_property_pair_t * property_pair_p)230 ecma_dealloc_property_pair (ecma_property_pair_t *property_pair_p) /**< property pair to be freed */
231 {
232 #if ENABLED (JERRY_MEM_STATS)
233   jmem_stats_free_property_bytes (sizeof (ecma_property_pair_t));
234 #endif /* ENABLED (JERRY_MEM_STATS) */
235 
236   jmem_heap_free_block (property_pair_p, sizeof (ecma_property_pair_t));
237 } /* ecma_dealloc_property_pair */
238 
239 /**
240  * @}
241  * @}
242  */
243