2015-05-12 22:25:15 +02:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-11-19 18:54:06 +01:00
|
|
|
#include <array>
|
2019-12-08 04:09:20 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2015-05-12 22:25:15 +02:00
|
|
|
#include "common/common_types.h"
|
2018-08-02 04:40:00 +02:00
|
|
|
#include "core/hle/kernel/object.h"
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2018-11-19 18:54:06 +01:00
|
|
|
union ResultCode;
|
|
|
|
|
2015-05-12 22:25:15 +02:00
|
|
|
namespace Kernel {
|
|
|
|
|
2018-08-28 18:30:33 +02:00
|
|
|
class KernelCore;
|
|
|
|
|
2018-11-27 00:48:07 +01:00
|
|
|
enum class ResourceType : u32 {
|
2018-11-19 18:54:06 +01:00
|
|
|
PhysicalMemory,
|
|
|
|
Threads,
|
|
|
|
Events,
|
|
|
|
TransferMemory,
|
|
|
|
Sessions,
|
|
|
|
|
|
|
|
// Used as a count, not an actual type.
|
|
|
|
ResourceTypeCount
|
2015-05-12 22:25:15 +02:00
|
|
|
};
|
|
|
|
|
2018-11-27 00:48:07 +01:00
|
|
|
constexpr bool IsValidResourceType(ResourceType type) {
|
|
|
|
return type < ResourceType::ResourceTypeCount;
|
|
|
|
}
|
|
|
|
|
2015-05-12 22:25:15 +02:00
|
|
|
class ResourceLimit final : public Object {
|
|
|
|
public:
|
2019-11-25 02:15:51 +01:00
|
|
|
explicit ResourceLimit(KernelCore& kernel);
|
|
|
|
~ResourceLimit() override;
|
|
|
|
|
2019-04-01 22:46:00 +02:00
|
|
|
/// Creates a resource limit object.
|
2019-11-25 02:15:51 +01:00
|
|
|
static std::shared_ptr<ResourceLimit> Create(KernelCore& kernel);
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
std::string GetTypeName() const override {
|
|
|
|
return "ResourceLimit";
|
|
|
|
}
|
|
|
|
std::string GetName() const override {
|
2019-04-01 22:46:00 +02:00
|
|
|
return GetTypeName();
|
2016-09-18 02:38:01 +02:00
|
|
|
}
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2019-04-11 22:30:52 +02:00
|
|
|
static constexpr HandleType HANDLE_TYPE = HandleType::ResourceLimit;
|
2016-09-18 02:38:01 +02:00
|
|
|
HandleType GetHandleType() const override {
|
|
|
|
return HANDLE_TYPE;
|
|
|
|
}
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2020-03-23 20:30:22 +01:00
|
|
|
bool Reserve(ResourceType resource, s64 amount);
|
|
|
|
bool Reserve(ResourceType resource, s64 amount, u64 timeout);
|
|
|
|
void Release(ResourceType resource, u64 amount);
|
|
|
|
void Release(ResourceType resource, u64 used_amount, u64 available_amount);
|
|
|
|
|
2015-05-12 22:25:15 +02:00
|
|
|
/**
|
|
|
|
* Gets the current value for the specified resource.
|
|
|
|
* @param resource Requested resource type
|
|
|
|
* @returns The current value of the resource type
|
|
|
|
*/
|
2018-11-19 18:54:06 +01:00
|
|
|
s64 GetCurrentResourceValue(ResourceType resource) const;
|
2015-05-12 22:25:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the max value for the specified resource.
|
|
|
|
* @param resource Requested resource type
|
|
|
|
* @returns The max value of the resource type
|
|
|
|
*/
|
2018-11-19 18:54:06 +01:00
|
|
|
s64 GetMaxResourceValue(ResourceType resource) const;
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2018-11-19 18:54:06 +01:00
|
|
|
/**
|
|
|
|
* Sets the limit value for a given resource type.
|
|
|
|
*
|
|
|
|
* @param resource The resource type to apply the limit to.
|
|
|
|
* @param value The limit to apply to the given resource type.
|
|
|
|
*
|
|
|
|
* @return A result code indicating if setting the limit value
|
|
|
|
* was successful or not.
|
|
|
|
*
|
|
|
|
* @note The supplied limit value *must* be greater than or equal to
|
|
|
|
* the current resource value for the given resource type,
|
|
|
|
* otherwise ERR_INVALID_STATE will be returned.
|
|
|
|
*/
|
|
|
|
ResultCode SetLimitValue(ResourceType resource, s64 value);
|
2015-05-25 20:34:09 +02:00
|
|
|
|
2018-11-19 18:54:06 +01:00
|
|
|
private:
|
|
|
|
// TODO(Subv): Increment resource limit current values in their respective Kernel::T::Create
|
|
|
|
// functions
|
|
|
|
//
|
2015-05-25 20:34:09 +02:00
|
|
|
// Currently we have no way of distinguishing if a Create was called by the running application,
|
2016-09-18 02:38:01 +02:00
|
|
|
// or by a service module. Approach this once we have separated the service modules into their
|
|
|
|
// own processes
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2018-11-19 18:54:06 +01:00
|
|
|
using ResourceArray =
|
|
|
|
std::array<s64, static_cast<std::size_t>(ResourceType::ResourceTypeCount)>;
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2020-03-23 20:30:22 +01:00
|
|
|
ResourceArray limit{};
|
|
|
|
ResourceArray current{};
|
|
|
|
ResourceArray available{};
|
2015-05-12 22:25:15 +02:00
|
|
|
};
|
|
|
|
|
2018-01-20 08:48:02 +01:00
|
|
|
} // namespace Kernel
|