@@ -65,12 +65,15 @@ void git_cache_dump_stats(git_cache *cache)
6565int git_cache_init (git_cache * cache )
6666{
6767 memset (cache , 0 , sizeof (* cache ));
68- cache -> map = git_oidmap_alloc ();
69- GIT_ERROR_CHECK_ALLOC (cache -> map );
68+
69+ if ((git_oidmap_new (& cache -> map )) < 0 )
70+ return -1 ;
71+
7072 if (git_rwlock_init (& cache -> lock )) {
7173 git_error_set (GIT_ERROR_OS , "failed to initialize cache rwlock" );
7274 return -1 ;
7375 }
76+
7477 return 0 ;
7578}
7679
@@ -112,8 +115,7 @@ void git_cache_dispose(git_cache *cache)
112115/* Called with lock */
113116static void cache_evict_entries (git_cache * cache )
114117{
115- uint32_t seed = rand ();
116- size_t evict_count = 8 ;
118+ size_t evict_count = 8 , i ;
117119 ssize_t evicted_memory = 0 ;
118120
119121 /* do not infinite loop if there's not enough entries to evict */
@@ -122,18 +124,19 @@ static void cache_evict_entries(git_cache *cache)
122124 return ;
123125 }
124126
127+ i = 0 ;
125128 while (evict_count > 0 ) {
126- size_t pos = seed ++ % git_oidmap_end (cache -> map );
129+ git_cached_obj * evict ;
130+ const git_oid * key ;
127131
128- if (git_oidmap_has_data ( cache -> map , pos )) {
129- git_cached_obj * evict = git_oidmap_value_at ( cache -> map , pos ) ;
132+ if (git_oidmap_iterate (( void * * ) & evict , cache -> map , & i , & key ) == GIT_ITEROVER )
133+ break ;
130134
131- evict_count -- ;
132- evicted_memory += evict -> size ;
133- git_cached_obj_decref (evict );
135+ evict_count -- ;
136+ evicted_memory += evict -> size ;
137+ git_cached_obj_decref (evict );
134138
135- git_oidmap_delete_at (cache -> map , pos );
136- }
139+ git_oidmap_delete (cache -> map , key );
137140 }
138141
139142 cache -> used_memory -= evicted_memory ;
@@ -148,16 +151,12 @@ static bool cache_should_store(git_object_t object_type, size_t object_size)
148151
149152static void * cache_get (git_cache * cache , const git_oid * oid , unsigned int flags )
150153{
151- size_t pos ;
152- git_cached_obj * entry = NULL ;
154+ git_cached_obj * entry ;
153155
154156 if (!git_cache__enabled || git_rwlock_rdlock (& cache -> lock ) < 0 )
155157 return NULL ;
156158
157- pos = git_oidmap_lookup_index (cache -> map , oid );
158- if (git_oidmap_valid_index (cache -> map , pos )) {
159- entry = git_oidmap_value_at (cache -> map , pos );
160-
159+ if ((entry = git_oidmap_get (cache -> map , oid )) != NULL ) {
161160 if (flags && entry -> flags != flags ) {
162161 entry = NULL ;
163162 } else {
@@ -172,7 +171,7 @@ static void *cache_get(git_cache *cache, const git_oid *oid, unsigned int flags)
172171
173172static void * cache_store (git_cache * cache , git_cached_obj * entry )
174173{
175- size_t pos ;
174+ git_cached_obj * stored_entry ;
176175
177176 git_cached_obj_incref (entry );
178177
@@ -191,34 +190,26 @@ static void *cache_store(git_cache *cache, git_cached_obj *entry)
191190 if (git_cache__current_storage .val > git_cache__max_storage )
192191 cache_evict_entries (cache );
193192
194- pos = git_oidmap_lookup_index (cache -> map , & entry -> oid );
195-
196193 /* not found */
197- if (!git_oidmap_valid_index (cache -> map , pos )) {
198- int rval ;
199-
200- git_oidmap_insert (cache -> map , & entry -> oid , entry , & rval );
201- if (rval >= 0 ) {
194+ if ((stored_entry = git_oidmap_get (cache -> map , & entry -> oid )) == NULL ) {
195+ if (git_oidmap_set (cache -> map , & entry -> oid , entry ) == 0 ) {
202196 git_cached_obj_incref (entry );
203197 cache -> used_memory += entry -> size ;
204198 git_atomic_ssize_add (& git_cache__current_storage , (ssize_t )entry -> size );
205199 }
206200 }
207201 /* found */
208202 else {
209- git_cached_obj * stored_entry = git_oidmap_value_at (cache -> map , pos );
210-
211203 if (stored_entry -> flags == entry -> flags ) {
212204 git_cached_obj_decref (entry );
213205 git_cached_obj_incref (stored_entry );
214206 entry = stored_entry ;
215207 } else if (stored_entry -> flags == GIT_CACHE_STORE_RAW &&
216- entry -> flags == GIT_CACHE_STORE_PARSED ) {
208+ entry -> flags == GIT_CACHE_STORE_PARSED ) {
217209 git_cached_obj_decref (stored_entry );
218210 git_cached_obj_incref (entry );
219211
220- git_oidmap_set_key_at (cache -> map , pos , & entry -> oid );
221- git_oidmap_set_value_at (cache -> map , pos , entry );
212+ git_oidmap_set (cache -> map , & entry -> oid , entry );
222213 } else {
223214 /* NO OP */
224215 }
0 commit comments