stdlib.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #if defined(__need_malloc_and_calloc)
  10. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  11. # pragma GCC system_header
  12. #endif
  13. #include_next <stdlib.h>
  14. #elif !defined(_LIBCPP_STDLIB_H)
  15. #define _LIBCPP_STDLIB_H
  16. /*
  17. stdlib.h synopsis
  18. Macros:
  19. EXIT_FAILURE
  20. EXIT_SUCCESS
  21. MB_CUR_MAX
  22. NULL
  23. RAND_MAX
  24. Types:
  25. size_t
  26. div_t
  27. ldiv_t
  28. lldiv_t // C99
  29. double atof (const char* nptr);
  30. int atoi (const char* nptr);
  31. long atol (const char* nptr);
  32. long long atoll(const char* nptr); // C99
  33. double strtod (const char* restrict nptr, char** restrict endptr);
  34. float strtof (const char* restrict nptr, char** restrict endptr); // C99
  35. long double strtold (const char* restrict nptr, char** restrict endptr); // C99
  36. long strtol (const char* restrict nptr, char** restrict endptr, int base);
  37. long long strtoll (const char* restrict nptr, char** restrict endptr, int base); // C99
  38. unsigned long strtoul (const char* restrict nptr, char** restrict endptr, int base);
  39. unsigned long long strtoull(const char* restrict nptr, char** restrict endptr, int base); // C99
  40. int rand(void);
  41. void srand(unsigned int seed);
  42. void* calloc(size_t nmemb, size_t size);
  43. void free(void* ptr);
  44. void* malloc(size_t size);
  45. void* realloc(void* ptr, size_t size);
  46. void abort(void);
  47. int atexit(void (*func)(void));
  48. void exit(int status);
  49. void _Exit(int status);
  50. char* getenv(const char* name);
  51. int system(const char* string);
  52. void* bsearch(const void* key, const void* base, size_t nmemb, size_t size,
  53. int (*compar)(const void *, const void *));
  54. void qsort(void* base, size_t nmemb, size_t size,
  55. int (*compar)(const void *, const void *));
  56. int abs( int j);
  57. long abs( long j);
  58. long long abs(long long j); // C++0X
  59. long labs( long j);
  60. long long llabs(long long j); // C99
  61. div_t div( int numer, int denom);
  62. ldiv_t div( long numer, long denom);
  63. lldiv_t div(long long numer, long long denom); // C++0X
  64. ldiv_t ldiv( long numer, long denom);
  65. lldiv_t lldiv(long long numer, long long denom); // C99
  66. int mblen(const char* s, size_t n);
  67. int mbtowc(wchar_t* restrict pwc, const char* restrict s, size_t n);
  68. int wctomb(char* s, wchar_t wchar);
  69. size_t mbstowcs(wchar_t* restrict pwcs, const char* restrict s, size_t n);
  70. size_t wcstombs(char* restrict s, const wchar_t* restrict pwcs, size_t n);
  71. int at_quick_exit(void (*func)(void)) // C++11
  72. void quick_exit(int status); // C++11
  73. void *aligned_alloc(size_t alignment, size_t size); // C11
  74. */
  75. #include <__config>
  76. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  77. # pragma GCC system_header
  78. #endif
  79. # if __has_include_next(<stdlib.h>)
  80. # include_next <stdlib.h>
  81. # endif
  82. #ifdef __cplusplus
  83. extern "C++" {
  84. // abs
  85. #ifdef abs
  86. # undef abs
  87. #endif
  88. #ifdef labs
  89. # undef labs
  90. #endif
  91. #ifdef llabs
  92. # undef llabs
  93. #endif
  94. // MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
  95. #if !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
  96. _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT {
  97. return __builtin_labs(__x);
  98. }
  99. _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {
  100. return __builtin_llabs(__x);
  101. }
  102. #endif // !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
  103. #if !defined(__sun__)
  104. _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY float abs(float __lcpp_x) _NOEXCEPT {
  105. return __builtin_fabsf(__lcpp_x); // Use builtins to prevent needing math.h
  106. }
  107. _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT {
  108. return __builtin_fabs(__lcpp_x);
  109. }
  110. _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY long double
  111. abs(long double __lcpp_x) _NOEXCEPT {
  112. return __builtin_fabsl(__lcpp_x);
  113. }
  114. #endif // !defined(__sun__)
  115. // div
  116. #ifdef div
  117. # undef div
  118. #endif
  119. #ifdef ldiv
  120. # undef ldiv
  121. #endif
  122. #ifdef lldiv
  123. # undef lldiv
  124. #endif
  125. // MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
  126. #if !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
  127. inline _LIBCPP_INLINE_VISIBILITY ldiv_t div(long __x, long __y) _NOEXCEPT {
  128. return ::ldiv(__x, __y);
  129. }
  130. #if !(defined(__FreeBSD__) && !defined(__LONG_LONG_SUPPORTED))
  131. inline _LIBCPP_INLINE_VISIBILITY lldiv_t div(long long __x,
  132. long long __y) _NOEXCEPT {
  133. return ::lldiv(__x, __y);
  134. }
  135. #endif
  136. #endif // _LIBCPP_MSVCRT / __sun__
  137. } // extern "C++"
  138. #endif // __cplusplus
  139. #endif // _LIBCPP_STDLIB_H