Hash :
94af9155
Author :
Date :
2017-03-20T09:01:18
map: remove `*map_free` macros The `map_free` functions were not implemented as functions but instead as macros which also set the map to NULL. While this is most certainly sensible in most cases, we should prefer the more obvious behavior, namingly leaving the map pointer intact. Furthermore, this macro has been refactored incorrectly during the map-refactorings: the two statements are not actually grouped together by a `do { ... } while (0)` block, as it is required for macros to match the behavior of functions more closely. This has led to at least one subtle nesting error in `pack-objects.c`. The following code block ``` if (pb->object_ix) git_oidmap_free(pb->object_ix); ``` would be expanded to ``` if (pb->object_ix) git_oidmap__free(pb->object_ix); pb->object_ix = NULL; ``` which is not what one woudl expect. While it is not a bug here as it would simply become a no-op, the wrong implementation could lead to bugs in other occasions. Fix this by simply removing the macro altogether and replacing it with real function calls. This leaves the burden of setting the pointer to NULL afterwards to the caller, but this is actually expected and behaves like other `free` functions.
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
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "idxmap.h"
/* This is __ac_X31_hash_string but with tolower and it takes the entry's stage into account */
static kh_inline khint_t idxentry_hash(const git_index_entry *e)
{
const char *s = e->path;
khint_t h = (khint_t)git__tolower(*s);
if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)git__tolower(*s);
return h + GIT_IDXENTRY_STAGE(e);
}
#define idxentry_equal(a, b) (GIT_IDXENTRY_STAGE(a) == GIT_IDXENTRY_STAGE(b) && strcmp(a->path, b->path) == 0)
#define idxentry_icase_equal(a, b) (GIT_IDXENTRY_STAGE(a) == GIT_IDXENTRY_STAGE(b) && strcasecmp(a->path, b->path) == 0)
__KHASH_IMPL(idx, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_equal)
__KHASH_IMPL(idxicase, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_icase_equal)
int git_idxmap_alloc(git_idxmap **map)
{
if ((*map = kh_init(idx)) == NULL) {
giterr_set_oom();
return -1;
}
return 0;
}
int git_idxmap_icase_alloc(git_idxmap_icase **map)
{
if ((*map = kh_init(idxicase)) == NULL) {
giterr_set_oom();
return -1;
}
return 0;
}
void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval)
{
khiter_t idx = kh_put(idx, map, key, rval);
if ((*rval) >= 0) {
if ((*rval) == 0)
kh_key(map, idx) = key;
kh_val(map, idx) = value;
}
}
void git_idxmap_icase_insert(git_idxmap_icase *map, const git_index_entry *key, void *value, int *rval)
{
khiter_t idx = kh_put(idxicase, map, key, rval);
if ((*rval) >= 0) {
if ((*rval) == 0)
kh_key(map, idx) = key;
kh_val(map, idx) = value;
}
}
size_t git_idxmap_lookup_index(git_idxmap *map, const git_index_entry *key)
{
return kh_get(idx, map, key);
}
size_t git_idxmap_icase_lookup_index(git_idxmap_icase *map, const git_index_entry *key)
{
return kh_get(idxicase, map, key);
}
void *git_idxmap_value_at(git_idxmap *map, size_t idx)
{
return kh_val(map, idx);
}
int git_idxmap_valid_index(git_idxmap *map, size_t idx)
{
return idx != kh_end(map);
}
int git_idxmap_has_data(git_idxmap *map, size_t idx)
{
return kh_exist(map, idx);
}
void git_idxmap_resize(git_idxmap *map, size_t size)
{
kh_resize(idx, map, size);
}
void git_idxmap_icase_resize(git_idxmap_icase *map, size_t size)
{
kh_resize(idxicase, map, size);
}
void git_idxmap_free(git_idxmap *map)
{
kh_destroy(idx, map);
}
void git_idxmap_clear(git_idxmap *map)
{
kh_clear(idx, map);
}
void git_idxmap_delete_at(git_idxmap *map, size_t idx)
{
kh_del(idx, map, idx);
}
void git_idxmap_icase_delete_at(git_idxmap_icase *map, size_t idx)
{
kh_del(idxicase, map, idx);
}
void git_idxmap_delete(git_idxmap *map, const git_index_entry *key)
{
khiter_t idx = git_idxmap_lookup_index(map, key);
if (git_idxmap_valid_index(map, idx))
git_idxmap_delete_at(map, idx);
}
void git_idxmap_icase_delete(git_idxmap_icase *map, const git_index_entry *key)
{
khiter_t idx = git_idxmap_icase_lookup_index(map, key);
if (git_idxmap_valid_index((git_idxmap *)map, idx))
git_idxmap_icase_delete_at(map, idx);
}