| 1 | #ifndef AWS_COMMON_STDINT_H |
| 2 | #define AWS_COMMON_STDINT_H |
| 3 | /* |
| 4 | * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"). |
| 7 | * You may not use this file except in compliance with the License. |
| 8 | * A copy of the License is located at |
| 9 | * |
| 10 | * http://aws.amazon.com/apache2.0 |
| 11 | * |
| 12 | * or in the "license" file accompanying this file. This file is distributed |
| 13 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 14 | * express or implied. See the License for the specific language governing |
| 15 | * permissions and limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <aws/common/assert.h> |
| 19 | #include <stddef.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | |
| 23 | #ifndef NO_STDINT |
| 24 | # include <stdint.h> /* NOLINT(fuchsia-restrict-system-includes) */ |
| 25 | /* Android defines SIZE_MAX in limits.h, not stdint.h */ |
| 26 | # ifdef ANDROID |
| 27 | # include <limits.h> |
| 28 | # endif |
| 29 | #else |
| 30 | # if defined(__x86_64__) || defined(_M_AMD64) || defined(__aarch64__) || defined(__ia64__) || defined(__powerpc64__) |
| 31 | # define PTR_SIZE 8 |
| 32 | # else |
| 33 | # define PTR_SIZE 4 |
| 34 | # endif |
| 35 | |
| 36 | typedef signed char int8_t; |
| 37 | typedef short int int16_t; |
| 38 | typedef int int32_t; |
| 39 | # if (PTR_SIZE == 8) |
| 40 | typedef long int int64_t; |
| 41 | # else |
| 42 | typedef long long int int64_t; |
| 43 | # endif /* (PTR_SIZE == 8) */ |
| 44 | |
| 45 | typedef unsigned char uint8_t; |
| 46 | typedef unsigned short int uint16_t; |
| 47 | |
| 48 | typedef unsigned int uint32_t; |
| 49 | |
| 50 | # if (PTR_SIZE == 8) |
| 51 | typedef unsigned long int uint64_t; |
| 52 | # else |
| 53 | typedef unsigned long long int uint64_t; |
| 54 | # endif /* (PTR_SIZE == 8) */ |
| 55 | |
| 56 | # if (PTR_SIZE == 8) |
| 57 | typedef long int intptr_t; |
| 58 | typedef unsigned long int uintptr_t; |
| 59 | # else |
| 60 | typedef int intptr_t; |
| 61 | typedef unsigned int uintptr_t; |
| 62 | # endif |
| 63 | |
| 64 | # if (PTR_SIZE == 8) |
| 65 | # define __INT64_C(c) c##L |
| 66 | # define __UINT64_C(c) c##UL |
| 67 | # else |
| 68 | # define __INT64_C(c) c##LL |
| 69 | # define __UINT64_C(c) c##ULL |
| 70 | # endif |
| 71 | |
| 72 | # define INT8_MIN (-128) |
| 73 | # define INT16_MIN (-32767 - 1) |
| 74 | # define INT32_MIN (-2147483647 - 1) |
| 75 | # define INT64_MIN (-__INT64_C(9223372036854775807) - 1) |
| 76 | # define INT8_MAX (127) |
| 77 | # define INT16_MAX (32767) |
| 78 | # define INT32_MAX (2147483647) |
| 79 | # define INT64_MAX (__INT64_C(9223372036854775807)) |
| 80 | # define UINT8_MAX (255) |
| 81 | # define UINT16_MAX (65535) |
| 82 | # define UINT32_MAX (4294967295U) |
| 83 | # define UINT64_MAX (__UINT64_C(18446744073709551615)) |
| 84 | |
| 85 | AWS_STATIC_ASSERT(sizeof(uint64_t) == 8); |
| 86 | AWS_STATIC_ASSERT(sizeof(uint32_t) == 4); |
| 87 | AWS_STATIC_ASSERT(sizeof(uint16_t) == 2); |
| 88 | AWS_STATIC_ASSERT(sizeof(uint8_t) == 1); |
| 89 | AWS_STATIC_ASSERT(sizeof(int64_t) == 8); |
| 90 | AWS_STATIC_ASSERT(sizeof(int32_t) == 4); |
| 91 | AWS_STATIC_ASSERT(sizeof(int16_t) == 2); |
| 92 | AWS_STATIC_ASSERT(sizeof(int8_t) == 1); |
| 93 | AWS_STATIC_ASSERT(sizeof(uintptr_t) == sizeof(void *)); |
| 94 | AWS_STATIC_ASSERT(sizeof(intptr_t) == sizeof(void *)); |
| 95 | AWS_STATIC_ASSERT(sizeof(char) == 1); |
| 96 | #endif /* NO_STDINT */ |
| 97 | |
| 98 | #if defined(_MSC_VER) |
| 99 | typedef int64_t aws_off_t; |
| 100 | #else |
| 101 | # if _FILE_OFFSET_BITS == 64 || _POSIX_C_SOURCE >= 200112L |
| 102 | typedef off_t aws_off_t; |
| 103 | # else |
| 104 | typedef long aws_off_t; |
| 105 | # endif /* _FILE_OFFSET_BITS == 64 || _POSIX_C_SOURCE >= 200112L */ |
| 106 | #endif /* defined(_MSC_VER) */ |
| 107 | |
| 108 | AWS_STATIC_ASSERT(sizeof(int64_t) >= sizeof(aws_off_t)); |
| 109 | |
| 110 | #endif /* AWS_COMMON_STDINT_H */ |
| 111 | |