/indra/llcommon/llapr.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 272 lines · 129 code · 42 blank · 101 comment · 1 complexity · f3c4a1dca745796be401b4351787131e MD5 · raw file

  1. /**
  2. * @file llapr.h
  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=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #ifndef LL_LLAPR_H
  29. #define LL_LLAPR_H
  30. #if LL_LINUX || LL_SOLARIS
  31. #include <sys/param.h> // Need PATH_MAX in APR headers...
  32. #endif
  33. #if LL_WINDOWS
  34. // Limit Windows API to small and manageable set.
  35. // If you get undefined symbols, find the appropriate
  36. // Windows header file and include that in your .cpp file.
  37. #define WIN32_LEAN_AND_MEAN
  38. #include <winsock2.h>
  39. #include <windows.h>
  40. #endif
  41. #include <boost/noncopyable.hpp>
  42. #include "apr_thread_proc.h"
  43. #include "apr_thread_mutex.h"
  44. #include "apr_getopt.h"
  45. #include "apr_signal.h"
  46. #include "apr_atomic.h"
  47. #include "llstring.h"
  48. extern LL_COMMON_API apr_thread_mutex_t* gLogMutexp;
  49. extern apr_thread_mutex_t* gCallStacksLogMutexp;
  50. struct apr_dso_handle_t;
  51. /**
  52. * @brief initialize the common apr constructs -- apr itself, the
  53. * global pool, and a mutex.
  54. */
  55. void LL_COMMON_API ll_init_apr();
  56. /**
  57. * @brief Cleanup those common apr constructs.
  58. */
  59. void LL_COMMON_API ll_cleanup_apr();
  60. //
  61. //LL apr_pool
  62. //manage apr_pool_t, destroy allocated apr_pool in the destruction function.
  63. //
  64. class LL_COMMON_API LLAPRPool
  65. {
  66. public:
  67. LLAPRPool(apr_pool_t *parent = NULL, apr_size_t size = 0, BOOL releasePoolFlag = TRUE) ;
  68. virtual ~LLAPRPool() ;
  69. virtual apr_pool_t* getAPRPool() ;
  70. apr_status_t getStatus() {return mStatus ; }
  71. protected:
  72. void releaseAPRPool() ;
  73. void createAPRPool() ;
  74. protected:
  75. apr_pool_t* mPool ; //pointing to an apr_pool
  76. apr_pool_t* mParent ; //parent pool
  77. apr_size_t mMaxSize ; //max size of mPool, mPool should return memory to system if allocated memory beyond this limit. However it seems not to work.
  78. apr_status_t mStatus ; //status when creating the pool
  79. BOOL mReleasePoolFlag ; //if set, mPool is destroyed when LLAPRPool is deleted. default value is true.
  80. };
  81. //
  82. //volatile LL apr_pool
  83. //which clears memory automatically.
  84. //so it can not hold static data or data after memory is cleared
  85. //
  86. class LL_COMMON_API LLVolatileAPRPool : public LLAPRPool
  87. {
  88. public:
  89. LLVolatileAPRPool(BOOL is_local = TRUE, apr_pool_t *parent = NULL, apr_size_t size = 0, BOOL releasePoolFlag = TRUE);
  90. virtual ~LLVolatileAPRPool();
  91. /*virtual*/ apr_pool_t* getAPRPool() ; //define this virtual function to avoid any mistakenly calling LLAPRPool::getAPRPool().
  92. apr_pool_t* getVolatileAPRPool() ;
  93. void clearVolatileAPRPool() ;
  94. BOOL isFull() ;
  95. private:
  96. S32 mNumActiveRef ; //number of active pointers pointing to the apr_pool.
  97. S32 mNumTotalRef ; //number of total pointers pointing to the apr_pool since last creating.
  98. apr_thread_mutex_t *mMutexp;
  99. apr_pool_t *mMutexPool;
  100. } ;
  101. /**
  102. * @class LLScopedLock
  103. * @brief Small class to help lock and unlock mutexes.
  104. *
  105. * This class is used to have a stack level lock once you already have
  106. * an apr mutex handy. The constructor handles the lock, and the
  107. * destructor handles the unlock. Instances of this class are
  108. * <b>not</b> thread safe.
  109. */
  110. class LL_COMMON_API LLScopedLock : private boost::noncopyable
  111. {
  112. public:
  113. /**
  114. * @brief Constructor which accepts a mutex, and locks it.
  115. *
  116. * @param mutex An allocated APR mutex. If you pass in NULL,
  117. * this wrapper will not lock.
  118. */
  119. LLScopedLock(apr_thread_mutex_t* mutex);
  120. /**
  121. * @brief Destructor which unlocks the mutex if still locked.
  122. */
  123. ~LLScopedLock();
  124. /**
  125. * @brief Check lock.
  126. */
  127. bool isLocked() const { return mLocked; }
  128. /**
  129. * @brief This method unlocks the mutex.
  130. */
  131. void unlock();
  132. protected:
  133. bool mLocked;
  134. apr_thread_mutex_t* mMutex;
  135. };
  136. template <typename Type> class LLAtomic32
  137. {
  138. public:
  139. LLAtomic32<Type>() {};
  140. LLAtomic32<Type>(Type x) {apr_atomic_set32(&mData, apr_uint32_t(x)); };
  141. ~LLAtomic32<Type>() {};
  142. operator const Type() { apr_uint32_t data = apr_atomic_read32(&mData); return Type(data); }
  143. Type operator =(const Type& x) { apr_atomic_set32(&mData, apr_uint32_t(x)); return Type(mData); }
  144. void operator -=(Type x) { apr_atomic_sub32(&mData, apr_uint32_t(x)); }
  145. void operator +=(Type x) { apr_atomic_add32(&mData, apr_uint32_t(x)); }
  146. Type operator ++(int) { return apr_atomic_inc32(&mData); } // Type++
  147. Type operator --(int) { return apr_atomic_dec32(&mData); } // Type--
  148. private:
  149. apr_uint32_t mData;
  150. };
  151. typedef LLAtomic32<U32> LLAtomicU32;
  152. typedef LLAtomic32<S32> LLAtomicS32;
  153. // File IO convenience functions.
  154. // Returns NULL if the file fails to open, sets *sizep to file size if not NULL
  155. // abbreviated flags
  156. #define LL_APR_R (APR_READ) // "r"
  157. #define LL_APR_W (APR_CREATE|APR_TRUNCATE|APR_WRITE) // "w"
  158. #define LL_APR_RB (APR_READ|APR_BINARY) // "rb"
  159. #define LL_APR_WB (APR_CREATE|APR_TRUNCATE|APR_WRITE|APR_BINARY) // "wb"
  160. #define LL_APR_RPB (APR_READ|APR_WRITE|APR_BINARY) // "r+b"
  161. #define LL_APR_WPB (APR_CREATE|APR_TRUNCATE|APR_READ|APR_WRITE|APR_BINARY) // "w+b"
  162. //
  163. //apr_file manager
  164. //which: 1)only keeps one file open;
  165. // 2)closes the open file in the destruction function
  166. // 3)informs the apr_pool to clean the memory when the file is closed.
  167. //Note: please close an open file at the earliest convenience.
  168. // especially do not put some time-costly operations between open() and close().
  169. // otherwise it might lock the APRFilePool.
  170. //there are two different apr_pools the APRFile can use:
  171. // 1, a temporary pool passed to an APRFile function, which is used within this function and only once.
  172. // 2, a global pool.
  173. //
  174. class LL_COMMON_API LLAPRFile : boost::noncopyable
  175. {
  176. // make this non copyable since a copy closes the file
  177. private:
  178. apr_file_t* mFile ;
  179. LLVolatileAPRPool *mCurrentFilePoolp ; //currently in use apr_pool, could be one of them: sAPRFilePoolp, or a temp pool.
  180. public:
  181. LLAPRFile() ;
  182. LLAPRFile(const std::string& filename, apr_int32_t flags, LLVolatileAPRPool* pool = NULL);
  183. ~LLAPRFile() ;
  184. apr_status_t open(const std::string& filename, apr_int32_t flags, LLVolatileAPRPool* pool = NULL, S32* sizep = NULL);
  185. apr_status_t open(const std::string& filename, apr_int32_t flags, BOOL use_global_pool); //use gAPRPoolp.
  186. apr_status_t close() ;
  187. // Returns actual offset, -1 if seek fails
  188. S32 seek(apr_seek_where_t where, S32 offset);
  189. apr_status_t eof() { return apr_file_eof(mFile);}
  190. // Returns bytes read/written, 0 if read/write fails:
  191. S32 read(void* buf, S32 nbytes);
  192. S32 write(const void* buf, S32 nbytes);
  193. apr_file_t* getFileHandle() {return mFile;}
  194. private:
  195. apr_pool_t* getAPRFilePool(apr_pool_t* pool) ;
  196. //
  197. //*******************************************************************************************************************************
  198. //static components
  199. //
  200. public:
  201. static LLVolatileAPRPool *sAPRFilePoolp ; //a global apr_pool for APRFile, which is used only when local pool does not exist.
  202. private:
  203. static apr_file_t* open(const std::string& filename, LLVolatileAPRPool* pool, apr_int32_t flags);
  204. static apr_status_t close(apr_file_t* file, LLVolatileAPRPool* pool) ;
  205. static S32 seek(apr_file_t* file, apr_seek_where_t where, S32 offset);
  206. public:
  207. // returns false if failure:
  208. static bool remove(const std::string& filename, LLVolatileAPRPool* pool = NULL);
  209. static bool rename(const std::string& filename, const std::string& newname, LLVolatileAPRPool* pool = NULL);
  210. static bool isExist(const std::string& filename, LLVolatileAPRPool* pool = NULL, apr_int32_t flags = APR_READ);
  211. static S32 size(const std::string& filename, LLVolatileAPRPool* pool = NULL);
  212. static bool makeDir(const std::string& dirname, LLVolatileAPRPool* pool = NULL);
  213. static bool removeDir(const std::string& dirname, LLVolatileAPRPool* pool = NULL);
  214. // Returns bytes read/written, 0 if read/write fails:
  215. static S32 readEx(const std::string& filename, void *buf, S32 offset, S32 nbytes, LLVolatileAPRPool* pool = NULL);
  216. static S32 writeEx(const std::string& filename, void *buf, S32 offset, S32 nbytes, LLVolatileAPRPool* pool = NULL); // offset<0 means append
  217. //*******************************************************************************************************************************
  218. };
  219. /**
  220. * @brief Function which appropriately logs error or remains quiet on
  221. * APR_SUCCESS.
  222. * @return Returns <code>true</code> if status is an error condition.
  223. */
  224. bool LL_COMMON_API ll_apr_warn_status(apr_status_t status);
  225. /// There's a whole other APR error-message function if you pass a DSO handle.
  226. bool LL_COMMON_API ll_apr_warn_status(apr_status_t status, apr_dso_handle_t* handle);
  227. void LL_COMMON_API ll_apr_assert_status(apr_status_t status);
  228. void LL_COMMON_API ll_apr_assert_status(apr_status_t status, apr_dso_handle_t* handle);
  229. extern "C" LL_COMMON_API apr_pool_t* gAPRPoolp; // Global APR memory pool
  230. #endif // LL_LLAPR_H