XCache is a fast, stable PHP opcode cacher that has been proven and is now running on production servers under high load. https://xcache.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.0 KiB

  1. #ifndef XC_VECTOR_H_0957AC4E1A44E838C7B8DBECFF9C4B3B
  2. #define XC_VECTOR_H_0957AC4E1A44E838C7B8DBECFF9C4B3B
  3. #if _MSC_VER > 1000
  4. #pragma once
  5. #endif /* _MSC_VER > 1000 */
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <assert.h>
  9. typedef struct {
  10. size_t size;
  11. void *data;
  12. size_t capacity;
  13. size_t data_size;
  14. int persistent;
  15. } xc_vector_t;
  16. #define xc_vector_initializer(type, persistent_) { \
  17. 0, \
  18. NULL, \
  19. \
  20. 0, \
  21. sizeof(type), \
  22. persistent_, \
  23. }
  24. #define xc_vector_init(type, vector, persistent_) do { \
  25. (vector)->size = 0; \
  26. (vector)->data = NULL; \
  27. \
  28. (vector)->capacity = 0; \
  29. (vector)->data_size = sizeof(type); \
  30. (vector)->persistent = persistent_; \
  31. } while (0)
  32. static inline void xc_vector_destroy_impl(xc_vector_t *vector TSRMLS_DC)
  33. {
  34. if (vector->data) {
  35. pefree(vector->data, vector->persistent);
  36. }
  37. vector->capacity = 0;
  38. vector->size = 0;
  39. }
  40. #define xc_vector_destroy(vector) xc_vector_destroy_impl(vector TSRMLS_CC)
  41. #define xc_vector_size(vector) ((vector)->size)
  42. #define xc_vector_element_ptr_(vector, index) ( \
  43. (void *) ( \
  44. ((char *) (vector)->data) + (index) * (vector)->data_size \
  45. ) \
  46. )
  47. static inline xc_vector_t *xc_vector_check_type_(xc_vector_t *vector, size_t data_size)
  48. {
  49. assert(vector->data_size = data_size);
  50. return vector;
  51. }
  52. #define xc_vector_data(type, vector) ((type *) xc_vector_check_type_(vector, sizeof(type))->data)
  53. static inline void xc_vector_check_reserve_(xc_vector_t *vector TSRMLS_DC)
  54. {
  55. if (vector->size == vector->capacity) {
  56. if (vector->capacity) {
  57. vector->capacity <<= 1;
  58. vector->data = perealloc(vector->data, vector->data_size * vector->capacity, vector->persistent);
  59. }
  60. else {
  61. vector->capacity = 8;
  62. vector->data = pemalloc(vector->data_size * vector->capacity, vector->persistent);
  63. }
  64. }
  65. }
  66. #define xc_vector_push_back(vector, value_ptr) do { \
  67. xc_vector_check_reserve_(vector TSRMLS_CC); \
  68. memcpy(xc_vector_element_ptr_(vector, (vector)->size++), value_ptr, (vector)->data_size); \
  69. } while (0)
  70. static inline void *xc_vector_detach_impl(xc_vector_t *vector)
  71. {
  72. void *data = vector->data;
  73. vector->data = NULL;
  74. vector->capacity = 0;
  75. vector->size = 0;
  76. return data;
  77. }
  78. #define xc_vector_detach(type, vector) ((type *) xc_vector_detach_impl(xc_vector_check_type_(vector, sizeof(type))))
  79. static inline xc_vector_t *xc_vector_pop_back_check_(xc_vector_t *vector, size_t data_size)
  80. {
  81. assert(vector);
  82. assert(vector->data_size == data_size);
  83. assert(vector->capacity > 0);
  84. return vector;
  85. }
  86. #define xc_vector_pop_back(type, vector) xc_vector_data(type, \
  87. xc_vector_pop_back_check_(vector, sizeof(type)) \
  88. )[--(vector)->size]
  89. static inline void xc_vector_reverse(xc_vector_t *vector)
  90. {
  91. char *left, *right;
  92. void *tmp;
  93. assert(vector);
  94. tmp = alloca(vector->data_size);
  95. for (left = vector->data, right = xc_vector_element_ptr_(vector, vector->size - 1); left < right; left += vector->data_size, right -= vector->data_size) {
  96. memcpy(tmp, left, vector->data_size);
  97. memcpy(left, right, vector->data_size);
  98. memcpy(right, tmp, vector->data_size);
  99. }
  100. }
  101. #endif /* XC_VECTOR_H_0957AC4E1A44E838C7B8DBECFF9C4B3B */