]> wimlib.net Git - wimlib/blob - src/compress_serial.c
lz_sarray: Performance and memory usage optimizations
[wimlib] / src / compress_serial.c
1 /*
2  * compress_serial.c
3  *
4  * Compress chunks of data (serial version).
5  */
6
7 /*
8  * Copyright (C) 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU General Public License as published by the Free Software
14  * Foundation; either version 3 of the License, or (at your option) any later
15  * version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along with
22  * wimlib; if not, see http://www.gnu.org/licenses/.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h"
27 #endif
28
29 #include "wimlib.h"
30 #include "wimlib/assert.h"
31 #include "wimlib/chunk_compressor.h"
32 #include "wimlib/util.h"
33
34 #include <string.h>
35
36 struct serial_chunk_compressor {
37         struct chunk_compressor base;
38         struct wimlib_compressor *compressor;
39         u8 *udata;
40         u8 *cdata;
41         unsigned ulen;
42         unsigned clen;
43 };
44
45 static void
46 serial_chunk_compressor_destroy(struct chunk_compressor *_ctx)
47 {
48         struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
49
50         if (ctx == NULL)
51                 return;
52
53         wimlib_free_compressor(ctx->compressor);
54         FREE(ctx->udata);
55         FREE(ctx->cdata);
56         FREE(ctx);
57 }
58
59 static bool
60 serial_chunk_compressor_submit_chunk(struct chunk_compressor *_ctx,
61                                      const void *chunk, size_t size)
62 {
63         struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
64
65         if (ctx->ulen != 0)
66                 return false;
67
68         wimlib_assert(size > 0);
69         wimlib_assert(size <= ctx->base.out_chunk_size);
70
71         memcpy(ctx->udata, chunk, size);
72         ctx->ulen = size;
73         return true;
74 }
75
76 static bool
77 serial_chunk_compressor_get_chunk(struct chunk_compressor *_ctx,
78                                   const void **cdata_ret, unsigned *csize_ret,
79                                   unsigned *usize_ret)
80 {
81         struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
82
83         if (ctx->ulen == 0)
84                 return false;
85
86         ctx->clen = wimlib_compress(ctx->udata, ctx->ulen,
87                                     ctx->cdata, ctx->ulen - 1,
88                                     ctx->compressor);
89
90         if (ctx->clen) {
91                 *cdata_ret = ctx->cdata;
92                 *csize_ret = ctx->clen;
93         } else {
94                 *cdata_ret = ctx->udata;
95                 *csize_ret = ctx->ulen;
96         }
97         *usize_ret = ctx->ulen;
98
99         ctx->ulen = 0;
100         return true;
101 }
102
103 int
104 new_serial_chunk_compressor(int out_ctype, u32 out_chunk_size,
105                             struct chunk_compressor **compressor_ret)
106 {
107         struct serial_chunk_compressor *ctx;
108         int ret;
109
110         ctx = CALLOC(1, sizeof(*ctx));
111         if (ctx == NULL)
112                 return WIMLIB_ERR_NOMEM;
113
114         ctx->base.out_ctype = out_ctype;
115         ctx->base.out_chunk_size = out_chunk_size;
116         ctx->base.num_threads = 1;
117         ctx->base.destroy = serial_chunk_compressor_destroy;
118         ctx->base.submit_chunk = serial_chunk_compressor_submit_chunk;
119         ctx->base.get_chunk = serial_chunk_compressor_get_chunk;
120
121
122         ret = wimlib_create_compressor(out_ctype, out_chunk_size,
123                                        NULL, &ctx->compressor);
124         if (ret)
125                 goto err;
126
127         ctx->udata = MALLOC(out_chunk_size);
128         ctx->cdata = MALLOC(out_chunk_size - 1);
129         ctx->ulen = 0;
130         if (ctx->udata == NULL || ctx->cdata == NULL) {
131                 ret = WIMLIB_ERR_NOMEM;
132                 goto err;
133         }
134
135         *compressor_ret = &ctx->base;
136         return 0;
137
138 err:
139         serial_chunk_compressor_destroy(&ctx->base);
140         return ret;
141 }