1251881Speter/*
2251881Speter * ====================================================================
3251881Speter *    Licensed to the Apache Software Foundation (ASF) under one
4251881Speter *    or more contributor license agreements.  See the NOTICE file
5251881Speter *    distributed with this work for additional information
6251881Speter *    regarding copyright ownership.  The ASF licenses this file
7251881Speter *    to you under the Apache License, Version 2.0 (the
8251881Speter *    "License"); you may not use this file except in compliance
9251881Speter *    with the License.  You may obtain a copy of the License at
10251881Speter *
11251881Speter *      http://www.apache.org/licenses/LICENSE-2.0
12251881Speter *
13251881Speter *    Unless required by applicable law or agreed to in writing,
14251881Speter *    software distributed under the License is distributed on an
15251881Speter *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16251881Speter *    KIND, either express or implied.  See the License for the
17251881Speter *    specific language governing permissions and limitations
18251881Speter *    under the License.
19251881Speter * ====================================================================
20251881Speter */
21251881Speter
22251881Speter#include <apr_thread_mutex.h>
23251881Speter#include <apr_hash.h>
24251881Speter
25251881Speter#include "svn_hash.h"
26251881Speter#include "svn_dso.h"
27251881Speter#include "svn_pools.h"
28251881Speter#include "svn_private_config.h"
29251881Speter
30251881Speter#include "private/svn_mutex.h"
31251881Speter
32251881Speter/* A mutex to protect our global pool and cache. */
33251881Speterstatic svn_mutex__t *dso_mutex = NULL;
34251881Speter
35251881Speter/* Global pool to allocate DSOs in. */
36251881Speterstatic apr_pool_t *dso_pool;
37251881Speter
38251881Speter/* Global cache for storing DSO objects. */
39251881Speterstatic apr_hash_t *dso_cache;
40251881Speter
41251881Speter/* Just an arbitrary location in memory... */
42251881Speterstatic int not_there_sentinel;
43251881Speter
44251881Speter/* A specific value we store in the dso_cache to indicate that the
45251881Speter   library wasn't found.  This keeps us from allocating extra memory
46251881Speter   from dso_pool when trying to find libraries we already know aren't
47251881Speter   there.  */
48251881Speter#define NOT_THERE ((void *) &not_there_sentinel)
49251881Speter
50251881Spetersvn_error_t *
51251881Spetersvn_dso_initialize2(void)
52251881Speter{
53251881Speter  if (dso_pool)
54251881Speter    return SVN_NO_ERROR;
55251881Speter
56251881Speter  dso_pool = svn_pool_create(NULL);
57251881Speter
58251881Speter  SVN_ERR(svn_mutex__init(&dso_mutex, TRUE, dso_pool));
59251881Speter
60251881Speter  dso_cache = apr_hash_make(dso_pool);
61251881Speter  return SVN_NO_ERROR;
62251881Speter}
63251881Speter
64251881Speter#if APR_HAS_DSO
65251881Speterstatic svn_error_t *
66251881Spetersvn_dso_load_internal(apr_dso_handle_t **dso, const char *fname)
67251881Speter{
68251881Speter  *dso = svn_hash_gets(dso_cache, fname);
69251881Speter
70251881Speter  /* First check to see if we've been through this before...  We do this
71251881Speter     to avoid calling apr_dso_load multiple times for a given library,
72251881Speter     which would result in wasting small amounts of memory each time. */
73251881Speter  if (*dso == NOT_THERE)
74251881Speter    {
75251881Speter      *dso = NULL;
76251881Speter      return SVN_NO_ERROR;
77251881Speter    }
78251881Speter
79251881Speter  /* If we got nothing back from the cache, try and load the library. */
80251881Speter  if (! *dso)
81251881Speter    {
82251881Speter      apr_status_t status = apr_dso_load(dso, fname, dso_pool);
83251881Speter      if (status)
84251881Speter        {
85251881Speter#ifdef SVN_DEBUG_DSO
86251881Speter          char buf[1024];
87251881Speter          fprintf(stderr,
88251881Speter                  "Dynamic loading of '%s' failed with the following error:\n%s\n",
89251881Speter                  fname,
90251881Speter                  apr_dso_error(*dso, buf, 1024));
91251881Speter#endif
92251881Speter          *dso = NULL;
93251881Speter
94251881Speter          /* It wasn't found, so set the special "we didn't find it" value. */
95251881Speter          svn_hash_sets(dso_cache, apr_pstrdup(dso_pool, fname), NOT_THERE);
96251881Speter
97251881Speter          return SVN_NO_ERROR;
98251881Speter        }
99251881Speter
100251881Speter      /* Stash the dso so we can use it next time. */
101251881Speter      svn_hash_sets(dso_cache, apr_pstrdup(dso_pool, fname), *dso);
102251881Speter    }
103251881Speter
104251881Speter  return SVN_NO_ERROR;
105251881Speter}
106251881Speter
107251881Spetersvn_error_t *
108251881Spetersvn_dso_load(apr_dso_handle_t **dso, const char *fname)
109251881Speter{
110251881Speter  if (! dso_pool)
111251881Speter    SVN_ERR(svn_dso_initialize2());
112251881Speter
113251881Speter  SVN_MUTEX__WITH_LOCK(dso_mutex, svn_dso_load_internal(dso, fname));
114251881Speter
115251881Speter  return SVN_NO_ERROR;
116251881Speter}
117251881Speter#endif /* APR_HAS_DSO */
118