-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFdObject.h
70 lines (55 loc) · 1.23 KB
/
FdObject.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma once
#include "CppUtils.h"
#include <Referenceable.h>
#include <util/AVLTree.h>
#include <AutoDeleterPosix.h>
#include <locks.h>
class FdObject: public BReferenceable {
public:
struct Key {
dev_t dev;
ino_t ino;
inline int Compare(const Key &b) const
{
if (dev < b.dev) return -1;
if (dev > b.dev) return 1;
if (ino < b.ino) return -1;
if (ino > b.ino) return 1;
return 0;
}
};
private:
FileDescriptorCloser fFd;
Key fKey{};
AVLTreeNode fNode;
struct NodeDef {
typedef FdObject::Key Key;
typedef FdObject Value;
inline AVLTreeNode* GetAVLTreeNode(Value* value) const
{
return &value->fNode;
}
inline Value* GetValue(AVLTreeNode* node) const
{
return &ContainerOf(*node, &FdObject::fNode);
}
inline int Compare(const Key& a, const Value* b) const
{
return a.Compare(b->fKey);
}
inline int Compare(const Value* a, const Value* b) const
{
return a->fKey.Compare(b->fKey);
}
};
static AVLTree<NodeDef> fMap;
mutable mutex fLock = MUTEX_INITIALIZER("FdObject");
static mutex fMapLock;
void SetFd(int fd);
public:
virtual ~FdObject();
static FdObject *Lookup(const Key &key);
int GetFd() const;
int AllocFd();
inline Key const &GetKey() const {return fKey;}
};