llapr.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @file llapr.cpp
  3. * @author Phoenix
  4. * @date 2004-11-28
  5. * @brief Helper functions for using the apache portable runtime library.
  6. *
  7. * $LicenseInfo:firstyear=2004&license=viewergpl$
  8. *
  9. * Copyright (c) 2004-2009, Linden Research, Inc.
  10. *
  11. * Second Life Viewer Source Code
  12. * The source code in this file ("Source Code") is provided by Linden Lab
  13. * to you under the terms of the GNU General Public License, version 2.0
  14. * ("GPL"), unless you have obtained a separate licensing agreement
  15. * ("Other License"), formally executed by you and Linden Lab. Terms of
  16. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18. *
  19. * There are special exceptions to the terms and conditions of the GPL as
  20. * it is applied to this Source Code. View the full text of the exception
  21. * in the file doc/FLOSS-exception.txt in this software distribution, or
  22. * online at
  23. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24. *
  25. * By copying, modifying or distributing this software, you acknowledge
  26. * that you have read and understood your obligations described above,
  27. * and agree to abide by those obligations.
  28. *
  29. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31. * COMPLETENESS OR PERFORMANCE.
  32. * $/LicenseInfo$
  33. */
  34. #include "linden_common.h"
  35. #include <stdlib.h> // For atexit()
  36. #include "llapr.h"
  37. // Global APR memory pool:
  38. apr_pool_t* gAPRPoolp = NULL;
  39. bool gAPRInitialized = false;
  40. void ll_init_apr()
  41. {
  42. if (gAPRInitialized)
  43. {
  44. return;
  45. }
  46. gAPRInitialized = true;
  47. // Initialize APR and create the global pool
  48. apr_initialize();
  49. // Register apr_terminate() so that it is called on exit. DO NOT use
  50. // apr_terminate() directly in ll_cleanup_apr(): there is no guarantee
  51. // whatsoever that globally or statically declared class members will
  52. // not keep using APR after ll_cleanup_apr() is called and so terminating
  53. // APR must be done at the program late exit step. HB
  54. atexit(apr_terminate);
  55. if (!gAPRPoolp)
  56. {
  57. apr_pool_create(&gAPRPoolp, NULL);
  58. }
  59. }
  60. void ll_cleanup_apr()
  61. {
  62. if (gAPRPoolp)
  63. {
  64. llinfos << "Cleaning up APR" << llendl;
  65. apr_pool_destroy(gAPRPoolp);
  66. gAPRPoolp = NULL;
  67. }
  68. }
  69. ///////////////////////////////////////////////////////////////////////////////
  70. // LLAPRPool class
  71. ///////////////////////////////////////////////////////////////////////////////
  72. LLAPRPool::LLAPRPool(apr_pool_t* parent, apr_size_t size, bool release_pool)
  73. : mParent(parent),
  74. mReleasePoolFlag(release_pool),
  75. mMaxSize(size),
  76. mPool(NULL)
  77. {
  78. createAPRPool();
  79. }
  80. LLAPRPool::~LLAPRPool()
  81. {
  82. releaseAPRPool();
  83. }
  84. void LLAPRPool::createAPRPool()
  85. {
  86. if (mPool)
  87. {
  88. return;
  89. }
  90. mStatus = apr_pool_create(&mPool, mParent);
  91. ll_apr_warn_status(mStatus);
  92. // mMaxSize is the number of blocks (which is usually 4K), NOT bytes.
  93. if (mMaxSize > 0)
  94. {
  95. apr_allocator_t* allocator = apr_pool_allocator_get(mPool);
  96. if (allocator)
  97. {
  98. apr_allocator_max_free_set(allocator, mMaxSize);
  99. }
  100. }
  101. }
  102. void LLAPRPool::releaseAPRPool()
  103. {
  104. if (!mPool)
  105. {
  106. return;
  107. }
  108. if (!mParent || mReleasePoolFlag)
  109. {
  110. apr_pool_destroy(mPool);
  111. mPool = NULL;
  112. }
  113. }
  114. //virtual
  115. apr_pool_t* LLAPRPool::getAPRPool()
  116. {
  117. return mPool;
  118. }
  119. ///////////////////////////////////////////////////////////////////////////////
  120. // APR helpers
  121. ///////////////////////////////////////////////////////////////////////////////
  122. bool ll_apr_warn_status(apr_status_t status)
  123. {
  124. if (status == APR_SUCCESS)
  125. {
  126. return false;
  127. }
  128. // Do not warn about end of file, which is a "normal" occurrence of
  129. // some reads (reads till EOF). HB
  130. if (status == APR_EOF)
  131. {
  132. return true;
  133. }
  134. char buf[MAX_STRING];
  135. apr_strerror(status, buf, sizeof(buf));
  136. llwarns << "APR: " << buf << llendl;
  137. return true;
  138. }
  139. bool ll_apr_warn_status(apr_status_t status, apr_dso_handle_t* handle)
  140. {
  141. bool result = ll_apr_warn_status(status);
  142. // Despite observed truncation of actual Mac dylib load errors, increasing
  143. // this buffer to more than MAX_STRING does not help: it appears that APR
  144. // stores the output in a fixed 255-character internal buffer. (*sigh*)
  145. char buf[MAX_STRING];
  146. apr_dso_error(handle, buf, sizeof(buf));
  147. llwarns << "APR: " << buf << llendl;
  148. return result;
  149. }