|
16 | 16 | #include "lit-id-hash-table.h"
|
17 | 17 | #include "bytecode-data.h"
|
18 | 18 |
|
| 19 | +/** \addtogroup jsparser ECMAScript parser |
| 20 | + * @{ |
| 21 | + * |
| 22 | + * \addtogroup collections Collections |
| 23 | + * @{ |
| 24 | + * |
| 25 | + * \addtogroup lit_id_hash_table Literal identifiers hash table |
| 26 | + * The hash table connects pairs (opcode block, idx_t value) with literal identifiers. |
| 27 | + * @{ |
| 28 | + */ |
| 29 | + |
| 30 | +/** |
| 31 | + * Initialize literal identifiers hash table |
| 32 | + * |
| 33 | + * @return pointer to header of the table |
| 34 | + */ |
19 | 35 | lit_id_hash_table *
|
20 |
| -lit_id_hash_table_init (size_t buckets_count, size_t blocks_count) |
| 36 | +lit_id_hash_table_init (uint8_t *table_buffer_p, /**< buffer to initialize hash table in */ |
| 37 | + size_t buffer_size, /**< size of the buffer */ |
| 38 | + size_t buckets_count, /**< number of pairs */ |
| 39 | + size_t blocks_count) /**< number of opcode blocks */ |
| 40 | +{ |
| 41 | + const size_t header_size = JERRY_ALIGNUP (sizeof (lit_id_hash_table), MEM_ALIGNMENT); |
| 42 | + const size_t raw_buckets_size = JERRY_ALIGNUP (sizeof (lit_cpointer_t) * buckets_count, MEM_ALIGNMENT); |
| 43 | + const size_t buckets_size = JERRY_ALIGNUP (sizeof (lit_cpointer_t*) * blocks_count, MEM_ALIGNMENT); |
| 44 | + |
| 45 | + JERRY_ASSERT (header_size + raw_buckets_size + buckets_size <= buffer_size); |
| 46 | + |
| 47 | + lit_id_hash_table *table_p = (lit_id_hash_table *) table_buffer_p; |
| 48 | + |
| 49 | + table_p->current_bucket_pos = 0; |
| 50 | + table_p->raw_buckets = (lit_cpointer_t*) (table_buffer_p + header_size); |
| 51 | + table_p->buckets = (lit_cpointer_t **) (table_buffer_p + header_size + raw_buckets_size); |
| 52 | + |
| 53 | + memset (table_p->buckets, 0, buckets_size); |
| 54 | + |
| 55 | + return table_p; |
| 56 | +} /* lit_id_hash_table_init */ |
| 57 | + |
| 58 | +/** |
| 59 | + * Get size of buffer, necessary to hold hash table with specified parameters |
| 60 | + * |
| 61 | + * @return size of buffer |
| 62 | + */ |
| 63 | +size_t |
| 64 | +lit_id_hash_table_get_size_for_table (size_t buckets_count, /**< number of pairs */ |
| 65 | + size_t blocks_count) /**< number of opcode blocks */ |
21 | 66 | {
|
22 |
| - size_t size = mem_heap_recommend_allocation_size (sizeof (lit_id_hash_table)); |
23 |
| - lit_id_hash_table *table = (lit_id_hash_table *) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_LONG_TERM); |
24 |
| - memset (table, 0, size); |
25 |
| - size = mem_heap_recommend_allocation_size (sizeof (lit_cpointer_t) * buckets_count); |
26 |
| - table->raw_buckets = (lit_cpointer_t *) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_LONG_TERM); |
27 |
| - memset (table->raw_buckets, 0, size); |
28 |
| - size = mem_heap_recommend_allocation_size (sizeof (lit_cpointer_t *) * blocks_count); |
29 |
| - table->buckets = (lit_cpointer_t **) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_LONG_TERM); |
30 |
| - memset (table->buckets, 0, size); |
31 |
| - table->current_bucket_pos = 0; |
32 |
| - return table; |
33 |
| -} |
| 67 | + const size_t header_size = JERRY_ALIGNUP (sizeof (lit_id_hash_table), MEM_ALIGNMENT); |
| 68 | + const size_t raw_buckets_size = JERRY_ALIGNUP (sizeof (lit_cpointer_t) * buckets_count, MEM_ALIGNMENT); |
| 69 | + const size_t buckets_size = JERRY_ALIGNUP (sizeof (lit_cpointer_t*) * blocks_count, MEM_ALIGNMENT); |
| 70 | + |
| 71 | + return header_size + raw_buckets_size + buckets_size; |
| 72 | +} /* lit_id_hash_table_get_size_for_table */ |
34 | 73 |
|
| 74 | +/** |
| 75 | + * Free literal identifiers hash table |
| 76 | + */ |
35 | 77 | void
|
36 |
| -lit_id_hash_table_free (lit_id_hash_table *table) |
| 78 | +lit_id_hash_table_free (lit_id_hash_table *table_p) /**< table's header */ |
37 | 79 | {
|
38 |
| - JERRY_ASSERT (table); |
39 |
| - mem_heap_free_block ((uint8_t *) table->raw_buckets); |
40 |
| - mem_heap_free_block ((uint8_t *) table->buckets); |
41 |
| - mem_heap_free_block ((uint8_t *) table); |
42 |
| -} |
| 80 | + JERRY_ASSERT (table_p != NULL); |
| 81 | + |
| 82 | + mem_heap_free_block ((uint8_t *) table_p); |
| 83 | +} /* lit_id_hash_table_free */ |
43 | 84 |
|
| 85 | +/** |
| 86 | + * Register pair in the hash table |
| 87 | + */ |
44 | 88 | void
|
45 |
| -lit_id_hash_table_insert (lit_id_hash_table *table, idx_t uid, opcode_counter_t oc, lit_cpointer_t lit_cp) |
| 89 | +lit_id_hash_table_insert (lit_id_hash_table *table_p, /**< table's header */ |
| 90 | + idx_t uid, /**< value of byte-code instruction's argument */ |
| 91 | + opcode_counter_t oc, /**< opcode counter of the instruction */ |
| 92 | + lit_cpointer_t lit_cp) /**< literal identifier */ |
46 | 93 | {
|
47 |
| - JERRY_ASSERT (table); |
| 94 | + JERRY_ASSERT (table_p != NULL); |
| 95 | + |
48 | 96 | size_t block_id = oc / BLOCK_SIZE;
|
49 |
| - if (table->buckets[block_id] == NULL) |
| 97 | + |
| 98 | + if (table_p->buckets[block_id] == NULL) |
50 | 99 | {
|
51 |
| - table->buckets[block_id] = table->raw_buckets + table->current_bucket_pos; |
| 100 | + table_p->buckets[block_id] = table_p->raw_buckets + table_p->current_bucket_pos; |
52 | 101 | }
|
53 |
| - table->buckets[block_id][uid] = lit_cp; |
54 |
| - table->current_bucket_pos++; |
55 |
| -} |
56 | 102 |
|
| 103 | + table_p->buckets[block_id][uid] = lit_cp; |
| 104 | + table_p->current_bucket_pos++; |
| 105 | +} /* lit_id_hash_table_insert */ |
| 106 | + |
| 107 | +/** |
| 108 | + * Lookup literal identifier by pair |
| 109 | + * |
| 110 | + * @return literal identifier |
| 111 | + */ |
57 | 112 | lit_cpointer_t
|
58 |
| -lit_id_hash_table_lookup (lit_id_hash_table *table, idx_t uid, opcode_counter_t oc) |
| 113 | +lit_id_hash_table_lookup (lit_id_hash_table *table_p, /**< table's header */ |
| 114 | + idx_t uid, /**< value of byte-code instruction's argument */ |
| 115 | + opcode_counter_t oc) /**< opcode counter of the instruction */ |
59 | 116 | {
|
60 |
| - JERRY_ASSERT (table); |
| 117 | + JERRY_ASSERT (table_p != NULL); |
| 118 | + |
61 | 119 | size_t block_id = oc / BLOCK_SIZE;
|
62 |
| - JERRY_ASSERT (table->buckets[block_id]); |
63 |
| - return table->buckets[block_id][uid]; |
64 |
| -} |
| 120 | + JERRY_ASSERT (table_p->buckets[block_id] != NULL); |
| 121 | + |
| 122 | + return table_p->buckets[block_id][uid]; |
| 123 | +} /* lit_id_hash_table_lookup */ |
| 124 | + |
| 125 | +/** |
| 126 | + * @} |
| 127 | + * @} |
| 128 | + * @} |
| 129 | + */ |
0 commit comments