stdlib.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // -*- C++ -*-
  2. //===--------------------------- stdlib.h ---------------------------------===//
  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. #include_next <stdlib.h>
  80. #ifdef __cplusplus
  81. extern "C++" {
  82. // abs
  83. #undef abs
  84. #undef labs
  85. #ifndef _LIBCPP_HAS_NO_LONG_LONG
  86. #undef llabs
  87. #endif
  88. // MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
  89. #if !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
  90. inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT {
  91. return __builtin_labs(__x);
  92. }
  93. #ifndef _LIBCPP_HAS_NO_LONG_LONG
  94. inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {
  95. return __builtin_llabs(__x);
  96. }
  97. #endif // _LIBCPP_HAS_NO_LONG_LONG
  98. #endif // !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
  99. #if !defined(__sun__)
  100. inline _LIBCPP_INLINE_VISIBILITY float abs(float __lcpp_x) _NOEXCEPT {
  101. return __builtin_fabsf(__lcpp_x); // Use builtins to prevent needing math.h
  102. }
  103. inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT {
  104. return __builtin_fabs(__lcpp_x);
  105. }
  106. inline _LIBCPP_INLINE_VISIBILITY long double
  107. abs(long double __lcpp_x) _NOEXCEPT {
  108. return __builtin_fabsl(__lcpp_x);
  109. }
  110. #endif // !defined(__sun__)
  111. // div
  112. #undef div
  113. #undef ldiv
  114. #ifndef _LIBCPP_HAS_NO_LONG_LONG
  115. #undef lldiv
  116. #endif
  117. // MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
  118. #if !defined(_LIBCPP_MSVCRT) && !defined(__sun__)
  119. inline _LIBCPP_INLINE_VISIBILITY ldiv_t div(long __x, long __y) _NOEXCEPT {
  120. return ::ldiv(__x, __y);
  121. }
  122. #ifndef _LIBCPP_HAS_NO_LONG_LONG
  123. inline _LIBCPP_INLINE_VISIBILITY lldiv_t div(long long __x,
  124. long long __y) _NOEXCEPT {
  125. return ::lldiv(__x, __y);
  126. }
  127. #endif // _LIBCPP_HAS_NO_LONG_LONG
  128. #endif // _LIBCPP_MSVCRT / __sun__
  129. } // extern "C++"
  130. #endif // __cplusplus
  131. #endif // _LIBCPP_STDLIB_H