zend_accelerator_hash.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend OPcache |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Stanislav Malyshev <stas@zend.com> |
  18. | Dmitry Stogov <dmitry@php.net> |
  19. +----------------------------------------------------------------------+
  20. */
  21. #ifndef ZEND_ACCELERATOR_HASH_H
  22. #define ZEND_ACCELERATOR_HASH_H
  23. #include "zend.h"
  24. /*
  25. zend_accel_hash - is a hash table allocated in shared memory and
  26. distributed across simultaneously running processes. The hash tables have
  27. fixed sizen selected during construction by zend_accel_hash_init(). All the
  28. hash entries are preallocated in the 'hash_entries' array. 'num_entries' is
  29. initialized by zero and grows when new data is added.
  30. zend_accel_hash_update() just takes the next entry from 'hash_entries'
  31. array and puts it into appropriate place of 'hash_table'.
  32. Hash collisions are resolved by separate chaining with linked lists,
  33. however, entries are still taken from the same 'hash_entries' array.
  34. 'key' and 'data' passed to zend_accel_hash_update() must be already
  35. allocated in shared memory. Few keys may be resolved to the same data.
  36. using 'indirect' entries, that point to other entries ('data' is actually
  37. a pointer to another zend_accel_hash_entry).
  38. zend_accel_hash_update() requires exclusive lock, however,
  39. zend_accel_hash_find() does not.
  40. */
  41. typedef struct _zend_accel_hash_entry zend_accel_hash_entry;
  42. struct _zend_accel_hash_entry {
  43. zend_ulong hash_value;
  44. const char *key;
  45. zend_accel_hash_entry *next;
  46. void *data;
  47. uint32_t key_length;
  48. zend_bool indirect;
  49. };
  50. typedef struct _zend_accel_hash {
  51. zend_accel_hash_entry **hash_table;
  52. zend_accel_hash_entry *hash_entries;
  53. uint32_t num_entries;
  54. uint32_t max_num_entries;
  55. uint32_t num_direct_entries;
  56. } zend_accel_hash;
  57. void zend_accel_hash_init(zend_accel_hash *accel_hash, uint32_t hash_size);
  58. void zend_accel_hash_clean(zend_accel_hash *accel_hash);
  59. zend_accel_hash_entry* zend_accel_hash_update(
  60. zend_accel_hash *accel_hash,
  61. const char *key,
  62. uint32_t key_length,
  63. zend_bool indirect,
  64. void *data);
  65. void* zend_accel_hash_find(
  66. zend_accel_hash *accel_hash,
  67. zend_string *key);
  68. zend_accel_hash_entry* zend_accel_hash_find_entry(
  69. zend_accel_hash *accel_hash,
  70. zend_string *key);
  71. void* zend_accel_hash_str_find(
  72. zend_accel_hash *accel_hash,
  73. const char *key,
  74. uint32_t key_length);
  75. zend_accel_hash_entry* zend_accel_hash_str_find_entry(
  76. zend_accel_hash *accel_hash,
  77. const char *key,
  78. uint32_t key_length);
  79. int zend_accel_hash_unlink(
  80. zend_accel_hash *accel_hash,
  81. const char *key,
  82. uint32_t key_length);
  83. static inline zend_bool zend_accel_hash_is_full(zend_accel_hash *accel_hash)
  84. {
  85. if (accel_hash->num_entries == accel_hash->max_num_entries) {
  86. return 1;
  87. } else {
  88. return 0;
  89. }
  90. }
  91. #endif /* ZEND_ACCELERATOR_HASH_H */