int getBaseErrno(DWORD dwLastErrorCode);
static int total_handle_number = 0;
-//this increases 1 when one file or socket is created.
-static int windows_handle_fd_count = 0;
struct errno_lists {
unsigned long wincode;
static sdb_mutex_t _win32_lock;
static sdb_mutex_t sdb_handle_map_lock;
+static int get_available_fd()
+{
+ static int windows_handle_fd_count = 1;
+ int ret = -1;
+ int i = 0;
+
+ // fast logic
+ if (windows_handle_fd_count < WIN32_MAX_FHS) {
+ ret = windows_handle_fd_count;
+ windows_handle_fd_count++;
+ } else {
+ // slow logic
+ for (i = 1; i <= WIN32_MAX_FHS; i++) {
+ if (sdb_handle_map_get(i) == NULL) {
+ ret = i;
+ break;
+ }
+ if (i == WIN32_MAX_FHS) {
+ D("alloc_handle: no more free handle\n" );
+ return -1;
+ }
+ }
+ }
+ return ret;
+}
+
static SDB_HANDLE* alloc_handle(int socket) {
SDB_HANDLE* _h = NULL;
sdb_mutex_lock(&_win32_lock, "_fh_alloc _win32_lock");
+ int windows_handle_fd = -1;
if(total_handle_number < WIN32_MAX_FHS) {
- total_handle_number++;
- windows_handle_fd_count++;
+ windows_handle_fd = get_available_fd();
+ if (windows_handle_fd < 0) {
+ D("alloc_handle: no more free handle\n" );
+ sdb_mutex_unlock(&_win32_lock, "_fh_alloc _win32_lock");
+ return NULL;
+ }
+
+ total_handle_number++;
+
if(socket) {
SDB_SOCK_HANDLE* __h = malloc(sizeof(SDB_SOCK_HANDLE));
__h->event_location = -1;
_h = (SDB_HANDLE*)__h;
_h->is_socket = 1;
_h->u.socket = INVALID_SOCKET;
- LOG_INFO("assign socket fd FD(%d)\n", _h->fd);
}
else {
_h = malloc(sizeof(SDB_HANDLE));
_h->u.file_handle = INVALID_HANDLE_VALUE;
_h->is_socket = 0;
- LOG_INFO("assign file fd FD(%d)\n", _h->fd);
}
-
- _h->fd = windows_handle_fd_count;
+ _h->fd = windows_handle_fd;
+ LOG_INFO("assign FD(%d)\n", _h->fd);
sdb_handle_map_put(_h->fd, _h);
}
else {