aboutsummaryrefslogtreecommitdiff
path: root/jerry-core/ecma/base/ecma-alloc.c
blob: 05a505259ad04537e32efb888482acae1c89e344 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* Copyright JS Foundation and other contributors, http://js.foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ecma-alloc.h"

#include "ecma-gc.h"
#include "ecma-globals.h"

#include "jmem.h"
#include "jrt.h"

JERRY_STATIC_ASSERT (sizeof (ecma_property_value_t) == sizeof (ecma_value_t),
                     size_of_ecma_property_value_t_must_be_equal_to_size_of_ecma_value_t);
JERRY_STATIC_ASSERT (((sizeof (ecma_property_value_t) - 1) & sizeof (ecma_property_value_t)) == 0,
                     size_of_ecma_property_value_t_must_be_power_of_2);

JERRY_STATIC_ASSERT (sizeof (ecma_extended_object_t) - sizeof (ecma_object_t) <= sizeof (uint64_t),
                     size_of_ecma_extended_object_part_must_be_less_than_or_equal_to_8_bytes);

/** \addtogroup ecma ECMA
 * @{
 *
 * \addtogroup ecmaalloc Routines for allocation/freeing memory for ECMA data types
 * @{
 */

/**
 * Implementation of routines for allocation/freeing memory for ECMA data types.
 *
 * All allocation routines from this module have the same structure:
 *  1. Try to allocate memory.
 *  2. If allocation was successful, return pointer to the allocated block.
 *  3. Run garbage collection.
 *  4. Try to allocate memory.
 *  5. If allocation was successful, return pointer to the allocated block;
 *     else - shutdown engine.
 */

/**
 * Allocate memory for ecma-number
 *
 * @return pointer to allocated memory
 */
extern inline ecma_number_t *JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_number (void)
{
  return (ecma_number_t *) jmem_pools_alloc (sizeof (ecma_number_t));
} /* ecma_alloc_number */

/**
 * Dealloc memory from an ecma-number
 *
 * @return void
 */
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_number (ecma_number_t *number_p) /**< number to be freed */
{
  jmem_pools_free ((uint8_t *) number_p, sizeof (ecma_number_t));
} /* ecma_dealloc_number */

/**
 * Allocate memory for ecma-object
 *
 * @return pointer to allocated memory
 */
extern inline ecma_object_t *JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_object (void)
{
#if JERRY_MEM_STATS
  jmem_stats_allocate_object_bytes (sizeof (ecma_object_t));
#endif /* JERRY_MEM_STATS */

  return (ecma_object_t *) jmem_pools_alloc (sizeof (ecma_object_t));
} /* ecma_alloc_object */

/**
 * Dealloc memory from an ecma-object
 *
 * @return void
 */
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_object (ecma_object_t *object_p) /**< object to be freed */
{
#if JERRY_MEM_STATS
  jmem_stats_free_object_bytes (sizeof (ecma_object_t));
#endif /* JERRY_MEM_STATS */

  jmem_pools_free (object_p, sizeof (ecma_object_t));
} /* ecma_dealloc_object */

/**
 * Allocate memory for extended object
 *
 * @return pointer to allocated memory
 */
extern inline ecma_extended_object_t *JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_extended_object (size_t size) /**< size of object */
{
#if JERRY_MEM_STATS
  jmem_stats_allocate_object_bytes (size);
#endif /* JERRY_MEM_STATS */

  return jmem_heap_alloc_block (size);
} /* ecma_alloc_extended_object */

/**
 * Dealloc memory of an extended object
 *
 * @return void
 */
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_extended_object (ecma_object_t *object_p, /**< extended object */
                              size_t size) /**< size of object */
{
#if JERRY_MEM_STATS
  jmem_stats_free_object_bytes (size);
#endif /* JERRY_MEM_STATS */

  jmem_heap_free_block (object_p, size);
} /* ecma_dealloc_extended_object */

/**
 * Allocate memory for ecma-string descriptor
 *
 * @return pointer to allocated memory
 */
extern inline ecma_string_t *JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_string (void)
{
#if JERRY_MEM_STATS
  jmem_stats_allocate_string_bytes (sizeof (ecma_string_t));
#endif /* JERRY_MEM_STATS */

  return (ecma_string_t *) jmem_pools_alloc (sizeof (ecma_string_t));
} /* ecma_alloc_string */

/**
 * Dealloc memory from ecma-string descriptor
 *
 * @return void
 */
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_string (ecma_string_t *string_p) /**< string to be freed */
{
#if JERRY_MEM_STATS
  jmem_stats_free_string_bytes (sizeof (ecma_string_t));
#endif /* JERRY_MEM_STATS */

  jmem_pools_free (string_p, sizeof (ecma_string_t));
} /* ecma_dealloc_string */

/**
 * Allocate memory for extended ecma-string descriptor
 *
 * @return pointer to allocated memory
 */
extern inline ecma_extended_string_t *JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_extended_string (void)
{
#if JERRY_MEM_STATS
  jmem_stats_allocate_string_bytes (sizeof (ecma_extended_string_t));
#endif /* JERRY_MEM_STATS */

  return (ecma_extended_string_t *) jmem_heap_alloc_block (sizeof (ecma_extended_string_t));
} /* ecma_alloc_extended_string */

/**
 * Dealloc memory from extended ecma-string descriptor
 *
 * @return void
 */
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_extended_string (ecma_extended_string_t *ext_string_p) /**< extended string to be freed */
{
#if JERRY_MEM_STATS
  jmem_stats_free_string_bytes (sizeof (ecma_extended_string_t));
#endif /* JERRY_MEM_STATS */

  jmem_heap_free_block (ext_string_p, sizeof (ecma_extended_string_t));
} /* ecma_dealloc_extended_string */

/**
 * Allocate memory for external ecma-string descriptor
 *
 * @return pointer to allocated memory
 */
extern inline ecma_external_string_t *JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_external_string (void)
{
#if JERRY_MEM_STATS
  jmem_stats_allocate_string_bytes (sizeof (ecma_external_string_t));
#endif /* JERRY_MEM_STATS */

  return (ecma_external_string_t *) jmem_heap_alloc_block (sizeof (ecma_external_string_t));
} /* ecma_alloc_external_string */

/**
 * Dealloc memory from external ecma-string descriptor
 *
 * @return void
 */
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_external_string (ecma_external_string_t *ext_string_p) /**< external string to be freed */
{
#if JERRY_MEM_STATS
  jmem_stats_free_string_bytes (sizeof (ecma_external_string_t));
#endif /* JERRY_MEM_STATS */

  jmem_heap_free_block (ext_string_p, sizeof (ecma_external_string_t));
} /* ecma_dealloc_external_string */

/**
 * Allocate memory for an string with character data
 *
 * @return pointer to allocated memory
 */
extern inline ecma_string_t *JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_string_buffer (size_t size) /**< size of string */
{
#if JERRY_MEM_STATS
  jmem_stats_allocate_string_bytes (size);
#endif /* JERRY_MEM_STATS */

  return jmem_heap_alloc_block (size);
} /* ecma_alloc_string_buffer */

/**
 * Dealloc memory of a string with character data
 *
 * @return void
 */
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_string_buffer (ecma_string_t *string_p, /**< string with data */
                            size_t size) /**< size of string */
{
#if JERRY_MEM_STATS
  jmem_stats_free_string_bytes (size);
#endif /* JERRY_MEM_STATS */

  jmem_heap_free_block (string_p, size);
} /* ecma_dealloc_string_buffer */

/**
 * Allocate memory for ecma-property pair
 *
 * @return pointer to allocated memory
 */
extern inline ecma_property_pair_t *JERRY_ATTR_ALWAYS_INLINE
ecma_alloc_property_pair (void)
{
#if JERRY_MEM_STATS
  jmem_stats_allocate_property_bytes (sizeof (ecma_property_pair_t));
#endif /* JERRY_MEM_STATS */

  return jmem_heap_alloc_block (sizeof (ecma_property_pair_t));
} /* ecma_alloc_property_pair */

/**
 * Dealloc memory of an ecma-property
 *
 * @return void
 */
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_dealloc_property_pair (ecma_property_pair_t *property_pair_p) /**< property pair to be freed */
{
#if JERRY_MEM_STATS
  jmem_stats_free_property_bytes (sizeof (ecma_property_pair_t));
#endif /* JERRY_MEM_STATS */

  jmem_heap_free_block (property_pair_p, sizeof (ecma_property_pair_t));
} /* ecma_dealloc_property_pair */

/**
 * @}
 * @}
 */