#include #include #include #define HAS_OP(obj, opname) ((obj)->ob_type->ob_ops.opname) static struct queue object_types; static spin_lock_t object_types_lock = SPIN_LOCK_INIT; static koid_t koid_alloc(void) { static koid_t counter = 0; static spin_lock_t lock = SPIN_LOCK_INIT; unsigned long flags; spin_lock_irqsave(&lock, &flags); koid_t result = counter; counter++; spin_unlock_irqrestore(&lock, flags); return result; } kern_status_t object_bootstrap(void) { return KERN_OK; } kern_status_t object_type_register(struct object_type *p) { unsigned long flags; spin_lock_irqsave(&object_types_lock, &flags); queue_push_back(&object_types, &p->ob_list); spin_unlock_irqrestore(&object_types_lock, flags); p->ob_cache.c_name = p->ob_name; p->ob_cache.c_obj_size = p->ob_size; vm_cache_init(&p->ob_cache); p->ob_flags |= OBJTYPE_INIT; return KERN_OK; } kern_status_t object_type_unregister(struct object_type *p) { unsigned long flags; spin_lock_irqsave(&object_types_lock, &flags); queue_delete(&object_types, &p->ob_list); spin_unlock_irqrestore(&object_types_lock, flags); return KERN_OK; } struct object *object_create(struct object_type *type) { if (!(type->ob_flags & OBJTYPE_INIT)) { return NULL; } struct vm_cache *cache = &type->ob_cache; void *obj_buf = vm_cache_alloc(cache, 0); if (!obj_buf) { return NULL; } struct object *obj = (struct object *)((unsigned char *)obj_buf + type->ob_header_offset); obj->ob_id = koid_alloc(); obj->ob_type = type; obj->ob_lock = SPIN_LOCK_INIT; obj->ob_magic = OBJECT_MAGIC; obj->ob_refcount = 1; obj->ob_handles = 0; return obj; } struct object *object_ref(struct object *obj) { obj->ob_refcount++; return obj; } static void __cleanup(struct object *obj, struct queue *queue) { if (HAS_OP(obj, destroy)) { obj->ob_type->ob_ops.destroy(obj, queue); } vm_cache_free(&obj->ob_type->ob_cache, obj); } static void object_cleanup(struct object *obj, unsigned long flags) { if (obj->ob_refcount > 0 || obj->ob_handles > 0) { spin_unlock_irqrestore(&obj->ob_lock, flags); return; } struct queue queue = QUEUE_INIT; __cleanup(obj, &queue); if (!HAS_OP(obj, destroy_recurse)) { return; } while (!queue_empty(&queue)) { struct queue_entry *entry = queue_pop_front(&queue); struct object *child = NULL; obj->ob_type->ob_ops.destroy_recurse(entry, &child); if (!child) { continue; } if (child->ob_refcount > 1) { child->ob_refcount--; continue; } if (child->ob_refcount == 0 && child->ob_handles == 0) { __cleanup(child, &queue); } } } void object_unref(struct object *obj) { unsigned long flags; spin_lock_irqsave(&obj->ob_lock, &flags); if (obj->ob_refcount == 0) { spin_unlock_irqrestore(&obj->ob_lock, flags); return; } obj->ob_refcount--; object_cleanup(obj, flags); } void object_add_handle(struct object *obj) { obj->ob_handles++; } void object_remove_handle(struct object *obj) { unsigned long flags; spin_lock_irqsave(&obj->ob_lock, &flags); if (obj->ob_handles == 0) { spin_unlock_irqrestore(&obj->ob_lock, flags); return; } obj->ob_handles--; object_cleanup(obj, flags); } void object_lock(struct object *obj) { spin_lock(&obj->ob_lock); } void object_unlock(struct object *obj) { spin_unlock(&obj->ob_lock); } void object_lock_irqsave(struct object *obj, unsigned long *flags) { spin_lock_irqsave(&obj->ob_lock, flags); } void object_unlock_irqrestore(struct object *obj, unsigned long flags) { spin_unlock_irqrestore(&obj->ob_lock, flags); } void object_lock_pair_irqsave( struct object *a, struct object *b, unsigned long *flags) { if (a == b) { object_lock_irqsave(a, flags); } else if (a < b) { object_lock_irqsave(a, flags); object_lock(b); } else { object_lock_irqsave(b, flags); object_lock(a); } } void object_unlock_pair_irqrestore( struct object *a, struct object *b, unsigned long flags) { if (a == b) { object_unlock_irqrestore(a, flags); } else if (a < b) { object_unlock(b); object_unlock_irqrestore(a, flags); } else { object_unlock(a); object_unlock_irqrestore(b, flags); } } void *object_data(struct object *obj) { return (char *)obj + sizeof *obj; } struct object *object_header(void *p) { struct object *obj = (struct object *)((char *)p - sizeof *obj); if (obj->ob_magic != OBJECT_MAGIC) { return NULL; } return obj; }