PageRenderTime 32ms CodeModel.GetById 16ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 29#ifndef LL_LLAPR_H
 30#define LL_LLAPR_H
 31
 32#if LL_LINUX || LL_SOLARIS
 33#include <sys/param.h>  // Need PATH_MAX in APR headers...
 34#endif
 35#if LL_WINDOWS
 36	// Limit Windows API to small and manageable set.
 37	// If you get undefined symbols, find the appropriate
 38	// Windows header file and include that in your .cpp file.
 39	#define WIN32_LEAN_AND_MEAN
 40	#include <winsock2.h>
 41	#include <windows.h>
 42#endif
 43
 44#include <boost/noncopyable.hpp>
 45
 46#include "apr_thread_proc.h"
 47#include "apr_thread_mutex.h"
 48#include "apr_getopt.h"
 49#include "apr_signal.h"
 50#include "apr_atomic.h"
 51#include "llstring.h"
 52
 53extern LL_COMMON_API apr_thread_mutex_t* gLogMutexp;
 54extern apr_thread_mutex_t* gCallStacksLogMutexp;
 55
 56struct apr_dso_handle_t;
 57
 58/** 
 59 * @brief initialize the common apr constructs -- apr itself, the
 60 * global pool, and a mutex.
 61 */
 62void LL_COMMON_API ll_init_apr();
 63
 64/** 
 65 * @brief Cleanup those common apr constructs.
 66 */
 67void LL_COMMON_API ll_cleanup_apr();
 68
 69//
 70//LL apr_pool
 71//manage apr_pool_t, destroy allocated apr_pool in the destruction function.
 72//
 73class LL_COMMON_API LLAPRPool
 74{
 75public:
 76	LLAPRPool(apr_pool_t *parent = NULL, apr_size_t size = 0, BOOL releasePoolFlag = TRUE) ;
 77	virtual ~LLAPRPool() ;
 78
 79	virtual apr_pool_t* getAPRPool() ;
 80	apr_status_t getStatus() {return mStatus ; }
 81
 82protected:
 83	void releaseAPRPool() ;
 84	void createAPRPool() ;
 85
 86protected:
 87	apr_pool_t*  mPool ;              //pointing to an apr_pool
 88	apr_pool_t*  mParent ;			  //parent pool
 89	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.
 90	apr_status_t mStatus ;            //status when creating the pool
 91	BOOL         mReleasePoolFlag ;   //if set, mPool is destroyed when LLAPRPool is deleted. default value is true.
 92};
 93
 94//
 95//volatile LL apr_pool
 96//which clears memory automatically.
 97//so it can not hold static data or data after memory is cleared
 98//
 99class LL_COMMON_API LLVolatileAPRPool : public LLAPRPool
100{
101public:
102	LLVolatileAPRPool(BOOL is_local = TRUE, apr_pool_t *parent = NULL, apr_size_t size = 0, BOOL releasePoolFlag = TRUE);
103	virtual ~LLVolatileAPRPool();
104
105	/*virtual*/ apr_pool_t* getAPRPool() ; //define this virtual function to avoid any mistakenly calling LLAPRPool::getAPRPool().
106	apr_pool_t* getVolatileAPRPool() ;	
107	void        clearVolatileAPRPool() ;
108
109	BOOL        isFull() ;
110	
111private:
112	S32 mNumActiveRef ; //number of active pointers pointing to the apr_pool.
113	S32 mNumTotalRef ;  //number of total pointers pointing to the apr_pool since last creating.  
114
115	apr_thread_mutex_t *mMutexp;
116	apr_pool_t         *mMutexPool;
117} ;
118
119/** 
120 * @class LLScopedLock
121 * @brief Small class to help lock and unlock mutexes.
122 *
123 * This class is used to have a stack level lock once you already have
124 * an apr mutex handy. The constructor handles the lock, and the
125 * destructor handles the unlock. Instances of this class are
126 * <b>not</b> thread safe.
127 */
128class LL_COMMON_API LLScopedLock : private boost::noncopyable
129{
130public:
131	/**
132	 * @brief Constructor which accepts a mutex, and locks it.
133	 *
134	 * @param mutex An allocated APR mutex. If you pass in NULL,
135	 * this wrapper will not lock.
136	 */
137	LLScopedLock(apr_thread_mutex_t* mutex);
138
139	/**
140	 * @brief Destructor which unlocks the mutex if still locked.
141	 */
142	~LLScopedLock();
143
144	/** 
145	 * @brief Check lock.
146	 */
147	bool isLocked() const { return mLocked; }
148
149	/** 
150	 * @brief This method unlocks the mutex.
151	 */
152	void unlock();
153
154protected:
155	bool mLocked;
156	apr_thread_mutex_t* mMutex;
157};
158
159template <typename Type> class LLAtomic32
160{
161public:
162	LLAtomic32<Type>() {};
163	LLAtomic32<Type>(Type x) {apr_atomic_set32(&mData, apr_uint32_t(x)); };
164	~LLAtomic32<Type>() {};
165
166	operator const Type() { apr_uint32_t data = apr_atomic_read32(&mData); return Type(data); }
167	Type operator =(const Type& x) { apr_atomic_set32(&mData, apr_uint32_t(x)); return Type(mData); }
168	void operator -=(Type x) { apr_atomic_sub32(&mData, apr_uint32_t(x)); }
169	void operator +=(Type x) { apr_atomic_add32(&mData, apr_uint32_t(x)); }
170	Type operator ++(int) { return apr_atomic_inc32(&mData); } // Type++
171	Type operator --(int) { return apr_atomic_dec32(&mData); } // Type--
172	
173private:
174	apr_uint32_t mData;
175};
176
177typedef LLAtomic32<U32> LLAtomicU32;
178typedef LLAtomic32<S32> LLAtomicS32;
179
180// File IO convenience functions.
181// Returns NULL if the file fails to open, sets *sizep to file size if not NULL
182// abbreviated flags
183#define LL_APR_R (APR_READ) // "r"
184#define LL_APR_W (APR_CREATE|APR_TRUNCATE|APR_WRITE) // "w"
185#define LL_APR_RB (APR_READ|APR_BINARY) // "rb"
186#define LL_APR_WB (APR_CREATE|APR_TRUNCATE|APR_WRITE|APR_BINARY) // "wb"
187#define LL_APR_RPB (APR_READ|APR_WRITE|APR_BINARY) // "r+b"
188#define LL_APR_WPB (APR_CREATE|APR_TRUNCATE|APR_READ|APR_WRITE|APR_BINARY) // "w+b"
189
190//
191//apr_file manager
192//which: 1)only keeps one file open;
193//       2)closes the open file in the destruction function
194//       3)informs the apr_pool to clean the memory when the file is closed.
195//Note: please close an open file at the earliest convenience. 
196//      especially do not put some time-costly operations between open() and close().
197//      otherwise it might lock the APRFilePool.
198//there are two different apr_pools the APRFile can use:
199//      1, a temporary pool passed to an APRFile function, which is used within this function and only once.
200//      2, a global pool.
201//
202
203class LL_COMMON_API LLAPRFile : boost::noncopyable
204{
205	// make this non copyable since a copy closes the file
206private:
207	apr_file_t* mFile ;
208	LLVolatileAPRPool *mCurrentFilePoolp ; //currently in use apr_pool, could be one of them: sAPRFilePoolp, or a temp pool. 
209
210public:
211	LLAPRFile() ;
212	LLAPRFile(const std::string& filename, apr_int32_t flags, LLVolatileAPRPool* pool = NULL);
213	~LLAPRFile() ;
214	
215	apr_status_t open(const std::string& filename, apr_int32_t flags, LLVolatileAPRPool* pool = NULL, S32* sizep = NULL);
216	apr_status_t open(const std::string& filename, apr_int32_t flags, BOOL use_global_pool); //use gAPRPoolp.
217	apr_status_t close() ;
218
219	// Returns actual offset, -1 if seek fails
220	S32 seek(apr_seek_where_t where, S32 offset);
221	apr_status_t eof() { return apr_file_eof(mFile);}
222
223	// Returns bytes read/written, 0 if read/write fails:
224	S32 read(void* buf, S32 nbytes);
225	S32 write(const void* buf, S32 nbytes);
226	
227	apr_file_t* getFileHandle() {return mFile;}	
228
229private:
230	apr_pool_t* getAPRFilePool(apr_pool_t* pool) ;	
231	
232//
233//*******************************************************************************************************************************
234//static components
235//
236public:
237	static LLVolatileAPRPool *sAPRFilePoolp ; //a global apr_pool for APRFile, which is used only when local pool does not exist.
238
239private:
240	static apr_file_t* open(const std::string& filename, LLVolatileAPRPool* pool, apr_int32_t flags);
241	static apr_status_t close(apr_file_t* file, LLVolatileAPRPool* pool) ;
242	static S32 seek(apr_file_t* file, apr_seek_where_t where, S32 offset);
243public:
244	// returns false if failure:
245	static bool remove(const std::string& filename, LLVolatileAPRPool* pool = NULL);
246	static bool rename(const std::string& filename, const std::string& newname, LLVolatileAPRPool* pool = NULL);
247	static bool isExist(const std::string& filename, LLVolatileAPRPool* pool = NULL, apr_int32_t flags = APR_READ);
248	static S32 size(const std::string& filename, LLVolatileAPRPool* pool = NULL);
249	static bool makeDir(const std::string& dirname, LLVolatileAPRPool* pool = NULL);
250	static bool removeDir(const std::string& dirname, LLVolatileAPRPool* pool = NULL);
251
252	// Returns bytes read/written, 0 if read/write fails:
253	static S32 readEx(const std::string& filename, void *buf, S32 offset, S32 nbytes, LLVolatileAPRPool* pool = NULL);	
254	static S32 writeEx(const std::string& filename, void *buf, S32 offset, S32 nbytes, LLVolatileAPRPool* pool = NULL); // offset<0 means append
255//*******************************************************************************************************************************
256};
257
258/**
259 * @brief Function which appropriately logs error or remains quiet on
260 * APR_SUCCESS.
261 * @return Returns <code>true</code> if status is an error condition.
262 */
263bool LL_COMMON_API ll_apr_warn_status(apr_status_t status);
264/// There's a whole other APR error-message function if you pass a DSO handle.
265bool LL_COMMON_API ll_apr_warn_status(apr_status_t status, apr_dso_handle_t* handle);
266
267void LL_COMMON_API ll_apr_assert_status(apr_status_t status);
268void LL_COMMON_API ll_apr_assert_status(apr_status_t status, apr_dso_handle_t* handle);
269
270extern "C" LL_COMMON_API apr_pool_t* gAPRPoolp; // Global APR memory pool
271
272#endif // LL_LLAPR_H