pool.c revision 299742
1/* pool.c:  pool wrappers for Subversion
2 *
3 * ====================================================================
4 *    Licensed to the Apache Software Foundation (ASF) under one
5 *    or more contributor license agreements.  See the NOTICE file
6 *    distributed with this work for additional information
7 *    regarding copyright ownership.  The ASF licenses this file
8 *    to you under the Apache License, Version 2.0 (the
9 *    "License"); you may not use this file except in compliance
10 *    with the License.  You may obtain a copy of the License at
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
14 *    Unless required by applicable law or agreed to in writing,
15 *    software distributed under the License is distributed on an
16 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 *    KIND, either express or implied.  See the License for the
18 *    specific language governing permissions and limitations
19 *    under the License.
20 * ====================================================================
21 */
22
23
24
25#include <stdarg.h>
26#include <stdlib.h>
27#include <stdio.h>
28
29#include <apr_general.h>
30#include <apr_pools.h>
31#include <apr_thread_mutex.h>
32
33#include "svn_pools.h"
34
35
36#if APR_POOL_DEBUG
37/* file_line for the non-debug case. */
38static const char SVN_FILE_LINE_UNDEFINED[] = "svn:<undefined>";
39#endif /* APR_POOL_DEBUG */
40
41
42
43/*-----------------------------------------------------------------*/
44
45
46/* Pool allocation handler which just aborts, since we aren't generally
47   prepared to deal with out-of-memory errors.
48 */
49static int
50abort_on_pool_failure(int retcode)
51{
52  /* Don't translate this string! It requires memory allocation to do so!
53     And we don't have any of it... */
54  printf("libsvn: Out of memory - terminating application.\n");
55  abort();
56  return 0; /* not reached */
57}
58
59
60#if APR_POOL_DEBUG
61#undef svn_pool_create_ex
62#endif /* APR_POOL_DEBUG */
63
64#if !APR_POOL_DEBUG
65
66apr_pool_t *
67svn_pool_create_ex(apr_pool_t *parent_pool, apr_allocator_t *allocator)
68{
69  apr_pool_t *pool;
70  apr_pool_create_ex(&pool, parent_pool, abort_on_pool_failure, allocator);
71  return pool;
72}
73
74/* Wrapper that ensures binary compatibility */
75apr_pool_t *
76svn_pool_create_ex_debug(apr_pool_t *pool, apr_allocator_t *allocator,
77                         const char *file_line)
78{
79  return svn_pool_create_ex(pool, allocator);
80}
81
82#else /* APR_POOL_DEBUG */
83
84apr_pool_t *
85svn_pool_create_ex_debug(apr_pool_t *parent_pool, apr_allocator_t *allocator,
86                         const char *file_line)
87{
88  apr_pool_t *pool;
89  apr_pool_create_ex_debug(&pool, parent_pool, abort_on_pool_failure,
90                           allocator, file_line);
91  return pool;
92}
93
94/* Wrapper that ensures binary compatibility */
95apr_pool_t *
96svn_pool_create_ex(apr_pool_t *pool, apr_allocator_t *allocator)
97{
98  return svn_pool_create_ex_debug(pool, allocator, SVN_FILE_LINE_UNDEFINED);
99}
100
101#endif /* APR_POOL_DEBUG */
102
103apr_allocator_t *
104svn_pool_create_allocator(svn_boolean_t thread_safe)
105{
106  apr_allocator_t *allocator;
107  apr_pool_t *pool;
108
109  /* create the allocator and limit it's internal free list to keep
110   * memory usage in check */
111
112  if (apr_allocator_create(&allocator))
113    abort_on_pool_failure(EXIT_FAILURE);
114
115  apr_allocator_max_free_set(allocator, SVN_ALLOCATOR_RECOMMENDED_MAX_FREE);
116
117  /* create the root pool */
118
119  pool = svn_pool_create_ex(NULL, allocator);
120  apr_allocator_owner_set(allocator, pool);
121
122#if APR_POOL_DEBUG
123  apr_pool_tag (pool, "svn root pool");
124#endif
125
126  /* By default, allocators are *not* thread-safe. We must provide a mutex
127   * if we want thread-safety for that mutex. */
128
129#if APR_HAS_THREADS
130  if (thread_safe)
131    {
132      apr_thread_mutex_t *mutex;
133      apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, pool);
134      apr_allocator_mutex_set(allocator, mutex);
135    }
136#endif
137
138  /* better safe than sorry */
139  SVN_ERR_ASSERT_NO_RETURN(allocator != NULL);
140
141  return allocator;
142}
143