• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef MOJO_PUBLIC_C_SYSTEM_QUOTA_H_
6 #define MOJO_PUBLIC_C_SYSTEM_QUOTA_H_
7 
8 #include <stdint.h>
9 
10 #include "mojo/public/c/system/macros.h"
11 #include "mojo/public/c/system/system_export.h"
12 #include "mojo/public/c/system/types.h"
13 
14 // Flags passed to |MojoSetQuota| via |MojoSetQuotaOptions|.
15 typedef uint32_t MojoSetQuotaFlags;
16 
17 // No flags.
18 #define MOJO_SET_QUOTA_FLAG_NONE ((MojoSetQuotaFlags)0)
19 
20 // Options passed to |MojoSetQuota()|.
21 struct MOJO_ALIGNAS(8) MojoSetQuotaOptions {
22   // The size of this structure, used for versioning.
23   uint32_t struct_size;
24 
25   // See |MojoSetQuotaFlags| above.
26   MojoSetQuotaFlags flags;
27 };
28 MOJO_STATIC_ASSERT(sizeof(MojoSetQuotaOptions) == 8,
29                    "MojoSetQuotaOptions has wrong size.");
30 
31 // Flags passed to |MojoQueryQuota| via |MojoQueryQuotaOptions|.
32 typedef uint32_t MojoQueryQuotaFlags;
33 
34 // No flags.
35 #define MOJO_QUERY_QUOTA_FLAG_NONE ((MojoQueryQuotaFlags)0)
36 
37 // Options passed to |MojoQueryQuota()|.
38 struct MOJO_ALIGNAS(8) MojoQueryQuotaOptions {
39   // The size of this structure, used for versioning.
40   uint32_t struct_size;
41 
42   // See |MojoQueryQuotaFlags| above.
43   MojoQueryQuotaFlags flags;
44 };
45 MOJO_STATIC_ASSERT(sizeof(MojoQueryQuotaOptions) == 8,
46                    "MojoQueryQuotaOptions has wrong size.");
47 
48 // The maximum value any quota can be set to. Effectively means "no quota".
49 #define MOJO_QUOTA_LIMIT_NONE ((uint64_t)0xffffffffffffffff)
50 
51 // An enumeration of different types of quotas that can be set on a handle.
52 typedef uint32_t MojoQuotaType;
53 
54 // Limits the number of unread messages which can be queued on a message pipe
55 // endpoint before raising a |MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED| signal on that
56 // endpoint. May only be set on message pipe handles.
57 #define MOJO_QUOTA_TYPE_RECEIVE_QUEUE_LENGTH ((MojoQuotaType)0)
58 
59 // Limits the total size (in bytes) of unread messages which can be queued on a
60 // message pipe endpoint before raising a |MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED|
61 // signal on that endpoint. May only be set on message pipe handles.
62 #define MOJO_QUOTA_TYPE_RECEIVE_QUEUE_MEMORY_SIZE ((MojoQuotaType)1)
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 // Sets a quota on a given handle which will cause that handle to raise the
69 // |MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED| signal if the quota is exceeded. Signals
70 // can be trapped using |MojoCreateTrap()| and related APIs (see trap.h).
71 //
72 // All quota limits on a handle default to |MOJO_QUOTA_LIMIT_NONE|, meaning that
73 // the resource is unlimited.
74 //
75 // NOTE: A handle's quota is only enforced as long as the handle remains within
76 // the process which set the quota.
77 //
78 // Parameters:
79 //   |handle|: The handle on which a quota should be set.
80 //   |type|: The type of quota to set. Certain types of quotas may only be set
81 //       on certain types of handles. See notes on individual quota type
82 //       definitions above for meaning and restrictions.
83 //   |limit|: The limiting value of the quota. The meaning of this is determined
84 //       by |type|. See notes on individual quota type definitions above.
85 //   |options|: Additional options; may be null.
86 //
87 //  Returns:
88 //    |MOJO_RESULT_OK| if the quota was successfully set.
89 //    |MOJO_RESULT_INVALID_ARGUMENT| if |handle| is not a valid handle value,
90 //        |type| is not a known quota type, |options| is non-null but
91 //        |*options| is malformed, or the quota |type| cannot be set on |handle|
92 //        because the quota does not apply to that type of handle.
93 MOJO_SYSTEM_EXPORT MojoResult
94 MojoSetQuota(MojoHandle handle,
95              MojoQuotaType type,
96              uint64_t limit,
97              const struct MojoSetQuotaOptions* options);
98 
99 // Queries a handle for information about a specific quota.
100 //
101 // Parameters:
102 //   |handle|: The handle to query.
103 //   |type|: The type of quota to query.
104 //   |limit|: Receives the quota's currently set limit if non-null.
105 //   |usage|: Receives the quota's current usage if non-null.
106 //
107 // Returns:
108 //   |MOJO_RESULT_OK| if the quota was successfully queried on |handle|. Upon
109 //       return, |*limit| contains the quota's current limit if |limit| is
110 //       non-null, and |*usage| contains the quota's current usage if |usage| is
111 //       non-null.
112 //   |MOJO_RESULT_INVALID_ARGUMENT| if |handle| is not a valid handle value or
113 //       quota |type| does not apply to the type of object referenced by
114 //       |handle|.
115 MOJO_SYSTEM_EXPORT MojoResult
116 MojoQueryQuota(MojoHandle handle,
117                MojoQuotaType type,
118                const struct MojoQueryQuotaOptions* options,
119                uint64_t* limit,
120                uint64_t* usage);
121 
122 #ifdef __cplusplus
123 }  // extern "C"
124 #endif
125 
126 #endif  // MOJO_PUBLIC_C_SYSTEM_QUOTA_H_
127