1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_Limits.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46347 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn/config.h>
17 #if NN_PLATFORM_HAS_MMU
18 
19 #include <nn/os.h>
20 #include "os_Limits.h"
21 
22 namespace nn { namespace os { namespace detail {
23 
24     namespace
25     {
26         class AutoCloser
27         {
28         public:
AutoCloser(Handle h)29             AutoCloser(Handle h) : m_Handle(h)
30             {
31             }
~AutoCloser()32             ~AutoCloser()
33             {
34                 svc::CloseHandle(m_Handle);
35             }
36 
37         private:
38             Handle  m_Handle;
39         };
40     }
41 
GetLimitCurrentCount(LimitableResource res)42     s32 GetLimitCurrentCount(LimitableResource res)
43     {
44         Handle h;
45         s64 v;
46         Result result;
47 
48         result = svc::GetResourceLimit(&h, nn::PSEUDO_HANDLE_CURRENT_PROCESS);
49         if( result.IsFailure() )
50         {
51             return 0;
52         }
53 
54         AutoCloser ac(h);
55 
56         result = svc::GetResourceLimitCurrentValues(&v, h, &res, 1);
57         if( result.IsFailure() )
58         {
59             return 0;
60         }
61 
62         return static_cast<s32>(v);
63     }
64 
GetLimitMaxCount(LimitableResource res)65     s32 GetLimitMaxCount(LimitableResource res)
66     {
67         Handle h;
68         s64 v;
69         Result result;
70 
71         result = svc::GetResourceLimit(&h, nn::PSEUDO_HANDLE_CURRENT_PROCESS);
72         if( result.IsFailure() )
73         {
74             return 0;
75         }
76 
77         AutoCloser ac(h);
78 
79         result = svc::GetResourceLimitLimitValues(&v, h, &res, 1);
80         if( result.IsFailure() )
81         {
82             return 0;
83         }
84 
85         return static_cast<s32>(v);
86     }
87 
88 }}}
89 
90 
91 #endif  // if NN_PLATFORM_HAS_MMU
92