1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 2.00 of the Zend license,     |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.zend.com/license/2_00.txt.                                |
11   | If you did not receive a copy of the Zend license and are unable to  |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@zend.com so we can mail you a copy immediately.              |
14   +----------------------------------------------------------------------+
15   | Authors: Dmitry Stogov <dmitry@zend.com>                             |
16   +----------------------------------------------------------------------+
17*/
18
19/* $Id: $ */
20
21#include "zend.h"
22#include "zend_globals.h"
23
24ZEND_API zend_string *(*zend_new_interned_string)(zend_string *str);
25ZEND_API void (*zend_interned_strings_snapshot)(void);
26ZEND_API void (*zend_interned_strings_restore)(void);
27
28static zend_string *zend_new_interned_string_int(zend_string *str);
29static void zend_interned_strings_snapshot_int(void);
30static void zend_interned_strings_restore_int(void);
31
32ZEND_API zend_ulong zend_hash_func(const char *str, size_t len)
33{
34    return zend_inline_hash_func(str, len);
35}
36
37#ifndef ZTS
38static void _str_dtor(zval *zv)
39{
40    zend_string *str = Z_STR_P(zv);
41    GC_FLAGS(str) &= ~IS_STR_INTERNED;
42    GC_REFCOUNT(str) = 1;
43}
44#endif
45
46void zend_interned_strings_init(void)
47{
48#ifndef ZTS
49    zend_string *str;
50
51    zend_hash_init(&CG(interned_strings), 1024, NULL, _str_dtor, 1);
52
53    CG(interned_strings).nTableMask = CG(interned_strings).nTableSize - 1;
54    CG(interned_strings).arData = (Bucket*) pecalloc(CG(interned_strings).nTableSize, sizeof(Bucket), 1);
55    CG(interned_strings).arHash = (uint32_t*) pecalloc(CG(interned_strings).nTableSize, sizeof(uint32_t), 1);
56    memset(CG(interned_strings).arHash, INVALID_IDX, CG(interned_strings).nTableSize * sizeof(uint32_t));
57
58    /* interned empty string */
59    str = zend_string_alloc(sizeof("")-1, 1);
60    str->val[0] = '\000';
61    CG(empty_string) = zend_new_interned_string_int(str);
62#endif
63
64    /* one char strings (the actual interned strings are going to be created by ext/opcache) */
65    memset(CG(one_char_string), 0, sizeof(CG(one_char_string)));
66
67    zend_new_interned_string = zend_new_interned_string_int;
68    zend_interned_strings_snapshot = zend_interned_strings_snapshot_int;
69    zend_interned_strings_restore = zend_interned_strings_restore_int;
70}
71
72void zend_interned_strings_dtor(void)
73{
74#ifndef ZTS
75    zend_hash_destroy(&CG(interned_strings));
76#endif
77}
78
79static zend_string *zend_new_interned_string_int(zend_string *str)
80{
81#ifndef ZTS
82    zend_ulong h;
83    uint nIndex;
84    uint idx;
85    Bucket *p;
86
87    if (IS_INTERNED(str)) {
88        return str;
89    }
90
91    h = zend_string_hash_val(str);
92    nIndex = h & CG(interned_strings).nTableMask;
93    idx = CG(interned_strings).arHash[nIndex];
94    while (idx != INVALID_IDX) {
95        p = CG(interned_strings).arData + idx;
96        if ((p->h == h) && (p->key->len == str->len)) {
97            if (!memcmp(p->key->val, str->val, str->len)) {
98                zend_string_release(str);
99                return p->key;
100            }
101        }
102        idx = Z_NEXT(p->val);
103    }
104
105    GC_REFCOUNT(str) = 1;
106    GC_FLAGS(str) |= IS_STR_INTERNED;
107
108    if (CG(interned_strings).nNumUsed >= CG(interned_strings).nTableSize) {
109        if ((CG(interned_strings).nTableSize << 1) > 0) {   /* Let's double the table size */
110            Bucket *d = (Bucket *) perealloc_recoverable(CG(interned_strings).arData, (CG(interned_strings).nTableSize << 1) * sizeof(Bucket), 1);
111            uint32_t *h = (uint32_t *) perealloc_recoverable(CG(interned_strings).arHash, (CG(interned_strings).nTableSize << 1) * sizeof(uint32_t), 1);
112
113            if (d && h) {
114                HANDLE_BLOCK_INTERRUPTIONS();
115                CG(interned_strings).arData = d;
116                CG(interned_strings).arHash = h;
117                CG(interned_strings).nTableSize = (CG(interned_strings).nTableSize << 1);
118                CG(interned_strings).nTableMask = CG(interned_strings).nTableSize - 1;
119                zend_hash_rehash(&CG(interned_strings));
120                HANDLE_UNBLOCK_INTERRUPTIONS();
121            }
122        }
123    }
124
125    HANDLE_BLOCK_INTERRUPTIONS();
126
127    idx = CG(interned_strings).nNumUsed++;
128    CG(interned_strings).nNumOfElements++;
129    p = CG(interned_strings).arData + idx;
130    p->h = h;
131    p->key = str;
132    Z_STR(p->val) = str;
133    Z_TYPE_INFO(p->val) = IS_INTERNED_STRING_EX;
134    nIndex = h & CG(interned_strings).nTableMask;
135    Z_NEXT(p->val) = CG(interned_strings).arHash[nIndex];
136    CG(interned_strings).arHash[nIndex] = idx;
137
138    HANDLE_UNBLOCK_INTERRUPTIONS();
139
140    return str;
141#else
142    return str;
143#endif
144}
145
146static void zend_interned_strings_snapshot_int(void)
147{
148#ifndef ZTS
149    uint idx;
150    Bucket *p;
151
152    idx = CG(interned_strings).nNumUsed;
153    while (idx > 0) {
154        idx--;
155        p = CG(interned_strings).arData + idx;
156        ZEND_ASSERT(GC_FLAGS(p->key) & IS_STR_PERSISTENT);
157        GC_FLAGS(p->key) |= IS_STR_PERMANENT;
158    }
159#endif
160}
161
162static void zend_interned_strings_restore_int(void)
163{
164#ifndef ZTS
165    uint nIndex;
166    uint idx;
167    Bucket *p;
168
169    idx = CG(interned_strings).nNumUsed;
170    while (idx > 0) {
171        idx--;
172        p = CG(interned_strings).arData + idx;
173        if (GC_FLAGS(p->key) & IS_STR_PERMANENT) break;
174        CG(interned_strings).nNumUsed--;
175        CG(interned_strings).nNumOfElements--;
176
177        GC_FLAGS(p->key) &= ~IS_STR_INTERNED;
178        GC_REFCOUNT(p->key) = 1;
179        zend_string_free(p->key);
180
181        nIndex = p->h & CG(interned_strings).nTableMask;
182        if (CG(interned_strings).arHash[nIndex] == idx) {
183            CG(interned_strings).arHash[nIndex] = Z_NEXT(p->val);
184        } else {
185            uint prev = CG(interned_strings).arHash[nIndex];
186            while (Z_NEXT(CG(interned_strings).arData[prev].val) != idx) {
187                prev = Z_NEXT(CG(interned_strings).arData[prev].val);
188            }
189            Z_NEXT(CG(interned_strings).arData[prev].val) = Z_NEXT(p->val);
190        }
191    }
192#endif
193}
194
195/*
196 * Local variables:
197 * tab-width: 4
198 * c-basic-offset: 4
199 * indent-tabs-mode: t
200 * End:
201 */
202