avoid an extra memcpy in got_inflate_to_mem()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
diff --git a/got/Makefile b/got/Makefile
index 8e74120..b96fa54 100644
--- a/got/Makefile
+++ b/got/Makefile
@@ -7,7 +7,9 @@ SRCS= got.c delta.c error.c fileindex.c object.c path.c pack.c \
CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib
LDADD = -lutil -lz
DPADD = ${LIBZ} ${LIBUTIL}
-DEBUG = -O0 -g
+CC = gcc
+DEBUG = -O0 -pg
+CPPFLAGS += -DPROFILE
CFLAGS += -Werror -Wall -Wstrict-prototypes -Wunused-variable
# For now, default to installing binary in ~/bin
diff --git a/got/got.c b/got/got.c
index be95ec2..3ae4fc6 100644
--- a/got/got.c
+++ b/got/got.c
@@ -173,9 +173,10 @@ cmd_checkout(int argc, char *argv[])
argc -= optind;
argv += optind;
+#ifndef PROFILE
if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
err(1, "pledge");
-
+#endif
if (argc == 1) {
char *cwd, *base, *dotgit;
repo_path = argv[0];
@@ -298,9 +299,10 @@ cmd_log(int argc, char *argv[])
struct got_object *obj;
char *repo_path = NULL;
+#ifndef PROFILE
if (pledge("stdio rpath wpath cpath", NULL) == -1)
err(1, "pledge");
-
+#endif
if (argc == 1) {
repo_path = getcwd(NULL, 0);
if (repo_path == NULL)
diff --git a/lib/got_zbuf_lib.h b/lib/got_zbuf_lib.h
index 066d182..9285ed5 100644
--- a/lib/got_zbuf_lib.h
+++ b/lib/got_zbuf_lib.h
@@ -21,12 +21,14 @@ struct got_zstream_buf {
char *outbuf;
size_t outlen;
int flags;
-#define GOT_ZSTREAM_F_HAVE_MORE 0x01
+#define GOT_ZSTREAM_F_HAVE_MORE 0x01
+#define GOT_ZSTREAM_F_OWN_OUTBUF 0x02
};
-#define GOT_ZSTREAM_BUFSIZE 8192
+#define GOT_ZSTREAM_BUFSIZE 8192
-const struct got_error *got_inflate_init(struct got_zstream_buf *, size_t);
+const struct got_error *got_inflate_init(struct got_zstream_buf *, uint8_t *,
+ size_t);
const struct got_error *got_inflate_read(struct got_zstream_buf *, FILE *,
size_t *);
void got_inflate_end(struct got_zstream_buf *);
diff --git a/lib/object.c b/lib/object.c
index 6ad23fc..8a150ed 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -162,7 +162,7 @@ read_object_header(struct got_object **obj, struct got_repository *repo,
if (buf == NULL)
return got_error(GOT_ERR_NO_MEM);
- err = got_inflate_init(&zb, zbsize);
+ err = got_inflate_init(&zb, NULL, zbsize);
if (err)
return err;
@@ -759,7 +759,7 @@ got_object_blob_open(struct got_blob_object **blob,
return err;
}
- err = got_inflate_init(&(*blob)->zb, blocksize);
+ err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
if (err != NULL) {
fclose((*blob)->f);
free(*blob);
diff --git a/lib/zbuf.c b/lib/zbuf.c
index 4d83cf8..cf71b54 100644
--- a/lib/zbuf.c
+++ b/lib/zbuf.c
@@ -29,7 +29,7 @@
#include "got_zbuf_lib.h"
const struct got_error *
-got_inflate_init(struct got_zstream_buf *zb, size_t bufsize)
+got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
{
const struct got_error *err = NULL;
@@ -50,11 +50,15 @@ got_inflate_init(struct got_zstream_buf *zb, size_t bufsize)
goto done;
}
- zb->outbuf = calloc(1, zb->outlen);
- if (zb->outbuf == NULL) {
- err = got_error(GOT_ERR_NO_MEM);
- goto done;
- }
+ if (outbuf == NULL) {
+ zb->outbuf = calloc(1, zb->outlen);
+ if (zb->outbuf == NULL) {
+ err = got_error(GOT_ERR_NO_MEM);
+ goto done;
+ }
+ zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF;
+ } else
+ zb->outbuf = outbuf;
done:
if (err)
@@ -103,7 +107,8 @@ void
got_inflate_end(struct got_zstream_buf *zb)
{
free(zb->inbuf);
- free(zb->outbuf);
+ if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
+ free(zb->outbuf);
inflateEnd(&zb->z);
}
@@ -115,19 +120,23 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
struct got_zstream_buf zb;
void *newbuf;
- err = got_inflate_init(&zb, GOT_ZSTREAM_BUFSIZE);
+ *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
+ if (*outbuf == NULL)
+ return got_error(GOT_ERR_NO_MEM);
+ err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
if (err)
return err;
- *outbuf = NULL;
*outlen = 0;
do {
err = got_inflate_read(&zb, f, &avail);
if (err)
return err;
- if (avail > 0) {
- newbuf = reallocarray(*outbuf, 1, *outlen + avail);
+ *outlen += avail;
+ if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
+ newbuf = reallocarray(*outbuf, 1,
+ *outlen + GOT_ZSTREAM_BUFSIZE);
if (newbuf == NULL) {
free(*outbuf);
*outbuf = NULL;
@@ -135,9 +144,9 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
- memcpy(newbuf + *outlen, zb.outbuf, avail);
*outbuf = newbuf;
- *outlen += avail;
+ zb.outbuf = newbuf + *outlen;
+ zb.outlen = GOT_ZSTREAM_BUFSIZE;
}
} while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
@@ -153,7 +162,7 @@ got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
size_t avail;
struct got_zstream_buf zb;
- err = got_inflate_init(&zb, GOT_ZSTREAM_BUFSIZE);
+ err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
if (err)
goto done;