varnish-cache/bin/varnishd/common/common_vsc.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2015 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <errno.h>
33
#include <stdarg.h>
34
#include <stdio.h>
35
#include <string.h>
36
#include <stdlib.h>
37
38
#include "vdef.h"
39
#include "vrt.h"
40
41
#include "miniobj.h"
42
#include "vas.h"
43
#include "vend.h"
44
#include "vgz.h"
45
#include "vmb.h"
46
#include "vsmw.h"
47
#include "vqueue.h"
48
#include "vapi/vsc_int.h"
49
50
#include "common/heritage.h"
51
52
/*--------------------------------------------------------------------*/
53
54
struct vsc_segs {
55
        unsigned                magic;
56
#define VSC_SEGS_MAGIC          0x9b355991
57
58
        const char              *nm;
59
        VTAILQ_ENTRY(vsc_segs)  list;
60
        void                    *seg;
61
        void                    *ptr;
62
};
63
64
static VTAILQ_HEAD(,vsc_segs)   vsc_seglist =
65
    VTAILQ_HEAD_INITIALIZER(vsc_seglist);
66
67
vsc_callback_f *vsc_lock;
68
vsc_callback_f *vsc_unlock;
69
70
void *
71 37246
VRT_VSC_Alloc(const char *nm, size_t sd, size_t sj, const unsigned char *zj,
72
    size_t szj, const char *fmt, va_list va)
73
{
74
        char *p;
75
        z_stream vz;
76
        struct vsc_segs *vsg;
77
        char buf[1024];
78
79 37246
        if (vsc_lock != NULL)
80 35998
                vsc_lock();
81
82 37246
        if (*fmt == '\0')
83 2476
                bprintf(buf, "%s", nm);
84
        else
85 34770
                bprintf(buf, "%s.%s", nm, fmt);
86
87 37246
        AN(heritage.proc_vsmw);
88 37246
        p = VSMW_Allocv(heritage.proc_vsmw, VSC_CLASS, 8 + sd + sj, buf, va);
89 37246
        AN(p);
90
91 37246
        memset(&vz, 0, sizeof vz);
92 37246
        assert(Z_OK == inflateInit2(&vz, 31));
93 37246
        vz.next_in = TRUST_ME(zj);
94 37246
        vz.avail_in = szj;
95 37246
        vz.next_out = (void*)(p + 8 + sd);
96 37246
        vz.avail_out = sj;
97 37246
        assert(Z_STREAM_END == inflate(&vz, Z_FINISH));
98 37246
        assert(Z_OK == inflateEnd(&vz));
99 37246
        ALLOC_OBJ(vsg, VSC_SEGS_MAGIC);
100 37246
        AN(vsg);
101 37246
        vsg->seg = p;
102 37246
        vsg->ptr = p + 8;
103 37246
        VTAILQ_INSERT_TAIL(&vsc_seglist, vsg, list);
104 37246
        VWMB();
105 37246
        vbe64enc(p, sd);
106 37246
        vsg->nm = nm;
107 37246
        if (vsc_unlock != NULL)
108 35998
                vsc_unlock();
109 37246
        return (p + 8);
110
}
111
112
void
113 66
VRT_VSC_Destroy(const char *nm, const void *p)
114
{
115
        struct vsc_segs *vsg;
116
117 66
        if (vsc_lock != NULL)
118 66
                vsc_lock();
119
120 66
        AN(heritage.proc_vsmw);
121 1950
        VTAILQ_FOREACH(vsg, &vsc_seglist, list)
122 1950
                if (vsg->ptr == p)
123 66
                        break;
124 66
        AN(vsg);
125 66
        assert(vsg->nm == nm);
126 66
        VSMW_Free(heritage.proc_vsmw, &vsg->seg);
127 66
        VTAILQ_REMOVE(&vsc_seglist, vsg, list);
128 66
        FREE_OBJ(vsg);
129 66
        if (vsc_unlock != NULL)
130 66
                vsc_unlock();
131 66
}