IBNOS
object.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Michael Müller
3  * Copyright (c) 2014, Sebastian Lackner
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #ifndef _H_OBJECT_
29 #define _H_OBJECT_
30 
31 #ifdef __KERNEL__
32 
33  struct object;
34 
35  #include <stddef.h>
36  #include <stdint.h>
37  #include <stdbool.h>
38 
39  #include <util/util.h>
40  #include <util/list.h>
41 
43  {
44  /* destructor */
45  void (*destroy)(struct object *obj);
46  /* getMinHandle */
47  uint32_t (*getMinHandle)(struct object *obj);
48  /* shutdown */
49  void (*shutdown)(struct object *obj, uint32_t mode);
50  /* getStatus */
51  int32_t (*getStatus)(struct object *obj, uint32_t mode);
52  /* wait */
53  struct linkedList* (*wait)(struct object *obj, uint32_t mode, uint32_t *result);
54  /* signal */
55  void (*signal)(struct object *obj, uint32_t result);
56  /* write */
57  int32_t (*write)(struct object *obj, uint8_t *buf, uint32_t length);
58  /* read */
59  int32_t (*read)(struct object *obj, uint8_t *buf, uint32_t length);
60  /* attachObj */
61  bool (*attachObj)(struct object *obj, struct object *subObj, uint32_t mode, uint32_t ident);
62  /* remove */
63  bool (*detachObj)(struct object *obj, uint32_t ident);
64  };
65 
66  #define objectContainer(p, type, functions) \
67  ((type *)((uint8_t *)__objectCheckType((p), (functions)) - offsetof(type, obj)))
68 
69  struct object
70  {
71  uint32_t refcount;
72  const struct objectFunctions *functions;
73  struct linkedList entry; /* entry which is used if other applications might wait on it */
74  };
75 
76  static inline void __objectInit(struct object *obj, const struct objectFunctions *functions)
77  {
78  obj->refcount = 1;
79  obj->functions = functions;
80  /* no need to initialize obj->entry */
81  }
82 
83  static inline struct object *__objectCheckType(struct object *obj, const struct objectFunctions *functions)
84  {
85  assert(obj && obj->functions == functions);
86  return obj;
87  }
88 
89  #define objectAddRef(p) __objectAddRef(&(p)->obj)
90  static inline struct object *__objectAddRef(struct object *obj)
91  {
92  assert(obj);
93  obj->refcount++;
94  return obj;
95  }
96 
97  #define objectRelease(p) __objectRelease(&(p)->obj)
98  static inline void __objectRelease(struct object *obj)
99  {
100  assert(obj);
101  if (--obj->refcount) return;
102  if (obj->functions->destroy) obj->functions->destroy(obj);
103  }
104 
105  #define objectGetMinHandle(p) __objectGetMinHandle(&(p)->obj)
106  static inline uint32_t __objectGetMinHandle(struct object *obj)
107  {
108  return obj->functions->getMinHandle ? obj->functions->getMinHandle(obj) : 3;
109  }
110 
111  #define objectShutdown(p, a) __objectShutdown(&(p)->obj, a)
112  static inline void __objectShutdown(struct object *obj, uint32_t mode)
113  {
114  if (obj->functions->shutdown) obj->functions->shutdown(obj, mode);
115  }
116 
117  #define objectGetStatus(p, a) __objectGetStatus(&(p)->obj, a)
118  static inline int32_t __objectGetStatus(struct object *obj, uint32_t mode)
119  {
120  return obj->functions->getStatus ? obj->functions->getStatus(obj, mode) : (-1);
121  }
122 
123  #define objectWait(p, a, b) __objectWait(&(p)->obj, a, b)
124  static inline struct linkedList *__objectWait(struct object *obj, uint32_t mode, uint32_t *result)
125  {
126  return obj->functions->wait ? obj->functions->wait(obj, mode, result) : NULL;
127  }
128 
129  #define objectSignal(p, a) __objectSignal(&(p)->obj, a)
130  static inline void __objectSignal(struct object *obj, uint32_t result)
131  {
132  if (obj->functions->signal) obj->functions->signal(obj, result);
133  }
134 
135  #define objectWrite(p, a, b) __objectWrite(&(p)->obj, a, b)
136  static inline int32_t __objectWrite(struct object *obj, uint8_t *buf, uint32_t length)
137  {
138  return obj->functions->write ? obj->functions->write(obj, buf, length) : (-1);
139  }
140 
141  #define objectRead(p, a, b) __objectRead(&(p)->obj, a, b)
142  static inline int32_t __objectRead(struct object *obj, uint8_t *buf, uint32_t length)
143  {
144  return obj->functions->read ? obj->functions->read(obj, buf, length) : (-1);
145  }
146 
147  #define objectAttachObj(p, a, b, c) __objectAttachObj(&(p)->obj, a, b, c)
148  static inline bool __objectAttachObj(struct object *obj, struct object *subObj, uint32_t mode, uint32_t ident)
149  {
150  return obj->functions->attachObj ? obj->functions->attachObj(obj, subObj, mode, ident) : false;
151  }
152 
153  #define objectDetachObj(p, a) __objectDetachObj(&(p)->obj, a)
154  static inline bool __objectDetachObj(struct object *obj, uint32_t ident)
155  {
156  return obj->functions->detachObj ? obj->functions->detachObj(obj, ident) : false;
157  }
158 
159  static inline void queueWakeup(struct linkedList *queue, bool all, uint32_t eax)
160  {
161  struct object *obj, *__obj;
162  LL_FOR_EACH_SAFE(obj, __obj, queue, struct object, entry)
163  {
164  __objectSignal(obj, eax);
165  if (!all) break;
166  }
167  }
168 
169 #endif
170 
171 #endif /* _H_OBJECT_ */
const struct objectFunctions * functions
Definition: object.h:72
void(* shutdown)(struct object *obj, uint32_t mode)
Definition: object.h:49
#define assert(ex)
Definition: util.h:61
int32_t(* read)(struct object *obj, uint8_t *buf, uint32_t length)
Definition: object.h:59
bool(* attachObj)(struct object *obj, struct object *subObj, uint32_t mode, uint32_t ident)
Definition: object.h:61
int32_t(* write)(struct object *obj, uint8_t *buf, uint32_t length)
Definition: object.h:57
Definition: object.h:69
int32_t(* getStatus)(struct object *obj, uint32_t mode)
Definition: object.h:51
uint32_t refcount
Definition: object.h:71
struct linkedList *(* wait)(struct object *obj, uint32_t mode, uint32_t *result)
Definition: object.h:53
bool(* detachObj)(struct object *obj, uint32_t ident)
Definition: object.h:63
uint32_t(* getMinHandle)(struct object *obj)
Definition: object.h:47
void(* destroy)(struct object *obj)
Definition: object.h:45
void(* signal)(struct object *obj, uint32_t result)
Definition: object.h:55
#define LL_FOR_EACH_SAFE(element, next_element, list, type, field)
Allows to iterate a linkedList similar to a for-loop (safe when deleting elements).
Definition: list.h:148
char mode[8]
Definition: filesystem.h:65
uint32_t eax
Definition: context.h:189