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.
7 #ifndef NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_NACL_SIZE_T_H
8 #define NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_NACL_SIZE_T_H
10 #include "native_client/src/include/portability.h"
12 #ifndef nacl_abi_size_t_defined
13 #define nacl_abi_size_t_defined
14 typedef uint32_t nacl_abi_size_t;
17 #define NACL_ABI_SIZE_T_MIN ((nacl_abi_size_t) 0)
18 #define NACL_ABI_SIZE_T_MAX ((nacl_abi_size_t) -1)
20 #ifndef nacl_abi_ssize_t_defined
21 #define nacl_abi_ssize_t_defined
22 typedef int32_t nacl_abi_ssize_t;
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)
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
38 * Inline functions to aid in conversion between system (s)size_t and
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)
45 * If __native_client__ *is* defined, these turn into no-ops.
47 #ifdef __native_client__
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.
53 #define nacl_abi_size_t_saturate(x) (x)
54 #define nacl_abi_ssize_t_saturate(x) (x)
55 #else /* __native_client */
56 static INLINE nacl_abi_size_t nacl_abi_size_t_saturate(size_t x) {
57 if (x > NACL_ABI_SIZE_T_MAX) {
58 return NACL_ABI_SIZE_T_MAX;
60 return (nacl_abi_size_t)x;
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;
70 return (nacl_abi_ssize_t) x;
73 #endif /* __native_client */