aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llcommon/llapr.cpp
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llcommon/llapr.cpp
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to '')
-rw-r--r--linden/indra/llcommon/llapr.cpp220
1 files changed, 220 insertions, 0 deletions
diff --git a/linden/indra/llcommon/llapr.cpp b/linden/indra/llcommon/llapr.cpp
new file mode 100644
index 0000000..665ee75
--- /dev/null
+++ b/linden/indra/llcommon/llapr.cpp
@@ -0,0 +1,220 @@
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 * Copyright (c) 2004-2007, Linden Research, Inc.
8 *
9 * The source code in this file ("Source Code") is provided by Linden Lab
10 * to you under the terms of the GNU General Public License, version 2.0
11 * ("GPL"), unless you have obtained a separate licensing agreement
12 * ("Other License"), formally executed by you and Linden Lab. Terms of
13 * the GPL can be found in doc/GPL-license.txt in this distribution, or
14 * online at http://secondlife.com/developers/opensource/gplv2
15 *
16 * There are special exceptions to the terms and conditions of the GPL as
17 * it is applied to this Source Code. View the full text of the exception
18 * in the file doc/FLOSS-exception.txt in this software distribution, or
19 * online at http://secondlife.com/developers/opensource/flossexception
20 *
21 * By copying, modifying or distributing this software, you acknowledge
22 * that you have read and understood your obligations described above,
23 * and agree to abide by those obligations.
24 *
25 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
26 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
27 * COMPLETENESS OR PERFORMANCE.
28 */
29
30#include "linden_common.h"
31#include "llapr.h"
32
33apr_pool_t *gAPRPoolp = NULL; // Global APR memory pool
34apr_thread_mutex_t *gLogMutexp = NULL;
35
36
37void ll_init_apr()
38{
39 if (!gAPRPoolp)
40 {
41 // Initialize APR and create the global pool
42 apr_initialize();
43 apr_pool_create(&gAPRPoolp, NULL);
44
45 // Initialize the logging mutex
46 apr_thread_mutex_create(&gLogMutexp, APR_THREAD_MUTEX_DEFAULT, gAPRPoolp);
47 }
48}
49
50
51void ll_cleanup_apr()
52{
53 llinfos << "Cleaning up APR" << llendl;
54
55 if (gLogMutexp)
56 {
57 // Clean up the logging mutex
58
59 // All other threads NEED to be done before we clean up APR, so this is okay.
60 apr_thread_mutex_destroy(gLogMutexp);
61 gLogMutexp = NULL;
62 }
63 if (gAPRPoolp)
64 {
65 apr_pool_destroy(gAPRPoolp);
66 gAPRPoolp = NULL;
67 }
68 apr_terminate();
69}
70
71//
72// LLScopedLock
73//
74LLScopedLock::LLScopedLock(apr_thread_mutex_t* mutex) : mMutex(mutex)
75{
76 if(mutex)
77 {
78 if(ll_apr_warn_status(apr_thread_mutex_lock(mMutex)))
79 {
80 mLocked = false;
81 }
82 else
83 {
84 mLocked = true;
85 }
86 }
87 else
88 {
89 mLocked = false;
90 }
91}
92
93LLScopedLock::~LLScopedLock()
94{
95 unlock();
96}
97
98void LLScopedLock::unlock()
99{
100 if(mLocked)
101 {
102 if(!ll_apr_warn_status(apr_thread_mutex_unlock(mMutex)))
103 {
104 mLocked = false;
105 }
106 }
107}
108
109//
110// Misc functions
111//
112bool ll_apr_warn_status(apr_status_t status)
113{
114 if(APR_SUCCESS == status) return false;
115 char buf[MAX_STRING]; /* Flawfinder: ignore */
116 llwarns << "APR: " << apr_strerror(status, buf, MAX_STRING) << llendl;
117 return true;
118}
119
120void ll_apr_assert_status(apr_status_t status)
121{
122 llassert(ll_apr_warn_status(status) == false);
123}
124
125apr_file_t* ll_apr_file_open(const LLString& filename, apr_int32_t flags, S32* sizep)
126{
127 apr_file_t* apr_file;
128 apr_status_t s;
129 s = apr_file_open(&apr_file, filename.c_str(), flags, APR_OS_DEFAULT, gAPRPoolp);
130 if (s != APR_SUCCESS)
131 {
132 if (sizep)
133 {
134 *sizep = 0;
135 }
136 return NULL;
137 }
138
139 if (sizep)
140 {
141 S32 file_size = 0;
142 apr_off_t offset = 0;
143 if (apr_file_seek(apr_file, APR_END, &offset) == APR_SUCCESS)
144 {
145 file_size = (S32)offset;
146 offset = 0;
147 apr_file_seek(apr_file, APR_SET, &offset);
148 }
149 *sizep = file_size;
150 }
151
152 return apr_file;
153}
154
155S32 ll_apr_file_read(apr_file_t* apr_file, void *buf, S32 nbytes)
156{
157 apr_size_t sz = nbytes;
158 apr_status_t s = apr_file_read(apr_file, buf, &sz);
159 if (s != APR_SUCCESS)
160 {
161 return 0;
162 }
163 else
164 {
165 return (S32)sz;
166 }
167}
168
169
170S32 ll_apr_file_write(apr_file_t* apr_file, const void *buf, S32 nbytes)
171{
172 apr_size_t sz = nbytes;
173 apr_status_t s = apr_file_write(apr_file, buf, &sz);
174 if (s != APR_SUCCESS)
175 {
176 return 0;
177 }
178 else
179 {
180 return (S32)sz;
181 }
182}
183
184S32 ll_apr_file_seek(apr_file_t* apr_file, apr_seek_where_t where, S32 offset)
185{
186 apr_off_t apr_offset = offset;
187 apr_status_t s = apr_file_seek(apr_file, where, &apr_offset);
188 if (s != APR_SUCCESS)
189 {
190 return -1;
191 }
192 else
193 {
194 return (S32)apr_offset;
195 }
196}
197
198bool ll_apr_file_remove(const LLString& filename)
199{
200 apr_status_t s;
201 s = apr_file_remove(filename.c_str(), gAPRPoolp);
202 if (s != APR_SUCCESS)
203 {
204 llwarns << "ll_apr_file_remove failed on file: " << filename << llendl;
205 return false;
206 }
207 return true;
208}
209
210bool ll_apr_file_rename(const LLString& filename, const LLString& newname)
211{
212 apr_status_t s;
213 s = apr_file_rename(filename.c_str(), newname.c_str(), gAPRPoolp);
214 if (s != APR_SUCCESS)
215 {
216 llwarns << "ll_apr_file_rename failed on file: " << filename << llendl;
217 return false;
218 }
219 return true;
220}