Improved stability/performance
authorshingil.kang <shingil.kang@samsung.com>
Mon, 31 Aug 2015 03:38:28 +0000 (12:38 +0900)
committershingil.kang <shingil.kang@samsung.com>
Tue, 15 Sep 2015 06:58:50 +0000 (15:58 +0900)
- unlimit SDB FD

Change-Id: Id66e4746c01555f1b00a49cedc5b97c4f6668824
Signed-off-by: shingil.kang <shingil.kang@samsung.com>
src/utils_windows.c

index e8abe7b3d77ee2fe2b56765d7e88cca423a32e94..9a402960f559f371f2350b33bd76d3cda92b700c 100755 (executable)
@@ -34,8 +34,6 @@ void setBaseErrno(DWORD dwLastErrorCode);
 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;
@@ -235,30 +233,62 @@ static void _start_logging(void)
 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 {