* [PATCH 1/3] Convert compress helpers to use an extended z_stream
@ 2008-01-13 13:24 Marco Costalba
2008-01-13 13:24 ` [PATCH 2/3] Rename z_stream to ext_stream across the sources Marco Costalba
0 siblings, 1 reply; 3+ messages in thread
From: Marco Costalba @ 2008-01-13 13:24 UTC (permalink / raw)
To: gitster; +Cc: git, Marco Costalba
Instead of passing as argument to compress/decompress
helpers the zlib native z_stream create a new extended
one called ext_stream and use this instead.
This will allow us to associate the specified compress
algorithm on a 'per stream' base.
The patch aims to be as less intrusive as possible
so that ext_stream is really a (slightly) extended
z_stream and includes a z_stream member.
Choice to use a z_stream variable instead of a z_stream*
pointer as member is to avoid a xmalloc when a local
ext_stream variable is used.
This patch just introduces ext_stream and modifies the
helpers to cope with that. Next patch will rename z_stream
to ext_stream across the sources. Tough this patch will
not compile without the next I splitted the two for easing
the review: the interesting part is here, the next is
just renaming.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
---
The patch series apply above my last one sent yesterday.
This patch series is intended more as a RFC then to
be applied.
While the previous series introduced a cleanup, this one
extends the compress/decompress helpers framwork to support
different compress backends.
The compress backends are associated to each z_stream, it means
I can have multiple z_stream working with different backends at
the same time.
The rationale for this low level link instead of setting the chosen
comrpess library, say, at repository level, is that pulling/pushing
packs on the net must continue to work with zlib so to not break
things and also that, as Junio pointed out, today we don't
unpackage and repackage the objects that arrive with a git-pull,
but store the incoming pack as is.
So we can have different packs compressed with different backends
in the same repo. To allow this the compression algorithm info should
be linked at the z_stream level. Note that this is _almost_ trasparent
to the user.
Finally this is the first patch series sent with send-mail, so to try
to overcome the lines wrapping damage of my mailer.
BTW it took more time to setup send-mail and send the patches then to
write them! ;-)
compress.c | 64 ++++++++++++++++++++++++++++++------------------------------
compress.h | 52 ++++++++++++++++++++++++++++++++++++++----------
2 files changed, 73 insertions(+), 43 deletions(-)
diff --git a/compress.c b/compress.c
index f73cf2c..b28c389 100644
--- a/compress.c
+++ b/compress.c
@@ -5,46 +5,46 @@
* Compression helpers
*/
-unsigned long compress_alloc(z_stream *stream, int level, unsigned long size)
+unsigned long compress_alloc(ext_stream *stream, int level, unsigned long size)
{
memset(stream, 0, sizeof(*stream));
- deflateInit(stream, level);
- return deflateBound(stream, size);
+ deflateInit(&stream->z, level);
+ return deflateBound(&stream->z, size);
}
-int compress_start(z_stream *stream,
+int compress_start(ext_stream *stream,
unsigned char *in, unsigned long in_size,
unsigned char *out, unsigned long out_size)
{
- stream->next_out = out;
- stream->avail_out = out_size;
- stream->next_in = in;
- stream->avail_in = in_size;
+ stream->z.next_out = out;
+ stream->z.avail_out = out_size;
+ stream->z.next_in = in;
+ stream->z.avail_in = in_size;
return Z_OK;
}
-int compress_next(z_stream *stream, int flush)
+int compress_next(ext_stream *stream, int flush)
{
int result;
do {
- result = deflate(stream, flush);
+ result = deflate(&stream->z, flush);
} while (result == Z_OK);
return result;
}
-unsigned long compress_free(z_stream *stream)
+unsigned long compress_free(ext_stream *stream)
{
- deflateEnd(stream);
- return stream->total_out;
+ deflateEnd(&stream->z);
+ return stream->z.total_out;
}
unsigned long compress_all(int level, unsigned char *in,
unsigned long in_size, unsigned char **out)
{
unsigned long out_size;
- z_stream stream;
+ ext_stream stream;
out_size = compress_alloc(&stream, level, in_size);
*out = xmalloc(out_size);
@@ -65,47 +65,47 @@ unsigned long compress_all(int level, unsigned char *in,
* Decompression helpers
*/
-int decompress_alloc(z_stream *stream)
+int decompress_alloc(ext_stream *stream)
{
memset(stream, 0, sizeof(*stream));
- return inflateInit(stream);
+ return inflateInit(&stream->z);
}
-int decompress_from(z_stream *stream, unsigned char *in, unsigned long in_size)
+int decompress_from(ext_stream *stream, unsigned char *in, unsigned long in_size)
{
- stream->next_in = in;
- stream->avail_in = in_size;
+ stream->z.next_in = in;
+ stream->z.avail_in = in_size;
return Z_OK;
}
-int decompress_into(z_stream *stream, unsigned char *out, unsigned long out_size)
+int decompress_into(ext_stream *stream, unsigned char *out, unsigned long out_size)
{
- stream->next_out = out;
- stream->avail_out = out_size;
+ stream->z.next_out = out;
+ stream->z.avail_out = out_size;
return Z_OK;
}
-int decompress_next(z_stream *stream, int flush)
+int decompress_next(ext_stream *stream, int flush)
{
- return inflate(stream, flush);
+ return inflate(&stream->z, flush);
}
-int decompress_next_from(z_stream *stream, unsigned char *in, unsigned long in_size, int flush)
+int decompress_next_from(ext_stream *stream, unsigned char *in, unsigned long in_size, int flush)
{
decompress_from(stream, in, in_size);
- return inflate(stream, flush);
+ return inflate(&stream->z, flush);
}
-int decompress_next_into(z_stream *stream, unsigned char *out, unsigned long out_size, int flush)
+int decompress_next_into(ext_stream *stream, unsigned char *out, unsigned long out_size, int flush)
{
decompress_into(stream, out, out_size);
- return inflate(stream, flush);
+ return inflate(&stream->z, flush);
}
-unsigned long decompress_free(z_stream *stream)
+unsigned long decompress_free(ext_stream *stream)
{
- inflateEnd(stream);
- return stream->total_out;
+ inflateEnd(&stream->z);
+ return stream->z.total_out;
}
unsigned long decompress_all(unsigned char *in, unsigned long in_size,
@@ -113,7 +113,7 @@ unsigned long decompress_all(unsigned char *in, unsigned long in_size,
{
/* caller should check for return value != 0 */
- z_stream stream;
+ ext_stream stream;
int st;
if (decompress_alloc(&stream) != Z_OK)
diff --git a/compress.h b/compress.h
index a81d006..d1de31f 100644
--- a/compress.h
+++ b/compress.h
@@ -1,25 +1,55 @@
#ifndef COMPRESS_H
#define COMPRESS_H
-extern unsigned long compress_alloc(z_stream *stream, int level, unsigned long size);
-extern int compress_start(z_stream *stream, unsigned char *in, unsigned long in_size,
+/* Any compress/decompress engine must implement all the
+ * below functions that are modeled after the zlib ones.
+ */
+typedef int (*deflateInit_fn_t)(z_stream *stream, int level);
+typedef int (*deflate_fn_t)(z_stream *stream, int flush);
+typedef int (*deflateEnd_fn_t)(z_stream *stream);
+typedef unsigned long (*deflateBound_fn_t)(z_stream *stream, unsigned long size);
+
+typedef int (*inflateInit_fn_t)(z_stream *stream);
+typedef int (*inflate_fn_t)(z_stream *stream, int flush);
+typedef int (*inflateEnd_fn_t)(z_stream *stream);
+
+
+/* Extended struct used instead of the zlib native to
+ * call the compress/decompress helpers. It's just a
+ * thin extension of the zlib native one.
+ */
+typedef struct ext_stream_s {
+ z_stream z; /* defined in zlib.h to store stream state */
+
+ /* pointers to low level compress library functions */
+ deflateInit_fn_t deflateInit_fn;
+ deflate_fn_t deflate_fn;
+ deflateEnd_fn_t deflateEnd_fn;
+ deflateBound_fn_t deflateBound_fn;
+ inflateInit_fn_t inflateInit_fn;
+ inflate_fn_t inflate_fn;
+ inflateEnd_fn_t inflateEnd_fn;
+} ext_stream;
+
+extern unsigned long compress_alloc(ext_stream *stream, int level, unsigned long size);
+extern int compress_start(ext_stream *stream, unsigned char *in, unsigned long in_size,
unsigned char *out, unsigned long out_size);
-extern int compress_next(z_stream *stream, int flush);
-extern unsigned long compress_free(z_stream *stream);
+extern int compress_next(ext_stream *stream, int flush);
+extern unsigned long compress_free(ext_stream *stream);
extern unsigned long compress_all(int level, unsigned char *in, unsigned long in_size,
unsigned char **out);
-extern int decompress_alloc(z_stream *stream);
+extern int decompress_alloc(ext_stream *stream);
-extern int decompress_from(z_stream *stream, unsigned char *in, unsigned long in_size);
-extern int decompress_into(z_stream *stream, unsigned char *out, unsigned long out_size);
+extern int decompress_from(ext_stream *stream, unsigned char *in, unsigned long in_size);
+extern int decompress_into(ext_stream *stream, unsigned char *out, unsigned long out_size);
-extern int decompress_next(z_stream *stream, int flush);
-extern int decompress_next_from(z_stream *stream, unsigned char *in, unsigned long in_size, int flush);
-extern int decompress_next_into(z_stream *stream, unsigned char *out, unsigned long out_size, int flush);
+extern int decompress_next(ext_stream *stream, int flush);
+extern int decompress_next_from(ext_stream *stream, unsigned char *in, unsigned long in_size, int flush);
+extern int decompress_next_into(ext_stream *stream, unsigned char *out, unsigned long out_size, int flush);
-extern unsigned long decompress_free(z_stream *stream);
+extern unsigned long decompress_free(ext_stream *stream);
extern unsigned long decompress_all(unsigned char *in, unsigned long in_size,
unsigned char *out, unsigned long out_size);
--
1.5.4.rc2.98.g58cd2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/3] Rename z_stream to ext_stream across the sources
2008-01-13 13:24 [PATCH 1/3] Convert compress helpers to use an extended z_stream Marco Costalba
@ 2008-01-13 13:24 ` Marco Costalba
2008-01-13 13:24 ` [PATCH 3/3] Abstract out zlib Marco Costalba
0 siblings, 1 reply; 3+ messages in thread
From: Marco Costalba @ 2008-01-13 13:24 UTC (permalink / raw)
To: gitster; +Cc: git, Marco Costalba
Also fix the places where z_stream member access
is open coded.
In the future we could hide direct z_stream handling
behind some helpers, but for now leave it like it is
now to better understand what is going on.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
---
builtin-pack-objects.c | 8 +++---
builtin-unpack-objects.c | 6 ++--
http-push.c | 12 +++++-----
http-walker.c | 6 ++--
index-pack.c | 6 ++--
sha1_file.c | 56 +++++++++++++++++++++++-----------------------
6 files changed, 47 insertions(+), 47 deletions(-)
diff --git a/builtin-pack-objects.c b/builtin-pack-objects.c
index d2865fe..5165a23 100644
--- a/builtin-pack-objects.c
+++ b/builtin-pack-objects.c
@@ -300,7 +300,7 @@ static int check_pack_inflate(struct packed_git *p,
off_t len,
unsigned long expect)
{
- z_stream stream;
+ ext_stream stream;
unsigned char fakebuf[4096], *in;
unsigned int in_size = 0;
int st;
@@ -310,12 +310,12 @@ static int check_pack_inflate(struct packed_git *p,
decompress_into(&stream, fakebuf, sizeof(fakebuf));
in = use_pack(p, w_curs, offset, &in_size);
st = decompress_next_from(&stream, in, in_size, Z_FINISH);
- offset += stream.next_in - in;
+ offset += stream.z.next_in - in;
} while (st == Z_OK || st == Z_BUF_ERROR);
decompress_free(&stream);
return (st == Z_STREAM_END &&
- stream.total_out == expect &&
- stream.total_in == len) ? 0 : -1;
+ stream.z.total_out == expect &&
+ stream.z.total_in == len) ? 0 : -1;
}
static int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
diff --git a/builtin-unpack-objects.c b/builtin-unpack-objects.c
index c996560..066bf06 100644
--- a/builtin-unpack-objects.c
+++ b/builtin-unpack-objects.c
@@ -61,7 +61,7 @@ static void use(int bytes)
static void *get_data(unsigned long size)
{
- z_stream stream;
+ ext_stream stream;
unsigned char *buf = xmalloc(size);;
decompress_alloc(&stream);
@@ -71,8 +71,8 @@ static void *get_data(unsigned long size)
/* fill() modifies len, so be sure is evaluated as first */
void* tmp = fill(1);
int ret = decompress_next_from(&stream, tmp, len, Z_NO_FLUSH);
- use(len - stream.avail_in);
- if (stream.total_out == size && ret == Z_STREAM_END)
+ use(len - stream.z.avail_in);
+ if (stream.z.total_out == size && ret == Z_STREAM_END)
break;
if (ret != Z_OK) {
error("decompress returned %d\n", ret);
diff --git a/http-push.c b/http-push.c
index ec0568c..c7ea871 100644
--- a/http-push.c
+++ b/http-push.c
@@ -127,7 +127,7 @@ struct transfer_request
long http_code;
unsigned char real_sha1[20];
SHA_CTX c;
- z_stream stream;
+ ext_stream stream;
int zret;
int rename;
void *userData;
@@ -209,8 +209,8 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
request->zret = decompress_next_into(&request->stream, expn,
sizeof(expn), Z_SYNC_FLUSH);
SHA1_Update(&request->c, expn,
- sizeof(expn) - request->stream.avail_out);
- } while (request->stream.avail_in && request->zret == Z_OK);
+ sizeof(expn) - request->stream.z.avail_out);
+ } while (request->stream.z.avail_in && request->zret == Z_OK);
data_received++;
return size;
}
@@ -483,7 +483,7 @@ static void start_put(struct transfer_request *request)
unsigned long len;
int hdrlen;
ssize_t size;
- z_stream stream;
+ ext_stream stream;
unpacked = read_sha1_file(request->obj->sha1, &type, &len);
hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
@@ -501,8 +501,8 @@ static void start_put(struct transfer_request *request)
compress_next(&stream, Z_NO_FLUSH);
/* Then the data itself.. */
- stream.next_in = unpacked;
- stream.avail_in = len;
+ stream.z.next_in = unpacked;
+ stream.z.avail_in = len;
compress_next(&stream, Z_FINISH);
request->buffer.buf.len = compress_free(&stream);
diff --git a/http-walker.c b/http-walker.c
index b1d2a28..1581746 100644
--- a/http-walker.c
+++ b/http-walker.c
@@ -38,7 +38,7 @@ struct object_request
long http_code;
unsigned char real_sha1[20];
SHA_CTX c;
- z_stream stream;
+ ext_stream stream;
int zret;
int rename;
struct active_request_slot *slot;
@@ -83,8 +83,8 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
obj_req->zret = decompress_next_into(&obj_req->stream, expn,
sizeof(expn), Z_SYNC_FLUSH);
SHA1_Update(&obj_req->c, expn,
- sizeof(expn) - obj_req->stream.avail_out);
- } while (obj_req->stream.avail_in && obj_req->zret == Z_OK);
+ sizeof(expn) - obj_req->stream.z.avail_out);
+ } while (obj_req->stream.z.avail_in && obj_req->zret == Z_OK);
data_received++;
return size;
}
diff --git a/index-pack.c b/index-pack.c
index 929de39..7881a0b 100644
--- a/index-pack.c
+++ b/index-pack.c
@@ -166,7 +166,7 @@ static void bad_object(unsigned long offset, const char *format, ...)
static void *unpack_entry_data(unsigned long offset, unsigned long size)
{
- z_stream stream;
+ ext_stream stream;
void *buf = xmalloc(size);
decompress_alloc(&stream);
@@ -176,8 +176,8 @@ static void *unpack_entry_data(unsigned long offset, unsigned long size)
/* fill() modifies len, so be sure is evaluated as first */
void* tmp = fill(1);
int ret = decompress_next_from(&stream, tmp, input_len, Z_NO_FLUSH);
- use(input_len - stream.avail_in);
- if (stream.total_out == size && ret == Z_STREAM_END)
+ use(input_len - stream.z.avail_in);
+ if (stream.z.total_out == size && ret == Z_STREAM_END)
break;
if (ret != Z_OK)
bad_object(offset, "decompress returned %d", ret);
diff --git a/sha1_file.c b/sha1_file.c
index 708727a..a978f13 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1052,7 +1052,7 @@ unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned lon
return used;
}
-static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
+static int unpack_sha1_header(ext_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
{
unsigned long size, used;
static const char valid_loose_object_type[8] = {
@@ -1087,19 +1087,19 @@ static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned lon
decompress_from(stream, map, mapsize);
/* And generate the fake traditional header */
- stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu",
+ stream->z.total_out = 1 + snprintf(buffer, bufsiz, "%s %lu",
typename(type), size);
return 0;
}
-static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
+static void *unpack_sha1_rest(ext_stream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
{
int bytes = strlen(buffer) + 1;
unsigned char *buf = xmalloc(1+size);
unsigned long n;
int status = Z_OK;
- n = stream->total_out - bytes;
+ n = stream->z.total_out - bytes;
if (n > size)
n = size;
memcpy(buf, (char *) buffer + bytes, n);
@@ -1123,14 +1123,14 @@ static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size
status = decompress_next(stream, Z_FINISH);
}
buf[size] = 0;
- if (status == Z_STREAM_END && !stream->avail_in) {
+ if (status == Z_STREAM_END && !stream->z.avail_in) {
decompress_free(stream);
return buf;
}
if (status < 0)
error("corrupt loose object '%s'", sha1_to_hex(sha1));
- else if (stream->avail_in)
+ else if (stream->z.avail_in)
error("garbage at end of loose object '%s'",
sha1_to_hex(sha1));
free(buf);
@@ -1191,7 +1191,7 @@ static int parse_sha1_header(const char *hdr, unsigned long *sizep)
static void *unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size, const unsigned char *sha1)
{
int ret;
- z_stream stream;
+ ext_stream stream;
char hdr[8192];
ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
@@ -1207,7 +1207,7 @@ unsigned long get_size_from_delta(struct packed_git *p,
{
const unsigned char *data;
unsigned char delta_head[20], *in;
- z_stream stream;
+ ext_stream stream;
int st;
unsigned int in_size = 0;
@@ -1217,11 +1217,11 @@ unsigned long get_size_from_delta(struct packed_git *p,
do {
in = use_pack(p, w_curs, curpos, &in_size);
st = decompress_next_from(&stream, in, in_size, Z_FINISH);
- curpos += stream.next_in - in;
+ curpos += stream.z.next_in - in;
} while ((st == Z_OK || st == Z_BUF_ERROR) &&
- stream.total_out < sizeof(delta_head));
+ stream.z.total_out < sizeof(delta_head));
decompress_free(&stream);
- if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head))
+ if ((st != Z_STREAM_END) && stream.z.total_out != sizeof(delta_head))
die("delta data unpack-initial failed");
/* Examine the initial part of the delta to figure out
@@ -1416,7 +1416,7 @@ static void *unpack_compressed_entry(struct packed_git *p,
unsigned long size)
{
int st;
- z_stream stream;
+ ext_stream stream;
unsigned char *buffer, *in;
unsigned int in_size = 0;
@@ -1427,10 +1427,10 @@ static void *unpack_compressed_entry(struct packed_git *p,
do {
in = use_pack(p, w_curs, curpos, &in_size);
st = decompress_next_from(&stream, in, in_size, Z_FINISH);
- curpos += stream.next_in - in;
+ curpos += stream.z.next_in - in;
} while (st == Z_OK || st == Z_BUF_ERROR);
decompress_free(&stream);
- if ((st != Z_STREAM_END) || stream.total_out != size) {
+ if ((st != Z_STREAM_END) || stream.z.total_out != size) {
free(buffer);
return NULL;
}
@@ -1762,7 +1762,7 @@ static int sha1_loose_object_info(const unsigned char *sha1, unsigned long *size
int status;
unsigned long mapsize, size;
void *map;
- z_stream stream;
+ ext_stream stream;
char hdr[32];
map = map_sha1_file(sha1, &mapsize);
@@ -2033,7 +2033,7 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
{
int size, ret;
unsigned char *compressed;
- z_stream stream;
+ ext_stream stream;
unsigned char sha1[20];
char *filename;
static char tmpfile[PATH_MAX];
@@ -2084,8 +2084,8 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
compress_next(&stream, Z_NO_FLUSH);
/* Then the data itself.. */
- stream.next_in = buf;
- stream.avail_in = len;
+ stream.z.next_in = buf;
+ stream.z.avail_in = len;
ret = compress_next(&stream, Z_FINISH);
if (ret != Z_STREAM_END)
die("unable to deflate new object %s (%d)", sha1_to_hex(sha1), ret);
@@ -2109,7 +2109,7 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
static void *repack_object(const unsigned char *sha1, unsigned long *objsize)
{
size_t size;
- z_stream stream;
+ ext_stream stream;
unsigned char *unpacked;
unsigned long len;
enum object_type type;
@@ -2135,8 +2135,8 @@ static void *repack_object(const unsigned char *sha1, unsigned long *objsize)
compress_next(&stream, Z_NO_FLUSH);
/* Then the data itself.. */
- stream.next_in = unpacked;
- stream.avail_in = len;
+ stream.z.next_in = unpacked;
+ stream.z.avail_in = len;
compress_next(&stream, Z_FINISH);
*objsize = compress_free(&stream);
@@ -2167,7 +2167,7 @@ int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
{
char tmpfile[PATH_MAX];
int local;
- z_stream stream;
+ ext_stream stream;
unsigned char real_sha1[20];
unsigned char discard[4096];
int ret;
@@ -2193,13 +2193,13 @@ int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
do {
ret = decompress_next_into(&stream, discard, sizeof(discard), Z_SYNC_FLUSH);
SHA1_Update(&c, discard, sizeof(discard) -
- stream.avail_out);
- } while (stream.avail_in && ret == Z_OK);
- if (write_buffer(local, buffer, *bufposn - stream.avail_in) < 0)
+ stream.z.avail_out);
+ } while (stream.z.avail_in && ret == Z_OK);
+ if (write_buffer(local, buffer, *bufposn - stream.z.avail_in) < 0)
die("unable to write sha1 file");
- memmove(buffer, buffer + *bufposn - stream.avail_in,
- stream.avail_in);
- *bufposn = stream.avail_in;
+ memmove(buffer, buffer + *bufposn - stream.z.avail_in,
+ stream.z.avail_in);
+ *bufposn = stream.z.avail_in;
if (ret != Z_OK)
break;
}
--
1.5.4.rc2.98.g58cd2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 3/3] Abstract out zlib
2008-01-13 13:24 ` [PATCH 2/3] Rename z_stream to ext_stream across the sources Marco Costalba
@ 2008-01-13 13:24 ` Marco Costalba
0 siblings, 0 replies; 3+ messages in thread
From: Marco Costalba @ 2008-01-13 13:24 UTC (permalink / raw)
To: gitster; +Cc: git, Marco Costalba
Finally remove the hardcoded link between
compression helpers and zlib.
Association bewteen a stream and the compression library
is done at stream init time through a set of function
pointers. Library choice is based on compression level
parameter passed to compress_alloc(), this allow us to
stay 100% back compatible with current code.
Patch also adds the the necessary zlib engine plugin,
this turns out to be trivial since we have modeled
everything around zlib.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
---
compress.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++---------
compress.h | 6 ++++++
2 files changed, 52 insertions(+), 9 deletions(-)
diff --git a/compress.c b/compress.c
index b28c389..09a5df0 100644
--- a/compress.c
+++ b/compress.c
@@ -1,6 +1,41 @@
#include "cache.h"
#include "compress.h"
+/* Default zlib engine plugin definition
+ *
+ * Because everything is modeled after zlib the
+ * corresponding engine wrappers here are trivial
+ */
+static int zlib_deflateInit(z_stream *s, int l) { return deflateInit(s, l); }
+static int zlib_deflate(z_stream *s, int f) { return deflate(s, f); }
+static int zlib_deflateEnd(z_stream *s) { return deflateEnd(s); }
+static unsigned long zlib_deflateBound(z_stream *s, unsigned long sz) { return deflateBound(s, sz); }
+
+static int zlib_inflateInit(z_stream *s) { return inflateInit(s); }
+static int zlib_inflate(z_stream *s, int f) { return inflate(s, f); }
+static int zlib_inflateEnd(z_stream *s) { return inflateEnd(s); }
+
+
+/* link the stream to the compression library functions */
+static int register_engine(ext_stream *stream, int level)
+{
+ switch (level) {
+ case LZO_COMPRESSION:
+ die ("LZO compression still not implemented");
+ break;
+ default: /* assumed to be zlib */
+ stream->deflateInit_fn = zlib_deflateInit;
+ stream->deflate_fn = zlib_deflate;
+ stream->deflateEnd_fn = zlib_deflateEnd;
+ stream->deflateBound_fn = zlib_deflateBound;
+ stream->inflateInit_fn = zlib_inflateInit;
+ stream->inflate_fn = zlib_inflate;
+ stream->inflateEnd_fn = zlib_inflateEnd;
+ break;
+ };
+ return Z_OK;
+}
+
/*
* Compression helpers
*/
@@ -8,8 +43,9 @@
unsigned long compress_alloc(ext_stream *stream, int level, unsigned long size)
{
memset(stream, 0, sizeof(*stream));
- deflateInit(&stream->z, level);
- return deflateBound(&stream->z, size);
+ register_engine(stream, level);
+ stream->deflateInit_fn(&stream->z, level);
+ return stream->deflateBound_fn(&stream->z, size);
}
int compress_start(ext_stream *stream,
@@ -28,7 +64,7 @@ int compress_next(ext_stream *stream, int flush)
int result;
do {
- result = deflate(&stream->z, flush);
+ result = stream->deflate_fn(&stream->z, flush);
} while (result == Z_OK);
return result;
@@ -36,7 +72,7 @@ int compress_next(ext_stream *stream, int flush)
unsigned long compress_free(ext_stream *stream)
{
- deflateEnd(&stream->z);
+ stream->deflateEnd_fn(&stream->z);
return stream->z.total_out;
}
@@ -68,7 +104,8 @@ unsigned long compress_all(int level, unsigned char *in,
int decompress_alloc(ext_stream *stream)
{
memset(stream, 0, sizeof(*stream));
- return inflateInit(&stream->z);
+ register_engine(stream, Z_DEFAULT_COMPRESSION); // FIXME for now zlib assumed
+ return stream->inflateInit_fn(&stream->z);
}
int decompress_from(ext_stream *stream, unsigned char *in, unsigned long in_size)
@@ -87,24 +124,24 @@ int decompress_into(ext_stream *stream, unsigned char *out, unsigned long out_si
int decompress_next(ext_stream *stream, int flush)
{
- return inflate(&stream->z, flush);
+ return stream->inflate_fn(&stream->z, flush);
}
int decompress_next_from(ext_stream *stream, unsigned char *in, unsigned long in_size, int flush)
{
decompress_from(stream, in, in_size);
- return inflate(&stream->z, flush);
+ return stream->inflate_fn(&stream->z, flush);
}
int decompress_next_into(ext_stream *stream, unsigned char *out, unsigned long out_size, int flush)
{
decompress_into(stream, out, out_size);
- return inflate(&stream->z, flush);
+ return stream->inflate_fn(&stream->z, flush);
}
unsigned long decompress_free(ext_stream *stream)
{
- inflateEnd(&stream->z);
+ stream->inflateEnd_fn(&stream->z);
return stream->z.total_out;
}
diff --git a/compress.h b/compress.h
index d1de31f..151234a 100644
--- a/compress.h
+++ b/compress.h
@@ -1,6 +1,12 @@
#ifndef COMPRESS_H
#define COMPRESS_H
+/* Add here custom compression levels. First 0-9
+ * and -1 are reserved values used by zlib
+ */
+#define LZO_COMPRESSION 99
+
+
/* Any compress/decompress engine must implement all the
* below functions that are modeled after the zlib ones.
*/
--
1.5.4.rc2.98.g58cd2
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-01-13 13:25 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-01-13 13:24 [PATCH 1/3] Convert compress helpers to use an extended z_stream Marco Costalba
2008-01-13 13:24 ` [PATCH 2/3] Rename z_stream to ext_stream across the sources Marco Costalba
2008-01-13 13:24 ` [PATCH 3/3] Abstract out zlib Marco Costalba
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).