Просмотр исходного кода

修改fd get时的机制为只作fd到dfd_fd的转换

shaojinchun 5 лет назад
Родитель
Сommit
a727db0ebd

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

@@ -102,11 +102,11 @@ 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);
-void fdt_fd_put(struct dfs_fdtable* fdt, struct dfs_fd *fd);
+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);
 int fd_new(void);
-struct dfs_fd *fd_get(int fd);
-void fd_put(struct dfs_fd *fd);
+struct dfs_fd *dfs_fd_get(int fd);
+void dfs_fd_release(struct dfs_fd *fd);
 int fd_is_open(const char *pathname);
 
 struct dfs_fdtable *dfs_fdtable_get(void);

+ 9 - 8
components/dfs/src/dfs.c

@@ -239,7 +239,8 @@ int fd_new(void)
  * @return NULL on on this file descriptor or the file descriptor structure
  * pointer.
  */
-struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd)
+
+struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr)
 {
     struct dfs_fd *d;
 
@@ -262,19 +263,19 @@ struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd)
         return NULL;
     }
 
-    /* increase the reference count */
-    d->ref_count ++;
+    d->ref_count += ref_inc_nr;
+
     dfs_fd_unlock();
 
     return d;
 }
 
-struct dfs_fd *fd_get(int fd)
+struct dfs_fd *dfs_fd_get(int fd)
 {
     struct dfs_fdtable *fdt;
 
     fdt = dfs_fdtable_get();
-    return fdt_fd_get(fdt, fd);
+    return fdt_fd_get(fdt, fd, 0);
 }
 
 /**
@@ -282,7 +283,7 @@ struct dfs_fd *fd_get(int fd)
  *
  * This function will put the file descriptor.
  */
-void fdt_fd_put(struct dfs_fdtable* fdt, struct dfs_fd *fd)
+void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd)
 {
     RT_ASSERT(fd != NULL);
 
@@ -308,12 +309,12 @@ void fdt_fd_put(struct dfs_fdtable* fdt, struct dfs_fd *fd)
     dfs_fd_unlock();
 }
 
-void fd_put(struct dfs_fd *fd)
+void dfs_fd_release(struct dfs_fd *fd)
 {
     struct dfs_fdtable *fdt;
 
     fdt = dfs_fdtable_get();
-    fdt_fd_put(fdt, fd);
+    fdt_fd_release(fdt, fd);
 }
 
 /**

+ 21 - 59
components/dfs/src/dfs_posix.c

@@ -41,23 +41,19 @@ int open(const char *file, int flags, ...)
 
         return -1;
     }
-    d = fd_get(fd);
+    d = dfs_fd_get(fd);
 
     result = dfs_file_open(d, file, flags);
     if (result < 0)
     {
         /* release the ref-count of fd */
-        fd_put(d);
-        fd_put(d);
+        dfs_fd_release(d);
 
         rt_set_errno(result);
 
         return -1;
     }
 
-    /* release the ref-count of fd */
-    fd_put(d);
-
     return fd;
 }
 RTM_EXPORT(open);
@@ -120,7 +116,7 @@ int read(int fd, void *buf, size_t len)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = fd_get(fd);
+    d = dfs_fd_get(fd);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -131,15 +127,11 @@ int read(int fd, void *buf, size_t len)
     result = dfs_file_read(d, buf, len);
     if (result < 0)
     {
-        fd_put(d);
         rt_set_errno(result);
 
         return -1;
     }
 
-    /* release the ref-count of fd */
-    fd_put(d);
-
     return result;
 }
 RTM_EXPORT(read);
@@ -164,7 +156,7 @@ int write(int fd, const void *buf, size_t len)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = fd_get(fd);
+    d = dfs_fd_get(fd);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -175,15 +167,11 @@ int write(int fd, const void *buf, size_t len)
     result = dfs_file_write(d, buf, len);
     if (result < 0)
     {
-        fd_put(d);
         rt_set_errno(result);
 
         return -1;
     }
 
-    /* release the ref-count of fd */
-    fd_put(d);
-
     return result;
 }
 RTM_EXPORT(write);
@@ -203,7 +191,7 @@ off_t lseek(int fd, off_t offset, int whence)
     int result;
     struct dfs_fd *d;
 
-    d = fd_get(fd);
+    d = dfs_fd_get(fd);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -225,7 +213,6 @@ off_t lseek(int fd, off_t offset, int whence)
         break;
 
     default:
-        fd_put(d);
         rt_set_errno(-EINVAL);
 
         return -1;
@@ -233,7 +220,6 @@ off_t lseek(int fd, off_t offset, int whence)
 
     if (offset < 0)
     {
-        fd_put(d);
         rt_set_errno(-EINVAL);
 
         return -1;
@@ -241,15 +227,11 @@ off_t lseek(int fd, off_t offset, int whence)
     result = dfs_file_lseek(d, offset);
     if (result < 0)
     {
-        fd_put(d);
         rt_set_errno(result);
 
         return -1;
     }
 
-    /* release the ref-count of fd */
-    fd_put(d);
-
     return offset;
 }
 RTM_EXPORT(lseek);
@@ -343,7 +325,7 @@ int fstat(int fildes, struct stat *buf)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = fd_get(fildes);
+    d = dfs_fd_get(fildes);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -365,8 +347,6 @@ int fstat(int fildes, struct stat *buf)
     buf->st_size    = d->size;
     buf->st_mtime   = 0;
 
-    fd_put(d);
-
     return RT_EOK;
 }
 RTM_EXPORT(fstat);
@@ -388,7 +368,7 @@ int fsync(int fildes)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = fd_get(fildes);
+    d = dfs_fd_get(fildes);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -397,7 +377,6 @@ int fsync(int fildes)
 
     ret = dfs_file_flush(d);
 
-    fd_put(d);
     return ret;
 }
 RTM_EXPORT(fsync);
@@ -420,7 +399,7 @@ int fcntl(int fildes, int cmd, ...)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = fd_get(fildes);
+    d = dfs_fd_get(fildes);
     if (d)
     {
         void *arg;
@@ -431,7 +410,6 @@ int fcntl(int fildes, int cmd, ...)
         va_end(ap);
 
         ret = dfs_file_ioctl(d, cmd, arg);
-        fd_put(d);
     }
     else ret = -EBADF;
 
@@ -486,7 +464,7 @@ int ftruncate(int fd, off_t length)
     int result;
     struct dfs_fd *d;
 
-    d = fd_get(fd);
+    d = dfs_fd_get(fd);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -496,7 +474,6 @@ int ftruncate(int fd, off_t length)
 
     if (length < 0)
     {
-        fd_put(d);
         rt_set_errno(-EINVAL);
 
         return -1;
@@ -504,15 +481,11 @@ int ftruncate(int fd, off_t length)
     result = dfs_file_ftruncate(d, length);
     if (result < 0)
     {
-        fd_put(d);
         rt_set_errno(result);
 
         return -1;
     }
 
-    /* release the ref-count of fd */
-    fd_put(d);
-
     return 0;
 }
 RTM_EXPORT(ftruncate);
@@ -564,22 +537,20 @@ int mkdir(const char *path, mode_t mode)
         return -1;
     }
 
-    d = fd_get(fd);
+    d = dfs_fd_get(fd);
 
     result = dfs_file_open(d, path, O_DIRECTORY | O_CREAT);
 
     if (result < 0)
     {
-        fd_put(d);
-        fd_put(d);
+        dfs_fd_release(d);
         rt_set_errno(result);
 
         return -1;
     }
 
     dfs_file_close(d);
-    fd_put(d);
-    fd_put(d);
+    dfs_fd_release(d);
 
     return 0;
 }
@@ -636,7 +607,7 @@ DIR *opendir(const char *name)
 
         return NULL;
     }
-    d = fd_get(fd);
+    d = dfs_fd_get(fd);
 
     result = dfs_file_open(d, name, O_RDONLY | O_DIRECTORY);
     if (result >= 0)
@@ -646,7 +617,7 @@ DIR *opendir(const char *name)
         if (t == NULL)
         {
             dfs_file_close(d);
-            fd_put(d);
+            dfs_fd_release(d);
         }
         else
         {
@@ -654,14 +625,12 @@ DIR *opendir(const char *name)
 
             t->fd = fd;
         }
-        fd_put(d);
 
         return t;
     }
 
     /* open failed */
-    fd_put(d);
-    fd_put(d);
+    dfs_fd_release(d);
     rt_set_errno(result);
 
     return NULL;
@@ -682,7 +651,7 @@ struct dirent *readdir(DIR *d)
     int result;
     struct dfs_fd *fd;
 
-    fd = fd_get(d->fd);
+    fd = dfs_fd_get(d->fd);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -704,7 +673,6 @@ struct dirent *readdir(DIR *d)
                                    sizeof(d->buf) - 1);
         if (result <= 0)
         {
-            fd_put(fd);
             rt_set_errno(result);
 
             return NULL;
@@ -714,8 +682,6 @@ struct dirent *readdir(DIR *d)
         d->cur = 0; /* current entry index */
     }
 
-    fd_put(fd);
-
     return (struct dirent *)(d->buf + d->cur);
 }
 RTM_EXPORT(readdir);
@@ -733,7 +699,7 @@ long telldir(DIR *d)
     struct dfs_fd *fd;
     long result;
 
-    fd = fd_get(d->fd);
+    fd = dfs_fd_get(d->fd);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -742,7 +708,6 @@ long telldir(DIR *d)
     }
 
     result = fd->pos - d->num + d->cur;
-    fd_put(fd);
 
     return result;
 }
@@ -759,7 +724,7 @@ void seekdir(DIR *d, off_t offset)
 {
     struct dfs_fd *fd;
 
-    fd = fd_get(d->fd);
+    fd = dfs_fd_get(d->fd);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -770,7 +735,6 @@ void seekdir(DIR *d, off_t offset)
     /* seek to the offset position of directory */
     if (dfs_file_lseek(fd, offset) >= 0)
         d->num = d->cur = 0;
-    fd_put(fd);
 }
 RTM_EXPORT(seekdir);
 
@@ -784,7 +748,7 @@ void rewinddir(DIR *d)
 {
     struct dfs_fd *fd;
 
-    fd = fd_get(d->fd);
+    fd = dfs_fd_get(d->fd);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -795,7 +759,6 @@ void rewinddir(DIR *d)
     /* seek to the beginning of directory */
     if (dfs_file_lseek(fd, 0) >= 0)
         d->num = d->cur = 0;
-    fd_put(fd);
 }
 RTM_EXPORT(rewinddir);
 
@@ -812,7 +775,7 @@ int closedir(DIR *d)
     int result;
     struct dfs_fd *fd;
 
-    fd = fd_get(d->fd);
+    fd = dfs_fd_get(d->fd);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -821,9 +784,8 @@ int closedir(DIR *d)
     }
 
     result = dfs_file_close(fd);
-    fd_put(fd);
+    dfs_fd_release(fd);
 
-    fd_put(fd);
     rt_free(d);
 
     if (result < 0)

+ 1 - 3
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);
+        struct dfs_fd *f = dfs_fd_get(fd);
         mask = POLLNVAL;
 
         if (f)
@@ -143,14 +143,12 @@ static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req)
                 /* dealwith the device return error -1*/
                 if (mask < 0)
                 {
-                    fd_put(f);
                     pollfd->revents = 0;
                     return mask;
                 }
             }
             /* Mask out unneeded events. */
             mask &= pollfd->events | POLLERR | POLLHUP;
-            fd_put(f);
         }
     }
     pollfd->revents = mask;

+ 3 - 2
components/lwp/lwp.c

@@ -752,10 +752,11 @@ static void lwp_copy_stdio_fdt(struct rt_lwp *lwp)
     int fd;
     struct dfs_fd *d;
     struct dfs_fdtable *lwp_fdt;
+    struct dfs_fdtable *fdt;
 
     fd = libc_stdio_get_console();
-    d = fd_get(fd);
-    fd_put(d);
+    fdt = dfs_fdtable_get();
+    d = fdt_fd_get(fdt, fd, 0); /* inc 0 ref count */
 
     fd = fd - DFS_FD_OFFSET;
     if (d == NULL)

+ 4 - 7
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);
+    return fdt_fd_get(fdt, fd, 0);
 }
 
-static void lwp_fd_put(int fdt_type, struct dfs_fd *fd)
+static void lwp_fd_release(int fdt_type, struct dfs_fd *fd)
 {
     struct dfs_fdtable *fdt;
 
@@ -810,7 +810,7 @@ static void lwp_fd_put(int fdt_type, struct dfs_fd *fd)
     {
         fdt = dfs_fdtable_get();
     }
-    fdt_fd_put(fdt, fd);
+    fdt_fd_release(fdt, fd);
 }
 
 static int _chfd_alloc(int fdt_type)
@@ -837,8 +837,7 @@ static void _chfd_free(int fd, int fdt_type)
     {
         return;
     }
-    lwp_fd_put(fdt_type, d);
-    lwp_fd_put(fdt_type, d);
+    lwp_fd_release(fdt_type, d);
 }
 
 /* for fops */
@@ -924,7 +923,6 @@ int lwp_channel_open(int fdt_type, const char *name, int flags)
 
         /* set socket to the data of dfs_fd */
         d->data = (void *)ch;
-        lwp_fd_put(fdt_type, d);
     }
     else
     {
@@ -945,7 +943,6 @@ static rt_channel_t fd_2_channel(int fdt_type, int fd)
         rt_channel_t ch;
 
         ch = (rt_channel_t)d->data;
-        lwp_fd_put(fdt_type, d);
         if (ch)
         {
             return ch;

+ 1 - 2
components/lwp/lwp_syscall.c

@@ -2339,9 +2339,8 @@ int sys_getdents(int fd, struct libc_dirent *dirp, size_t nbytes)
         rt_set_errno(ENOMEM);
         return -1;
     }
-    dfs_fd = fd_get(fd);
+    dfs_fd = dfs_fd_get(fd);
     ret = dfs_file_getdents(dfs_fd, rtt_dirp, nbytes);
-    fd_put(dfs_fd);
     if (ret)
     {
         size_t i = 0;

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

@@ -23,13 +23,12 @@ int dfs_net_getsocket(int fd)
     int socket;
     struct dfs_fd *_dfs_fd; 
 
-    _dfs_fd = fd_get(fd);
+    _dfs_fd = dfs_fd_get(fd);
     if (_dfs_fd == NULL) return -1;
 
     if (_dfs_fd->type != FT_SOCKET) socket = -1;
     else socket = (int)_dfs_fd->data;
 
-    fd_put(_dfs_fd); /* put this dfs fd */
     return socket;
 }
 

+ 7 - 17
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);
+        d = dfs_fd_get(fd);
         if(d)
         {
             /* this is a socket fd */
@@ -53,9 +53,6 @@ int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
             /* set socket to the data of dfs_fd */
             d->data = (void *) new_socket;
 
-            /* release the ref-count of fd */
-            fd_put(d);
-
             return fd;
         }
 
@@ -89,13 +86,13 @@ int shutdown(int s, int how)
         return -1;
     }
 
-    d = fd_get(s);
+    d = dfs_fd_get(s);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
         return -1;
     }
-    
+
     if (sal_shutdown(socket, how) == 0)
     {
         error = 0;
@@ -105,8 +102,6 @@ int shutdown(int s, int how)
         rt_set_errno(-ENOTSOCK);
         error = -1;
     }
-    
-    fd_put(d);
 
     return error;
 }
@@ -209,7 +204,7 @@ int socket(int domain, int type, int protocol)
 
         return -1;
     }
-    d = fd_get(fd);
+    d = dfs_fd_get(fd);
 
     /* create socket  and then put it to the dfs_fd */
     socket = sal_socket(domain, type, protocol);
@@ -231,17 +226,13 @@ int socket(int domain, int type, int protocol)
     else
     {
         /* release fd */
-        fd_put(d);
-        fd_put(d);
+        dfs_fd_release(d);
 
         rt_set_errno(-ENOMEM);
 
         return -1;
     }
 
-    /* release the ref-count of fd */
-    fd_put(d);
-
     return fd;
 }
 RTM_EXPORT(socket);
@@ -259,7 +250,7 @@ int closesocket(int s)
         return -1;
     }
 
-    d = fd_get(s);
+    d = dfs_fd_get(s);
     if (d == RT_NULL)
     {
         rt_set_errno(-EBADF);
@@ -277,8 +268,7 @@ int closesocket(int s)
     }
 
     /* socket has been closed, delete it from file system fd */
-    fd_put(d);
-    fd_put(d);
+    dfs_fd_release(d);
 
     return error;
 }