]>
git.proxmox.com Git - libgit2.git/blob - src/util.h
1 #ifndef INCLUDE_util_h__
2 #define INCLUDE_util_h__
4 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
5 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
6 #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))
9 * Don't wrap malloc/calloc.
10 * Use the default versions in glibc, and make
11 * sure that any methods that allocate memory
12 * return a GIT_ENOMEM error when allocation
15 #define git__malloc malloc
16 #define git__calloc calloc
17 #define git__strdup strdup
19 extern int git__fmt(char *, size_t, const char *, ...)
20 GIT_FORMAT_PRINTF(3, 4);
21 extern int git__prefixcmp(const char *str
, const char *prefix
);
22 extern int git__suffixcmp(const char *str
, const char *suffix
);
25 * The dirname() function shall take a pointer to a character string
26 * that contains a pathname, and return a pointer to a string that is a
27 * pathname of the parent directory of that file. Trailing '/' characters
28 * in the path are not counted as part of the path.
30 * If path does not contain a '/', then dirname() shall return a pointer to
31 * the string ".". If path is a null pointer or points to an empty string,
32 * dirname() shall return a pointer to the string "." .
34 * The `git__dirname` implementation is thread safe. The returned
35 * string must be manually free'd.
37 * The `git__dirname_r` implementation expects a string allocated
38 * by the user with big enough size.
40 extern char *git__dirname(const char *path
);
41 extern int git__dirname_r(char *buffer
, size_t bufflen
, const char *path
);
44 * This function returns the basename of the file, which is the last
45 * part of its full name given by fname, with the drive letter and
46 * leading directories stripped off. For example, the basename of
47 * c:/foo/bar/file.ext is file.ext, and the basename of a:foo is foo.
49 * Trailing slashes and backslashes are significant: the basename of
50 * c:/foo/bar/ is an empty string after the rightmost slash.
52 * The `git__basename` implementation is thread safe. The returned
53 * string must be manually free'd.
55 * The `git__basename_r` implementation expects a string allocated
56 * by the user with big enough size.
58 extern char *git__basename(const char *path
);
59 extern int git__basename_r(char *buffer
, size_t bufflen
, const char *path
);
61 extern const char *git__topdir(const char *path
);
64 * Join two paths together. Takes care of properly fixing the
65 * middle slashes and everything
67 * The paths are joined together into buffer_out; this is expected
68 * to be an user allocated buffer of `GIT_PATH_MAX` size
70 extern void git__joinpath_n(char *buffer_out
, int npath
, ...);
72 GIT_INLINE(void) git__joinpath(char *buffer_out
, const char *path_a
, const char *path_b
)
74 git__joinpath_n(buffer_out
, 2, path_a
, path_b
);
77 extern void git__hexdump(const char *buffer
, size_t n
);
78 extern uint32_t git__hash(const void *key
, int len
, uint32_t seed
);
81 /** @return true if p fits into the range of a size_t */
82 GIT_INLINE(int) git__is_sizet(git_off_t p
)
85 return p
== (git_off_t
)r
;
88 /* 32-bit cross-platform rotl */
89 #ifdef _MSC_VER /* use built-in method in MSVC */
90 # define git__rotl(v, s) (uint32_t)_rotl(v, s)
91 #else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */
92 # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s))))
95 extern char *git__strtok(char *output
, char *src
, char *delimit
);
96 extern char *git__strtok_keep(char *output
, char *src
, char *delimit
);
98 #define STRLEN(str) (sizeof(str) - 1)
100 #define GIT_OID_LINE_LENGTH(header) (STRLEN(header) + 1 + GIT_OID_HEXSZ + 1)
103 * Realloc the buffer pointed at by variable 'x' so that it can hold
104 * at least 'nr' entries; the number of entries currently allocated
105 * is 'alloc', using the standard growing factor alloc_nr() macro.
107 * DO NOT USE any expression with side-effect for 'x' or 'alloc'.
109 #define alloc_nr(x) (((x)+16)*3/2)
110 #define ALLOC_GROW(x, nr, alloc) \
112 if ((nr) > alloc) { \
113 if (alloc_nr(alloc) < (nr)) \
116 alloc = alloc_nr(alloc); \
117 x = xrealloc((x), alloc * sizeof(*(x))); \
121 #endif /* INCLUDE_util_h__ */