Skip to content

Commit

Permalink
Move VSC_{Alloc|Destroy}() into common
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdphk committed May 28, 2017
1 parent b82c6d2 commit 59e5695
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 68 deletions.
1 change: 1 addition & 0 deletions bin/varnishd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ varnishd_SOURCES = \
cache/cache_vrt_vmod.c \
cache/cache_wrk.c \
cache/cache_ws.c \
common/common_vsc.c \
common/common_vsm.c \
hash/hash_classic.c \
hash/hash_critbit.c \
Expand Down
68 changes: 0 additions & 68 deletions bin/varnishd/cache/cache_shmlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include "vend.h"
#include "vgz.h"
#include "vsl_priv.h"
#include "vapi/vsc_int.h"
#include "vmb.h"
#include "vtim.h"

Expand Down Expand Up @@ -474,73 +473,6 @@ VSL_End(struct vsl_log *vsl)

/*--------------------------------------------------------------------*/

struct vsc_segs {
unsigned magic;
#define VSC_SEGS_MAGIC 0x9b355991

VTAILQ_ENTRY(vsc_segs) list;
void *seg;
void *ptr;
};

static VTAILQ_HEAD(,vsc_segs) vsc_seglist =
VTAILQ_HEAD_INITIALIZER(vsc_seglist);

void *
VSC_Alloc(const char *nm, size_t sd,
size_t sj, const unsigned char *zj, size_t szj,
const char *fmt, va_list va)
{
char *p;
z_stream vz;
struct vsc_segs *vsg;

(void)nm;
(void)fmt;
(void)va;


p = VSM_Alloc(8 + sd + sj, VSC_CLASS, nm, fmt);
AN(p);

memset(p, 0, sd);
vbe64enc(p, sd);

memset(&vz, 0, sizeof vz);
assert(Z_OK == inflateInit2(&vz, 31));
vz.next_in = TRUST_ME(zj);
vz.avail_in = szj;
vz.next_out = (void*)(p + 8 + sd);
vz.avail_out = sj;
assert(Z_STREAM_END == inflate(&vz, Z_FINISH));
assert(Z_OK == inflateEnd(&vz));
ALLOC_OBJ(vsg, VSC_SEGS_MAGIC);
AN(vsg);
vsg->seg = p;
vsg->ptr = p + 8;
VTAILQ_INSERT_TAIL(&vsc_seglist, vsg, list);
return (p + 8);
}

void
VSC_Destroy(const char *nm, const void *p)
{
struct vsc_segs *vsg;

(void)nm;
VTAILQ_FOREACH(vsg, &vsc_seglist, list) {
if (vsg->ptr != p)
continue;
VSM_Free(vsg->seg);
VTAILQ_REMOVE(&vsc_seglist, vsg, list);
FREE_OBJ(vsg);
return;
}
WRONG("Freeing unknown VSC");
}

/*--------------------------------------------------------------------*/

static void *
vsm_cleaner(void *priv)
{
Expand Down
115 changes: 115 additions & 0 deletions bin/varnishd/common/common_vsc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*-
* Copyright (c) 2006 Verdens Gang AS
* Copyright (c) 2006-2015 Varnish Software AS
* All rights reserved.
*
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include "config.h"

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "common.h"
#include "common/common_vsm.h"

#include "vend.h"
#include "vgz.h"
#include "vapi/vsc_int.h"

/*--------------------------------------------------------------------*/

void *VSM_Alloc(unsigned size, const char *class, const char *type,
const char *ident);
void VSM_Free(void *ptr);

/*--------------------------------------------------------------------*/

struct vsc_segs {
unsigned magic;
#define VSC_SEGS_MAGIC 0x9b355991

VTAILQ_ENTRY(vsc_segs) list;
void *seg;
void *ptr;
};

static VTAILQ_HEAD(,vsc_segs) vsc_seglist =
VTAILQ_HEAD_INITIALIZER(vsc_seglist);

void *
VSC_Alloc(const char *nm, size_t sd,
size_t sj, const unsigned char *zj, size_t szj,
const char *fmt, va_list va)
{
char *p;
z_stream vz;
struct vsc_segs *vsg;

(void)nm;
(void)fmt;
(void)va;


p = VSM_Alloc(8 + sd + sj, VSC_CLASS, nm, fmt);
AN(p);

memset(p, 0, sd);
vbe64enc(p, sd);

memset(&vz, 0, sizeof vz);
assert(Z_OK == inflateInit2(&vz, 31));
vz.next_in = TRUST_ME(zj);
vz.avail_in = szj;
vz.next_out = (void*)(p + 8 + sd);
vz.avail_out = sj;
assert(Z_STREAM_END == inflate(&vz, Z_FINISH));
assert(Z_OK == inflateEnd(&vz));
ALLOC_OBJ(vsg, VSC_SEGS_MAGIC);
AN(vsg);
vsg->seg = p;
vsg->ptr = p + 8;
VTAILQ_INSERT_TAIL(&vsc_seglist, vsg, list);
return (p + 8);
}

void
VSC_Destroy(const char *nm, const void *p)
{
struct vsc_segs *vsg;

(void)nm;
VTAILQ_FOREACH(vsg, &vsc_seglist, list) {
if (vsg->ptr != p)
continue;
VSM_Free(vsg->seg);
VTAILQ_REMOVE(&vsc_seglist, vsg, list);
FREE_OBJ(vsg);
return;
}
WRONG("Freeing unknown VSC");
}

0 comments on commit 59e5695

Please sign in to comment.