Max OS 0.3
Loading...
Searching...
No Matches
resource.h
Go to the documentation of this file.
1
9#ifndef MAXOS_PROCESSES_RESOURCES_RESOURCE_H
10#define MAXOS_PROCESSES_RESOURCES_RESOURCE_H
11
12#include <cstddef>
13
14#include <libcommon/map.h>
15#include <libcommon/vector.h>
16#include <libcommon/string.h>
17#include <libcommon/mem.h>
18#include <libkpi/syscalls.h>
19#include <libkpi/resource.h>
20
21#include <common/logger.h>
22
23namespace MaxOS::processes::resources {
24
25 typedef KPI::ResourceType resource_type_t;
26 typedef KPI::Resource Resource;
27
33
34 protected:
35 common::Map<string, Resource*> m_resources;
36 common::Map<string, uint64_t> m_resource_uses;
37
38 common::Spinlock m_lock;
39
41
42 public:
45
47
48 bool register_resource(Resource* resource);
49 void close_resource(Resource* resource, size_t flags);
50
51 virtual Resource* get_resource(const string& name);
52 virtual Resource* create_resource(const string& name, size_t flags, uintptr_t data);
53 };
54
61 template<class Type> class ResourceRegistry : public BaseResourceRegistry {
62
63 public:
65 ~ResourceRegistry() = default;
66
74 Resource* create_resource(const string& name, size_t flags, uintptr_t data) override {
75
76 auto resource = new Type(name, flags, type());
77
78 // Creation failed
79 if(!register_resource(resource)) {
80 delete resource;
81 return nullptr;
82 }
83
84 return resource;
85 }
86 };
87
96
102
103 private:
104 common::Map<resource_type_t, BaseResourceRegistry*> m_registries;
105 inline static GlobalResourceRegistry* s_current;
106
107 public:
110
112
113 static void add_registry(resource_type_t type, BaseResourceRegistry* registry);
114 static void remove_registry(BaseResourceRegistry* registry);
115 };
116
122
123 private:
124 common::Map<uint64_t, Resource*> m_resources;
125
126 uint64_t m_next_handle = 1;
127
128 public:
131
132 common::Map<uint64_t, Resource*> resources();
133
134 uint64_t open_resource(resource_type_t type, const string& name, size_t flags);
135 void close_resource(uint64_t handle, size_t flags);
136
137 Resource* get_resource(uint64_t handle);
138 Resource* get_resource(const string& name);
139 };
140}
141
142
143#endif //MAXOS_PROCESSES_RESOURCES_RESOURCE_H
Manages the creation, retention and destruction of resources of a certain type. Should be subclassed ...
Definition resource.h:32
resource_type_t m_type
The resource type that this registry manages.
Definition resource.h:40
resource_type_t type()
Gets the type of resources this registry handles.
Definition resource.cpp:41
common::Spinlock m_lock
The lock to make the registry threadsafe.
Definition resource.h:38
virtual Resource * get_resource(const string &name)
Gets a resource from the registry by name.
Definition resource.cpp:52
void close_resource(Resource *resource, size_t flags)
Close the resource provided, if it exists in this registry.
Definition resource.cpp:100
~BaseResourceRegistry()
Destructor for BaseResourceRegistry, removes it from the global list.
Definition resource.cpp:31
common::Map< string, uint64_t > m_resource_uses
The map of resource names to how many processes are using them.
Definition resource.h:36
common::Map< string, Resource * > m_resources
The map of resource names to resource instances.
Definition resource.h:35
bool register_resource(Resource *resource)
Registers a resource in the registry.
Definition resource.cpp:76
virtual Resource * create_resource(const string &name, size_t flags, uintptr_t data)
Try to create a new resource with the specified name, will fail if the name is in use.
Definition resource.cpp:134
Manages all the resource registries for each resource type.
Definition resource.h:101
static void add_registry(resource_type_t type, BaseResourceRegistry *registry)
Adds a registry to the global list if there is not already one for that type.
Definition resource.cpp:176
static BaseResourceRegistry * get_registry(resource_type_t type)
Gets a registry of a type.
Definition resource.cpp:161
~GlobalResourceRegistry()
Destructor for GlobalResourceRegistry, unsets it as the current global registry if it is.
Definition resource.cpp:149
static void remove_registry(BaseResourceRegistry *registry)
Adds a registry to the global list if there is not already one for that type.
Definition resource.cpp:190
GlobalResourceRegistry()
Constructs a new GlobalResourceRegistry and sets it as the current global registry.
Definition resource.cpp:141
Manages the open resources for a process.
Definition resource.h:121
Resource * get_resource(uint64_t handle)
Gets a resource based on the handle id.
Definition resource.cpp:282
void close_resource(uint64_t handle, size_t flags)
Un registers the resource and then closes it.
Definition resource.cpp:262
~ResourceManager()
Destructor for ResourceManager, closes all open resources.
Definition resource.cpp:205
common::Map< uint64_t, Resource * > resources()
Get the resources currently open.
Definition resource.cpp:223
uint64_t open_resource(resource_type_t type, const string &name, size_t flags)
Registers a resource with the resource manager and then opens it.
Definition resource.cpp:236
A resource registry for a specific resource type.
Definition resource.h:61
ResourceRegistry(resource_type_t type)
Constructor for ResourceRegistry of a specific type.
Definition resource.h:94
Resource * create_resource(const string &name, size_t flags, uintptr_t data) override
Create a new resource of the specific type.
Definition resource.h:74
Defines a Logger class for logging messages with different severity levels to multiple output streams...
KPI::Resource Resource
Alias to make the libsyscore Resource accessible here.
Definition resource.h:26
KPI::ResourceType resource_type_t
Alias to make the libsyscore ResourceType accessible here.
Definition resource.h:25