1
// Copyright 2010 Google Inc.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
15
#ifndef MOD_SPDY_APACHE_POOL_UTIL_H_
16
#define MOD_SPDY_APACHE_POOL_UTIL_H_
20
#include "apr_pools.h"
21
#include "base/logging.h"
26
* Wrapper object that creates a new apr_pool_t and then destroys it when
27
* deleted (handy for creating a local apr_pool_t on the stack).
31
* apr_status_t SomeFunction() {
33
* char* buffer = apr_palloc(local.pool(), 1024);
34
* // Do stuff with buffer; it will dealloc when we leave this scope.
40
LocalPool() : pool_(NULL) {
41
// apr_pool_create() only fails if we run out of memory. However, we make
42
// no effort elsewhere in this codebase to deal with running out of memory,
43
// so there's no sense in dealing with it here. Instead, just assert that
44
// pool creation succeeds.
45
const apr_status_t status = apr_pool_create(&pool_, NULL);
46
CHECK(status == APR_SUCCESS);
51
apr_pool_destroy(pool_);
54
apr_pool_t* pool() const { return pool_; }
59
DISALLOW_COPY_AND_ASSIGN(LocalPool);
62
// Helper function for PoolRegisterDelete.
64
apr_status_t DeletionFunction(void* object) {
65
delete static_cast<T*>(object);
69
// Register a C++ object to be deleted with a pool.
71
void PoolRegisterDelete(apr_pool_t* pool, T* object) {
72
// Note that the "child cleanup" argument below doesn't apply to us, so we
73
// use apr_pool_cleanup_null, which is a no-op cleanup function.
74
apr_pool_cleanup_register(pool, object,
75
DeletionFunction<T>, // cleanup function
76
apr_pool_cleanup_null); // child cleanup
79
// Un-register a C++ object from deletion with a pool. Essentially, this
80
// undoes a previous call to PoolRegisterDelete with the same pool and object.
82
void PoolUnregisterDelete(apr_pool_t* pool, T* object) {
83
apr_pool_cleanup_kill(pool, object, DeletionFunction<T>);
86
// Return a string describing the given APR status code.
87
std::string AprStatusString(apr_status_t status);
89
} // namespace mod_spdy
91
#endif // MOD_SPDY_APACHE_POOL_UTIL_H_