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/types.h>
28#include <err.h>
29#include <inttypes.h>
30#include <md5.h>
31#include <pthread.h>
32#include <stdlib.h>
33#include <strings.h>
34
35#if defined(MKUZ_DEBUG)
36# include <stdio.h>
37#endif
38
39#include "mkuz_conveyor.h"
40#include "mkuz_cfg.h"
41#include "mkuzip.h"
42#include "mkuz_blk.h"
43#include "mkuz_format.h"
44#include "mkuz_fqueue.h"
45#include "mkuz_blk_chain.h"
46
47static void compute_digest(struct mkuz_blk *);
48
49struct cw_args {
50    struct mkuz_conveyor *cvp;
51    struct mkuz_cfg *cfp;
52};
53
54static void *
55cworker(void *p)
56{
57    struct cw_args *cwp;
58    struct mkuz_cfg *cfp;
59    struct mkuz_blk *oblk, *iblk;
60    struct mkuz_conveyor *cvp;
61    void *c_ctx;
62
63    cwp = (struct cw_args *)p;
64    cfp = cwp->cfp;
65    cvp = cwp->cvp;
66    free(cwp);
67    c_ctx = cfp->handler->f_init(&cfp->comp_level);
68    for (;;) {
69        iblk = mkuz_fqueue_deq(cvp->wrk_queue);
70        if (iblk == MKUZ_BLK_EOF) {
71            /* Let other threads to see the EOF block */
72            mkuz_fqueue_enq(cvp->wrk_queue, iblk);
73            break;
74        }
75        if (cfp->no_zcomp == 0 &&
76          mkuz_memvcmp(iblk->data, '\0', iblk->info.len) != 0) {
77            /* All zeroes block */
78            oblk = mkuz_blk_ctor(0);
79        } else {
80            oblk = mkuz_blk_ctor(cfp->cbound_blksz);
81            cfp->handler->f_compress(c_ctx, iblk, oblk);
82            if (cfp->en_dedup != 0) {
83                compute_digest(oblk);
84            }
85        }
86        oblk->info.blkno = iblk->info.blkno;
87        mkuz_fqueue_enq(cvp->results, oblk);
88        free(iblk);
89    }
90    return (NULL);
91}
92
93static void
94compute_digest(struct mkuz_blk *bp)
95{
96    MD5_CTX mcontext;
97
98    MD5Init(&mcontext);
99    MD5Update(&mcontext, bp->data, bp->info.len);
100    MD5Final(bp->info.digest, &mcontext);
101}
102
103struct mkuz_conveyor *
104mkuz_conveyor_ctor(struct mkuz_cfg *cfp)
105{
106    struct mkuz_conveyor *cp;
107    struct cw_args *cwp;
108    int i, r;
109
110    cp = mkuz_safe_zmalloc(sizeof(struct mkuz_conveyor) +
111      (sizeof(pthread_t) * cfp->nworkers));
112
113    cp->wrk_queue = mkuz_fqueue_ctor(1);
114    cp->results = mkuz_fqueue_ctor(1);
115
116    for (i = 0; i < cfp->nworkers; i++) {
117        cwp = mkuz_safe_zmalloc(sizeof(struct cw_args));
118        cwp->cfp = cfp;
119        cwp->cvp = cp;
120        r = pthread_create(&cp->wthreads[i], NULL, cworker, (void *)cwp);
121        if (r != 0) {
122            errx(1, "mkuz_conveyor_ctor: pthread_create() failed");
123            /* Not reached */
124        }
125    }
126    return (cp);
127}
128