Author: brane
Date: Thu Jul 11 01:51:04 2013
New Revision: 1502087
URL: http://svn.apache.org/r1502087
Log:
C++HL Infrastructure: Add C++ wrappers for APR pools, arrays and hash tables.
* build.conf (private-includes): Add C++HL private header files.
(libsvncxxhl): Include sources from the aprwrap subdirectory.
* subversion/bindings/cxxhl/src/aprwrap.hpp: New file.
Includes all the APR wrapper API headers.
* subversion/bindings/cxxhl/src/aprwrap/pool.hpp: New file.
(Pool): New class.
* subversion/bindings/cxxhl/src/aprwrap/array.hpp: New file
(Array, ConstArray): New template classes.
* subversion/bindings/cxxhl/src/aprwrap/hash.hpp: New file.
(Hash): New template class.
* subversion/bindings/cxxhl/src/aprwrap/impl.cpp: New file.
(Pool::get_root_pool, Hash<void, void>::iterate): Implement.
Added:
subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/
subversion/trunk/subversion/bindings/cxxhl/src/aprwrap.hpp
subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/array.hpp
subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/hash.hpp
subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/impl.cpp
subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/pool.hpp
Modified:
subversion/trunk/build.conf
Modified: subversion/trunk/build.conf
URL:
http://svn.apache.org/viewvc/subversion/trunk/build.conf?rev=1502087&r1=1502086&r2=1502087&view=diff
==============================================================================
--- subversion/trunk/build.conf (original)
+++ subversion/trunk/build.conf Thu Jul 11 01:51:04 2013
@@ -40,6 +40,8 @@ private-includes =
subversion/libsvn_delta/compose_delta.c
subversion/bindings/cxxhl/include/*.hpp
subversion/bindings/cxxhl/include/svncxxhl/*.hpp
+ subversion/bindings/cxxhl/src/*.hpp
+ subversion/bindings/cxxhl/src/aprwrap/*.hpp
subversion/libsvn_subr/utf8proc/utf8proc.h
subversion/libsvn_subr/utf8proc/utf8proc.c
subversion/libsvn_subr/utf8proc/utf8proc_data.c
@@ -666,7 +668,7 @@ type = lib
path = subversion/bindings/cxxhl
libs = libsvn_repos libsvn_client libsvn_wc libsvn_ra libsvn_delta libsvn_diff
libsvn_subr libsvn_fs aprutil apriconv apr
-sources = src/*.cpp
+sources = src/*.cpp src/aprwrap/*.cpp
install = cxxhl-lib
msvc-static = yes
compile-cmd = $(COMPILE_CXXHL_CXX)
Added: subversion/trunk/subversion/bindings/cxxhl/src/aprwrap.hpp
URL:
http://svn.apache.org/viewvc/subversion/trunk/subversion/bindings/cxxhl/src/aprwrap.hpp?rev=1502087&view=auto
==============================================================================
--- subversion/trunk/subversion/bindings/cxxhl/src/aprwrap.hpp (added)
+++ subversion/trunk/subversion/bindings/cxxhl/src/aprwrap.hpp Thu Jul 11
01:51:04 2013
@@ -0,0 +1,34 @@
+/**
+ * @copyright
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ * @endcopyright
+ */
+
+#ifndef SVN_CXXHL_PRIVATE_APRWRAP_H
+#define SVN_CXXHL_PRIVATE_APRWRAP_H
+
+// Expose the whole APR wrapper API and alias the namespace
+#include "aprwrap/pool.hpp"
+#include "aprwrap/hash.hpp"
+#include "aprwrap/array.hpp"
+
+namespace APR = ::apache::subversion::cxxhl::apr;
+
+#endif // SVN_CXXHL_PRIVATE_APRWRAP_H
Added: subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/array.hpp
URL:
http://svn.apache.org/viewvc/subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/array.hpp?rev=1502087&view=auto
==============================================================================
--- subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/array.hpp (added)
+++ subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/array.hpp Thu Jul 11
01:51:04 2013
@@ -0,0 +1,283 @@
+/**
+ * @copyright
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ * @endcopyright
+ */
+
+#ifndef SVN_CXXHL_PRIVATE_APRWRAP_ARRAY_H
+#define SVN_CXXHL_PRIVATE_APRWRAP_ARRAY_H
+
+#include <stdexcept>
+
+#include <apr_tables.h>
+#include "pool.hpp"
+
+#include "svn_private_config.h"
+
+namespace apache {
+namespace subversion {
+namespace cxxhl {
+namespace apr {
+
+/**
+ * Proxy for an APR array.
+ *
+ * This class does not own the array. The array's lifetime is tied to
+ * its pool. The caller is responsible for making sure that the
+ * array's lifetime is longer than this proxy object's.
+ */
+template<typename T> class Array
+{
+public:
+ typedef T value_type;
+ typedef int size_type;
+
+ /**
+ * Create and proxy a new APR array allocated from @a pool.
+ * Reserve space for @a nelts array elements.
+ */
+ explicit Array(const Pool& pool, size_type nelts = 0) throw()
+ : m_array(apr_array_make(pool.get(), nelts, sizeof(value_type)))
+ {}
+
+ /**
+ * Create a new proxy for the APR array wrapped by @a that.
+ */
+ explicit Array(const Array& that) throw()
+ : m_array(that.m_array)
+ {}
+
+ /**
+ * Create a new proxy for the APR array @a array.
+ */
+ explicit Array(apr_array_header_t* array) throw()
+ : m_array(array)
+ {}
+
+ /**
+ * @return The wrapped APR array.
+ */
+ apr_array_header_t* array() const throw()
+ {
+ return m_array;
+ }
+
+ /**
+ * @return the number of elements in the wrapped APR array.
+ */
+ size_type size() const throw()
+ {
+ return m_array->nelts;
+ }
+
+ /**
+ * @return An immutable reference to the array element at @a index.
+ */
+ const value_type& operator[](size_type index) const throw()
+ {
+ return APR_ARRAY_IDX(m_array, index, value_type);
+ }
+
+ /**
+ * @return An immutable reference to the array element at @a index.
+ * Like operator[] but perfoms a range check on the index.
+ */
+ const value_type& at(size_type index) const throw(std::out_of_range)
+ {
+ if (index < 0 || index > size())
+ throw std::out_of_range(_("APR array index is out of range"));
+ return (*this)[index];
+ }
+
+ /**
+ * @return A mutable reference to the array element at @a index.
+ */
+ value_type& operator[](size_type index) throw()
+ {
+ return APR_ARRAY_IDX(m_array, index, value_type);
+ }
+
+ /**
+ * @return A mutable reference to the array element at @a index.
+ * Like operator[] but perfoms a range check on the index.
+ */
+ value_type& at(size_type index) throw(std::out_of_range)
+ {
+ if (index < 0 || index > size())
+ throw std::out_of_range(_("APR array index is out of range"));
+ return (*this)[index];
+ }
+
+ /**
+ * Push @a value onto the end of the APR array.
+ */
+ void push(const value_type& value) throw()
+ {
+ APR_ARRAY_PUSH(m_array, value_type) = value;
+ }
+
+ /**
+ * Pop a value from the end of the array.
+ * @return A pointer to the value that was removed, or @c NULL if
+ * the array was empty.
+ */
+ value_type* pop() throw()
+ {
+ return static_cast<value_type*>(apr_array_pop(m_array));
+ }
+
+ /**
+ * Abstract base class for mutable iteration callback functors.
+ */
+ struct Iteration
+ {
+ /**
+ * Called by Array::iterate for every value in the array.
+ * @return @c false to terminate the iteration, @c true otherwise.
+ */
+ virtual bool operator() (value_type& value) = 0;
+ };
+
+ /**
+ * Iterate over all the values pairs in the array, invoking
+ * @a callback for each one.
+ */
+ void iterate(Iteration& callback)
+ {
+ for (size_type n = 0; n < size(); ++n)
+ if (!callback((*this)[n]))
+ break;
+ }
+
+ /**
+ * Abstract base class for immutable iteration callback functors.
+ */
+ struct ConstIteration
+ {
+ /**
+ * Called by Array::iterate for every value in the array.
+ * @return @c false to terminate the iteration, @c true otherwise.
+ */
+ virtual bool operator() (const value_type& value) = 0;
+ };
+
+ /**
+ * Iterate over all the values pairs in the array, invoking
+ * @a callback for each one.
+ */
+ void iterate(ConstIteration& callback) const
+ {
+ for (size_type n = 0; n < size(); ++n)
+ if (!callback((*this)[n]))
+ break;
+ }
+
+private:
+ apr_array_header_t* const m_array; ///< The wrapperd APR array.
+};
+
+
+/**
+ * Proxy for an immutable APR array.
+ */
+template<typename T>
+class ConstArray : private Array<T>
+{
+ typedef Array<T> inherited;
+
+public:
+ typedef typename inherited::value_type value_type;
+ typedef typename inherited::size_type size_type;
+
+ /**
+ * Create a new proxy for the APR array wrapped by @a that.
+ */
+ explicit ConstArray(const ConstArray& that) throw()
+ : inherited(that)
+ {}
+
+ /**
+ * Create a new proxy for the APR array wrapped by @a that.
+ */
+ explicit ConstArray(const inherited& that) throw()
+ : inherited(that)
+ {}
+
+ /**
+ * Create a new proxy for the APR array @a array.
+ */
+ explicit ConstArray(const apr_array_header_t* array) throw()
+ : inherited(const_cast<apr_array_header_t*>(array))
+ {}
+
+ /**
+ * @return The wrapped APR array.
+ */
+ const apr_array_header_t* array() const throw()
+ {
+ return inherited::array();
+ }
+
+ /**
+ * @return The number of elements in the wrapped APR array.
+ */
+ size_type size() const throw()
+ {
+ return inherited::size();
+ }
+
+ /**
+ * @return An immutable reference to the array element at @a index.
+ */
+ const value_type& operator[](size_type index) const throw()
+ {
+ return inherited::operator[](index);
+ }
+
+ /**
+ * @return An immutable reference to the array element at @a index.
+ * Like operator[] but perfoms a range check on the index.
+ */
+ const value_type& at(size_type index) const throw(std::out_of_range)
+ {
+ return inherited::at(index);
+ }
+
+ /**
+ * Abstract base class for immutable iteration callback functors.
+ */
+ typedef typename inherited::ConstIteration ConstIteration;
+
+ /**
+ * Iterate over all the values pairs in the array, invoking
+ * @a callback for each one.
+ */
+ void iterate(ConstIteration& callback) const
+ {
+ inherited::iterate(callback);
+ }
+};
+
+} // namespace apr
+} // namespace cxxhl
+} // namespace subversion
+} // namespace apache
+
+#endif // SVN_CXXHL_PRIVATE_APRWRAP_HASH_H
Added: subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/hash.hpp
URL:
http://svn.apache.org/viewvc/subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/hash.hpp?rev=1502087&view=auto
==============================================================================
--- subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/hash.hpp (added)
+++ subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/hash.hpp Thu Jul 11
01:51:04 2013
@@ -0,0 +1,361 @@
+/**
+ * @copyright
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ * @endcopyright
+ */
+
+#ifndef SVN_CXXHL_PRIVATE_APRWRAP_HASH_H
+#define SVN_CXXHL_PRIVATE_APRWRAP_HASH_H
+
+#include <apr_hash.h>
+#include "pool.hpp"
+
+namespace apache {
+namespace subversion {
+namespace cxxhl {
+namespace apr {
+
+// Template forward declaration
+template<typename T, typename V, apr_ssize_t KeySize = APR_HASH_KEY_STRING>
+class Hash;
+
+/**
+ * Proxy for an APR hash table.
+ * This is a template specialization for the default hash type.
+ */
+template<>
+class Hash<void, void>
+{
+public:
+ /**
+ * Proxy for a key in an APR hash table.
+ * This is a template specialization for the default hash key type.
+ */
+ class Key
+ {
+ public:
+ typedef const void* key_type;
+ typedef apr_ssize_t size_type;
+
+ /**
+ * Public constructor. Uses the template @a Size parameter.
+ */
+ Key(key_type key) throw()
+ : m_key(key), m_size(APR_HASH_KEY_STRING)
+ {}
+
+ /**
+ * Get the value of the key.
+ */
+ key_type get() const throw() { return m_key; }
+
+ /**
+ * Get the size of the key.
+ */
+ size_type size() const throw() { return m_size; }
+
+ protected:
+ /**
+ * Constructor used by the generic template, specializations and
+ * hash table wrapper. Does not make assumptions about the key size.
+ */
+ Key(key_type key, size_type size) throw()
+ : m_key(key), m_size(size)
+ {}
+
+ /**
+ * The hash table wrapper must be able to call the protected constructor.
+ */
+ friend class Hash;
+
+ private:
+ const key_type m_key; ///< Immutable reference to the key
+ const size_type m_size; ///< The size of the key
+ };
+
+public:
+ typedef Key::key_type key_type;
+ typedef void* value_type;
+
+ /**
+ * Create and proxy a new APR hash table in @a pool.
+ */
+ explicit Hash(const Pool& pool) throw()
+ : m_hash(apr_hash_make(pool.get()))
+ {}
+
+ /**
+ * Create and proxy a new APR hash table in @a pool, using @a
+ * hash_func as the hash function.
+ */
+ explicit Hash(const Pool& pool, apr_hashfunc_t hash_func) throw()
+ : m_hash(apr_hash_make_custom(pool.get(), hash_func))
+ {}
+
+ /**
+ * Create a new proxy for the APR hash table wrapped by @a that.
+ */
+ explicit Hash(const Hash& that) throw()
+ : m_hash(that.m_hash)
+ {}
+
+ /**
+ * Create a proxy for the APR hash table @a hash.
+ */
+ explicit Hash(apr_hash_t* hash)
+ : m_hash(hash)
+ {}
+
+ /**
+ * Return the wrapped APR hash table.
+ */
+ apr_hash_t* hash() const throw() { return m_hash; }
+
+ /**
+ * Set @a key = @a value in the wrapped hash table.
+ */
+ void set(const Key& key, value_type value) throw()
+ {
+ apr_hash_set(m_hash, key.get(), key.size(), value);
+ }
+
+ /**
+ * Retreive the value associated with @a key.
+ */
+ value_type get(const Key& key) const throw()
+ {
+ return apr_hash_get(m_hash, key.get(), key.size());
+ }
+
+ /**
+ * Delete the entry for @a key.
+ */
+ void del(const Key& key) throw()
+ {
+ apr_hash_set(m_hash, key.get(), key.size(), NULL);
+ }
+
+ /**
+ * Abstract base class for iteration callback functors.
+ */
+ struct Iteration
+ {
+ /**
+ * Called by Hash::iterate for every key-value pair in the hash table.
+ * @return @c false to terminate the iteration, @c true otherwise.
+ */
+ virtual bool operator() (const Key& key, value_type value) = 0;
+ };
+
+ /**
+ * Iterate over all the key-value pairs in the hash table, invoking
+ * @a callback for each pair.
+ * Uses @a scratch_pool for temporary allocations.
+ */
+ void iterate(Iteration& callback, const Pool& scratch_pool);
+
+protected:
+ typedef const void* const_value_type;
+
+ /**
+ * Set @a key = @a value in the wrapped hash table. Overloaded for
+ * deroved template instantiations with constant values; for
+ * example, Hash<char, const char>.
+ */
+ void set(const Key& key, const_value_type const_value) throw()
+ {
+ set(key, const_cast<value_type>(const_value));
+ }
+
+private:
+ apr_hash_t* const m_hash; ///< The wrapped APR hash table.
+};
+
+
+/**
+ * Proxy for an APR hash table: the template.
+ *
+ * This class does not own the hash table. The hash table's lifetime
+ * is tied to its pool. The caller is responsible for making sure that
+ * the hash table's lifetime is longer than this proxy object's.
+ */
+template<typename K, typename V, apr_ssize_t KeySize>
+class Hash : private Hash<void, void>
+{
+ typedef Hash<void, void> inherited;
+
+public:
+ /**
+ * Proxy for a key in an APR hash table.
+ *
+ * This class does not own the key; it is the caller's responsibility
+ * to make sure that the key's lifetime is longer than the proxy
+ * object's.
+ */
+ class Key : private inherited::Key
+ {
+ typedef Hash<void, void>::Key inherited;
+
+ public:
+ typedef const K* key_type;
+ typedef inherited::size_type size_type;
+
+ Key(key_type key) throw()
+ : inherited(key, KeySize)
+ {}
+
+ /**
+ * Get the value of the key.
+ */
+ key_type get() const throw()
+ {
+ return static_cast<key_type>(inherited::get());
+ }
+
+ /**
+ * Get the size of the key.
+ */
+ size_type size() const throw()
+ {
+ return inherited::size();
+ }
+
+ private:
+ /**
+ * Constructor used by the hash table wrapper.
+ * Does not make assumptions about the key size.
+ */
+ Key(key_type key, size_type size) throw()
+ : inherited(key, size)
+ {}
+
+ /**
+ * The hash table wrapper must be able to call the private constructor.
+ */
+ friend class Hash;
+ };
+
+public:
+ typedef typename Key::key_type key_type;
+ typedef V* value_type;
+
+ /**
+ * Create and proxy a new APR hash table allocated from @a pool.
+ */
+ explicit Hash(const Pool& pool) throw()
+ : inherited(pool)
+ {}
+
+ /**
+ * Create and proxy a new APR hash table allocated from @a pool,
+ * using @a hash_func as the hash function.
+ */
+ explicit Hash(const Pool& pool, apr_hashfunc_t hash_func) throw()
+ : inherited(pool, hash_func)
+ {}
+
+ /**
+ * Create a new proxy for the APR hash table wrapped by @a that.
+ */
+ explicit Hash(const Hash& that) throw()
+ : inherited(that)
+ {}
+
+ /**
+ * Create a proxy for the APR hash table @a hash.
+ */
+ explicit Hash(apr_hash_t* hash)
+ : inherited(hash)
+ {}
+
+ /**
+ * @return The wrapped APR hash table.
+ */
+ apr_hash_t* hash() const throw() { return inherited::hash(); }
+
+ /**
+ * Set @a key = @a value in the wrapped hash table.
+ */
+ void set(const Key& key, value_type value) throw()
+ {
+ inherited::set(inherited::Key(key), value);
+ }
+
+ /**
+ * Retreive the value associated with @a key.
+ */
+ value_type get(const Key& key) const throw()
+ {
+ return static_cast<value_type>(inherited::get(inherited::Key(key)));
+ }
+
+ /**
+ * Delete the entry for @a key.
+ */
+ void del(const Key& key) throw()
+ {
+ inherited::del(inherited::Key(key));
+ }
+
+ /**
+ * Abstract base class for iteration callback functors.
+ */
+ struct Iteration : protected inherited::Iteration
+ {
+ /**
+ * Called by Hash::iterate for every key-value pair in the hash table.
+ * @return @c false to terminate the iteration, @c true otherwise.
+ */
+ virtual bool operator() (const Key& key, value_type value) = 0;
+
+ private:
+ friend void Hash::iterate(Iteration& callback, const Pool& scratch_pool);
+
+ /**
+ * Implementation of the derived virtual operator().
+ * Adapts the callback to the instantiated types.
+ */
+ virtual bool operator() (const inherited::Key& raw_key,
+ inherited::value_type raw_value)
+ {
+ return (*this)(
+ Key(static_cast<key_type>(raw_key.get()), raw_key.size()),
+ static_cast<value_type>(raw_value));
+ }
+ };
+
+ /**
+ * Iterate over all the key-value pairs in the hash table, invoking
+ * @a callback for each pair.
+ * Uses @a scratch_pool for temporary allocations.
+ */
+ void iterate(Iteration& callback, const Pool& scratch_pool)
+ {
+ inherited::iterate(callback, scratch_pool);
+ }
+};
+
+
+} // namespace apr
+} // namespace cxxhl
+} // namespace subversion
+} // namespace apache
+
+#endif // SVN_CXXHL_PRIVATE_APRWRAP_HASH_H
Added: subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/impl.cpp
URL:
http://svn.apache.org/viewvc/subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/impl.cpp?rev=1502087&view=auto
==============================================================================
--- subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/impl.cpp (added)
+++ subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/impl.cpp Thu Jul 11
01:51:04 2013
@@ -0,0 +1,110 @@
+/**
+ * @copyright
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ * @endcopyright
+ */
+
+#include <apr_time.h>
+
+#include "private/svn_atomic.h"
+#include "svn_private_config.h"
+#undef TRUE
+#undef FALSE
+
+#include "pool.hpp"
+#include "hash.hpp"
+
+namespace apache {
+namespace subversion {
+namespace cxxhl {
+namespace apr {
+
+//
+// Pool implementation
+//
+
+apr_pool_t* Pool::get_root_pool() throw(cxxhl::InternalError)
+{
+ static const svn_atomic_t NONE = 0;
+ static const svn_atomic_t START = 1;
+ static const svn_atomic_t DONE = 2;
+
+ static volatile svn_atomic_t init_state = NONE;
+ static apr_pool_t* root_pool = NULL;
+
+ svn_atomic_t state = svn_atomic_cas(&init_state, START, NONE);
+
+ switch (state)
+ {
+ case DONE:
+ // The root pool has already been initialized.
+ return root_pool;
+
+ case START:
+ // Another thread is currently initializing the pool; Spin and
+ // wait for it to finish, with exponential backoff, but no
+ // longer than half a second.
+ for (unsigned shift = 0; state == START && shift < 8; ++shift)
+ {
+ apr_sleep((APR_USEC_PER_SEC / 1000) << shift);
+ state = svn_atomic_cas(&init_state, NONE, NONE);
+ }
+ if (state == START)
+ throw cxxhl::InternalError(
+ _("APR pool initialization failed: Timed out"));
+ return root_pool;
+
+ case NONE:
+ // Initialize the root pool and release the lock.
+ // We'll assume that we always need thread-safe allocation.
+ root_pool = svn_pool_create_ex(NULL, svn_pool_create_allocator(true));
+ svn_atomic_cas(&init_state, DONE, START);
+ return root_pool;
+
+ default:
+ throw cxxhl::InternalError(
+ _("APR pool initialization failed: Invalid state"));
+ }
+}
+
+//
+// Hash implementation
+//
+
+void Hash<void, void>::iterate(Hash<void, void>::Iteration& callback,
+ const Pool& pool)
+{
+ for (apr_hash_index_t* hi = apr_hash_first(pool.get(), m_hash);
+ hi; hi = apr_hash_next(hi))
+ {
+ key_type key;
+ value_type val;
+ Key::size_type klen;
+
+ apr_hash_this(hi, &key, &klen, &val);
+ if (!callback(Key(key, klen), val))
+ break;
+ }
+}
+
+} // namespace apr
+} // namespace cxxhl
+} // namespace subversion
+} // namespace apache
Added: subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/pool.hpp
URL:
http://svn.apache.org/viewvc/subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/pool.hpp?rev=1502087&view=auto
==============================================================================
--- subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/pool.hpp (added)
+++ subversion/trunk/subversion/bindings/cxxhl/src/aprwrap/pool.hpp Thu Jul 11
01:51:04 2013
@@ -0,0 +1,105 @@
+/**
+ * @copyright
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ * @endcopyright
+ */
+
+#ifndef SVN_CXXHL_PRIVATE_APRWRAP_POOL_H
+#define SVN_CXXHL_PRIVATE_APRWRAP_POOL_H
+
+#include <cstdlib>
+
+#include "svncxxhl/exception.hpp"
+
+#include "svn_pools.h"
+#undef TRUE
+#undef FALSE
+
+namespace apache {
+namespace subversion {
+namespace cxxhl {
+namespace apr {
+
+/**
+ * Encapsulates an APR pool.
+ */
+class Pool
+{
+public:
+ /**
+ * Create a pool as a child of the applications' root pool.
+ */
+ Pool() throw(cxxhl::InternalError)
+ : m_pool(svn_pool_create(get_root_pool()))
+ {}
+
+ /**
+ * Create a pool as a child of @a parent.
+ */
+ Pool(const Pool* parent) throw()
+ : m_pool(svn_pool_create(parent->m_pool))
+ {}
+
+ /**
+ * Destroy the pool.
+ */
+ ~Pool() throw() { svn_pool_destroy(m_pool); }
+
+ /**
+ * Clear the pool.
+ */
+ void clear() throw() { apr_pool_clear(m_pool); }
+
+ /**
+ * Retuurn a pool pointer that can be used by the C APIs.
+ */
+ apr_pool_t* get() const throw() { return m_pool; }
+
+ /**
+ * Allocate space for @a count elements of type @a T from the pool.
+ * The contents of the allocated buffer will contain unspecified data.
+ */
+ template<typename T>
+ T* alloc(std::size_t count) throw()
+ {
+ return static_cast<T*>(apr_palloc(m_pool, count * sizeof(T)));
+ }
+
+ /**
+ * Allocate space for @a count elements of type @a T from the pool.
+ * The contents of the allocated buffer will be initialized to zero.
+ */
+ template<typename T>
+ T* allocz(std::size_t count) throw()
+ {
+ return static_cast<T*>(apr_pcalloc(m_pool, count * sizeof(T)));
+ }
+
+private:
+ static apr_pool_t* get_root_pool() throw(cxxhl::InternalError);
+ apr_pool_t* const m_pool;
+};
+
+} // namespace apr
+} // namespace cxxhl
+} // namespace subversion
+} // namespace apache
+
+#endif // SVN_CXXHL_PRIVATE_APRWRAP_POOL_H