Code

git-tag(1): -v option is a subcommand; fix code block
[git.git] / git-compat-util.h
index e3cf3703bbb896067f4c2d5b5e1f3ce898d8b6fc..c08688c8f355ccf61a746bbe1ac9a6788bdea7e3 100644 (file)
 
 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
 
+#ifdef __GNUC__
+#define TYPEOF(x) (__typeof__(x))
+#else
+#define TYPEOF(x)
+#endif
+
+#define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (sizeof(x) * 8 - (bits))))
+
 #if !defined(__APPLE__) && !defined(__FreeBSD__)
 #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
 #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
@@ -189,6 +197,19 @@ static inline void *xmalloc(size_t size)
        return ret;
 }
 
+static inline char *xstrndup(const char *str, size_t len)
+{
+       char *p;
+
+       p = memchr(str, '\0', len);
+       if (p)
+               len = p - str;
+       p = xmalloc(len + 1);
+       memcpy(p, str, len);
+       p[len] = '\0';
+       return p;
+}
+
 static inline void *xrealloc(void *ptr, size_t size)
 {
        void *ret = realloc(ptr, size);