cache.h 1.66 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
Vicent Marti committed
7 8 9
#ifndef INCLUDE_cache_h__
#define INCLUDE_cache_h__

10 11
#include "common.h"

Vicent Marti committed
12 13 14 15 16
#include "git2/common.h"
#include "git2/oid.h"
#include "git2/odb.h"

#include "thread-utils.h"
17
#include "oidmap.h"
Vicent Marti committed
18

19 20 21 22 23
enum {
	GIT_CACHE_STORE_ANY = 0,
	GIT_CACHE_STORE_RAW = 1,
	GIT_CACHE_STORE_PARSED = 2
};
Vicent Marti committed
24 25

typedef struct {
26
	git_oid    oid;
27
	int16_t    type;  /* git_object_t value */
28 29
	uint16_t   flags; /* GIT_CACHE_STORE value */
	size_t     size;
Vicent Marti committed
30
	git_atomic refcount;
Vicent Marti committed
31 32 33
} git_cached_obj;

typedef struct {
34
	git_oidmap *map;
35
	git_rwlock  lock;
36
	ssize_t     used_memory;
Vicent Marti committed
37 38
} git_cache;

39
extern bool git_cache__enabled;
40 41
extern ssize_t git_cache__max_storage;
extern git_atomic_ssize git_cache__current_storage;
42

43
int git_cache_set_max_object_size(git_object_t type, size_t size);
44

45
int git_cache_init(git_cache *cache);
46
void git_cache_dispose(git_cache *cache);
47
void git_cache_clear(git_cache *cache);
Vicent Marti committed
48

49 50 51 52 53 54
void *git_cache_store_raw(git_cache *cache, git_odb_object *entry);
void *git_cache_store_parsed(git_cache *cache, git_object *entry);

git_odb_object *git_cache_get_raw(git_cache *cache, const git_oid *oid);
git_object *git_cache_get_parsed(git_cache *cache, const git_oid *oid);
void *git_cache_get_any(git_cache *cache, const git_oid *oid);
Vicent Marti committed
55

56 57
GIT_INLINE(size_t) git_cache_size(git_cache *cache)
{
58
	return (size_t)git_oidmap_size(cache->map);
59 60
}

61
GIT_INLINE(void) git_cached_obj_incref(void *_obj)
Vicent Marti committed
62
{
63
	git_cached_obj *obj = _obj;
Vicent Marti committed
64 65 66
	git_atomic_inc(&obj->refcount);
}

67
void git_cached_obj_decref(void *_obj);
Vicent Marti committed
68

Vicent Marti committed
69
#endif