root/Whitix/trunk/include/keobject.h

Revision 1993, 4.9 KB (checked in by mwhitworth, 3 years ago)

Remove attributes from objects, now added by higher level code.

Line 
1/*****************************************************************************
2 *                                                                                                                                               *
3 * kobject.h - Kernel object infrastructure                                                              *
4 *                                                                                                                                               *
5 ****************************************************************************/
6
7/*  This file is part of Whitix.
8 *
9 *  Whitix is free software; you can redistribute it and/or modify
10 *  it under the terms of the GNU General Public License as published by
11 *  the Free Software Foundation; either version 2 of the License, or
12 *  (at your option) any later version.
13 *
14 *  Whitix is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with Whitix; if not, write to the Free Software
21 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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
35struct IcObject;
36struct KeObject;
37struct KeSet;
38
39struct 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
50struct KeObject
51{
52        /* The object's name; this can be null. */
53        char* name;
54       
55        int refs;
56       
57        /* The parent set. Every KeObject has a parent set, since they link to the
58         * object's type. Even KeObjects that are not linked to the filesystem must
59         * have a parent KeSet.
60         */
61        struct KeSet* parent;
62       
63        /* dir is the directory containing all the object attributes in icfs */
64        struct KeFsEntry* dir;
65       
66        /* Used in the parent KeSet list */
67        struct ListHead next;
68};
69
70struct KeSet
71{
72        struct ListHead head;
73        Spinlock spinLock;
74        struct KeObject object;
75        struct KeFsEntry* devDir; /* TODO: Create bus and class structures, use those instead. */
76        struct KeObjType* type;
77};
78
79/* Prototypes */
80void KeObjectInit(struct KeObject* object, struct KeSet* parent);
81void KeObjectFree(struct KeObject* object);
82
83int KeObjectAttach(struct KeObject* object, const char* name, ...);
84int KeObjectVaAttach(struct KeObject* object, const char* name, VaList vaList);
85int KeObjectRemove(struct KeObject* object);
86
87static 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/* All tests can probably be debug code */
104
105/* Get the type most specific to the object. */
106static inline struct KeObjType* KeObjectGetType(struct KeObject* object)
107{
108        if (!object)
109                return NULL;
110               
111        return object->parent->type;
112}
113
114static inline void KeObjectSetType(struct KeObject* object, struct KeObjType* type)
115{
116}
117
118static inline int KeObjGet(struct KeObject* object)
119{
120        if (!object)
121                return NULL;
122               
123        object->refs++;
124       
125        return object->refs;
126}
127
128static 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
144static 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
158static 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/* KeObjGetParentDir
170 * -----------------
171 *
172 * Since every KeObject has a KeSet as a parent, we can use this fact to get
173 * the DevFs directory that the KeObject should go in easily.
174 *
175 * TODO: Like elsewhere, create bus and class hierarchies.
176 */
177
178static 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
186static inline struct KeSet* KeObjGetSet(struct KeObject* object)
187{
188        return object->parent;
189}
190
191/* KeSet API */
192void KeSetInit(struct KeSet* set, struct KeSet* parent, struct KeObjType* type);
193int KeSetAttach(struct KeSet* set, const char* name, ...);
194
195static 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/* KeSetAdd
204 *
205 * Add the KeObject object to the KeSet set.
206 */
207 
208static 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
220struct KeObject* KeSetFind(struct KeSet* set, const char* name);
221
222#endif
Note: See TracBrowser for help on using the browser.