]> git.proxmox.com Git - libgit2.git/blob - src/cache.h
Merge pull request #411 from boyski/gcc4
[libgit2.git] / src / cache.h
1 /*
2 * Copyright (C) 2009-2011 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #ifndef INCLUDE_cache_h__
8 #define INCLUDE_cache_h__
9
10 #include "git2/common.h"
11 #include "git2/oid.h"
12 #include "git2/odb.h"
13
14 #include "thread-utils.h"
15
16 #define GIT_DEFAULT_CACHE_SIZE 128
17
18 typedef void (*git_cached_obj_freeptr)(void *);
19
20 typedef struct {
21 git_oid oid;
22 git_atomic refcount;
23 } git_cached_obj;
24
25 typedef struct {
26 git_cached_obj *ptr;
27 git_mutex lock;
28 } cache_node;
29
30 typedef struct {
31 cache_node *nodes;
32
33 unsigned int lru_count;
34 size_t size_mask;
35 git_cached_obj_freeptr free_obj;
36 } git_cache;
37
38
39 int git_cache_init(git_cache *cache, size_t size, git_cached_obj_freeptr free_ptr);
40 void git_cache_free(git_cache *cache);
41
42 void *git_cache_try_store(git_cache *cache, void *entry);
43 void *git_cache_get(git_cache *cache, const git_oid *oid);
44
45
46 GIT_INLINE(int) git_cached_obj_compare(git_cached_obj *obj, const git_oid *oid)
47 {
48 return git_oid_cmp(&obj->oid, oid);
49 }
50
51 GIT_INLINE(void) git_cached_obj_incref(git_cached_obj *obj)
52 {
53 git_atomic_inc(&obj->refcount);
54 }
55
56 GIT_INLINE(void) git_cached_obj_decref(git_cached_obj *obj, git_cached_obj_freeptr free_obj)
57 {
58 if (git_atomic_dec(&obj->refcount) == 0)
59 free_obj(obj);
60 }
61
62
63
64 #endif