summaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-04-19 22:08:50 -0400
committerIan Moffett <ian@osmora.org>2024-04-20 15:51:51 -0400
commit852b00cbc75b83390984a77048e443a43dd0ae48 (patch)
tree6498eb754b4389fcb0964e97c247c7ae2bcc3740 /sys
parent7c8707f06fd8d5953b7f4721f6625b87d3225f8e (diff)
kernel: vm: Add vm_object refcount
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'sys')
-rw-r--r--sys/include/vm/obj.h8
-rw-r--r--sys/vm/vm_obj.c5
2 files changed, 13 insertions, 0 deletions
diff --git a/sys/include/vm/obj.h b/sys/include/vm/obj.h
index 6129889..c1c2f17 100644
--- a/sys/include/vm/obj.h
+++ b/sys/include/vm/obj.h
@@ -42,9 +42,17 @@ struct vm_object {
struct vm_pagerops *pgops; /* Pager operations */
uint8_t is_anon : 1; /* Is an anonymous mapping */
+ int ref; /* Ref count */
struct vnode *vnode; /* Only used if `is_anon` is 0 */
};
+#define vm_object_ref(OBJPTR) (++(OBJPTR)->ref)
+#define vm_object_unref(OBJPTR) do { \
+ if ((OBJPTR)->ref > 1) { \
+ --(OBJPTR)->ref; \
+ } \
+ } while (0);
+
int vm_obj_init(struct vm_object **res, struct vnode *vnode);
int vm_obj_destroy(struct vm_object *obj);
diff --git a/sys/vm/vm_obj.c b/sys/vm/vm_obj.c
index 589ca47..7487b47 100644
--- a/sys/vm/vm_obj.c
+++ b/sys/vm/vm_obj.c
@@ -59,6 +59,7 @@ vm_obj_init(struct vm_object **res, struct vnode *vnode)
memset(obj, 0, sizeof(struct vm_object));
obj->vnode = vnode;
+ obj->ref = 1;
vm_set_pgops(obj, vnode);
*res = obj;
@@ -73,6 +74,10 @@ vm_obj_destroy(struct vm_object *obj)
if (vp->vmobj != NULL)
vp->vmobj = NULL;
+ /* Check the ref count */
+ if (obj->ref > 1)
+ return -EBUSY;
+
dynfree(obj);
return 0;
}