| 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 | #ifndef KEOBJECT_H |
|---|
| 26 | #define KEOBJECT_H |
|---|
| 27 | |
|---|
| 28 | #include <llist.h> |
|---|
| 29 | #include <fs/icfs.h> |
|---|
| 30 | #include <fs/kfs.h> |
|---|
| 31 | #include <spinlock.h> |
|---|
| 32 | #include <typedefs.h> |
|---|
| 33 | #include <stdarg.h> |
|---|
| 34 | |
|---|
| 35 | struct IcObject; |
|---|
| 36 | struct KeObject; |
|---|
| 37 | struct KeSet; |
|---|
| 38 | |
|---|
| 39 | struct KeObjType |
|---|
| 40 | { |
|---|
| 41 | void (*release)(struct KeObject* object); |
|---|
| 42 | }; |
|---|
| 43 | |
|---|
| 44 | #define KE_OBJECT_TYPE(name, releaseFunc) \ |
|---|
| 45 | struct KeObjType name = \ |
|---|
| 46 | { \ |
|---|
| 47 | .release = releaseFunc, \ |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | struct KeObject |
|---|
| 51 | { |
|---|
| 52 | |
|---|
| 53 | char* name; |
|---|
| 54 | |
|---|
| 55 | int refs; |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | struct KeSet* parent; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | struct KeFsEntry* dir; |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | struct ListHead next; |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | struct KeSet |
|---|
| 71 | { |
|---|
| 72 | struct ListHead head; |
|---|
| 73 | Spinlock spinLock; |
|---|
| 74 | struct KeObject object; |
|---|
| 75 | struct KeFsEntry* devDir; |
|---|
| 76 | struct KeObjType* type; |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | void KeObjectInit(struct KeObject* object, struct KeSet* parent); |
|---|
| 81 | void KeObjectFree(struct KeObject* object); |
|---|
| 82 | |
|---|
| 83 | int KeObjectAttach(struct KeObject* object, const char* name, ...); |
|---|
| 84 | int KeObjectVaAttach(struct KeObject* object, const char* name, VaList vaList); |
|---|
| 85 | int KeObjectRemove(struct KeObject* object); |
|---|
| 86 | |
|---|
| 87 | static inline int KeObjectCreate(struct KeObject* object, struct KeSet* parent, const char* name, ...) |
|---|
| 88 | { |
|---|
| 89 | VaList vaList; |
|---|
| 90 | int err; |
|---|
| 91 | |
|---|
| 92 | KeObjectInit(object, parent); |
|---|
| 93 | |
|---|
| 94 | VaStart(vaList, name); |
|---|
| 95 | |
|---|
| 96 | err = KeObjectVaAttach(object, name, vaList); |
|---|
| 97 | |
|---|
| 98 | VaEnd(vaList); |
|---|
| 99 | |
|---|
| 100 | return err; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | static inline struct KeObjType* KeObjectGetType(struct KeObject* object) |
|---|
| 107 | { |
|---|
| 108 | if (!object) |
|---|
| 109 | return NULL; |
|---|
| 110 | |
|---|
| 111 | return object->parent->type; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | static inline void KeObjectSetType(struct KeObject* object, struct KeObjType* type) |
|---|
| 115 | { |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | static inline int KeObjGet(struct KeObject* object) |
|---|
| 119 | { |
|---|
| 120 | if (!object) |
|---|
| 121 | return NULL; |
|---|
| 122 | |
|---|
| 123 | object->refs++; |
|---|
| 124 | |
|---|
| 125 | return object->refs; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | static inline int KeObjPut(struct KeObject* object) |
|---|
| 129 | { |
|---|
| 130 | if (!object) |
|---|
| 131 | return NULL; |
|---|
| 132 | |
|---|
| 133 | object->refs--; |
|---|
| 134 | |
|---|
| 135 | if (object->refs == 0) |
|---|
| 136 | { |
|---|
| 137 | KeObjectFree(object); |
|---|
| 138 | return 0; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | return object->refs; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | static inline const char* KeObjGetName(struct KeObject* object) |
|---|
| 145 | { |
|---|
| 146 | if (!object) |
|---|
| 147 | return NULL; |
|---|
| 148 | |
|---|
| 149 | if (object->name) |
|---|
| 150 | return object->name; |
|---|
| 151 | |
|---|
| 152 | if (!object->dir) |
|---|
| 153 | return NULL; |
|---|
| 154 | |
|---|
| 155 | return object->dir->name; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | static inline struct KeFsEntry* KeObjGetParentDir(struct KeObject* object) |
|---|
| 159 | { |
|---|
| 160 | if (!object) |
|---|
| 161 | return NULL; |
|---|
| 162 | |
|---|
| 163 | if (!object->parent) |
|---|
| 164 | return NULL; |
|---|
| 165 | |
|---|
| 166 | return object->parent->object.dir; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | static inline struct KeFsDir* KeObjGetParentDevDir(struct KeObject* object) |
|---|
| 179 | { |
|---|
| 180 | if (!object || !object->parent || !object->parent->devDir) |
|---|
| 181 | return NULL; |
|---|
| 182 | |
|---|
| 183 | return &object->parent->devDir->dir; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | static inline struct KeSet* KeObjGetSet(struct KeObject* object) |
|---|
| 187 | { |
|---|
| 188 | return object->parent; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | void KeSetInit(struct KeSet* set, struct KeSet* parent, struct KeObjType* type); |
|---|
| 193 | int KeSetAttach(struct KeSet* set, const char* name, ...); |
|---|
| 194 | |
|---|
| 195 | static inline int KeSetCreate(struct KeSet* set, struct KeSet* parent, |
|---|
| 196 | struct KeObjType* type, const char* name) |
|---|
| 197 | { |
|---|
| 198 | KeSetInit(set, parent, type); |
|---|
| 199 | |
|---|
| 200 | return KeSetAttach(set, name); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | static inline void KeSetAdd(struct KeSet* set, struct KeObject* object) |
|---|
| 209 | { |
|---|
| 210 | if (!set) |
|---|
| 211 | return; |
|---|
| 212 | |
|---|
| 213 | KeObjGet(&set->object); |
|---|
| 214 | |
|---|
| 215 | SpinLock(&set->spinLock); |
|---|
| 216 | ListAddTail(&object->next, &set->head); |
|---|
| 217 | SpinUnlock(&set->spinLock); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | struct KeObject* KeSetFind(struct KeSet* set, const char* name); |
|---|
| 221 | |
|---|
| 222 | #endif |
|---|