1 /*
2  * (c) 2010 Alexander Warg <warg@os.inf.tu-dresden.de>
3  *     economic rights: Technische Universität Dresden (Germany)
4  *
5  * This file is part of TUD:OS and distributed under the terms of the
6  * GNU General Public License 2.
7  * Please see the COPYING-GPL-2 file for details.
8  *
9  * As a special exception, you may use this file as part of a free software
10  * library without restriction.  Specifically, if other files instantiate
11  * templates or use macros or inline functions from this file, or you compile
12  * this file and link it with other files to produce an executable, this
13  * file does not by itself cause the resulting executable to be covered by
14  * the GNU General Public License.  This exception does not however
15  * invalidate any other reasons why the executable file might be covered by
16  * the GNU General Public License.
17  */
18 #pragma once
19 
20 #include <l4/l4re_vfs/vfs.h>
21 
22 namespace L4Re { namespace Core {
23 
24 using cxx::Ref_ptr;
25 
26 class Fd_store
27 {
28 public:
29   enum { MAX_FILES = 50 };
30 
throw()31   Fd_store() throw() : _fd_hint(0) {}
32 
33   int alloc() throw();
34   void free(int fd) throw();
35   bool check_fd(int fd) throw();
36   Ref_ptr<L4Re::Vfs::File> get(int fd) throw();
37   void set(int fd, Ref_ptr<L4Re::Vfs::File> const &f) throw();
38 
39 private:
40   int _fd_hint;
41   Ref_ptr<L4Re::Vfs::File> _files[MAX_FILES];
42 };
43 
44 inline
45 bool
check_fd(int fd)46 Fd_store::check_fd(int fd) throw()
47 {
48   return fd >= 0 && fd < MAX_FILES;
49 }
50 
51 inline
52 Ref_ptr<L4Re::Vfs::File>
get(int fd)53 Fd_store::get(int fd) throw()
54 {
55   if (check_fd(fd))
56     return _files[fd];
57 
58   return Ref_ptr<>::Nil;
59 }
60 
61 inline
62 void
set(int fd,Ref_ptr<L4Re::Vfs::File> const & f)63 Fd_store::set(int fd, Ref_ptr<L4Re::Vfs::File> const &f) throw()
64 {
65   _files[fd] = f;
66 }
67 
68 }}
69