The Pedigree Project  0.1
modules/subsys/posix/util.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "modules/subsys/posix/FileDescriptor.h"
21 #include "modules/subsys/posix/PosixSubsystem.h"
22 #include "pedigree/kernel/process/Process.h"
23 #include "pedigree/kernel/processor/Processor.h"
24 
25 #ifdef UTILITY_LINUX
26 #include <vector>
27 
28 std::vector<FileDescriptor *> g_Descriptors;
29 
30 FileDescriptor *getDescriptor(int fd)
31 {
32  if (fd >= g_Descriptors.size())
33  {
34  return nullptr;
35  }
36 
37  return g_Descriptors[fd];
38 }
39 
40 void addDescriptor(int fd, FileDescriptor *f)
41 {
42  FileDescriptor *old = getDescriptor(fd);
43  if (old)
44  {
45  delete old;
46  }
47 
48  if (fd > g_Descriptors.capacity())
49  {
50  g_Descriptors.reserve(fd + 1);
51  }
52 
53  g_Descriptors.insert(g_Descriptors.begin() + fd, f);
54 }
55 
56 size_t getAvailableDescriptor()
57 {
58  return g_Descriptors.size();
59 }
60 #else
61 PosixSubsystem *getSubsystem()
63 {
64  Process *pProcess =
65  Processor::information().getCurrentThread()->getParent();
66  PosixSubsystem *pSubsystem =
67  reinterpret_cast<PosixSubsystem *>(pProcess->getSubsystem());
68  if (!pSubsystem)
69  {
70  ERROR("No subsystem for this process!");
71  return nullptr;
72  }
73 
74  return pSubsystem;
75 }
76 
77 FileDescriptor *getDescriptor(int fd)
78 {
79  PosixSubsystem *pSubsystem = getSubsystem();
80  return pSubsystem->getFileDescriptor(fd);
81 }
82 
83 void addDescriptor(int fd, FileDescriptor *f)
84 {
85  PosixSubsystem *pSubsystem = getSubsystem();
86  pSubsystem->addFileDescriptor(fd, f);
87 }
88 
89 size_t getAvailableDescriptor()
90 {
91  PosixSubsystem *pSubsystem = getSubsystem();
92  return pSubsystem->getFd();
93 }
94 #endif
static ProcessorInformation & information()
Definition: Processor.cc:45
void addFileDescriptor(size_t fd, FileDescriptor *pFd)
#define ERROR(text)
Definition: Log.h:82
FileDescriptor * getFileDescriptor(size_t fd)