diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/CMakeLists.txt | 44 | ||||
-rw-r--r-- | src/common.h | 168 | ||||
-rw-r--r-- | util/CMakeLists.txt | 108 | ||||
-rw-r--r-- | util/alloc.c (renamed from src/alloc.c) | 0 | ||||
-rw-r--r-- | util/alloc.h (renamed from src/alloc.h) | 0 | ||||
-rw-r--r-- | util/allocators/stdalloc.c (renamed from src/allocators/stdalloc.c) | 0 | ||||
-rw-r--r-- | util/allocators/stdalloc.h (renamed from src/allocators/stdalloc.h) | 2 | ||||
-rw-r--r-- | util/allocators/win32_crtdbg.c (renamed from src/allocators/win32_crtdbg.c) | 0 | ||||
-rw-r--r-- | util/allocators/win32_crtdbg.h (renamed from src/allocators/win32_crtdbg.h) | 2 | ||||
-rw-r--r-- | util/buf_text.c (renamed from src/buf_text.c) | 0 | ||||
-rw-r--r-- | util/buf_text.h (renamed from src/buf_text.h) | 2 | ||||
-rw-r--r-- | util/buffer.c (renamed from src/buffer.c) | 0 | ||||
-rw-r--r-- | util/buffer.h (renamed from src/buffer.h) | 2 | ||||
-rw-r--r-- | util/cc-compat.h (renamed from src/cc-compat.h) | 0 | ||||
-rw-r--r-- | util/errors.c (renamed from src/errors.c) | 0 | ||||
-rw-r--r-- | util/errors.h (renamed from src/errors.h) | 2 | ||||
-rw-r--r-- | util/futils.c (renamed from src/futils.c) | 2 | ||||
-rw-r--r-- | util/futils.h (renamed from src/futils.h) | 3 | ||||
-rw-r--r-- | util/hash.c (renamed from src/hash.c) | 0 | ||||
-rw-r--r-- | util/hash.h (renamed from src/hash.h) | 2 | ||||
-rw-r--r-- | util/hash/sha1.h (renamed from src/hash/sha1.h) | 2 | ||||
-rw-r--r-- | util/hash/sha1/collisiondetect.c (renamed from src/hash/sha1/collisiondetect.c) | 0 | ||||
-rw-r--r-- | util/hash/sha1/collisiondetect.h (renamed from src/hash/sha1/collisiondetect.h) | 0 | ||||
-rw-r--r-- | util/hash/sha1/common_crypto.c (renamed from src/hash/sha1/common_crypto.c) | 0 | ||||
-rw-r--r-- | util/hash/sha1/common_crypto.h (renamed from src/hash/sha1/common_crypto.h) | 0 | ||||
-rw-r--r-- | util/hash/sha1/generic.c (renamed from src/hash/sha1/generic.c) | 0 | ||||
-rw-r--r-- | util/hash/sha1/generic.h (renamed from src/hash/sha1/generic.h) | 0 | ||||
-rw-r--r-- | util/hash/sha1/mbedtls.c (renamed from src/hash/sha1/mbedtls.c) | 0 | ||||
-rw-r--r-- | util/hash/sha1/mbedtls.h (renamed from src/hash/sha1/mbedtls.h) | 0 | ||||
-rw-r--r-- | util/hash/sha1/openssl.c (renamed from src/hash/sha1/openssl.c) | 0 | ||||
-rw-r--r-- | util/hash/sha1/openssl.h (renamed from src/hash/sha1/openssl.h) | 0 | ||||
-rw-r--r-- | util/hash/sha1/sha1dc/sha1.c (renamed from src/hash/sha1/sha1dc/sha1.c) | 0 | ||||
-rw-r--r-- | util/hash/sha1/sha1dc/sha1.h (renamed from src/hash/sha1/sha1dc/sha1.h) | 0 | ||||
-rw-r--r-- | util/hash/sha1/sha1dc/ubc_check.c (renamed from src/hash/sha1/sha1dc/ubc_check.c) | 0 | ||||
-rw-r--r-- | util/hash/sha1/sha1dc/ubc_check.h (renamed from src/hash/sha1/sha1dc/ubc_check.h) | 0 | ||||
-rw-r--r-- | util/hash/sha1/win32.c (renamed from src/hash/sha1/win32.c) | 0 | ||||
-rw-r--r-- | util/hash/sha1/win32.h (renamed from src/hash/sha1/win32.h) | 0 | ||||
-rw-r--r-- | util/integer.h (renamed from src/integer.h) | 0 | ||||
-rw-r--r-- | util/khash.h (renamed from src/khash.h) | 0 | ||||
-rw-r--r-- | util/map.h (renamed from src/map.h) | 2 | ||||
-rw-r--r-- | util/path.c (renamed from src/path.c) | 2 | ||||
-rw-r--r-- | util/path.h (renamed from src/path.h) | 2 | ||||
-rw-r--r-- | util/pool.c (renamed from src/pool.c) | 0 | ||||
-rw-r--r-- | util/pool.h (renamed from src/pool.h) | 2 | ||||
-rw-r--r-- | util/posix.c (renamed from src/posix.c) | 0 | ||||
-rw-r--r-- | util/posix.h (renamed from src/posix.h) | 2 | ||||
-rw-r--r-- | util/strmap.c (renamed from src/strmap.c) | 0 | ||||
-rw-r--r-- | util/strmap.h (renamed from src/strmap.h) | 2 | ||||
-rw-r--r-- | util/strnlen.h (renamed from src/strnlen.h) | 0 | ||||
-rw-r--r-- | util/thread-utils.c (renamed from src/thread-utils.c) | 0 | ||||
-rw-r--r-- | util/thread-utils.h (renamed from src/thread-utils.h) | 0 | ||||
-rw-r--r-- | util/tsort.c (renamed from src/tsort.c) | 2 | ||||
-rw-r--r-- | util/unix/posix.h (renamed from src/unix/posix.h) | 4 | ||||
-rw-r--r-- | util/util.c (renamed from src/util.c) | 2 | ||||
-rw-r--r-- | util/util.h (renamed from src/util.h) | 4 | ||||
-rw-r--r-- | util/util_common.h | 178 | ||||
-rw-r--r-- | util/vector.c (renamed from src/vector.c) | 0 | ||||
-rw-r--r-- | util/vector.h (renamed from src/vector.h) | 2 |
59 files changed, 310 insertions, 234 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8feccdb86..e2c9c1edd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -289,6 +289,7 @@ IF(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS) ENDIF () ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(util) # Tests IF (NOT MSVC) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 42f4b5ca4..6af486896 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,6 +16,7 @@ SET(LIBGIT2_PC_LIBS "") SET(LIBGIT2_INCLUDES "${CMAKE_CURRENT_BINARY_DIR}" + "${libgit2_SOURCE_DIR}/util" "${libgit2_SOURCE_DIR}/src" "${libgit2_SOURCE_DIR}/include") SET(LIBGIT2_SYSTEM_INCLUDES "") @@ -27,30 +28,6 @@ IF(ENABLE_TRACE) ENDIF() ADD_FEATURE_INFO(tracing GIT_TRACE "tracing support") -CHECK_FUNCTION_EXISTS(futimens HAVE_FUTIMENS) -IF (HAVE_FUTIMENS) - SET(GIT_USE_FUTIMENS 1) -ENDIF () - -CHECK_PROTOTYPE_DEFINITION(qsort_r - "void qsort_r(void *base, size_t nmemb, size_t size, void *thunk, int (*compar)(void *, const void *, const void *))" - "" "stdlib.h" HAVE_QSORT_R_BSD) -IF (HAVE_QSORT_R_BSD) - ADD_DEFINITIONS(-DHAVE_QSORT_R_BSD) -ENDIF() - -CHECK_PROTOTYPE_DEFINITION(qsort_r - "void qsort_r(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg)" - "" "stdlib.h" HAVE_QSORT_R_GNU) -IF (HAVE_QSORT_R_GNU) - ADD_DEFINITIONS(-DHAVE_QSORT_R_GNU) -ENDIF() - -CHECK_FUNCTION_EXISTS(qsort_s HAVE_QSORT_S) -IF (HAVE_QSORT_S) - ADD_DEFINITIONS(-DHAVE_QSORT_S) -ENDIF () - # Find required dependencies IF(WIN32) @@ -289,12 +266,6 @@ IF (USE_LEAK_CHECKER STREQUAL "valgrind") ADD_DEFINITIONS(-DVALGRIND) ENDIF() -FILE(GLOB SRC_UTIL alloc.c alloc.h buffer.c buffer.h buf_text.c buf_text.h - futils.c futils.h hash.c hash.h ${SRC_SHA1} path.c path.h - pool.c pool.h posix.c posix.h strarray.c strarray.h - strmap.c strmap.h tsort.c util.c util.h vector.c vector.h - allocators/*.c allocators/*.h) - IF (WIN32 AND NOT CYGWIN) ELSE() FILE(GLOB SRC_UTIL_OS @@ -332,17 +303,6 @@ ENDIF() CONFIGURE_FILE(features.h.in git2/sys/features.h) -# Utility functions shared with other parts of the libgit2 - -SET(UTIL_SOURCES ${SRC_UTIL} ${SRC_UTIL_OS}) -ADD_LIBRARY(git2util OBJECT ${UTIL_SOURCES}) -LIST(APPEND UTIL_OBJECTS $<TARGET_OBJECTS:git2util>) - -TARGET_INCLUDE_DIRECTORIES(git2util PRIVATE ${LIBGIT2_INCLUDES} PUBLIC ${libgit2_SOURCE_DIR}/include) -TARGET_INCLUDE_DIRECTORIES(git2util SYSTEM PRIVATE ${LIBGIT2_SYSTEM_INCLUDES}) - -SET(LIBGIT2_UTIL_OBJECTS ${UTIL_OBJECTS} PARENT_SCOPE) - # libgit2 itself SET(LIBGIT2_SOURCES ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_SSH} ${SRC_SHA1}) @@ -369,7 +329,7 @@ ENDIF() # Compile and link libgit2 ADD_LIBRARY(git2 ${WIN_RC} ${LIBGIT2_OBJECTS}) -TARGET_LINK_LIBRARIES(git2 ${LIBGIT2_LIBS}) +TARGET_LINK_LIBRARIES(git2 git2util ${LIBGIT2_LIBS}) SET_TARGET_PROPERTIES(git2 PROPERTIES C_STANDARD 90) SET_TARGET_PROPERTIES(git2 PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR}) diff --git a/src/common.h b/src/common.h index a4152caf2..1e353760e 100644 --- a/src/common.h +++ b/src/common.h @@ -12,172 +12,6 @@ #endif #include "git2/common.h" -#include "cc-compat.h" - -/** Declare a function as always inlined. */ -#if defined(_MSC_VER) -# define GIT_INLINE(type) static __inline type -#elif defined(__GNUC__) -# define GIT_INLINE(type) static __inline__ type -#else -# define GIT_INLINE(type) static type -#endif - -/** Support for gcc/clang __has_builtin intrinsic */ -#ifndef __has_builtin -# define __has_builtin(x) 0 -#endif - -#include <assert.h> -#include <errno.h> -#include <limits.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include <sys/types.h> -#include <sys/stat.h> - -#ifdef GIT_WIN32 - -# include <io.h> -# include <direct.h> -# include <winsock2.h> -# include <windows.h> -# include <ws2tcpip.h> -# include "win32/msvc-compat.h" -# include "win32/mingw-compat.h" -# include "win32/w32_common.h" -# include "win32/win32-compat.h" -# include "win32/error.h" -# include "win32/version.h" -# ifdef GIT_THREADS -# include "win32/thread.h" -# endif - -#else - -# include <unistd.h> -# include <strings.h> -# ifdef GIT_THREADS -# include <pthread.h> -# include <sched.h> -# endif -#define GIT_STDLIB_CALL - -#ifdef GIT_USE_STAT_ATIMESPEC -# define st_atim st_atimespec -# define st_ctim st_ctimespec -# define st_mtim st_mtimespec -#endif - -# include <arpa/inet.h> - -#endif - -#include "git2/types.h" -#include "git2/errors.h" -#include "errors.h" -#include "thread-utils.h" -#include "integer.h" - -/* - * Include the declarations for deprecated functions; this ensures - * that they're decorated with the proper extern/visibility attributes. - */ -#include "git2/deprecated.h" - -#include "posix.h" - -#define DEFAULT_BUFSIZE 65536 -#define FILEIO_BUFSIZE DEFAULT_BUFSIZE -#define FILTERIO_BUFSIZE DEFAULT_BUFSIZE -#define NETIO_BUFSIZE DEFAULT_BUFSIZE - -/** - * Check a pointer allocation result, returning -1 if it failed. - */ -#define GIT_ERROR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; } - -/** - * Check a buffer allocation result, returning -1 if it failed. - */ -#define GIT_ERROR_CHECK_ALLOC_BUF(buf) if ((void *)(buf) == NULL || git_buf_oom(buf)) { return -1; } - -/** - * Check a return value and propagate result if non-zero. - */ -#define GIT_ERROR_CHECK_ERROR(code) \ - do { int _err = (code); if (_err) return _err; } while (0) - -/** - * Check a versioned structure for validity - */ -GIT_INLINE(int) git_error__check_version(const void *structure, unsigned int expected_max, const char *name) -{ - unsigned int actual; - - if (!structure) - return 0; - - actual = *(const unsigned int*)structure; - if (actual > 0 && actual <= expected_max) - return 0; - - git_error_set(GIT_ERROR_INVALID, "invalid version %d on %s", actual, name); - return -1; -} -#define GIT_ERROR_CHECK_VERSION(S,V,N) if (git_error__check_version(S,V,N) < 0) return -1 - -/** - * Initialize a structure with a version. - */ -GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int version) -{ - memset(structure, 0, len); - *((int*)structure) = version; -} -#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V) - -#define GIT_INIT_STRUCTURE_FROM_TEMPLATE(PTR,VERSION,TYPE,TPL) do { \ - TYPE _tmpl = TPL; \ - GIT_ERROR_CHECK_VERSION(&(VERSION), _tmpl.version, #TYPE); \ - memcpy((PTR), &_tmpl, sizeof(_tmpl)); } while (0) - - -/** Check for additive overflow, setting an error if would occur. */ -#define GIT_ADD_SIZET_OVERFLOW(out, one, two) \ - (git__add_sizet_overflow(out, one, two) ? (git_error_set_oom(), 1) : 0) - -/** Check for additive overflow, setting an error if would occur. */ -#define GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize) \ - (git__multiply_sizet_overflow(out, nelem, elsize) ? (git_error_set_oom(), 1) : 0) - -/** Check for additive overflow, failing if it would occur. */ -#define GIT_ERROR_CHECK_ALLOC_ADD(out, one, two) \ - if (GIT_ADD_SIZET_OVERFLOW(out, one, two)) { return -1; } - -#define GIT_ERROR_CHECK_ALLOC_ADD3(out, one, two, three) \ - if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ - GIT_ADD_SIZET_OVERFLOW(out, *(out), three)) { return -1; } - -#define GIT_ERROR_CHECK_ALLOC_ADD4(out, one, two, three, four) \ - if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ - GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \ - GIT_ADD_SIZET_OVERFLOW(out, *(out), four)) { return -1; } - -#define GIT_ERROR_CHECK_ALLOC_ADD5(out, one, two, three, four, five) \ - if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ - GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \ - GIT_ADD_SIZET_OVERFLOW(out, *(out), four) || \ - GIT_ADD_SIZET_OVERFLOW(out, *(out), five)) { return -1; } - -/** Check for multiplicative overflow, failing if it would occur. */ -#define GIT_ERROR_CHECK_ALLOC_MULTIPLY(out, nelem, elsize) \ - if (GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize)) { return -1; } - -/* NOTE: other git_error functions are in the public errors.h header file */ - -#include "util.h" +#include "util_common.h" #endif diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt new file mode 100644 index 000000000..736402153 --- /dev/null +++ b/util/CMakeLists.txt @@ -0,0 +1,108 @@ +IF(DEBUG_POOL) + SET(GIT_DEBUG_POOL 1) +ENDIF() +ADD_FEATURE_INFO(debugpool GIT_DEBUG_POOL "debug pool allocator") + +INCLUDE(PkgBuildConfig) + +SET(LIBGIT2_UTIL_INCLUDES + "${CMAKE_CURRENT_BINARY_DIR}" + "${libgit2_SOURCE_DIR}/util") +SET(LIBGIT2_SYSTEM_INCLUDES "") +SET(LIBGIT2_UTIL_LIBS "") + +CHECK_FUNCTION_EXISTS(futimens HAVE_FUTIMENS) +IF (HAVE_FUTIMENS) + SET(GIT_USE_FUTIMENS 1) +ENDIF () + +CHECK_PROTOTYPE_DEFINITION(qsort_r + "void qsort_r(void *base, size_t nmemb, size_t size, void *thunk, int (*compar)(void *, const void *, const void *))" + "" "stdlib.h" HAVE_QSORT_R_BSD) +IF (HAVE_QSORT_R_BSD) + ADD_DEFINITIONS(-DHAVE_QSORT_R_BSD) +ENDIF() + +CHECK_PROTOTYPE_DEFINITION(qsort_r + "void qsort_r(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg)" + "" "stdlib.h" HAVE_QSORT_R_GNU) +IF (HAVE_QSORT_R_GNU) + ADD_DEFINITIONS(-DHAVE_QSORT_R_GNU) +ENDIF() + +CHECK_FUNCTION_EXISTS(qsort_s HAVE_QSORT_S) +IF (HAVE_QSORT_S) + ADD_DEFINITIONS(-DHAVE_QSORT_S) +ENDIF () + +CHECK_LIBRARY_EXISTS(rt clock_gettime "time.h" NEED_LIBRT) +IF(NEED_LIBRT) + LIST(APPEND LIBGIT2_UTIL_LIBS rt) + LIST(APPEND LIBGIT2_UTIL_PC_LIBS "-lrt") +ENDIF() + +Include(SelectHashes) + +# Optional external dependency: iconv +IF (USE_ICONV) + FIND_PACKAGE(Iconv) +ENDIF() +IF (ICONV_FOUND) + SET(GIT_USE_ICONV 1) + LIST(APPEND LIBGIT2_UTIL_SYSTEM_INCLUDES ${ICONV_INCLUDE_DIR}) + LIST(APPEND LIBGIT2_UTIL_LIBS ${ICONV_LIBRARIES}) + LIST(APPEND LIBGIT2_UTIL_PC_LIBS ${ICONV_LIBRARIES}) +ENDIF() +ADD_FEATURE_INFO(iconv GIT_USE_ICONV "iconv encoding conversion support") + +IF (THREADSAFE) + IF (NOT WIN32) + FIND_PACKAGE(Threads REQUIRED) + ENDIF() + + SET(GIT_THREADS 1) +ENDIF() + +IF (USE_NSEC) + SET(GIT_USE_NSEC 1) +ENDIF() + +# Collect sourcefiles +FILE(GLOB SRC_H + "${libgit2_SOURCE_DIR}/include/git2.h" + "${libgit2_SOURCE_DIR}/include/git2/*.h" + "${libgit2_SOURCE_DIR}/include/git2/sys/*.h") + +# On Windows use specific platform sources +IF (WIN32 AND NOT CYGWIN) + IF(MSVC) + SET(WIN_RC "win32/git2.rc") + ENDIF() + + FILE(GLOB SRC_OS win32/*.c win32/*.h) +ELSE() + FILE(GLOB SRC_OS unix/*.c unix/*.h) +ENDIF() + +FILE(GLOB SRC_UTIL alloc.c alloc.h buffer.c buffer.h buf_text.c buf_text.h + futils.c futils.h hash.c hash.h ${SRC_SHA1} path.c path.h + pool.c pool.h posix.c posix.h strarray.c strarray.h + strmap.c strmap.h tsort.c util.c util.h vector.c vector.h + allocators/*.c allocators/*.h) + +IF (WIN32 AND NOT CYGWIN) +ELSE() + FILE(GLOB SRC_UTIL_OS + unix/map.c unix/posix.h unix/pthread.h unix/realpath.c) +ENDIF() + +# Utility functions shared with other parts of the libgit2 +SET(UTIL_SOURCES ${SRC_UTIL} ${SRC_UTIL_OS}) +ADD_LIBRARY(git2util OBJECT ${UTIL_SOURCES}) +LIST(APPEND UTIL_OBJECTS $<TARGET_OBJECTS:git2util>) + +TARGET_INCLUDE_DIRECTORIES(git2util PRIVATE ${LIBGIT2_UTIL_INCLUDES} PUBLIC ${libgit2_SOURCE_DIR}/include) +TARGET_INCLUDE_DIRECTORIES(git2util SYSTEM PRIVATE ${LIBGIT2_UTIL_SYSTEM_INCLUDES}) + +SET(LIBGIT2_UTIL_OBJECTS ${UTIL_OBJECTS} PARENT_SCOPE) + diff --git a/src/alloc.c b/util/alloc.c index 51c4d8029..51c4d8029 100644 --- a/src/alloc.c +++ b/util/alloc.c diff --git a/src/alloc.h b/util/alloc.h index 04fb7e101..04fb7e101 100644 --- a/src/alloc.h +++ b/util/alloc.h diff --git a/src/allocators/stdalloc.c b/util/allocators/stdalloc.c index c4938e32b..c4938e32b 100644 --- a/src/allocators/stdalloc.c +++ b/util/allocators/stdalloc.c diff --git a/src/allocators/stdalloc.h b/util/allocators/stdalloc.h index fa23fe6e3..1387d8bee 100644 --- a/src/allocators/stdalloc.h +++ b/util/allocators/stdalloc.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_allocators_stdalloc_h__ #define INCLUDE_allocators_stdalloc_h__ -#include "common.h" +#include "util_common.h" #include "alloc.h" diff --git a/src/allocators/win32_crtdbg.c b/util/allocators/win32_crtdbg.c index 1187e2fcd..1187e2fcd 100644 --- a/src/allocators/win32_crtdbg.c +++ b/util/allocators/win32_crtdbg.c diff --git a/src/allocators/win32_crtdbg.h b/util/allocators/win32_crtdbg.h index 754c6b6fb..af7c087fa 100644 --- a/src/allocators/win32_crtdbg.h +++ b/util/allocators/win32_crtdbg.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_allocators_crtdbg_h #define INCLUDE_allocators_crtdbg_h -#include "common.h" +#include "util_common.h" #include "alloc.h" diff --git a/src/buf_text.c b/util/buf_text.c index 88fcb87dd..88fcb87dd 100644 --- a/src/buf_text.c +++ b/util/buf_text.c diff --git a/src/buf_text.h b/util/buf_text.h index 726b0ae7b..24330f7c8 100644 --- a/src/buf_text.h +++ b/util/buf_text.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_buf_text_h__ #define INCLUDE_buf_text_h__ -#include "common.h" +#include "util_common.h" #include "buffer.h" diff --git a/src/buffer.c b/util/buffer.c index 328fdfe7f..328fdfe7f 100644 --- a/src/buffer.c +++ b/util/buffer.c diff --git a/src/buffer.h b/util/buffer.h index 6b717d2e9..5dbfc3c50 100644 --- a/src/buffer.h +++ b/util/buffer.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_buffer_h__ #define INCLUDE_buffer_h__ -#include "common.h" +#include "util_common.h" #include "git2/strarray.h" #include "git2/buffer.h" diff --git a/src/cc-compat.h b/util/cc-compat.h index 7ade561f3..7ade561f3 100644 --- a/src/cc-compat.h +++ b/util/cc-compat.h diff --git a/src/errors.c b/util/errors.c index 6cabb97ee..6cabb97ee 100644 --- a/src/errors.c +++ b/util/errors.c diff --git a/src/errors.h b/util/errors.h index a2f60f752..f863fade4 100644 --- a/src/errors.h +++ b/util/errors.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_errors_h__ #define INCLUDE_errors_h__ -#include "common.h" +#include "util_common.h" /* * Set the error message for this thread, formatting as needed. diff --git a/src/futils.c b/util/futils.c index a7c360a1c..d2271a5c4 100644 --- a/src/futils.c +++ b/util/futils.c @@ -7,8 +7,8 @@ #include "futils.h" -#include "global.h" #include "strmap.h" +#include "hash.h" #include <ctype.h> #if GIT_WIN32 #include "win32/findfile.h" diff --git a/src/futils.h b/util/futils.h index 3d5664679..40095a6ea 100644 --- a/src/futils.h +++ b/util/futils.h @@ -7,14 +7,13 @@ #ifndef INCLUDE_futils_h__ #define INCLUDE_futils_h__ -#include "common.h" +#include "util_common.h" #include "map.h" #include "posix.h" #include "path.h" #include "pool.h" #include "strmap.h" -#include "oid.h" /** * Filebuffer methods diff --git a/src/hash.c b/util/hash.c index 405c46a9a..405c46a9a 100644 --- a/src/hash.c +++ b/util/hash.c diff --git a/src/hash.h b/util/hash.h index 017bb286c..b7bbdab74 100644 --- a/src/hash.h +++ b/util/hash.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_hash_h__ #define INCLUDE_hash_h__ -#include "common.h" +#include "util_common.h" #include "git2/oid.h" diff --git a/src/hash/sha1.h b/util/hash/sha1.h index fb8d62f80..608849623 100644 --- a/src/hash/sha1.h +++ b/util/hash/sha1.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_hash_sha1_h__ #define INCLUDE_hash_sha1_h__ -#include "common.h" +#include "util_common.h" typedef struct git_hash_sha1_ctx git_hash_sha1_ctx; diff --git a/src/hash/sha1/collisiondetect.c b/util/hash/sha1/collisiondetect.c index e6a126780..e6a126780 100644 --- a/src/hash/sha1/collisiondetect.c +++ b/util/hash/sha1/collisiondetect.c diff --git a/src/hash/sha1/collisiondetect.h b/util/hash/sha1/collisiondetect.h index eb88e86c1..eb88e86c1 100644 --- a/src/hash/sha1/collisiondetect.h +++ b/util/hash/sha1/collisiondetect.h diff --git a/src/hash/sha1/common_crypto.c b/util/hash/sha1/common_crypto.c index 0449a3c9d..0449a3c9d 100644 --- a/src/hash/sha1/common_crypto.c +++ b/util/hash/sha1/common_crypto.c diff --git a/src/hash/sha1/common_crypto.h b/util/hash/sha1/common_crypto.h index a5fcfb33e..a5fcfb33e 100644 --- a/src/hash/sha1/common_crypto.h +++ b/util/hash/sha1/common_crypto.h diff --git a/src/hash/sha1/generic.c b/util/hash/sha1/generic.c index 607fe3a43..607fe3a43 100644 --- a/src/hash/sha1/generic.c +++ b/util/hash/sha1/generic.c diff --git a/src/hash/sha1/generic.h b/util/hash/sha1/generic.h index e4cc6026b..e4cc6026b 100644 --- a/src/hash/sha1/generic.h +++ b/util/hash/sha1/generic.h diff --git a/src/hash/sha1/mbedtls.c b/util/hash/sha1/mbedtls.c index e44343fcf..e44343fcf 100644 --- a/src/hash/sha1/mbedtls.c +++ b/util/hash/sha1/mbedtls.c diff --git a/src/hash/sha1/mbedtls.h b/util/hash/sha1/mbedtls.h index 15f7462a4..15f7462a4 100644 --- a/src/hash/sha1/mbedtls.h +++ b/util/hash/sha1/mbedtls.h diff --git a/src/hash/sha1/openssl.c b/util/hash/sha1/openssl.c index ba3212ff2..ba3212ff2 100644 --- a/src/hash/sha1/openssl.c +++ b/util/hash/sha1/openssl.c diff --git a/src/hash/sha1/openssl.h b/util/hash/sha1/openssl.h index a223ca03e..a223ca03e 100644 --- a/src/hash/sha1/openssl.h +++ b/util/hash/sha1/openssl.h diff --git a/src/hash/sha1/sha1dc/sha1.c b/util/hash/sha1/sha1dc/sha1.c index 9d3cf81d4..9d3cf81d4 100644 --- a/src/hash/sha1/sha1dc/sha1.c +++ b/util/hash/sha1/sha1dc/sha1.c diff --git a/src/hash/sha1/sha1dc/sha1.h b/util/hash/sha1/sha1dc/sha1.h index 1e4e94be5..1e4e94be5 100644 --- a/src/hash/sha1/sha1dc/sha1.h +++ b/util/hash/sha1/sha1dc/sha1.h diff --git a/src/hash/sha1/sha1dc/ubc_check.c b/util/hash/sha1/sha1dc/ubc_check.c index b3beff2af..b3beff2af 100644 --- a/src/hash/sha1/sha1dc/ubc_check.c +++ b/util/hash/sha1/sha1dc/ubc_check.c diff --git a/src/hash/sha1/sha1dc/ubc_check.h b/util/hash/sha1/sha1dc/ubc_check.h index d7e17dc73..d7e17dc73 100644 --- a/src/hash/sha1/sha1dc/ubc_check.h +++ b/util/hash/sha1/sha1dc/ubc_check.h diff --git a/src/hash/sha1/win32.c b/util/hash/sha1/win32.c index c73695665..c73695665 100644 --- a/src/hash/sha1/win32.c +++ b/util/hash/sha1/win32.c diff --git a/src/hash/sha1/win32.h b/util/hash/sha1/win32.h index 791d20a42..791d20a42 100644 --- a/src/hash/sha1/win32.h +++ b/util/hash/sha1/win32.h diff --git a/src/integer.h b/util/integer.h index 067c0be1f..067c0be1f 100644 --- a/src/integer.h +++ b/util/integer.h diff --git a/src/khash.h b/util/khash.h index 40e2d1848..40e2d1848 100644 --- a/src/khash.h +++ b/util/khash.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_map_h__ #define INCLUDE_map_h__ -#include "common.h" +#include "util_common.h" /* p_mmap() prot values */ diff --git a/src/path.c b/util/path.c index f97cceddd..6781be24f 100644 --- a/src/path.c +++ b/util/path.c @@ -8,7 +8,7 @@ #include "path.h" #include "posix.h" -#include "repository.h" +#include "futils.h" #ifdef GIT_WIN32 #include "win32/posix.h" #include "win32/w32_buffer.h" diff --git a/src/path.h b/util/path.h index 5e8590dee..ca2fc5624 100644 --- a/src/path.h +++ b/util/path.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_path_h__ #define INCLUDE_path_h__ -#include "common.h" +#include "util_common.h" #include "posix.h" #include "buffer.h" diff --git a/src/pool.c b/util/pool.c index b3bc8d489..b3bc8d489 100644 --- a/src/pool.c +++ b/util/pool.c diff --git a/src/pool.h b/util/pool.h index 23f68990f..fd42ed37d 100644 --- a/src/pool.h +++ b/util/pool.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_pool_h__ #define INCLUDE_pool_h__ -#include "common.h" +#include "util_common.h" #include "vector.h" diff --git a/src/posix.c b/util/posix.c index fbaa7c3ca..fbaa7c3ca 100644 --- a/src/posix.c +++ b/util/posix.c diff --git a/src/posix.h b/util/posix.h index eef667762..4902b2a27 100644 --- a/src/posix.h +++ b/util/posix.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_posix_h__ #define INCLUDE_posix_h__ -#include "common.h" +#include "util_common.h" #include <fcntl.h> #include <time.h> diff --git a/src/strmap.c b/util/strmap.c index c6e5b6dc7..c6e5b6dc7 100644 --- a/src/strmap.c +++ b/util/strmap.c diff --git a/src/strmap.h b/util/strmap.h index 9f5e4cc8b..0a62f871a 100644 --- a/src/strmap.h +++ b/util/strmap.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_strmap_h__ #define INCLUDE_strmap_h__ -#include "common.h" +#include "util_common.h" /** A map with C strings as key. */ typedef struct kh_str_s git_strmap; diff --git a/src/strnlen.h b/util/strnlen.h index eecfe3c02..eecfe3c02 100644 --- a/src/strnlen.h +++ b/util/strnlen.h diff --git a/src/thread-utils.c b/util/thread-utils.c index e5ec6a843..e5ec6a843 100644 --- a/src/thread-utils.c +++ b/util/thread-utils.c diff --git a/src/thread-utils.h b/util/thread-utils.h index 035de699f..035de699f 100644 --- a/src/thread-utils.h +++ b/util/thread-utils.h diff --git a/src/tsort.c b/util/tsort.c index 8d1ed9787..8bc25788d 100644 --- a/src/tsort.c +++ b/util/tsort.c @@ -5,7 +5,7 @@ * a Linking Exception. For full terms see the included COPYING file. */ -#include "common.h" +#include "util_common.h" /** * An array-of-pointers implementation of Python's Timsort diff --git a/src/unix/posix.h b/util/unix/posix.h index 4fa725013..86e7a122a 100644 --- a/src/unix/posix.h +++ b/util/unix/posix.h @@ -7,10 +7,6 @@ #ifndef INCLUDE_unix_posix_h__ #define INCLUDE_unix_posix_h__ -#ifndef LIBGIT2_NO_FEATURES_H -# include "git2/sys/features.h" -#endif - #include <stdio.h> #include <dirent.h> #include <sys/param.h> diff --git a/src/util.c b/util/util.c index 51537f0e2..d28e4a47c 100644 --- a/src/util.c +++ b/util/util.c @@ -7,7 +7,7 @@ #include "util.h" -#include "common.h" +#include "util_common.h" #ifdef GIT_WIN32 # include "win32/utf-conv.h" diff --git a/src/util.h b/util/util.h index 39c935c09..145ce255c 100644 --- a/src/util.h +++ b/util/util.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_util_h__ #define INCLUDE_util_h__ -#include "common.h" +#include "util_common.h" #ifndef GIT_WIN32 # include <ctype.h> @@ -16,7 +16,7 @@ #include "git2/buffer.h" #include "buffer.h" -#include "common.h" +#include "util_common.h" #include "strnlen.h" #include "thread-utils.h" diff --git a/util/util_common.h b/util/util_common.h new file mode 100644 index 000000000..20d504f48 --- /dev/null +++ b/util/util_common.h @@ -0,0 +1,178 @@ +/* + * 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. + */ +#ifndef INCLUDE_common_util_h__ +#define INCLUDE_common_util_h__ + +#include "cc-compat.h" + +/** Declare a function as always inlined. */ +#if defined(_MSC_VER) +# define GIT_INLINE(type) static __inline type +#elif defined(__GNUC__) +# define GIT_INLINE(type) static __inline__ type +#else +# define GIT_INLINE(type) static type +#endif + +/** Support for gcc/clang __has_builtin intrinsic */ +#ifndef __has_builtin +# define __has_builtin(x) 0 +#endif + +#include <assert.h> +#include <errno.h> +#include <limits.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include <sys/types.h> +#include <sys/stat.h> + +#ifdef GIT_WIN32 + +# include <io.h> +# include <direct.h> +# include <winsock2.h> +# include <windows.h> +# include <ws2tcpip.h> +# include "win32/msvc-compat.h" +# include "win32/mingw-compat.h" +# include "win32/w32_common.h" +# include "win32/win32-compat.h" +# include "win32/error.h" +# include "win32/version.h" +# ifdef GIT_THREADS +# include "win32/thread.h" +# endif + +#else + +# include <unistd.h> +# include <strings.h> +# ifdef GIT_THREADS +# include <pthread.h> +# include <sched.h> +# endif +#define GIT_STDLIB_CALL + +#ifdef GIT_USE_STAT_ATIMESPEC +# define st_atim st_atimespec +# define st_ctim st_ctimespec +# define st_mtim st_mtimespec +#endif + +# include <arpa/inet.h> + +#endif + +#include "git2/types.h" +#include "git2/errors.h" +#include "errors.h" +#include "thread-utils.h" +#include "integer.h" + +/* + * Include the declarations for deprecated functions; this ensures + * that they're decorated with the proper extern/visibility attributes. + */ +#include "git2/deprecated.h" + +#include "posix.h" + +#define DEFAULT_BUFSIZE 65536 +#define FILEIO_BUFSIZE DEFAULT_BUFSIZE +#define FILTERIO_BUFSIZE DEFAULT_BUFSIZE +#define NETIO_BUFSIZE DEFAULT_BUFSIZE + +/** + * Check a pointer allocation result, returning -1 if it failed. + */ +#define GIT_ERROR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; } + +/** + * Check a buffer allocation result, returning -1 if it failed. + */ +#define GIT_ERROR_CHECK_ALLOC_BUF(buf) if ((void *)(buf) == NULL || git_buf_oom(buf)) { return -1; } + +/** + * Check a return value and propagate result if non-zero. + */ +#define GIT_ERROR_CHECK_ERROR(code) \ + do { int _err = (code); if (_err) return _err; } while (0) + +/** + * Check a versioned structure for validity + */ +GIT_INLINE(int) git_error__check_version(const void *structure, unsigned int expected_max, const char *name) +{ + unsigned int actual; + + if (!structure) + return 0; + + actual = *(const unsigned int*)structure; + if (actual > 0 && actual <= expected_max) + return 0; + + git_error_set(GIT_ERROR_INVALID, "invalid version %d on %s", actual, name); + return -1; +} +#define GIT_ERROR_CHECK_VERSION(S,V,N) if (git_error__check_version(S,V,N) < 0) return -1 + +/** + * Initialize a structure with a version. + */ +GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int version) +{ + memset(structure, 0, len); + *((int*)structure) = version; +} +#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V) + +#define GIT_INIT_STRUCTURE_FROM_TEMPLATE(PTR,VERSION,TYPE,TPL) do { \ + TYPE _tmpl = TPL; \ + GIT_ERROR_CHECK_VERSION(&(VERSION), _tmpl.version, #TYPE); \ + memcpy((PTR), &_tmpl, sizeof(_tmpl)); } while (0) + + +/** Check for additive overflow, setting an error if would occur. */ +#define GIT_ADD_SIZET_OVERFLOW(out, one, two) \ + (git__add_sizet_overflow(out, one, two) ? (git_error_set_oom(), 1) : 0) + +/** Check for additive overflow, setting an error if would occur. */ +#define GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize) \ + (git__multiply_sizet_overflow(out, nelem, elsize) ? (git_error_set_oom(), 1) : 0) + +/** Check for additive overflow, failing if it would occur. */ +#define GIT_ERROR_CHECK_ALLOC_ADD(out, one, two) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two)) { return -1; } + +#define GIT_ERROR_CHECK_ALLOC_ADD3(out, one, two, three) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), three)) { return -1; } + +#define GIT_ERROR_CHECK_ALLOC_ADD4(out, one, two, three, four) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), four)) { return -1; } + +#define GIT_ERROR_CHECK_ALLOC_ADD5(out, one, two, three, four, five) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), four) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), five)) { return -1; } + +/** Check for multiplicative overflow, failing if it would occur. */ +#define GIT_ERROR_CHECK_ALLOC_MULTIPLY(out, nelem, elsize) \ + if (GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize)) { return -1; } + +/* NOTE: other git_error functions are in the public errors.h header file */ + +#include "util.h" + +#endif diff --git a/src/vector.c b/util/vector.c index b51e7644b..b51e7644b 100644 --- a/src/vector.c +++ b/util/vector.c diff --git a/src/vector.h b/util/vector.h index cc4c314d5..63633bcb1 100644 --- a/src/vector.h +++ b/util/vector.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_vector_h__ #define INCLUDE_vector_h__ -#include "common.h" +#include "util_common.h" typedef int (*git_vector_cmp)(const void *, const void *); |