]> git.proxmox.com Git - libgit2.git/blame - src/cache.h
Merge pull request #444 from carlosmn/fetch-fixes
[libgit2.git] / src / cache.h
CommitLineData
bb742ede
VM
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 */
bb3de0c4
VM
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
72a3fe42
VM
16#define GIT_DEFAULT_CACHE_SIZE 128
17
bb3de0c4
VM
18typedef void (*git_cached_obj_freeptr)(void *);
19
20typedef struct {
21 git_oid oid;
22 git_atomic refcount;
23} git_cached_obj;
24
25typedef struct {
26 git_cached_obj *ptr;
27 git_mutex lock;
bb3de0c4
VM
28} cache_node;
29
30typedef 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
81201a4c 39int git_cache_init(git_cache *cache, size_t size, git_cached_obj_freeptr free_ptr);
bb3de0c4
VM
40void git_cache_free(git_cache *cache);
41
42void *git_cache_try_store(git_cache *cache, void *entry);
43void *git_cache_get(git_cache *cache, const git_oid *oid);
44
72a3fe42
VM
45
46GIT_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
51GIT_INLINE(void) git_cached_obj_incref(git_cached_obj *obj)
52{
53 git_atomic_inc(&obj->refcount);
54}
55
56GIT_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
bb3de0c4 64#endif