|
Packit Service |
e08953 |
#ifndef UV_WIN_FS_FD_HASH_INL_H_
|
|
Packit Service |
e08953 |
#define UV_WIN_FS_FD_HASH_INL_H_
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
#include "uv.h"
|
|
Packit Service |
e08953 |
#include "internal.h"
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
/* Files are only inserted in uv__fd_hash when the UV_FS_O_FILEMAP flag is
|
|
Packit Service |
e08953 |
* specified. Thus, when uv__fd_hash_get returns true, the file mapping in the
|
|
Packit Service |
e08953 |
* info structure should be used for read/write operations.
|
|
Packit Service |
e08953 |
*
|
|
Packit Service |
e08953 |
* If the file is empty, the mapping field will be set to
|
|
Packit Service |
e08953 |
* INVALID_HANDLE_VALUE. This is not an issue since the file mapping needs to
|
|
Packit Service |
e08953 |
* be created anyway when the file size changes.
|
|
Packit Service |
e08953 |
*
|
|
Packit Service |
e08953 |
* Since file descriptors are sequential integers, the modulo operator is used
|
|
Packit Service |
e08953 |
* as hashing function. For each bucket, a single linked list of arrays is
|
|
Packit Service |
e08953 |
* kept to minimize allocations. A statically allocated memory buffer is kept
|
|
Packit Service |
e08953 |
* for the first array in each bucket. */
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
#define UV__FD_HASH_SIZE 256
|
|
Packit Service |
e08953 |
#define UV__FD_HASH_GROUP_SIZE 16
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
struct uv__fd_info_s {
|
|
Packit Service |
e08953 |
int flags;
|
|
Packit Service |
e08953 |
BOOLEAN is_directory;
|
|
Packit Service |
e08953 |
HANDLE mapping;
|
|
Packit Service |
e08953 |
LARGE_INTEGER size;
|
|
Packit Service |
e08953 |
LARGE_INTEGER current_pos;
|
|
Packit Service |
e08953 |
};
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
struct uv__fd_hash_entry_s {
|
|
Packit Service |
e08953 |
uv_file fd;
|
|
Packit Service |
e08953 |
struct uv__fd_info_s info;
|
|
Packit Service |
e08953 |
};
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
struct uv__fd_hash_entry_group_s {
|
|
Packit Service |
e08953 |
struct uv__fd_hash_entry_s entries[UV__FD_HASH_GROUP_SIZE];
|
|
Packit Service |
e08953 |
struct uv__fd_hash_entry_group_s* next;
|
|
Packit Service |
e08953 |
};
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
struct uv__fd_hash_bucket_s {
|
|
Packit Service |
e08953 |
size_t size;
|
|
Packit Service |
e08953 |
struct uv__fd_hash_entry_group_s* data;
|
|
Packit Service |
e08953 |
};
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
static uv_mutex_t uv__fd_hash_mutex;
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
static struct uv__fd_hash_entry_group_s
|
|
Packit Service |
e08953 |
uv__fd_hash_entry_initial[UV__FD_HASH_SIZE * UV__FD_HASH_GROUP_SIZE];
|
|
Packit Service |
e08953 |
static struct uv__fd_hash_bucket_s uv__fd_hash[UV__FD_HASH_SIZE];
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
INLINE static void uv__fd_hash_init(void) {
|
|
Packit Service |
e08953 |
int i, err;
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
err = uv_mutex_init(&uv__fd_hash_mutex);
|
|
Packit Service |
e08953 |
if (err) {
|
|
Packit Service |
e08953 |
uv_fatal_error(err, "uv_mutex_init");
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
for (i = 0; i < ARRAY_SIZE(uv__fd_hash); ++i) {
|
|
Packit Service |
e08953 |
uv__fd_hash[i].size = 0;
|
|
Packit Service |
e08953 |
uv__fd_hash[i].data =
|
|
Packit Service |
e08953 |
uv__fd_hash_entry_initial + i * UV__FD_HASH_GROUP_SIZE;
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
#define FIND_COMMON_VARIABLES \
|
|
Packit Service |
e08953 |
unsigned i; \
|
|
Packit Service |
e08953 |
unsigned bucket = fd % ARRAY_SIZE(uv__fd_hash); \
|
|
Packit Service |
e08953 |
struct uv__fd_hash_entry_s* entry_ptr = NULL; \
|
|
Packit Service |
e08953 |
struct uv__fd_hash_entry_group_s* group_ptr; \
|
|
Packit Service |
e08953 |
struct uv__fd_hash_bucket_s* bucket_ptr = &uv__fd_hash[bucket];
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
#define FIND_IN_GROUP_PTR(group_size) \
|
|
Packit Service |
e08953 |
do { \
|
|
Packit Service |
e08953 |
for (i = 0; i < group_size; ++i) { \
|
|
Packit Service |
e08953 |
if (group_ptr->entries[i].fd == fd) { \
|
|
Packit Service |
e08953 |
entry_ptr = &group_ptr->entries[i]; \
|
|
Packit Service |
e08953 |
break; \
|
|
Packit Service |
e08953 |
} \
|
|
Packit Service |
e08953 |
} \
|
|
Packit Service |
e08953 |
} while (0)
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
#define FIND_IN_BUCKET_PTR() \
|
|
Packit Service |
e08953 |
do { \
|
|
Packit Service |
e08953 |
size_t first_group_size = bucket_ptr->size % UV__FD_HASH_GROUP_SIZE; \
|
|
Packit Service |
e08953 |
if (bucket_ptr->size != 0 && first_group_size == 0) \
|
|
Packit Service |
e08953 |
first_group_size = UV__FD_HASH_GROUP_SIZE; \
|
|
Packit Service |
e08953 |
group_ptr = bucket_ptr->data; \
|
|
Packit Service |
e08953 |
FIND_IN_GROUP_PTR(first_group_size); \
|
|
Packit Service |
e08953 |
for (group_ptr = group_ptr->next; \
|
|
Packit Service |
e08953 |
group_ptr != NULL && entry_ptr == NULL; \
|
|
Packit Service |
e08953 |
group_ptr = group_ptr->next) \
|
|
Packit Service |
e08953 |
FIND_IN_GROUP_PTR(UV__FD_HASH_GROUP_SIZE); \
|
|
Packit Service |
e08953 |
} while (0)
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
INLINE static int uv__fd_hash_get(int fd, struct uv__fd_info_s* info) {
|
|
Packit Service |
e08953 |
FIND_COMMON_VARIABLES
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
uv_mutex_lock(&uv__fd_hash_mutex);
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
FIND_IN_BUCKET_PTR();
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
if (entry_ptr != NULL) {
|
|
Packit Service |
e08953 |
*info = entry_ptr->info;
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
uv_mutex_unlock(&uv__fd_hash_mutex);
|
|
Packit Service |
e08953 |
return entry_ptr != NULL;
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
INLINE static void uv__fd_hash_add(int fd, struct uv__fd_info_s* info) {
|
|
Packit Service |
e08953 |
FIND_COMMON_VARIABLES
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
uv_mutex_lock(&uv__fd_hash_mutex);
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
FIND_IN_BUCKET_PTR();
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
if (entry_ptr == NULL) {
|
|
Packit Service |
e08953 |
i = bucket_ptr->size % UV__FD_HASH_GROUP_SIZE;
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
if (bucket_ptr->size != 0 && i == 0) {
|
|
Packit Service |
e08953 |
struct uv__fd_hash_entry_group_s* new_group_ptr =
|
|
Packit Service |
e08953 |
uv__malloc(sizeof(*new_group_ptr));
|
|
Packit Service |
e08953 |
if (new_group_ptr == NULL) {
|
|
Packit Service |
e08953 |
uv_fatal_error(ERROR_OUTOFMEMORY, "uv__malloc");
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
new_group_ptr->next = bucket_ptr->data;
|
|
Packit Service |
e08953 |
bucket_ptr->data = new_group_ptr;
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
bucket_ptr->size += 1;
|
|
Packit Service |
e08953 |
entry_ptr = &bucket_ptr->data->entries[i];
|
|
Packit Service |
e08953 |
entry_ptr->fd = fd;
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
entry_ptr->info = *info;
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
uv_mutex_unlock(&uv__fd_hash_mutex);
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
INLINE static int uv__fd_hash_remove(int fd, struct uv__fd_info_s* info) {
|
|
Packit Service |
e08953 |
FIND_COMMON_VARIABLES
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
uv_mutex_lock(&uv__fd_hash_mutex);
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
FIND_IN_BUCKET_PTR();
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
if (entry_ptr != NULL) {
|
|
Packit Service |
e08953 |
*info = entry_ptr->info;
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
bucket_ptr->size -= 1;
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
i = bucket_ptr->size % UV__FD_HASH_GROUP_SIZE;
|
|
Packit Service |
e08953 |
if (entry_ptr != &bucket_ptr->data->entries[i]) {
|
|
Packit Service |
e08953 |
*entry_ptr = bucket_ptr->data->entries[i];
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
if (bucket_ptr->size != 0 &&
|
|
Packit Service |
e08953 |
bucket_ptr->size % UV__FD_HASH_GROUP_SIZE == 0) {
|
|
Packit Service |
e08953 |
struct uv__fd_hash_entry_group_s* old_group_ptr = bucket_ptr->data;
|
|
Packit Service |
e08953 |
bucket_ptr->data = old_group_ptr->next;
|
|
Packit Service |
e08953 |
uv__free(old_group_ptr);
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
uv_mutex_unlock(&uv__fd_hash_mutex);
|
|
Packit Service |
e08953 |
return entry_ptr != NULL;
|
|
Packit Service |
e08953 |
}
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
#undef FIND_COMMON_VARIABLES
|
|
Packit Service |
e08953 |
#undef FIND_IN_GROUP_PTR
|
|
Packit Service |
e08953 |
#undef FIND_IN_BUCKET_PTR
|
|
Packit Service |
e08953 |
|
|
Packit Service |
e08953 |
#endif /* UV_WIN_FS_FD_HASH_INL_H_ */
|