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.h>
30 #include <apr_version.h>
31 #include <apr_general.h>
32 #include <apr_pools.h>
33
34 #include "svn_pools.h"
35
36 #include "pools.h"
37
38 #if APR_POOL_DEBUG
39 /* file_line for the non-debug case. */
40 static const char SVN_FILE_LINE_UNDEFINED[] = "svn:<undefined>";
41 #endif /* APR_POOL_DEBUG */
42
43
44
45 /*-----------------------------------------------------------------*/
46
47
48 /* Pool allocation handler which just aborts, since we aren't generally
49 prepared to deal with out-of-memory errors.
50 */
51 static int
abort_on_pool_failure(int retcode)52 abort_on_pool_failure(int retcode)
53 {
54 /* Don't translate this string! It requires memory allocation to do so!
55 And we don't have any of it... */
56 printf("libsvn: Out of memory - terminating application.\n");
57
58 #ifdef WIN32
59 /* Provide a way to distinguish the out-of-memory error from abort(). */
60 if (retcode == APR_ENOMEM)
61 RaiseException(STATUS_NO_MEMORY, EXCEPTION_NONCONTINUABLE, 0, NULL);
62 #endif
63
64 abort();
65 return 0; /* not reached */
66 }
67
68
69 #if APR_POOL_DEBUG
70 #undef svn_pool_create_ex
71 #endif /* APR_POOL_DEBUG */
72
73 #if !APR_POOL_DEBUG
74
75 apr_pool_t *
svn_pool_create_ex(apr_pool_t * parent_pool,apr_allocator_t * allocator)76 svn_pool_create_ex(apr_pool_t *parent_pool, apr_allocator_t *allocator)
77 {
78 apr_pool_t *pool;
79 apr_pool_create_ex(&pool, parent_pool, abort_on_pool_failure, allocator);
80 return pool;
81 }
82
83 /* Wrapper that ensures binary compatibility */
84 apr_pool_t *
svn_pool_create_ex_debug(apr_pool_t * pool,apr_allocator_t * allocator,const char * file_line)85 svn_pool_create_ex_debug(apr_pool_t *pool, apr_allocator_t *allocator,
86 const char *file_line)
87 {
88 return svn_pool_create_ex(pool, allocator);
89 }
90
91 #else /* APR_POOL_DEBUG */
92
93 apr_pool_t *
svn_pool_create_ex_debug(apr_pool_t * parent_pool,apr_allocator_t * allocator,const char * file_line)94 svn_pool_create_ex_debug(apr_pool_t *parent_pool, apr_allocator_t *allocator,
95 const char *file_line)
96 {
97 apr_pool_t *pool;
98 apr_pool_create_ex_debug(&pool, parent_pool, abort_on_pool_failure,
99 allocator, file_line);
100 return pool;
101 }
102
103 /* Wrapper that ensures binary compatibility */
104 apr_pool_t *
svn_pool_create_ex(apr_pool_t * pool,apr_allocator_t * allocator)105 svn_pool_create_ex(apr_pool_t *pool, apr_allocator_t *allocator)
106 {
107 return svn_pool_create_ex_debug(pool, allocator, SVN_FILE_LINE_UNDEFINED);
108 }
109
110 #endif /* APR_POOL_DEBUG */
111
112 apr_allocator_t *
svn_pool_create_allocator(svn_boolean_t thread_safe)113 svn_pool_create_allocator(svn_boolean_t thread_safe)
114 {
115 apr_allocator_t *allocator;
116 apr_pool_t *pool;
117
118 /* create the allocator and limit it's internal free list to keep
119 * memory usage in check */
120
121 if (apr_allocator_create(&allocator))
122 abort_on_pool_failure(EXIT_FAILURE);
123
124 apr_allocator_max_free_set(allocator, SVN_ALLOCATOR_RECOMMENDED_MAX_FREE);
125
126 /* create the root pool */
127
128 pool = svn_pool_create_ex(NULL, allocator);
129 apr_allocator_owner_set(allocator, pool);
130
131 #if APR_POOL_DEBUG
132 apr_pool_tag (pool, "svn root pool");
133 #endif
134
135 /* By default, allocators are *not* thread-safe. We must provide a mutex
136 * if we want thread-safety for that pool. */
137
138 #if APR_HAS_THREADS
139 if (thread_safe)
140 {
141 apr_thread_mutex_t *mutex;
142 apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, pool);
143 apr_allocator_mutex_set(allocator, mutex);
144 }
145 #endif
146
147 /* better safe than sorry */
148 SVN_ERR_ASSERT_NO_RETURN(allocator != NULL);
149
150 return allocator;
151 }
152
153
154 /* Private function that creates an unmanaged pool. */
155 apr_pool_t *
svn_pool__create_unmanaged(svn_boolean_t thread_safe)156 svn_pool__create_unmanaged(svn_boolean_t thread_safe)
157 {
158 apr_pool_t *pool;
159 apr_pool_create_unmanaged_ex(&pool, abort_on_pool_failure,
160 svn_pool_create_allocator(thread_safe));
161 return pool;
162 }
163