Files
jsix/src/kernel/syscalls/helpers.h
Justin C. Miller cd9b85b555 [util] Replace kutil with util
Now that kutil has no kernel-specific code in it anymore, it can
actually be linked to by anything, so I'm renaming it 'util'.

Also, I've tried to unify the way that the system libraries from
src/libraries are #included using <> instead of "".

Other small change: util::bip_buffer got a spinlock to guard against
state corruption.
2022-01-03 00:03:29 -08:00

50 lines
1014 B
C++

#pragma once
/// \file syscalls/helpers.h
/// Utility functions for use in syscall handler implementations
#include <j6/types.h>
#include "objects/kobject.h"
#include "objects/process.h"
namespace syscalls {
template <typename T, typename... Args>
T * construct_handle(j6_handle_t *handle, Args... args)
{
process &p = process::current();
T *o = new T {args...};
*handle = p.add_handle(o);
return o;
}
template <typename T>
T * get_handle(j6_handle_t handle)
{
process &p = process::current();
kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != T::type)
return nullptr;
return static_cast<T*>(o);
}
template <>
inline kobject * get_handle<kobject>(j6_handle_t handle)
{
process &p = process::current();
return p.lookup_handle(handle);
}
template <typename T>
T * remove_handle(j6_handle_t handle)
{
T *o = get_handle<T>(handle);
if (o) {
process &p = process::current();
p.remove_handle(handle);
}
return o;
}
}