resource_limit.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <memory>
  7. #include "common/common_types.h"
  8. #include "core/hle/kernel/object.h"
  9. union ResultCode;
  10. namespace Kernel {
  11. class KernelCore;
  12. enum class ResourceType : u32 {
  13. PhysicalMemory,
  14. Threads,
  15. Events,
  16. TransferMemory,
  17. Sessions,
  18. // Used as a count, not an actual type.
  19. ResourceTypeCount
  20. };
  21. constexpr bool IsValidResourceType(ResourceType type) {
  22. return type < ResourceType::ResourceTypeCount;
  23. }
  24. class ResourceLimit final : public Object {
  25. public:
  26. explicit ResourceLimit(KernelCore& kernel);
  27. ~ResourceLimit() override;
  28. /// Creates a resource limit object.
  29. static std::shared_ptr<ResourceLimit> Create(KernelCore& kernel);
  30. std::string GetTypeName() const override {
  31. return "ResourceLimit";
  32. }
  33. std::string GetName() const override {
  34. return GetTypeName();
  35. }
  36. static constexpr HandleType HANDLE_TYPE = HandleType::ResourceLimit;
  37. HandleType GetHandleType() const override {
  38. return HANDLE_TYPE;
  39. }
  40. bool Reserve(ResourceType resource, s64 amount);
  41. bool Reserve(ResourceType resource, s64 amount, u64 timeout);
  42. void Release(ResourceType resource, u64 amount);
  43. void Release(ResourceType resource, u64 used_amount, u64 available_amount);
  44. /**
  45. * Gets the current value for the specified resource.
  46. * @param resource Requested resource type
  47. * @returns The current value of the resource type
  48. */
  49. s64 GetCurrentResourceValue(ResourceType resource) const;
  50. /**
  51. * Gets the max value for the specified resource.
  52. * @param resource Requested resource type
  53. * @returns The max value of the resource type
  54. */
  55. s64 GetMaxResourceValue(ResourceType resource) const;
  56. /**
  57. * Sets the limit value for a given resource type.
  58. *
  59. * @param resource The resource type to apply the limit to.
  60. * @param value The limit to apply to the given resource type.
  61. *
  62. * @return A result code indicating if setting the limit value
  63. * was successful or not.
  64. *
  65. * @note The supplied limit value *must* be greater than or equal to
  66. * the current resource value for the given resource type,
  67. * otherwise ERR_INVALID_STATE will be returned.
  68. */
  69. ResultCode SetLimitValue(ResourceType resource, s64 value);
  70. private:
  71. // TODO(Subv): Increment resource limit current values in their respective Kernel::T::Create
  72. // functions
  73. //
  74. // Currently we have no way of distinguishing if a Create was called by the running application,
  75. // or by a service module. Approach this once we have separated the service modules into their
  76. // own processes
  77. using ResourceArray =
  78. std::array<s64, static_cast<std::size_t>(ResourceType::ResourceTypeCount)>;
  79. ResourceArray limit{};
  80. ResourceArray current{};
  81. ResourceArray available{};
  82. };
  83. } // namespace Kernel