zend_string.h 2.8 KB

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