OSDN Git Service

Success build TortoiseMerge.
[tortoisegit/TortoiseGitJp.git] / src / TortoiseMerge / libsvn_diff / dso.c
1 /*\r
2  * ====================================================================\r
3  * Copyright (c) 2006 CollabNet.  All rights reserved.\r
4  *\r
5  * This software is licensed as described in the file COPYING, which\r
6  * you should have received as part of this distribution.  The terms\r
7  * are also available at http://subversion.tigris.org/license-1.html.\r
8  * If newer versions of this license are posted there, you may use a\r
9  * newer version instead, at your option.\r
10  *\r
11  * This software consists of voluntary contributions made by many\r
12  * individuals.  For exact contribution history, see the revision\r
13  * history and logs, available at http://subversion.tigris.org/.\r
14  * ====================================================================\r
15  */\r
16 \r
17 #include <apr_thread_mutex.h>\r
18 #include <apr_hash.h>\r
19 \r
20 #include "svn_dso.h"\r
21 #include "svn_pools.h"\r
22 //#include "svn_private_config.h"\r
23 \r
24 /* A mutex to protect our global pool and cache. */\r
25 #if APR_HAS_THREADS\r
26 static apr_thread_mutex_t *dso_mutex;\r
27 #endif\r
28 \r
29 /* Global pool to allocate DSOs in. */\r
30 static apr_pool_t *dso_pool;\r
31 \r
32 /* Global cache for storing DSO objects. */\r
33 static apr_hash_t *dso_cache;\r
34 \r
35 /* Just an arbitrary location in memory... */\r
36 static int not_there_sentinel;\r
37 \r
38 /* A specific value we store in the dso_cache to indicate that the\r
39    library wasn't found.  This keeps us from allocating extra memory\r
40    from dso_pool when trying to find libraries we already know aren't\r
41    there.  */\r
42 #define NOT_THERE ((void *) &not_there_sentinel)\r
43 \r
44 svn_error_t *\r
45 svn_dso_initialize2(void)\r
46 {\r
47   apr_status_t status;\r
48   if (dso_pool)\r
49     return SVN_NO_ERROR;\r
50 \r
51   dso_pool = svn_pool_create(NULL);\r
52 \r
53 #if APR_HAS_THREADS\r
54   status = apr_thread_mutex_create(&dso_mutex,\r
55                                    APR_THREAD_MUTEX_DEFAULT, dso_pool);\r
56   if (status)\r
57     return svn_error_wrap_apr(status, _("Can't create DSO mutex"));\r
58 #endif\r
59 \r
60   dso_cache = apr_hash_make(dso_pool);\r
61   return SVN_NO_ERROR;\r
62 }\r
63 \r
64 #if APR_HAS_DSO\r
65 svn_error_t *\r
66 svn_dso_load(apr_dso_handle_t **dso, const char *fname)\r
67 {\r
68   apr_status_t status;\r
69 \r
70   if (! dso_pool)\r
71     SVN_ERR(svn_dso_initialize2());\r
72 \r
73 #if APR_HAS_THREADS\r
74   status = apr_thread_mutex_lock(dso_mutex);\r
75   if (status)\r
76     return svn_error_wrap_apr(status, _("Can't grab DSO mutex"));\r
77 #endif\r
78 \r
79   *dso = apr_hash_get(dso_cache, fname, APR_HASH_KEY_STRING);\r
80 \r
81   /* First check to see if we've been through this before...  We do this\r
82      to avoid calling apr_dso_load multiple times for a given library,\r
83      which would result in wasting small amounts of memory each time. */\r
84   if (*dso == NOT_THERE)\r
85     {\r
86       *dso = NULL;\r
87 #if APR_HAS_THREADS\r
88       status = apr_thread_mutex_unlock(dso_mutex);\r
89       if (status)\r
90         return svn_error_wrap_apr(status, _("Can't ungrab DSO mutex"));\r
91 #endif\r
92       return SVN_NO_ERROR;\r
93     }\r
94 \r
95   /* If we got nothing back from the cache, try and load the library. */\r
96   if (! *dso)\r
97     {\r
98       status = apr_dso_load(dso, fname, dso_pool);\r
99       if (status)\r
100         {\r
101           *dso = NULL;\r
102 \r
103           /* It wasn't found, so set the special "we didn't find it" value. */\r
104           apr_hash_set(dso_cache,\r
105                        apr_pstrdup(dso_pool, fname),\r
106                        APR_HASH_KEY_STRING,\r
107                        NOT_THERE);\r
108 \r
109 #if APR_HAS_THREADS\r
110           status = apr_thread_mutex_unlock(dso_mutex);\r
111           if (status)\r
112             return svn_error_wrap_apr(status, _("Can't ungrab DSO mutex"));\r
113 #endif\r
114           return SVN_NO_ERROR;\r
115         }\r
116 \r
117       /* Stash the dso so we can use it next time. */\r
118       apr_hash_set(dso_cache,\r
119                    apr_pstrdup(dso_pool, fname),\r
120                    APR_HASH_KEY_STRING,\r
121                    *dso);\r
122     }\r
123 \r
124 #if APR_HAS_THREADS\r
125   status = apr_thread_mutex_unlock(dso_mutex);\r
126   if (status)\r
127     return svn_error_wrap_apr(status, _("Can't ungrab DSO mutex"));\r
128 #endif\r
129 \r
130   return SVN_NO_ERROR;\r
131 }\r
132 #endif /* APR_HAS_DSO */\r