OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 // This file contains the definition of |MojoResult| and related macros. | |
6 // | |
7 // Note: This header should be compilable as C. | |
8 // | |
9 // TODO(vtl): Should separate out "system" error space stuff to a separate | |
10 // header, mojo/system/result.h. | |
11 | |
12 #ifndef MOJO_PUBLIC_C_INCLUDE_MOJO_RESULT_H_ | |
13 #define MOJO_PUBLIC_C_INCLUDE_MOJO_RESULT_H_ | |
14 | |
15 #include <stdint.h> | |
16 | |
17 // |MojoResult|: A type to hold error codes, which are composed of a generic | |
18 // portion (the base error code) and a more specific portion (the error subcode | |
19 // and the error space). | |
20 // | |
21 // The format of a |MojoResult| is, bitwise (least significant bits on the | |
22 // right): | |
23 // | |
24 // sSSSSSSSSSSSSSSSeeeeeeeeeeeeEEEE | |
25 // | |
26 // where: | |
27 // - EEEE is the base error code (4 bits, 0-15); | |
28 // - eeeeeeeeeeee is the error subcode (12 bits, 0-4095); and | |
29 // - sSSSSSSSSSSSSSSS (16 bits, 0-65535) is the error space. | |
30 // | |
31 // The meaning of the base error codes is always the same. |MOJO_ERROR_CODE_OK|, | |
32 // 0, always means "OK"; for this case, both the error subcode and error space | |
33 // must also be 0. See the definitions of the |MOJO_ERROR_CODE_...| below for | |
34 // information on the other base error codes. | |
35 // | |
36 // The meaning of the error subcode depends on both the error space and also the | |
37 // base error code, though |MOJO_ERROR_SUBCODE_GENERIC|, 0, always means | |
38 // "generic"/"unspecified". | |
39 // | |
40 // The error space gives meaning to error subcodes: | |
41 // - Error spaces with the high ('s') bit set (i.e., greater than or equal to | |
42 // 32768) are reserved for private use, i.e., should not be used in public | |
43 // interfaces. | |
44 // - Error spaces without the high bit set are assigned "centrally", with | |
45 // error subcodes within them assigned by their "owners". (TODO(vtl): Figure | |
46 // out what all of this means.) | |
47 // - |MOJO_ERROR_SPACE_SYSTEM|, 0, is the system/generic error space. Error | |
48 // subcodes for this error space are assigned below. | |
49 | |
50 typedef uint32_t MojoResult; | |
51 | |
52 // |MOJO_RESULT_MAKE()|: Helper macro to make a |MojoResult| from the base error | |
53 // code, the error space, and the error subcode. | |
54 #define MOJO_RESULT_MAKE(base_error_code, error_space, error_subcode) \ | |
55 ((base_error_code) | ((error_space) << 16u) | ((error_subcode) << 4u)) | |
56 | |
57 // |MOJO_RESULT_GET_{CODE,SPACE,SUBCODE}()|: Helper macros to get the base error | |
58 // code, the error space, and the error subcode from a |MojoResult|. | |
59 #define MOJO_RESULT_GET_CODE(result) ((result) & ((MojoResult)0xfu)) | |
60 #define MOJO_RESULT_GET_SPACE(result) \ | |
61 (((result) & ((MojoResult)0xffff0000u)) >> 16u) | |
62 #define MOJO_RESULT_GET_SUBCODE(result) \ | |
63 (((result) & ((MojoResult)0xfff0u)) >> 4u) | |
64 | |
65 // Base error codes ------------------------------------------------------------ | |
66 | |
67 // The base error code indicates the general type of error (as described below). | |
68 // For non-success codes, additional information may be conveyed via the error | |
69 // space and error subcodes (for |MOJO_ERROR_CODE_OK|, the error space and error | |
70 // subcode must be zero): | |
71 // |MOJO_ERROR_CODE_OK| - Not an error; returned on success. | |
72 // |MOJO_ERROR_CODE_CANCELLED| - Operation was cancelled, typically by the | |
73 // caller. | |
74 // |MOJO_ERROR_CODE_UNKNOWN| - Unknown error (e.g., if not enough information | |
75 // is available for a more specific error). | |
76 // |MOJO_ERROR_CODE_INVALID_ARGUMENT| - Caller specified an invalid argument. | |
77 // This differs from |MOJO_ERROR_CODE_FAILED_PRECONDITION| in that the | |
78 // former indicates arguments that are invalid regardless of the state of | |
79 // the system. | |
80 // |MOJO_ERROR_CODE_DEADLINE_EXCEEDED| - Deadline expired before the operation | |
81 // could complete. | |
82 // |MOJO_ERROR_CODE_NOT_FOUND| - Some requested entity was not found (i.e., | |
83 // does not exist). | |
84 // |MOJO_ERROR_CODE_ALREADY_EXISTS| - Some entity or condition that we | |
85 // attempted to create already exists. | |
86 // |MOJO_ERROR_CODE_PERMISSION_DENIED| - The caller does not have permission | |
87 // to for the operation (use |MOJO_ERROR_CODE_RESOURCE_EXHAUSTED| for | |
88 // rejections caused by exhausting some resource instead). | |
89 // |MOJO_ERROR_CODE_RESOURCE_EXHAUSTED| - Some resource required for the call | |
90 // (possibly some quota) has been exhausted. | |
91 // |MOJO_ERROR_CODE_FAILED_PRECONDITION| - The system is not in a state | |
92 // required for the operation (use this if the caller must do something to | |
93 // rectify the state before retrying). | |
94 // |MOJO_ERROR_CODE_ABORTED| - The operation was aborted by the system, | |
95 // possibly due to a concurrency issue (use this if the caller may retry | |
96 // at a higher level). | |
97 // |MOJO_ERROR_CODE_OUT_OF_RANGE| - The operation was attempted past the valid | |
98 // range. Unlike |MOJO_ERROR_CODE_INVALID_ARGUMENT|, this indicates that | |
99 // the operation may be/become valid depending on the system state. (This | |
100 // error is similar to |MOJO_ERROR_CODE_FAILED_PRECONDITION|, but is more | |
101 // specific.) | |
102 // |MOJO_ERROR_CODE_UNIMPLEMENTED| - The operation is not implemented, | |
103 // supported, or enabled. | |
104 // |MOJO_ERROR_CODE_INTERNAL| - Internal error: this should never happen and | |
105 // indicates that some invariant expected by the system has been broken. | |
106 // |MOJO_ERROR_CODE_UNAVAILABLE| - The operation is (temporarily) currently | |
107 // unavailable. The caller may simply retry the operation (possibly with a | |
108 // backoff). | |
109 // |MOJO_ERROR_CODE_DATA_LOSS| - Unrecoverable data loss or corruption. | |
110 // | |
111 // The codes correspond to Google3's canonical error codes. | |
112 | |
113 #define MOJO_ERROR_CODE_OK ((MojoResult)0x0u) | |
114 #define MOJO_ERROR_CODE_CANCELLED ((MojoResult)0x1u) | |
115 #define MOJO_ERROR_CODE_UNKNOWN ((MojoResult)0x2u) | |
116 #define MOJO_ERROR_CODE_INVALID_ARGUMENT ((MojoResult)0x3u) | |
117 #define MOJO_ERROR_CODE_DEADLINE_EXCEEDED ((MojoResult)0x4u) | |
118 #define MOJO_ERROR_CODE_NOT_FOUND ((MojoResult)0x5u) | |
119 #define MOJO_ERROR_CODE_ALREADY_EXISTS ((MojoResult)0x6u) | |
120 #define MOJO_ERROR_CODE_PERMISSION_DENIED ((MojoResult)0x7u) | |
121 #define MOJO_ERROR_CODE_RESOURCE_EXHAUSTED ((MojoResult)0x8u) | |
122 #define MOJO_ERROR_CODE_FAILED_PRECONDITION ((MojoResult)0x9u) | |
123 #define MOJO_ERROR_CODE_ABORTED ((MojoResult)0xau) | |
124 #define MOJO_ERROR_CODE_OUT_OF_RANGE ((MojoResult)0xbu) | |
125 #define MOJO_ERROR_CODE_UNIMPLEMENTED ((MojoResult)0xcu) | |
126 #define MOJO_ERROR_CODE_INTERNAL ((MojoResult)0xdu) | |
127 #define MOJO_ERROR_CODE_UNAVAILABLE ((MojoResult)0xeu) | |
128 #define MOJO_ERROR_CODE_DATA_LOSS ((MojoResult)0xfu) | |
129 | |
130 // System/generic error space -------------------------------------------------- | |
131 | |
132 #define MOJO_ERROR_SPACE_SYSTEM ((MojoResult)0x0000u) | |
133 | |
134 // Common/generic subcode, valid for all error codes: | |
135 // |MOJO_ERROR_SUBCODE_GENERIC| - No additional details about the error are | |
136 // specified. | |
137 #define MOJO_ERROR_SUBCODE_GENERIC ((MojoResult)0x000u) | |
138 | |
139 // Subcodes valid for |MOJO_ERROR_CODE_FAILED_PRECONDITION|: | |
140 // |MOJO_ERROR_CODE_FAILED_PRECONDITION_SUBCODE_BUSY| - One of the resources | |
141 // involved is currently being used (possibly on another thread) in a way | |
142 // that prevents the current operation from proceeding, e.g., if the other | |
143 // operation may result in the resource being invalidated. TODO(vtl): We | |
144 // should probably get rid of this, and report "invalid argument" instead | |
145 // (with a different subcode scheme). This is here now for ease of | |
146 // conversion with the existing |MOJO_RESULT_BUSY|. | |
147 #define MOJO_ERROR_CODE_FAILED_PRECONDITION_SUBCODE_BUSY ((MojoResult)0x001u) | |
148 | |
149 // Subcodes valid for MOJO_ERROR_CODE_UNAVAILABLE: | |
150 // |MOJO_ERROR_CODE_UNAVAILABLE_SUBCODE_SHOULD_WAIT| - The request cannot | |
151 // currently be completed (e.g., if the data requested is not yet | |
152 // available). The caller should wait for it to be feasible using one of | |
153 // the wait primitives. | |
154 #define MOJO_ERROR_CODE_UNAVAILABLE_SUBCODE_SHOULD_WAIT ((MojoResult)0x001u) | |
155 | |
156 // Complete results: | |
157 | |
158 // Generic results: | |
159 #define MOJO_RESULT_OK \ | |
160 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_OK, MOJO_ERROR_SPACE_SYSTEM, \ | |
161 MOJO_ERROR_SUBCODE_GENERIC) | |
162 #define MOJO_RESULT_CANCELLED \ | |
163 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_CANCELLED, MOJO_ERROR_SPACE_SYSTEM, \ | |
164 MOJO_ERROR_SUBCODE_GENERIC) | |
165 #define MOJO_RESULT_UNKNOWN \ | |
166 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_UNKNOWN, MOJO_ERROR_SPACE_SYSTEM, \ | |
167 MOJO_ERROR_SUBCODE_GENERIC) | |
168 #define MOJO_RESULT_INVALID_ARGUMENT \ | |
169 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_INVALID_ARGUMENT, MOJO_ERROR_SPACE_SYSTEM, \ | |
170 MOJO_ERROR_SUBCODE_GENERIC) | |
171 #define MOJO_RESULT_DEADLINE_EXCEEDED \ | |
172 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_DEADLINE_EXCEEDED, MOJO_ERROR_SPACE_SYSTEM, \ | |
173 MOJO_ERROR_SUBCODE_GENERIC) | |
174 #define MOJO_RESULT_NOT_FOUND \ | |
175 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_NOT_FOUND, MOJO_ERROR_SPACE_SYSTEM, \ | |
176 MOJO_ERROR_SUBCODE_GENERIC) | |
177 #define MOJO_RESULT_ALREADY_EXISTS \ | |
178 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_ALREADY_EXISTS, MOJO_ERROR_SPACE_SYSTEM, \ | |
179 MOJO_ERROR_SUBCODE_GENERIC) | |
180 #define MOJO_RESULT_PERMISSION_DENIED \ | |
181 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_PERMISSION_DENIED, MOJO_ERROR_SPACE_SYSTEM, \ | |
182 MOJO_ERROR_SUBCODE_GENERIC) | |
183 #define MOJO_RESULT_RESOURCE_EXHAUSTED \ | |
184 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_RESOURCE_EXHAUSTED, \ | |
185 MOJO_ERROR_SPACE_SYSTEM, MOJO_ERROR_SUBCODE_GENERIC) | |
186 #define MOJO_RESULT_FAILED_PRECONDITION \ | |
187 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_FAILED_PRECONDITION, \ | |
188 MOJO_ERROR_SPACE_SYSTEM, MOJO_ERROR_SUBCODE_GENERIC) | |
189 #define MOJO_RESULT_ABORTED \ | |
190 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_ABORTED, MOJO_ERROR_SPACE_SYSTEM, \ | |
191 MOJO_ERROR_SUBCODE_GENERIC) | |
192 #define MOJO_RESULT_OUT_OF_RANGE \ | |
193 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_OUT_OF_RANGE, MOJO_ERROR_SPACE_SYSTEM, \ | |
194 MOJO_ERROR_SUBCODE_GENERIC) | |
195 #define MOJO_RESULT_UNIMPLEMENTED \ | |
196 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_UNIMPLEMENTED, MOJO_ERROR_SPACE_SYSTEM, \ | |
197 MOJO_ERROR_SUBCODE_GENERIC) | |
198 #define MOJO_RESULT_INTERNAL \ | |
199 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_INTERNAL, MOJO_ERROR_SPACE_SYSTEM, \ | |
200 MOJO_ERROR_SUBCODE_GENERIC) | |
201 #define MOJO_RESULT_UNAVAILABLE \ | |
202 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_UNAVAILABLE, MOJO_ERROR_SPACE_SYSTEM, \ | |
203 MOJO_ERROR_SUBCODE_GENERIC) | |
204 #define MOJO_RESULT_DATA_LOSS \ | |
205 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_DATA_LOSS, MOJO_ERROR_SPACE_SYSTEM, \ | |
206 MOJO_ERROR_SUBCODE_GENERIC) | |
207 | |
208 // Specific results (for backwards compatibility): | |
209 #define MOJO_RESULT_BUSY \ | |
210 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_FAILED_PRECONDITION, \ | |
211 MOJO_ERROR_SPACE_SYSTEM, \ | |
212 MOJO_ERROR_CODE_FAILED_PRECONDITION_SUBCODE_BUSY) | |
213 #define MOJO_RESULT_SHOULD_WAIT \ | |
214 MOJO_RESULT_MAKE(MOJO_ERROR_CODE_UNAVAILABLE, MOJO_ERROR_SPACE_SYSTEM, \ | |
215 MOJO_ERROR_CODE_UNAVAILABLE_SUBCODE_SHOULD_WAIT) | |
216 | |
217 #endif // MOJO_PUBLIC_C_INCLUDE_MOJO_RESULT_H_ | |
OLD | NEW |