]> git.proxmox.com Git - libgit2.git/blob - src/cache.h
Merge pull request #184 from nulltoken/repo-error-handling
[libgit2.git] / src / cache.h
1 #ifndef INCLUDE_cache_h__
2 #define INCLUDE_cache_h__
3
4 #include "git2/common.h"
5 #include "git2/oid.h"
6 #include "git2/odb.h"
7
8 #include "thread-utils.h"
9
10 #define GIT_DEFAULT_CACHE_SIZE 128
11
12 typedef void (*git_cached_obj_freeptr)(void *);
13
14 typedef struct {
15 git_oid oid;
16 git_atomic refcount;
17 } git_cached_obj;
18
19 typedef struct {
20 git_cached_obj *ptr;
21 git_mutex lock;
22 unsigned int lru;
23 } cache_node;
24
25 typedef struct {
26 cache_node *nodes;
27
28 unsigned int lru_count;
29 size_t size_mask;
30 git_cached_obj_freeptr free_obj;
31 } git_cache;
32
33
34 int git_cache_init(git_cache *cache, size_t size, git_cached_obj_freeptr free_ptr);
35 void git_cache_free(git_cache *cache);
36
37 void *git_cache_try_store(git_cache *cache, void *entry);
38 void *git_cache_get(git_cache *cache, const git_oid *oid);
39
40
41 GIT_INLINE(int) git_cached_obj_compare(git_cached_obj *obj, const git_oid *oid)
42 {
43 return git_oid_cmp(&obj->oid, oid);
44 }
45
46 GIT_INLINE(void) git_cached_obj_incref(git_cached_obj *obj)
47 {
48 git_atomic_inc(&obj->refcount);
49 }
50
51 GIT_INLINE(void) git_cached_obj_decref(git_cached_obj *obj, git_cached_obj_freeptr free_obj)
52 {
53 if (git_atomic_dec(&obj->refcount) == 0)
54 free_obj(obj);
55 }
56
57
58
59 #endif