1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <log4cxx/logstring.h>
19 #include <log4cxx/helpers/pool.h>
20 #include <apr_strings.h>
21 #include <log4cxx/helpers/exception.h>
22 #include <apr_pools.h>
23 #include <assert.h>
24 #if !defined(LOG4CXX)
25 #define LOG4CXX 1
26 #endif
27 #include <log4cxx/helpers/aprinitializer.h>
28
29 using namespace log4cxx::helpers;
30 using namespace log4cxx;
31
32
Pool()33 Pool::Pool() : pool(0), release(true)
34 {
35 apr_status_t stat = apr_pool_create(&pool, APRInitializer::getRootPool());
36
37 if (stat != APR_SUCCESS)
38 {
39 throw PoolException(stat);
40 }
41 }
42
Pool(apr_pool_t * p,bool release1)43 Pool::Pool(apr_pool_t* p, bool release1) : pool(p), release(release1)
44 {
45 assert(p != NULL);
46 }
47
~Pool()48 Pool::~Pool()
49 {
50 if (release)
51 {
52 apr_pool_destroy(pool);
53 }
54 }
55
56
getAPRPool()57 apr_pool_t* Pool::getAPRPool()
58 {
59 return pool;
60 }
61
create()62 apr_pool_t* Pool::create()
63 {
64 apr_pool_t* child;
65 apr_status_t stat = apr_pool_create(&child, pool);
66
67 if (stat != APR_SUCCESS)
68 {
69 throw PoolException(stat);
70 }
71
72 return child;
73 }
74
palloc(size_t size)75 void* Pool::palloc(size_t size)
76 {
77 return apr_palloc(pool, size);
78 }
79
pstralloc(size_t size)80 char* Pool::pstralloc(size_t size)
81 {
82 return (char*) palloc(size);
83 }
84
itoa(int n)85 char* Pool::itoa(int n)
86 {
87 return apr_itoa(pool, n);
88 }
89
pstrndup(const char * s,size_t len)90 char* Pool::pstrndup(const char* s, size_t len)
91 {
92 return apr_pstrndup(pool, s, len);
93 }
94
pstrdup(const char * s)95 char* Pool::pstrdup(const char* s)
96 {
97 return apr_pstrdup(pool, s);
98 }
99
pstrdup(const std::string & s)100 char* Pool::pstrdup(const std::string& s)
101 {
102 return apr_pstrndup(pool, s.data(), s.length());
103 }
104