]>
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__realloc realloc
18 #define git__strdup strdup
20 extern int git__fmt(char *, size_t, const char *, ...)
21 GIT_FORMAT_PRINTF(3, 4);
22 extern int git__prefixcmp(const char *str
, const char *prefix
);
23 extern int git__suffixcmp(const char *str
, const char *suffix
);
26 * The dirname() function shall take a pointer to a character string
27 * that contains a pathname, and return a pointer to a string that is a
28 * pathname of the parent directory of that file. Trailing '/' characters
29 * in the path are not counted as part of the path.
31 * If path does not contain a '/', then dirname() shall return a pointer to
32 * the string ".". If path is a null pointer or points to an empty string,
33 * dirname() shall return a pointer to the string "." .
35 * The `git__dirname` implementation is thread safe. The returned
36 * string must be manually free'd.
38 * The `git__dirname_r` implementation expects a string allocated
39 * by the user with big enough size.
41 extern char *git__dirname(const char *path
);
42 extern int git__dirname_r(char *buffer
, size_t bufflen
, const char *path
);
45 * This function returns the basename of the file, which is the last
46 * part of its full name given by fname, with the drive letter and
47 * leading directories stripped off. For example, the basename of
48 * c:/foo/bar/file.ext is file.ext, and the basename of a:foo is foo.
50 * Trailing slashes and backslashes are significant: the basename of
51 * c:/foo/bar/ is an empty string after the rightmost slash.
53 * The `git__basename` implementation is thread safe. The returned
54 * string must be manually free'd.
56 * The `git__basename_r` implementation expects a string allocated
57 * by the user with big enough size.
59 extern char *git__basename(const char *path
);
60 extern int git__basename_r(char *buffer
, size_t bufflen
, const char *path
);
62 extern const char *git__topdir(const char *path
);
65 * Join two paths together. Takes care of properly fixing the
66 * middle slashes and everything
68 * The paths are joined together into buffer_out; this is expected
69 * to be an user allocated buffer of `GIT_PATH_MAX` size
71 extern void git__joinpath_n(char *buffer_out
, int npath
, ...);
73 GIT_INLINE(void) git__joinpath(char *buffer_out
, const char *path_a
, const char *path_b
)
75 git__joinpath_n(buffer_out
, 2, path_a
, path_b
);
78 extern void git__hexdump(const char *buffer
, size_t n
);
79 extern uint32_t git__hash(const void *key
, int len
, uint32_t seed
);
82 /** @return true if p fits into the range of a size_t */
83 GIT_INLINE(int) git__is_sizet(git_off_t p
)
86 return p
== (git_off_t
)r
;
89 /* 32-bit cross-platform rotl */
90 #ifdef _MSC_VER /* use built-in method in MSVC */
91 # define git__rotl(v, s) (uint32_t)_rotl(v, s)
92 #else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */
93 # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s))))
96 extern char *git__strtok(char *output
, char *src
, char *delimit
);
97 extern char *git__strtok_keep(char *output
, char *src
, char *delimit
);
99 #define STRLEN(str) (sizeof(str) - 1)
101 #define GIT_OID_LINE_LENGTH(header) (STRLEN(header) + 1 + GIT_OID_HEXSZ + 1)
104 * Realloc the buffer pointed at by variable 'x' so that it can hold
105 * at least 'nr' entries; the number of entries currently allocated
106 * is 'alloc', using the standard growing factor alloc_nr() macro.
108 * DO NOT USE any expression with side-effect for 'x' or 'alloc'.
110 #define alloc_nr(x) (((x)+16)*3/2)
111 #define ALLOC_GROW(x, nr, alloc) \
113 if ((nr) > alloc) { \
114 if (alloc_nr(alloc) < (nr)) \
117 alloc = alloc_nr(alloc); \
118 x = xrealloc((x), alloc * sizeof(*(x))); \
122 #endif /* INCLUDE_util_h__ */