Sfoglia il codice sorgente

删除fd_get的ref_inc_nr参数
修改fd_release参数由dfs_fd为fd索引

shaojinchun 5 anni fa
parent
commit
c5d32c93a0

+ 4 - 4
components/dfs/include/dfs.h

@@ -102,13 +102,13 @@ void dfs_fd_unlock(void);
 
 /* FD APIs */
 int fdt_fd_new(struct dfs_fdtable *fdt);
-struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr);
-void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd);
+struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd);
+void fdt_fd_release(struct dfs_fdtable* fdt, int fd);
 int fd_new(void);
 int fd_dup(int fd);
 int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_fd *file);
-struct dfs_fd *fd_get(int fd, int ref_inc_nr);
-void fd_release(struct dfs_fd *fd);
+struct dfs_fd *fd_get(int fd);
+void fd_release(int fd);
 
 void fd_init(struct dfs_fd *fd);
 

+ 25 - 25
components/dfs/src/dfs.c

@@ -256,7 +256,7 @@ int fd_new(void)
  * pointer.
  */
 
-struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr)
+struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd)
 {
     struct dfs_fd *d;
 
@@ -273,19 +273,17 @@ struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr)
         return NULL;
     }
 
-    d->ref_count += ref_inc_nr;
-
     dfs_fd_unlock();
 
     return d;
 }
 
-struct dfs_fd *fd_get(int fd, int ref_inc_nr)
+struct dfs_fd *fd_get(int fd)
 {
     struct dfs_fdtable *fdt;
 
     fdt = dfs_fdtable_get();
-    return fdt_fd_get(fdt, fd, ref_inc_nr);
+    return fdt_fd_get(fdt, fd);
 }
 
 /**
@@ -293,55 +291,47 @@ struct dfs_fd *fd_get(int fd, int ref_inc_nr)
  *
  * This function will put the file descriptor.
  */
-void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd)
+void fdt_fd_release(struct dfs_fdtable* fdt, int fd)
 {
-    int idx = 0;
-    struct dfs_fd *fd_iter = NULL;
+    struct dfs_fd *fd_slot = NULL;
 
     RT_ASSERT(fdt != NULL);
 
     dfs_fd_lock();
 
-    if (fd == NULL)
+    if ((fd < 0) || (fd >= fdt->maxfd))
     {
         dfs_fd_unlock();
         return;
     }
 
-    for (idx = 0; idx < fdt->maxfd; idx++)
-    {
-        fd_iter = fdt->fds[idx];
-        if (fd_iter == fd)
-        {
-            break;
-        }
-    }
-    if (idx == fdt->maxfd)
+    fd_slot = fdt->fds[fd];
+    if (fd_slot == NULL)
     {
         dfs_fd_unlock();
         return;
     }
+    fdt->fds[fd] = NULL;
 
     /* check fd */
-    RT_ASSERT(fd->magic == DFS_FD_MAGIC);
+    RT_ASSERT(fd_slot->magic == DFS_FD_MAGIC);
 
-    fd->ref_count--;
+    fd_slot->ref_count--;
 
     /* clear this fd entry */
-    if (fd->ref_count == 0)
+    if (fd_slot->ref_count == 0)
     {
-        struct dfs_fnode *fnode = fd->fnode;
+        struct dfs_fnode *fnode = fd_slot->fnode;
         if (fnode)
         {
             fnode->ref_count--;
         }
-        rt_free(fd);
-        fdt->fds[idx] = 0;
+        rt_free(fd_slot);
     }
     dfs_fd_unlock();
 }
 
-void fd_release(struct dfs_fd *fd)
+void fd_release(int fd)
 {
     struct dfs_fdtable *fdt;
 
@@ -372,6 +362,16 @@ int fd_dup(int oldfd)
         goto exit;
     }
 
+    if (fdt->fds[newfd])
+    {
+        ret = dfs_file_close(fdt->fds[newfd]);
+        if (ret < 0)
+        {
+            goto exit;
+        }
+        fd_release(newfd);
+    }
+
     fdt->fds[newfd] = fdt->fds[oldfd];
     /* inc ref_count */
     fdt->fds[newfd]->ref_count++;

+ 23 - 23
components/dfs/src/dfs_posix.c

@@ -41,13 +41,13 @@ int open(const char *file, int flags, ...)
 
         return -1;
     }
-    d = fd_get(fd, 0);
+    d = fd_get(fd);
 
     result = dfs_file_open(d, file, flags);
     if (result < 0)
     {
         /* release the ref-count of fd */
-        fd_release(d);
+        fd_release(fd);
 
         rt_set_errno(result);
 
@@ -71,7 +71,7 @@ int close(int fd)
     int result;
     struct dfs_fd *d;
 
-    d = fd_get(fd, 0);
+    d = fd_get(fd);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -88,7 +88,7 @@ int close(int fd)
         return -1;
     }
 
-    fd_release(d);
+    fd_release(fd);
 
     return 0;
 }
@@ -115,7 +115,7 @@ int read(int fd, void *buf, size_t len)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = fd_get(fd, 0);
+    d = fd_get(fd);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -155,7 +155,7 @@ int write(int fd, const void *buf, size_t len)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = fd_get(fd, 0);
+    d = fd_get(fd);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -190,7 +190,7 @@ off_t lseek(int fd, off_t offset, int whence)
     int result;
     struct dfs_fd *d;
 
-    d = fd_get(fd, 0);
+    d = fd_get(fd);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -324,7 +324,7 @@ int fstat(int fildes, struct stat *buf)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = fd_get(fildes, 0);
+    d = fd_get(fildes);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -367,7 +367,7 @@ int fsync(int fildes)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = fd_get(fildes, 0);
+    d = fd_get(fildes);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -398,7 +398,7 @@ int fcntl(int fildes, int cmd, ...)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = fd_get(fildes, 0);
+    d = fd_get(fildes);
     if (d)
     {
         void *arg;
@@ -463,7 +463,7 @@ int ftruncate(int fd, off_t length)
     int result;
     struct dfs_fd *d;
 
-    d = fd_get(fd, 0);
+    d = fd_get(fd);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -536,20 +536,20 @@ int mkdir(const char *path, mode_t mode)
         return -1;
     }
 
-    d = fd_get(fd, 0);
+    d = fd_get(fd);
 
     result = dfs_file_open(d, path, O_DIRECTORY | O_CREAT);
 
     if (result < 0)
     {
-        fd_release(d);
+        fd_release(fd);
         rt_set_errno(result);
 
         return -1;
     }
 
     dfs_file_close(d);
-    fd_release(d);
+    fd_release(fd);
 
     return 0;
 }
@@ -606,7 +606,7 @@ DIR *opendir(const char *name)
 
         return NULL;
     }
-    d = fd_get(fd, 0);
+    d = fd_get(fd);
 
     result = dfs_file_open(d, name, O_RDONLY | O_DIRECTORY);
     if (result >= 0)
@@ -616,7 +616,7 @@ DIR *opendir(const char *name)
         if (t == NULL)
         {
             dfs_file_close(d);
-            fd_release(d);
+            fd_release(fd);
         }
         else
         {
@@ -629,7 +629,7 @@ DIR *opendir(const char *name)
     }
 
     /* open failed */
-    fd_release(d);
+    fd_release(fd);
     rt_set_errno(result);
 
     return NULL;
@@ -650,7 +650,7 @@ struct dirent *readdir(DIR *d)
     int result;
     struct dfs_fd *fd;
 
-    fd = fd_get(d->fd, 0);
+    fd = fd_get(d->fd);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -698,7 +698,7 @@ long telldir(DIR *d)
     struct dfs_fd *fd;
     long result;
 
-    fd = fd_get(d->fd, 0);
+    fd = fd_get(d->fd);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -723,7 +723,7 @@ void seekdir(DIR *d, off_t offset)
 {
     struct dfs_fd *fd;
 
-    fd = fd_get(d->fd, 0);
+    fd = fd_get(d->fd);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -747,7 +747,7 @@ void rewinddir(DIR *d)
 {
     struct dfs_fd *fd;
 
-    fd = fd_get(d->fd, 0);
+    fd = fd_get(d->fd);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -774,7 +774,7 @@ int closedir(DIR *d)
     int result;
     struct dfs_fd *fd;
 
-    fd = fd_get(d->fd, 0);
+    fd = fd_get(d->fd);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -783,7 +783,7 @@ int closedir(DIR *d)
     }
 
     result = dfs_file_close(fd);
-    fd_release(fd);
+    fd_release(d->fd);
 
     rt_free(d);
 

+ 1 - 1
components/dfs/src/poll.c

@@ -128,7 +128,7 @@ static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req)
 
     if (fd >= 0)
     {
-        struct dfs_fd *f = fd_get(fd, 0);
+        struct dfs_fd *f = fd_get(fd);
         mask = POLLNVAL;
 
         if (f)

+ 3 - 3
components/libc/compilers/musl/stdio.c

@@ -52,9 +52,9 @@ int libc_stdio_set_console(const char* device_name, int mode)
         int fd = fileno(std_console);
 
         /* set fd (0, 1, 2) */
-        fd_associate(fdt, 0, fd_get(fd, 0));
-        fd_associate(fdt, 1, fd_get(fd, 0));
-        fd_associate(fdt, 2, fd_get(fd, 0));
+        fd_associate(fdt, 0, fd_get(fd));
+        fd_associate(fdt, 1, fd_get(fd));
+        fd_associate(fdt, 2, fd_get(fd));
         return fd;
     }
 

+ 3 - 3
components/lwp/lwp.c

@@ -758,11 +758,11 @@ static void lwp_copy_stdio_fdt(struct rt_lwp *lwp)
     if (lwp_fdt->fds)
     {
         lwp_fdt->maxfd = 4;
-        d = fd_get(0, 0);
+        d = fd_get(0);
         fd_associate(lwp_fdt, 0, d);
-        d = fd_get(1, 0);
+        d = fd_get(1);
         fd_associate(lwp_fdt, 1, d);
-        d = fd_get(2, 0);
+        d = fd_get(2);
         fd_associate(lwp_fdt, 2, d);
     }
 

+ 3 - 3
components/lwp/lwp_ipc.c

@@ -795,10 +795,10 @@ static struct dfs_fd *lwp_fd_get(int fdt_type, int fd)
     {
         fdt = dfs_fdtable_get();
     }
-    return fdt_fd_get(fdt, fd, 0);
+    return fdt_fd_get(fdt, fd);
 }
 
-static void lwp_fd_release(int fdt_type, struct dfs_fd *fd)
+static void lwp_fd_release(int fdt_type, int fd)
 {
     struct dfs_fdtable *fdt;
 
@@ -837,7 +837,7 @@ static void _chfd_free(int fd, int fdt_type)
     {
         return;
     }
-    lwp_fd_release(fdt_type, d);
+    lwp_fd_release(fdt_type, fd);
 }
 
 /* for fops */

+ 6 - 4
components/lwp/lwp_pid.c

@@ -41,17 +41,19 @@ int libc_stdio_get_console(void);
 
 static void __exit_files(struct rt_lwp *lwp)
 {
-    while (lwp->fdt.maxfd > 0)
+    int fd = lwp->fdt.maxfd - 1;
+
+    while (fd >= 0)
     {
         struct dfs_fd *d;
 
-        d = lwp->fdt.fds[lwp->fdt.maxfd - 1];
+        d = lwp->fdt.fds[fd];
         if (d)
         {
             dfs_file_close(d);
-            fdt_fd_release(&lwp->fdt, d);
+            fdt_fd_release(&lwp->fdt, fd);
         }
-        lwp->fdt.maxfd --;
+        fd--;
     }
 }
 

+ 1 - 1
components/lwp/lwp_syscall.c

@@ -2335,7 +2335,7 @@ int sys_getdents(int fd, struct libc_dirent *dirp, size_t nbytes)
         rt_set_errno(ENOMEM);
         return -1;
     }
-    dfs_fd = fd_get(fd, 0);
+    dfs_fd = fd_get(fd);
     ret = dfs_file_getdents(dfs_fd, rtt_dirp, nbytes);
     if (ret)
     {

+ 1 - 1
components/net/sal_socket/dfs_net/dfs_net.c

@@ -23,7 +23,7 @@ int dfs_net_getsocket(int fd)
     int socket;
     struct dfs_fd *_dfs_fd;
 
-    _dfs_fd = fd_get(fd, 0);
+    _dfs_fd = fd_get(fd);
     if (_dfs_fd == NULL) return -1;
 
     if (_dfs_fd->fnode->type != FT_SOCKET) socket = -1;

+ 6 - 6
components/net/sal_socket/socket/net_sockets.c

@@ -37,7 +37,7 @@ int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
             return -1;
         }
 
-        d = fd_get(fd, 0);
+        d = fd_get(fd);
         if(d)
         {
             /* this is a socket fd */
@@ -86,7 +86,7 @@ int shutdown(int s, int how)
         return -1;
     }
 
-    d = fd_get(s, 0);
+    d = fd_get(s);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -204,7 +204,7 @@ int socket(int domain, int type, int protocol)
 
         return -1;
     }
-    d = fd_get(fd, 0);
+    d = fd_get(fd);
 
     /* create socket  and then put it to the dfs_fd */
     socket = sal_socket(domain, type, protocol);
@@ -226,7 +226,7 @@ int socket(int domain, int type, int protocol)
     else
     {
         /* release fd */
-        fd_release(d);
+        fd_release(fd);
 
         rt_set_errno(-ENOMEM);
 
@@ -250,7 +250,7 @@ int closesocket(int s)
         return -1;
     }
 
-    d = fd_get(s, 0);
+    d = fd_get(s);
     if (d == RT_NULL)
     {
         rt_set_errno(-EBADF);
@@ -268,7 +268,7 @@ int closesocket(int s)
     }
 
     /* socket has been closed, delete it from file system fd */
-    fd_release(d);
+    fd_release(s);
 
     return error;
 }