You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.0 KiB

  1. /**
  2. @file
  3. @ingroup util
  4. @brief Access to datasize limit.
  5. @copyright@parblock
  6. Copyright (c) 1995-2015, Regents of the University of Colorado
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. Neither the name of the University of Colorado nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  29. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. @endparblock
  32. */
  33. #include "config.h"
  34. #if HAVE_STDINT_H == 1
  35. #include <stdint.h>
  36. #endif
  37. #if HAVE_STDDEF_H == 1
  38. #include <stddef.h>
  39. #endif
  40. #if HAVE_SYS_RESOURCE_H == 1
  41. #if HAVE_SYS_TIME_H == 1
  42. #include <sys/time.h>
  43. #endif
  44. #include <sys/resource.h>
  45. #endif
  46. #ifdef _WIN32
  47. #include <windows.h>
  48. #endif
  49. /**
  50. * @brief Default value returned if getrlimit not available.
  51. */
  52. #ifndef RLIMIT_DATA_DEFAULT
  53. #define RLIMIT_DATA_DEFAULT 268435456 /* assume 256MB by default */
  54. #endif
  55. /**
  56. * @def EXTERN
  57. * @brief Allows C linkage when compiling as C++.
  58. */
  59. #ifndef EXTERN
  60. # ifdef __cplusplus
  61. # define EXTERN extern "C"
  62. # else
  63. # define EXTERN extern
  64. # endif
  65. #endif
  66. EXTERN size_t getSoftDataLimit(void);
  67. /**
  68. * @brief Gets the soft datasize limit.
  69. */
  70. size_t
  71. getSoftDataLimit(void)
  72. {
  73. #if HAVE_SYS_RESOURCE_H == 1 && HAVE_GETRLIMIT == 1 && defined(RLIMIT_DATA)
  74. struct rlimit rl;
  75. int result;
  76. result = getrlimit(RLIMIT_DATA, &rl);
  77. if (result != 0 || rl.rlim_cur == RLIM_INFINITY)
  78. return (size_t) RLIMIT_DATA_DEFAULT;
  79. else
  80. return (size_t) rl.rlim_cur;
  81. #elif defined(_WIN32)
  82. /* Not quite the same, because this returns available physical memory. */
  83. MEMORYSTATUSEX statex;
  84. statex.dwLength = sizeof(statex);
  85. if (GlobalMemoryStatusEx(&statex))
  86. return (size_t) statex.ullTotalPhys;
  87. else
  88. return (size_t) RLIMIT_DATA_DEFAULT;
  89. #else
  90. return (size_t) RLIMIT_DATA_DEFAULT;
  91. #endif
  92. } /* end of getSoftDataLimit */