fs-fd-hash-inl.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #ifndef UV_WIN_FS_FD_HASH_INL_H_
  2. #define UV_WIN_FS_FD_HASH_INL_H_
  3. #include "uv.h"
  4. #include "internal.h"
  5. /* Files are only inserted in uv__fd_hash when the UV_FS_O_FILEMAP flag is
  6. * specified. Thus, when uv__fd_hash_get returns true, the file mapping in the
  7. * info structure should be used for read/write operations.
  8. *
  9. * If the file is empty, the mapping field will be set to
  10. * INVALID_HANDLE_VALUE. This is not an issue since the file mapping needs to
  11. * be created anyway when the file size changes.
  12. *
  13. * Since file descriptors are sequential integers, the modulo operator is used
  14. * as hashing function. For each bucket, a single linked list of arrays is
  15. * kept to minimize allocations. A statically allocated memory buffer is kept
  16. * for the first array in each bucket. */
  17. #define UV__FD_HASH_SIZE 256
  18. #define UV__FD_HASH_GROUP_SIZE 16
  19. struct uv__fd_info_s {
  20. int flags;
  21. BOOLEAN is_directory;
  22. HANDLE mapping;
  23. LARGE_INTEGER size;
  24. LARGE_INTEGER current_pos;
  25. };
  26. struct uv__fd_hash_entry_s {
  27. uv_file fd;
  28. struct uv__fd_info_s info;
  29. };
  30. struct uv__fd_hash_entry_group_s {
  31. struct uv__fd_hash_entry_s entries[UV__FD_HASH_GROUP_SIZE];
  32. struct uv__fd_hash_entry_group_s* next;
  33. };
  34. struct uv__fd_hash_bucket_s {
  35. size_t size;
  36. struct uv__fd_hash_entry_group_s* data;
  37. };
  38. static uv_mutex_t uv__fd_hash_mutex;
  39. static struct uv__fd_hash_entry_group_s
  40. uv__fd_hash_entry_initial[UV__FD_HASH_SIZE * UV__FD_HASH_GROUP_SIZE];
  41. static struct uv__fd_hash_bucket_s uv__fd_hash[UV__FD_HASH_SIZE];
  42. INLINE static void uv__fd_hash_init(void) {
  43. int i, err;
  44. err = uv_mutex_init(&uv__fd_hash_mutex);
  45. if (err) {
  46. uv_fatal_error(err, "uv_mutex_init");
  47. }
  48. for (i = 0; i < ARRAY_SIZE(uv__fd_hash); ++i) {
  49. uv__fd_hash[i].size = 0;
  50. uv__fd_hash[i].data =
  51. uv__fd_hash_entry_initial + i * UV__FD_HASH_GROUP_SIZE;
  52. }
  53. }
  54. #define FIND_COMMON_VARIABLES \
  55. unsigned i; \
  56. unsigned bucket = fd % ARRAY_SIZE(uv__fd_hash); \
  57. struct uv__fd_hash_entry_s* entry_ptr = NULL; \
  58. struct uv__fd_hash_entry_group_s* group_ptr; \
  59. struct uv__fd_hash_bucket_s* bucket_ptr = &uv__fd_hash[bucket];
  60. #define FIND_IN_GROUP_PTR(group_size) \
  61. do { \
  62. for (i = 0; i < group_size; ++i) { \
  63. if (group_ptr->entries[i].fd == fd) { \
  64. entry_ptr = &group_ptr->entries[i]; \
  65. break; \
  66. } \
  67. } \
  68. } while (0)
  69. #define FIND_IN_BUCKET_PTR() \
  70. do { \
  71. size_t first_group_size = bucket_ptr->size % UV__FD_HASH_GROUP_SIZE; \
  72. if (bucket_ptr->size != 0 && first_group_size == 0) \
  73. first_group_size = UV__FD_HASH_GROUP_SIZE; \
  74. group_ptr = bucket_ptr->data; \
  75. FIND_IN_GROUP_PTR(first_group_size); \
  76. for (group_ptr = group_ptr->next; \
  77. group_ptr != NULL && entry_ptr == NULL; \
  78. group_ptr = group_ptr->next) \
  79. FIND_IN_GROUP_PTR(UV__FD_HASH_GROUP_SIZE); \
  80. } while (0)
  81. INLINE static int uv__fd_hash_get(int fd, struct uv__fd_info_s* info) {
  82. FIND_COMMON_VARIABLES
  83. uv_mutex_lock(&uv__fd_hash_mutex);
  84. FIND_IN_BUCKET_PTR();
  85. if (entry_ptr != NULL) {
  86. *info = entry_ptr->info;
  87. }
  88. uv_mutex_unlock(&uv__fd_hash_mutex);
  89. return entry_ptr != NULL;
  90. }
  91. INLINE static void uv__fd_hash_add(int fd, struct uv__fd_info_s* info) {
  92. FIND_COMMON_VARIABLES
  93. uv_mutex_lock(&uv__fd_hash_mutex);
  94. FIND_IN_BUCKET_PTR();
  95. if (entry_ptr == NULL) {
  96. i = bucket_ptr->size % UV__FD_HASH_GROUP_SIZE;
  97. if (bucket_ptr->size != 0 && i == 0) {
  98. struct uv__fd_hash_entry_group_s* new_group_ptr =
  99. uv__malloc(sizeof(*new_group_ptr));
  100. if (new_group_ptr == NULL) {
  101. uv_fatal_error(ERROR_OUTOFMEMORY, "uv__malloc");
  102. }
  103. new_group_ptr->next = bucket_ptr->data;
  104. bucket_ptr->data = new_group_ptr;
  105. }
  106. bucket_ptr->size += 1;
  107. entry_ptr = &bucket_ptr->data->entries[i];
  108. entry_ptr->fd = fd;
  109. }
  110. entry_ptr->info = *info;
  111. uv_mutex_unlock(&uv__fd_hash_mutex);
  112. }
  113. INLINE static int uv__fd_hash_remove(int fd, struct uv__fd_info_s* info) {
  114. FIND_COMMON_VARIABLES
  115. uv_mutex_lock(&uv__fd_hash_mutex);
  116. FIND_IN_BUCKET_PTR();
  117. if (entry_ptr != NULL) {
  118. *info = entry_ptr->info;
  119. bucket_ptr->size -= 1;
  120. i = bucket_ptr->size % UV__FD_HASH_GROUP_SIZE;
  121. if (entry_ptr != &bucket_ptr->data->entries[i]) {
  122. *entry_ptr = bucket_ptr->data->entries[i];
  123. }
  124. if (bucket_ptr->size != 0 &&
  125. bucket_ptr->size % UV__FD_HASH_GROUP_SIZE == 0) {
  126. struct uv__fd_hash_entry_group_s* old_group_ptr = bucket_ptr->data;
  127. bucket_ptr->data = old_group_ptr->next;
  128. uv__free(old_group_ptr);
  129. }
  130. }
  131. uv_mutex_unlock(&uv__fd_hash_mutex);
  132. return entry_ptr != NULL;
  133. }
  134. #undef FIND_COMMON_VARIABLES
  135. #undef FIND_IN_GROUP_PTR
  136. #undef FIND_IN_BUCKET_PTR
  137. #endif /* UV_WIN_FS_FD_HASH_INL_H_ */