nckernel  0.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
object.c
Go to the documentation of this file.
1 #include <sys/types.h>
2 #include <stddef.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <errno.h>
6 
7 #include <object.h>
8 
9 int object_init(struct object *object, struct object_ops *ops)
10 {
11  object->refcnt = 0;
12 
13  memcpy(&object->ops, ops, sizeof(*ops));
14  return 0;
15 }
16 
17 int object_create(struct object *object, void *data)
18 {
19  int ret;
20 
21  if (object->refcnt > 0) {
22  return -EINVAL;
23  }
24 
25  if (object->ops.create) {
26  ret = object->ops.create(object, data);
27  } else {
28  ret = 0;
29  }
30 
31  object->refcnt = 1;
32  return ret;
33 }
34 
35 int object_destroy(struct object *object)
36 {
37  int ret;
38 
39  if (object->refcnt <= 0) {
40  return -EINVAL;
41  }
42 
43  object->refcnt--;
44 
45  if (object->refcnt) {
46  return 0;
47  }
48 
49  if (object->ops.destroy) {
50  ret = object->ops.destroy(object);
51  } else {
52  ret = 0;
53  }
54 
55  return ret;
56 }
57 
58 int object_copy(struct object *dest, struct object *src)
59 {
60  int ret;
61 
62  if (src->refcnt <= 0) {
63  return -EINVAL;
64  }
65 
66  memcpy(dest, src, sizeof(*src));
67 
68  if (src->ops.copy) {
69  ret = src->ops.copy(dest, src);
70  } else {
71  ret = 0;
72  }
73 
74  dest->refcnt = 1;
75  return ret;
76 }
77 
78 struct object *object_link(struct object *object)
79 {
80  struct object *ret;
81 
82  if (object->refcnt <= 0) {
83  return NULL;
84  }
85 
86  if (object->ops.link) {
87  ret = object->ops.link(object);
88  } else {
89  ret = object;
90  }
91 
92  if (ret) {
93  object->refcnt++;
94  }
95 
96  return ret;
97 }
98 
99 /* End of a file */