]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/mkuzip/mkuz_conveyor.c
Import libxo-0.9.0:
[FreeBSD/FreeBSD.git] / usr.bin / mkuzip / mkuz_conveyor.c
1 /*
2  * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31 #include <err.h>
32 #include <inttypes.h>
33 #include <md5.h>
34 #include <pthread.h>
35 #include <stdlib.h>
36 #include <strings.h>
37
38 #if defined(MKUZ_DEBUG)
39 # include <stdio.h>
40 #endif
41
42 #include "mkuz_conveyor.h"
43 #include "mkuz_cfg.h"
44 #include "mkuzip.h"
45 #include "mkuz_format.h"
46 #include "mkuz_blk.h"
47 #include "mkuz_fqueue.h"
48 #include "mkuz_blk_chain.h"
49
50 static void compute_digest(struct mkuz_blk *);
51
52 struct cw_args {
53     struct mkuz_conveyor *cvp;
54     struct mkuz_cfg *cfp;
55 };
56
57 static void *
58 cworker(void *p)
59 {
60     struct cw_args *cwp;
61     struct mkuz_cfg *cfp;
62     struct mkuz_blk *oblk, *iblk;
63     struct mkuz_conveyor *cvp;
64     void *c_ctx;
65
66     cwp = (struct cw_args *)p;
67     cfp = cwp->cfp;
68     cvp = cwp->cvp;
69     free(cwp);
70     c_ctx = cfp->handler->f_init(cfp->blksz);
71     for (;;) {
72         iblk = mkuz_fqueue_deq(cvp->wrk_queue);
73         if (iblk == MKUZ_BLK_EOF) {
74             /* Let other threads to see the EOF block */
75             mkuz_fqueue_enq(cvp->wrk_queue, iblk);
76             break;
77         }
78         if (cfp->no_zcomp == 0 &&
79           mkuz_memvcmp(iblk->data, '\0', iblk->info.len) != 0) {
80             /* All zeroes block */
81             oblk = mkuz_blk_ctor(0);
82         } else {
83             oblk = cfp->handler->f_compress(c_ctx, iblk);
84             if (cfp->en_dedup != 0) {
85                 compute_digest(oblk);
86             }
87         }
88         oblk->info.blkno = iblk->info.blkno;
89         mkuz_fqueue_enq(cvp->results, oblk);
90         free(iblk);
91     }
92     return (NULL);
93 }
94
95 static void
96 compute_digest(struct mkuz_blk *bp)
97 {
98     MD5_CTX mcontext;
99
100     MD5Init(&mcontext);
101     MD5Update(&mcontext, bp->data, bp->info.len);
102     MD5Final(bp->info.digest, &mcontext);
103 }
104
105 struct mkuz_conveyor *
106 mkuz_conveyor_ctor(struct mkuz_cfg *cfp)
107 {
108     struct mkuz_conveyor *cp;
109     struct cw_args *cwp;
110     int i, r;
111
112     cp = mkuz_safe_zmalloc(sizeof(struct mkuz_conveyor) +
113       (sizeof(pthread_t) * cfp->nworkers));
114
115     cp->wrk_queue = mkuz_fqueue_ctor(1);
116     cp->results = mkuz_fqueue_ctor(1);
117
118     for (i = 0; i < cfp->nworkers; i++) {
119         cwp = mkuz_safe_zmalloc(sizeof(struct cw_args));
120         cwp->cfp = cfp;
121         cwp->cvp = cp;
122         r = pthread_create(&cp->wthreads[i], NULL, cworker, (void *)cwp);
123         if (r != 0) {
124             errx(1, "mkuz_conveyor_ctor: pthread_create() failed");
125             /* Not reached */
126         }
127     }
128     return (cp);
129 }