1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2016 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 #ifndef ZEND_STRING_H
22 #define ZEND_STRING_H
23
24 #include "zend.h"
25
26 BEGIN_EXTERN_C()
27 ZEND_API extern const char *(*zend_new_interned_string)(const char *str, int len, int free_src TSRMLS_DC);
28 ZEND_API extern void (*zend_interned_strings_snapshot)(TSRMLS_D);
29 ZEND_API extern void (*zend_interned_strings_restore)(TSRMLS_D);
30
31 void zend_interned_strings_init(TSRMLS_D);
32 void zend_interned_strings_dtor(TSRMLS_D);
END_EXTERN_C()33 END_EXTERN_C()
34
35 #ifndef ZTS
36
37 #define IS_INTERNED(s) \
38 (((s) >= CG(interned_strings_start)) && ((s) < CG(interned_strings_end)))
39
40 #else
41
42 #define IS_INTERNED(s) \
43 (0)
44
45 #endif
46
47 #define INTERNED_LEN(s) \
48 (((Bucket*)(((char*)(s))-sizeof(Bucket)))->nKeyLength)
49
50 #define INTERNED_HASH(s) \
51 (((Bucket*)(((char*)(s))-sizeof(Bucket)))->h)
52
53 #define str_efree(s) do { \
54 if (!IS_INTERNED(s)) { \
55 efree((char*)s); \
56 } \
57 } while (0)
58
59 #define str_efree_rel(s) do { \
60 if (!IS_INTERNED(s)) { \
61 efree_rel((char *)s); \
62 } \
63 } while (0)
64
65 #define str_free(s) do { \
66 if (!IS_INTERNED(s)) { \
67 free((char*)s); \
68 } \
69 } while (0)
70
71 #define str_erealloc(str, new_len) \
72 (IS_INTERNED(str) \
73 ? _str_erealloc(str, new_len, INTERNED_LEN(str)) \
74 : erealloc(str, new_len))
75
76 static inline char *_str_erealloc(char *str, size_t new_len, size_t old_len) {
77 char *buf = (char *) emalloc(new_len);
78 memcpy(buf, str, old_len);
79 return buf;
80 }
81
82 #define str_estrndup(str, len) \
83 (IS_INTERNED(str) ? (str) : estrndup((str), (len)))
84
85 #define str_strndup(str, len) \
86 (IS_INTERNED(str) ? (str) : zend_strndup((str), (len)));
87
88 #define str_hash(str, len) \
89 (IS_INTERNED(str) ? INTERNED_HASH(str) : zend_hash_func((str), (len)+1))
90
91
92 #endif /* ZEND_STRING_H */
93
94 /*
95 * Local variables:
96 * tab-width: 4
97 * c-basic-offset: 4
98 * indent-tabs-mode: t
99 * End:
100 */
101