libgit2/src/offmap.h
Patrick Steinhardt 94af9155cf 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.
2017-03-20 09:01:18 +01:00

46 lines
1.4 KiB
C

/*
* Copyright (C) 2012 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_offmap_h__
#define INCLUDE_offmap_h__
#include "common.h"
#include "git2/types.h"
#define kmalloc git__malloc
#define kcalloc git__calloc
#define krealloc git__realloc
#define kreallocarray git__reallocarray
#define kfree git__free
#include "khash.h"
__KHASH_TYPE(off, git_off_t, void *)
typedef khash_t(off) git_offmap;
git_offmap *git_offmap_alloc(void);
void git_offmap_free(git_offmap *map);
void git_offmap_clear(git_offmap *map);
size_t git_offmap_num_entries(git_offmap *map);
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key);
int git_offmap_valid_index(git_offmap *map, size_t idx);
int git_offmap_exists(git_offmap *map, const git_off_t key);
void *git_offmap_value_at(git_offmap *map, size_t idx);
void git_offmap_set_value_at(git_offmap *map, size_t idx, void *value);
void git_offmap_delete_at(git_offmap *map, size_t idx);
int git_offmap_put(git_offmap *map, const git_off_t key, int *err);
void git_offmap_insert(git_offmap *map, const git_off_t key, void *value, int *rval);
void git_offmap_delete(git_offmap *map, const git_off_t key);
#define git_offmap_foreach kh_foreach
#define git_offmap_foreach_value kh_foreach_value
#endif