Refactor pack memory management and locking to be safer Using an atomic reference counter is difficult to make cross-platform, as the reference count implementations are generally processor specific. Its also hard to do a proper multi-read/single-write implementation. We now use a simple mutex around the reference count for the list of packs. Readers grab the mutex and either build the list, or increment the existing one's reference count. When the reader is done with the list, the reference count is decremented. In this way parallel readers are able to operate on the list without worrying about it being deallocated out from under them. Individual pack structures are held by reference counts, but we only care about the list the pack structure is held in. There is no need to increment/decrement the pack reference counts as we scan through them during a read operation, the caller holds the git_packlist and that is sufficient to hold the packs it references. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
diff --git a/src/odb.c b/src/odb.c
index 9d3c39d..412ec4e 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -32,11 +32,18 @@
#define GIT_PACK_NAME_MAX (5 + 40 + 1)
typedef struct {
- git_refcnt ref;
+ git_lck lock;
+ unsigned int refcnt;
char pack_name[GIT_PACK_NAME_MAX];
} git_pack;
+typedef struct {
+ size_t n_packs;
+ unsigned int refcnt;
+ git_pack *packs[FLEX_ARRAY];
+} git_packlist;
+
struct git_odb {
git_lck lock;
@@ -44,8 +51,7 @@ struct git_odb {
char *objects_dir;
/** Known pack files from ${objects_dir}/packs. */
- git_pack **packs;
- size_t n_packs;
+ git_packlist *packlist;
/** Alternate databases to search. */
git_odb **alternates;
@@ -449,10 +455,34 @@ static int open_alternates(git_odb *db)
return 0;
}
-static void free_pack(git_pack *p)
+static void pack_dec(git_pack *p)
+{
+ int need_free;
+
+ gitlck_lock(&p->lock);
+ need_free = !--p->refcnt;
+ gitlck_unlock(&p->lock);
+
+ if (need_free) {
+ gitlck_free(&p->lock);
+ free(p);
+ }
+}
+
+static void packlist_dec(git_odb *db, git_packlist *pl)
{
- gitrc_free(&p->ref);
- free(p);
+ int need_free;
+
+ gitlck_lock(&db->lock);
+ need_free = !--pl->refcnt;
+ gitlck_unlock(&db->lock);
+
+ if (need_free) {
+ size_t j;
+ for (j = 0; j < pl->n_packs; j++)
+ pack_dec(pl->packs[j]);
+ free(pl);
+ }
}
static git_pack *alloc_pack(const char *pack_name)
@@ -461,9 +491,9 @@ static git_pack *alloc_pack(const char *pack_name)
if (!p)
return NULL;
- gitrc_init(&p->ref);
+ gitlck_init(&p->lock);
strcpy(p->pack_name, pack_name);
- gitrc_inc(&p->ref);
+ p->refcnt = 1;
return p;
}
@@ -501,49 +531,57 @@ static int scan_one_pack(void *state, char *name)
return 0;
}
-static int scan_packs(git_odb *db)
+static git_packlist* scan_packs(git_odb *db)
{
char pb[GIT_PATH_MAX];
struct scanned_pack *state = NULL, *c;
size_t cnt;
+ git_packlist *new_list;
if (git__fmt(pb, sizeof(pb), "%s/pack", db->objects_dir) < 0)
- return GIT_ERROR;
+ return NULL;
gitfo_dirent(pb, sizeof(pb), scan_one_pack, &state);
- gitlck_lock(&db->lock);
- if (!db->packs) {
- for (cnt = 0, c = state; c; c = c->next)
- cnt++;
-
- db->packs = git__malloc(sizeof(*db->packs) * (cnt + 1));
- if (!db->packs)
- goto unlock_fail;
- for (cnt = 0, c = state; c; ) {
- struct scanned_pack *n = c->next;
- db->packs[cnt++] = c->pack;
- free(c);
- c = n;
- }
- db->packs[cnt] = NULL;
- db->n_packs = cnt;
- } else {
- /* TODO - merge new entries into the existing array */
- goto unlock_fail;
+ /* TODO - merge old entries into the new array */
+ for (cnt = 0, c = state; c; c = c->next)
+ cnt++;
+ new_list = git__malloc(sizeof(*new_list)
+ + (sizeof(new_list->packs[0]) * cnt));
+ if (!new_list)
+ goto fail;
+
+ for (cnt = 0, c = state; c; ) {
+ struct scanned_pack *n = c->next;
+ new_list->packs[cnt++] = c->pack;
+ free(c);
+ c = n;
}
+ new_list->n_packs = cnt;
+ new_list->refcnt = 2;
+ db->packlist = new_list;
+ return new_list;
- gitlck_unlock(&db->lock);
- return 0;
-
-unlock_fail:
- gitlck_unlock(&db->lock);
+fail:
while (state) {
struct scanned_pack *n = state->next;
- free_pack(state->pack);
+ pack_dec(state->pack);
free(state);
state = n;
}
- return GIT_ERROR;
+ return NULL;
+}
+
+static git_packlist *packlist_get(git_odb *db)
+{
+ git_packlist *pl;
+
+ gitlck_lock(&db->lock);
+ if ((pl = db->packlist))
+ pl->refcnt++;
+ else
+ pl = scan_packs(db);
+ gitlck_unlock(&db->lock);
+ return pl;
}
int git_odb_open(git_odb **out, const char *objects_dir)
@@ -559,10 +597,6 @@ int git_odb_open(git_odb **out, const char *objects_dir)
}
gitlck_init(&db->lock);
- if (scan_packs(db)) {
- git_odb_close(db);
- return GIT_ERROR;
- }
*out = db;
return GIT_SUCCESS;
@@ -570,18 +604,15 @@ int git_odb_open(git_odb **out, const char *objects_dir)
void git_odb_close(git_odb *db)
{
+ git_packlist *pl;
+
if (!db)
return;
gitlck_lock(&db->lock);
- if (db->packs) {
- git_pack **p;
- for (p = db->packs; *p; p++)
- if (gitrc_dec(&(*p)->ref))
- free_pack(*p);
- free(db->packs);
- }
+ pl = db->packlist;
+ db->packlist = NULL;
if (db->alternates) {
git_odb **alt;
@@ -593,6 +624,8 @@ void git_odb_close(git_odb *db)
free(db->objects_dir);
gitlck_unlock(&db->lock);
+ if (pl)
+ packlist_dec(db, pl);
gitlck_free(&db->lock);
free(db);
}
@@ -641,8 +674,27 @@ int git_odb__read_loose(git_obj *out, git_odb *db, const git_oid *id)
return GIT_SUCCESS;
}
+static int read_packed(git_obj *out, git_pack *p, const git_oid *id)
+{
+ return GIT_ENOTFOUND;
+}
+
int git_odb__read_packed(git_obj *out, git_odb *db, const git_oid *id)
{
+ git_packlist *pl = packlist_get(db);
+ size_t j;
+
+ if (!pl)
+ return GIT_ENOTFOUND;
+
+ for (j = 0; j < pl->n_packs; j++) {
+ if (!read_packed(out, pl->packs[j], id)) {
+ packlist_dec(db, pl);
+ return GIT_SUCCESS;
+ }
+ }
+
+ packlist_dec(db, pl);
return GIT_ENOTFOUND;
}