]>
git.proxmox.com Git - libgit2.git/blob - src/oid.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
10 #include "repository.h"
14 static char to_hex
[] = "0123456789abcdef";
16 static int oid_error_invalid(const char *msg
)
18 giterr_set(GITERR_INVALID
, "Unable to parse OID - %s", msg
);
22 int git_oid_fromstrn(git_oid
*out
, const char *str
, size_t length
)
27 if (length
> GIT_OID_HEXSZ
)
28 length
= GIT_OID_HEXSZ
;
30 for (p
= 0; p
< length
- 1; p
+= 2) {
31 v
= (git__fromhex(str
[p
+ 0]) << 4)
32 | git__fromhex(str
[p
+ 1]);
35 return oid_error_invalid("contains invalid characters");
37 out
->id
[p
/ 2] = (unsigned char)v
;
41 v
= (git__fromhex(str
[p
+ 0]) << 4);
43 return oid_error_invalid("contains invalid characters");
45 out
->id
[p
/ 2] = (unsigned char)v
;
49 memset(out
->id
+ p
/ 2, 0, (GIT_OID_HEXSZ
- p
) / 2);
54 int git_oid_fromstrp(git_oid
*out
, const char *str
)
56 return git_oid_fromstrn(out
, str
, min(strlen(str
), GIT_OID_HEXSZ
));
59 int git_oid_fromstr(git_oid
*out
, const char *str
)
61 return git_oid_fromstrn(out
, str
, GIT_OID_HEXSZ
);
64 GIT_INLINE(char) *fmt_one(char *str
, unsigned int val
)
66 *str
++ = to_hex
[val
>> 4];
67 *str
++ = to_hex
[val
& 0xf];
71 void git_oid_fmt(char *str
, const git_oid
*oid
)
75 for (i
= 0; i
< sizeof(oid
->id
); i
++)
76 str
= fmt_one(str
, oid
->id
[i
]);
79 void git_oid_pathfmt(char *str
, const git_oid
*oid
)
83 str
= fmt_one(str
, oid
->id
[0]);
85 for (i
= 1; i
< sizeof(oid
->id
); i
++)
86 str
= fmt_one(str
, oid
->id
[i
]);
89 char *git_oid_allocfmt(const git_oid
*oid
)
91 char *str
= git__malloc(GIT_OID_HEXSZ
+ 1);
94 git_oid_fmt(str
, oid
);
95 str
[GIT_OID_HEXSZ
] = '\0';
99 char *git_oid_tostr(char *out
, size_t n
, const git_oid
*oid
)
101 char str
[GIT_OID_HEXSZ
];
106 n
--; /* allow room for terminating NUL */
112 git_oid_fmt(str
, oid
);
113 if (n
> GIT_OID_HEXSZ
)
124 git_oid
*oid
, const char **buffer_out
,
125 const char *buffer_end
, const char *header
)
127 const size_t sha_len
= GIT_OID_HEXSZ
;
128 const size_t header_len
= strlen(header
);
130 const char *buffer
= *buffer_out
;
132 if (buffer
+ (header_len
+ sha_len
+ 1) > buffer_end
)
135 if (memcmp(buffer
, header
, header_len
) != 0)
138 if (buffer
[header_len
+ sha_len
] != '\n')
141 if (git_oid_fromstr(oid
, buffer
+ header_len
) < 0)
144 *buffer_out
= buffer
+ (header_len
+ sha_len
+ 1);
149 void git_oid__writebuf(git_buf
*buf
, const char *header
, const git_oid
*oid
)
151 char hex_oid
[GIT_OID_HEXSZ
];
153 git_oid_fmt(hex_oid
, oid
);
154 git_buf_puts(buf
, header
);
155 git_buf_put(buf
, hex_oid
, GIT_OID_HEXSZ
);
156 git_buf_putc(buf
, '\n');
159 void git_oid_fromraw(git_oid
*out
, const unsigned char *raw
)
161 memcpy(out
->id
, raw
, sizeof(out
->id
));
164 void git_oid_cpy(git_oid
*out
, const git_oid
*src
)
166 memcpy(out
->id
, src
->id
, sizeof(out
->id
));
169 int git_oid_ncmp(const git_oid
*oid_a
, const git_oid
*oid_b
, size_t len
)
171 const unsigned char *a
= oid_a
->id
;
172 const unsigned char *b
= oid_b
->id
;
183 if ((*a
^ *b
) & 0xf0)
189 int git_oid_streq(const git_oid
*a
, const char *str
)
193 if (git_oid_fromstr(&id
, str
) < 0)
196 return git_oid_cmp(a
, &id
) == 0 ? 0 : -1;
199 int git_oid_iszero(const git_oid
*oid_a
)
201 const unsigned char *a
= oid_a
->id
;
203 for (i
= 0; i
< GIT_OID_RAWSZ
; ++i
, ++a
)
209 typedef short node_index
;
213 node_index children
[16];
216 struct git_oid_shorten
{
218 size_t node_count
, size
;
219 int min_length
, full
;
222 static int resize_trie(git_oid_shorten
*self
, size_t new_size
)
224 self
->nodes
= git__realloc(self
->nodes
, new_size
* sizeof(trie_node
));
225 GITERR_CHECK_ALLOC(self
->nodes
);
227 if (new_size
> self
->size
) {
228 memset(&self
->nodes
[self
->size
], 0x0, (new_size
- self
->size
) * sizeof(trie_node
));
231 self
->size
= new_size
;
235 static trie_node
*push_leaf(git_oid_shorten
*os
, node_index idx
, int push_at
, const char *oid
)
237 trie_node
*node
, *leaf
;
240 if (os
->node_count
>= os
->size
) {
241 if (resize_trie(os
, os
->size
* 2) < 0)
245 idx_leaf
= (node_index
)os
->node_count
++;
247 if (os
->node_count
== SHRT_MAX
)
250 node
= &os
->nodes
[idx
];
251 node
->children
[push_at
] = -idx_leaf
;
253 leaf
= &os
->nodes
[idx_leaf
];
259 git_oid_shorten
*git_oid_shorten_new(size_t min_length
)
263 assert((size_t)((int)min_length
) == min_length
);
265 os
= git__calloc(1, sizeof(git_oid_shorten
));
269 if (resize_trie(os
, 16) < 0) {
275 os
->min_length
= (int)min_length
;
280 void git_oid_shorten_free(git_oid_shorten
*os
)
282 git__free(os
->nodes
);
288 * What wizardry is this?
290 * This is just a memory-optimized trie: basically a very fancy
291 * 16-ary tree, which is used to store the prefixes of the OID
294 * Read more: http://en.wikipedia.org/wiki/Trie
296 * Magic that happens in this method:
298 * - Each node in the trie is an union, so it can work both as
299 * a normal node, or as a leaf.
301 * - Each normal node points to 16 children (one for each possible
302 * character in the oid). This is *not* stored in an array of
303 * pointers, because in a 64-bit arch this would be sucking
304 * 16*sizeof(void*) = 128 bytes of memory per node, which is
305 * insane. What we do is store Node Indexes, and use these indexes
306 * to look up each node in the om->index array. These indexes are
307 * signed shorts, so this limits the amount of unique OIDs that
308 * fit in the structure to about 20000 (assuming a more or less uniform
311 * - All the nodes in om->index array are stored contiguously in
312 * memory, and each of them is 32 bytes, so we fit 2x nodes per
313 * cache line. Convenient for speed.
315 * - To differentiate the leafs from the normal nodes, we store all
316 * the indexes towards a leaf as a negative index (indexes to normal
317 * nodes are positives). When we find that one of the children for
318 * a node has a negative value, that means it's going to be a leaf.
319 * This reduces the amount of indexes we have by two, but also reduces
320 * the size of each node by 1-4 bytes (the amount we would need to
321 * add a `is_leaf` field): this is good because it allows the nodes
322 * to fit cleanly in cache lines.
324 * - Once we reach an empty children, instead of continuing to insert
325 * new nodes for each remaining character of the OID, we store a pointer
326 * to the tail in the leaf; if the leaf is reached again, we turn it
327 * into a normal node and use the tail to create a new leaf.
329 * This is a pretty good balance between performance and memory usage.
331 int git_oid_shorten_add(git_oid_shorten
*os
, const char *text_oid
)
340 if (text_oid
== NULL
)
341 return os
->min_length
;
346 for (i
= 0; i
< GIT_OID_HEXSZ
; ++i
) {
347 int c
= git__fromhex(text_oid
[i
]);
351 giterr_set(GITERR_INVALID
, "Unable to shorten OID - invalid hex value");
355 node
= &os
->nodes
[idx
];
363 node
= push_leaf(os
, idx
, git__fromhex(tail
[0]), &tail
[1]);
364 GITERR_CHECK_ALLOC(node
);
367 if (node
->children
[c
] == 0) {
368 if (push_leaf(os
, idx
, c
, &text_oid
[i
+ 1]) == NULL
)
373 idx
= node
->children
[c
];
377 node
->children
[c
] = idx
= -idx
;
382 if (++i
> os
->min_length
)
385 return os
->min_length
;