extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
extern int git_munmap(void *start, size_t length);
+#define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
+
#else /* NO_MMAP */
#include <sys/mman.h>
+#define DEFAULT_PACKED_GIT_WINDOW_SIZE \
+ (sizeof(void*) >= 8 \
+ ? 1 * 1024 * 1024 * 1024 \
+ : 32 * 1024 * 1024)
#endif /* NO_MMAP */
+#define DEFAULT_PACKED_GIT_LIMIT \
+ ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
+
+#ifdef NO_PREAD
+#define pread git_pread
+extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
+#endif
+
#ifdef NO_SETENV
#define setenv gitsetenv
extern int gitsetenv(const char *, const char *, int);
extern size_t gitstrlcpy(char *, const char *, size_t);
#endif
+extern void release_pack_memory(size_t);
+
static inline char* xstrdup(const char *str)
{
char *ret = strdup(str);
- if (!ret)
- die("Out of memory, strdup failed");
+ if (!ret) {
+ release_pack_memory(strlen(str) + 1);
+ ret = strdup(str);
+ if (!ret)
+ die("Out of memory, strdup failed");
+ }
return ret;
}
void *ret = malloc(size);
if (!ret && !size)
ret = malloc(1);
- if (!ret)
- die("Out of memory, malloc failed");
+ if (!ret) {
+ release_pack_memory(size);
+ ret = malloc(size);
+ if (!ret && !size)
+ ret = malloc(1);
+ if (!ret)
+ die("Out of memory, malloc failed");
+ }
#ifdef XMALLOC_POISON
memset(ret, 0xA5, size);
#endif
void *ret = realloc(ptr, size);
if (!ret && !size)
ret = realloc(ptr, 1);
- if (!ret)
- die("Out of memory, realloc failed");
+ if (!ret) {
+ release_pack_memory(size);
+ ret = realloc(ptr, size);
+ if (!ret && !size)
+ ret = realloc(ptr, 1);
+ if (!ret)
+ die("Out of memory, realloc failed");
+ }
return ret;
}
void *ret = calloc(nmemb, size);
if (!ret && (!nmemb || !size))
ret = calloc(1, 1);
- if (!ret)
- die("Out of memory, calloc failed");
+ if (!ret) {
+ release_pack_memory(nmemb * size);
+ ret = calloc(nmemb, size);
+ if (!ret && (!nmemb || !size))
+ ret = calloc(1, 1);
+ if (!ret)
+ die("Out of memory, calloc failed");
+ }
+ return ret;
+}
+
+static inline void *xmmap(void *start, size_t length,
+ int prot, int flags, int fd, off_t offset)
+{
+ void *ret = mmap(start, length, prot, flags, fd, offset);
+ if (ret == MAP_FAILED) {
+ if (!length)
+ return NULL;
+ release_pack_memory(length);
+ ret = mmap(start, length, prot, flags, fd, offset);
+ if (ret == MAP_FAILED)
+ die("Out of memory? mmap failed: %s", strerror(errno));
+ }
return ret;
}