1 : /*
2 : * Copyright (c) 2014 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 : #ifndef NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_NACL_SIZE_T_H
8 : #define NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_NACL_SIZE_T_H
9 :
10 : #include "native_client/src/include/portability.h"
11 :
12 : #ifndef nacl_abi_size_t_defined
13 : #define nacl_abi_size_t_defined
14 : typedef uint32_t nacl_abi_size_t;
15 : #endif
16 :
17 : #define NACL_ABI_SIZE_T_MIN ((nacl_abi_size_t) 0)
18 : #define NACL_ABI_SIZE_T_MAX ((nacl_abi_size_t) -1)
19 :
20 : #ifndef nacl_abi_ssize_t_defined
21 : #define nacl_abi_ssize_t_defined
22 : typedef int32_t nacl_abi_ssize_t;
23 : #endif
24 :
25 : #define NACL_ABI_SSIZE_T_MAX \
26 : ((nacl_abi_ssize_t) (NACL_ABI_SIZE_T_MAX >> 1))
27 : #define NACL_ABI_SSIZE_T_MIN \
28 : (~NACL_ABI_SSIZE_T_MAX)
29 :
30 : #define NACL_PRIdNACL_SIZE NACL_PRId32
31 : #define NACL_PRIiNACL_SIZE NACL_PRIi32
32 : #define NACL_PRIoNACL_SIZE NACL_PRIo32
33 : #define NACL_PRIuNACL_SIZE NACL_PRIu32
34 : #define NACL_PRIxNACL_SIZE NACL_PRIx32
35 : #define NACL_PRIXNACL_SIZE NACL_PRIX32
36 :
37 : /**
38 : * Inline functions to aid in conversion between system (s)size_t and
39 : * nacl_abi_(s)size_t
40 : *
41 : * These are defined as inline functions only if __native_client__ is
42 : * undefined, since in a nacl module size_t and nacl_abi_size_t are always
43 : * the same (and we don't have a definition for INLINE, so these won't compile)
44 : *
45 : * If __native_client__ *is* defined, these turn into no-ops.
46 : */
47 : #ifdef __native_client__
48 : /**
49 : * NB: The "no-op" version of these functions does NO type conversion.
50 : * Please DO NOT CHANGE THIS. If you get a type error using these functions
51 : * in a NaCl module, it's a real error and should be fixed in your code.
52 : */
53 : #define nacl_abi_size_t_saturate(x) (x)
54 : #define nacl_abi_ssize_t_saturate(x) (x)
55 : #else /* __native_client */
56 2 : static INLINE nacl_abi_size_t nacl_abi_size_t_saturate(size_t x) {
57 2 : if (x > NACL_ABI_SIZE_T_MAX) {
58 0 : return NACL_ABI_SIZE_T_MAX;
59 : } else {
60 2 : return (nacl_abi_size_t)x;
61 : }
62 2 : }
63 :
64 : static INLINE nacl_abi_ssize_t nacl_abi_ssize_t_saturate(ssize_t x) {
65 : if (x > NACL_ABI_SSIZE_T_MAX) {
66 : return NACL_ABI_SSIZE_T_MAX;
67 : } else if (x < NACL_ABI_SSIZE_T_MIN) {
68 : return NACL_ABI_SSIZE_T_MIN;
69 : } else {
70 : return (nacl_abi_ssize_t) x;
71 : }
72 : }
73 : #endif /* __native_client */
74 :
75 : #endif
|