pool.c revision 251881
1259698Sdim/* pool.c:  pool wrappers for Subversion
2259698Sdim *
3259698Sdim * ====================================================================
4259698Sdim *    Licensed to the Apache Software Foundation (ASF) under one
5259698Sdim *    or more contributor license agreements.  See the NOTICE file
6259698Sdim *    distributed with this work for additional information
7259698Sdim *    regarding copyright ownership.  The ASF licenses this file
8259698Sdim *    to you under the Apache License, Version 2.0 (the
9259698Sdim *    "License"); you may not use this file except in compliance
10259698Sdim *    with the License.  You may obtain a copy of the License at
11259698Sdim *
12259698Sdim *      http://www.apache.org/licenses/LICENSE-2.0
13259698Sdim *
14259698Sdim *    Unless required by applicable law or agreed to in writing,
15259698Sdim *    software distributed under the License is distributed on an
16259698Sdim *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17259698Sdim *    KIND, either express or implied.  See the License for the
18259698Sdim *    specific language governing permissions and limitations
19259698Sdim *    under the License.
20259698Sdim * ====================================================================
21259698Sdim */
22259698Sdim
23259698Sdim
24259698Sdim
25259698Sdim#include <stdarg.h>
26259698Sdim#include <stdlib.h>
27259698Sdim#include <stdio.h>
28259698Sdim
29259698Sdim#include <apr_general.h>
30259698Sdim#include <apr_pools.h>
31259698Sdim#include <apr_thread_mutex.h>
32259698Sdim
33259698Sdim#include "svn_pools.h"
34259698Sdim
35259698Sdim
36259698Sdim#if APR_POOL_DEBUG
37259698Sdim/* file_line for the non-debug case. */
38259698Sdimstatic const char SVN_FILE_LINE_UNDEFINED[] = "svn:<undefined>";
39259698Sdim#endif /* APR_POOL_DEBUG */
40259698Sdim
41259698Sdim
42259698Sdim
43259698Sdim/*-----------------------------------------------------------------*/
44259698Sdim
45259698Sdim
46259698Sdim/* Pool allocation handler which just aborts, since we aren't generally
47259698Sdim   prepared to deal with out-of-memory errors.
48259698Sdim */
49259698Sdimstatic int
50259698Sdimabort_on_pool_failure(int retcode)
51259698Sdim{
52259698Sdim  /* Don't translate this string! It requires memory allocation to do so!
53259698Sdim     And we don't have any of it... */
54259698Sdim  printf("Out of memory - terminating application.\n");
55259698Sdim  abort();
56259698Sdim  return 0; /* not reached */
57259698Sdim}
58259698Sdim
59259698Sdim
60259698Sdim#if APR_POOL_DEBUG
61259698Sdim#undef svn_pool_create_ex
62259698Sdim#endif /* APR_POOL_DEBUG */
63259698Sdim
64259698Sdim#if !APR_POOL_DEBUG
65259698Sdim
66259698Sdimapr_pool_t *
67259698Sdimsvn_pool_create_ex(apr_pool_t *parent_pool, apr_allocator_t *allocator)
68259698Sdim{
69259698Sdim  apr_pool_t *pool;
70259698Sdim  apr_pool_create_ex(&pool, parent_pool, abort_on_pool_failure, allocator);
71259698Sdim  return pool;
72259698Sdim}
73259698Sdim
74259698Sdim/* Wrapper that ensures binary compatibility */
75259698Sdimapr_pool_t *
76259698Sdimsvn_pool_create_ex_debug(apr_pool_t *pool, apr_allocator_t *allocator,
77259698Sdim                         const char *file_line)
78259698Sdim{
79259698Sdim  return svn_pool_create_ex(pool, allocator);
80259698Sdim}
81259698Sdim
82259698Sdim#else /* APR_POOL_DEBUG */
83259698Sdim
84259698Sdimapr_pool_t *
85259698Sdimsvn_pool_create_ex_debug(apr_pool_t *parent_pool, apr_allocator_t *allocator,
86259698Sdim                         const char *file_line)
87259698Sdim{
88259698Sdim  apr_pool_t *pool;
89259698Sdim  apr_pool_create_ex_debug(&pool, parent_pool, abort_on_pool_failure,
90259698Sdim                           allocator, file_line);
91259698Sdim  return pool;
92259698Sdim}
93259698Sdim
94259698Sdim/* Wrapper that ensures binary compatibility */
95259698Sdimapr_pool_t *
96259698Sdimsvn_pool_create_ex(apr_pool_t *pool, apr_allocator_t *allocator)
97259698Sdim{
98259698Sdim  return svn_pool_create_ex_debug(pool, allocator, SVN_FILE_LINE_UNDEFINED);
99259698Sdim}
100259698Sdim
101259698Sdim#endif /* APR_POOL_DEBUG */
102259698Sdim
103259698Sdimapr_allocator_t *
104259698Sdimsvn_pool_create_allocator(svn_boolean_t thread_safe)
105259698Sdim{
106259698Sdim  apr_allocator_t *allocator;
107259698Sdim  apr_pool_t *pool;
108259698Sdim
109259698Sdim  /* create the allocator and limit it's internal free list to keep
110259698Sdim   * memory usage in check */
111259698Sdim
112259698Sdim  if (apr_allocator_create(&allocator))
113259698Sdim    abort_on_pool_failure(EXIT_FAILURE);
114259698Sdim
115259698Sdim  apr_allocator_max_free_set(allocator, SVN_ALLOCATOR_RECOMMENDED_MAX_FREE);
116259698Sdim
117259698Sdim  /* 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