Commit 9703c6cd authored by Dridi Boukelmoune's avatar Dridi Boukelmoune

ws: Move common code to a new cache_ws_common.c

parent 9a0a613c
...@@ -53,6 +53,7 @@ varnishd_SOURCES = \ ...@@ -53,6 +53,7 @@ varnishd_SOURCES = \
cache/cache_vrt_vcl.c \ cache/cache_vrt_vcl.c \
cache/cache_vrt_vmod.c \ cache/cache_vrt_vmod.c \
cache/cache_wrk.c \ cache/cache_wrk.c \
cache/cache_ws_common.c \
common/common_vsc.c \ common/common_vsc.c \
common/common_vsmw.c \ common/common_vsmw.c \
hash/hash_classic.c \ hash/hash_classic.c \
......
...@@ -780,13 +780,10 @@ void WRK_BgThread(pthread_t *thr, const char *name, bgthread_t *func, ...@@ -780,13 +780,10 @@ void WRK_BgThread(pthread_t *thr, const char *name, bgthread_t *func,
void *priv); void *priv);
/* cache_ws.c */ /* cache_ws.c */
void WS_Init(struct ws *ws, const char *id, void *space, unsigned len); void WS_Init(struct ws *ws, const char *id, void *space, unsigned len);
unsigned WS_ReserveSize(struct ws *, unsigned); unsigned WS_ReserveSize(struct ws *, unsigned);
unsigned WS_ReserveAll(struct ws *); unsigned WS_ReserveAll(struct ws *);
unsigned WS_ReserveLumps(struct ws *ws, size_t sz);
void WS_MarkOverflow(struct ws *ws);
void WS_Release(struct ws *ws, unsigned bytes); void WS_Release(struct ws *ws, unsigned bytes);
void WS_ReleaseP(struct ws *ws, const char *ptr); void WS_ReleaseP(struct ws *ws, const char *ptr);
void WS_Assert(const struct ws *ws); void WS_Assert(const struct ws *ws);
...@@ -794,14 +791,9 @@ void WS_Reset(struct ws *ws, uintptr_t); ...@@ -794,14 +791,9 @@ void WS_Reset(struct ws *ws, uintptr_t);
void *WS_Alloc(struct ws *ws, unsigned bytes); void *WS_Alloc(struct ws *ws, unsigned bytes);
void *WS_Copy(struct ws *ws, const void *str, int len); void *WS_Copy(struct ws *ws, const void *str, int len);
uintptr_t WS_Snapshot(struct ws *ws); uintptr_t WS_Snapshot(struct ws *ws);
int WS_Overflowed(const struct ws *ws);
const char *WS_Printf(struct ws *ws, const char *fmt, ...) v_printflike_(2, 3);
int WS_Allocated(const struct ws *ws, const void *ptr, ssize_t len); int WS_Allocated(const struct ws *ws, const void *ptr, ssize_t len);
unsigned WS_Dump(const struct ws *ws, char, size_t off, void *buf, size_t len); unsigned WS_Dump(const struct ws *ws, char, size_t off, void *buf, size_t len);
void WS_VSB_new(struct vsb *, struct ws *);
char *WS_VSB_finish(struct vsb *, struct ws *, size_t *);
static inline void * static inline void *
WS_Reservation(const struct ws *ws) WS_Reservation(const struct ws *ws)
{ {
...@@ -820,6 +812,23 @@ WS_ReservationSize(const struct ws *ws) ...@@ -820,6 +812,23 @@ WS_ReservationSize(const struct ws *ws)
return (ws->r - ws->f); return (ws->r - ws->f);
} }
static inline unsigned
WS_ReserveLumps(struct ws *ws, size_t sz)
{
AN(sz);
return (WS_ReserveAll(ws) / sz);
}
/* cache_ws_common.c */
void WS_MarkOverflow(struct ws *ws);
int WS_Overflowed(const struct ws *ws);
const char *WS_Printf(struct ws *ws, const char *fmt, ...) v_printflike_(2, 3);
void WS_VSB_new(struct vsb *, struct ws *);
char *WS_VSB_finish(struct vsb *, struct ws *, size_t *);
/* cache_rfc2616.c */ /* cache_rfc2616.c */
void RFC2616_Ttl(struct busyobj *, vtim_real now, vtim_real *t_origin, void RFC2616_Ttl(struct busyobj *, vtim_real now, vtim_real *t_origin,
float *ttl, float *grace, float *keep); float *ttl, float *grace, float *keep);
......
...@@ -527,7 +527,6 @@ void WRK_Init(void); ...@@ -527,7 +527,6 @@ void WRK_Init(void);
void WRK_AddStat(const struct worker *); void WRK_AddStat(const struct worker *);
/* cache_ws.c */ /* cache_ws.c */
void WS_Id(const struct ws *ws, char *id);
void WS_Panic(struct vsb *, const struct ws *); void WS_Panic(struct vsb *, const struct ws *);
static inline int static inline int
WS_IsReserved(const struct ws *ws) WS_IsReserved(const struct ws *ws)
...@@ -536,11 +535,14 @@ WS_IsReserved(const struct ws *ws) ...@@ -536,11 +535,14 @@ WS_IsReserved(const struct ws *ws)
return (ws->r != NULL); return (ws->r != NULL);
} }
void WS_Rollback(struct ws *, uintptr_t);
void *WS_AtOffset(const struct ws *ws, unsigned off, unsigned len); void *WS_AtOffset(const struct ws *ws, unsigned off, unsigned len);
unsigned WS_ReservationOffset(const struct ws *ws); unsigned WS_ReservationOffset(const struct ws *ws);
unsigned WS_ReqPipeline(struct ws *, const void *b, const void *e); unsigned WS_ReqPipeline(struct ws *, const void *b, const void *e);
/* cache_ws_common.c */
void WS_Id(const struct ws *ws, char *id);
void WS_Rollback(struct ws *, uintptr_t);
/* http1/cache_http1_pipe.c */ /* http1/cache_http1_pipe.c */
void V1P_Init(void); void V1P_Init(void);
......
/*- /*-
* Copyright (c) 2006 Verdens Gang AS * Copyright (c) 2006 Verdens Gang AS
* Copyright (c) 2006-2011 Varnish Software AS * Copyright (c) 2006-2021 Varnish Software AS
* All rights reserved. * All rights reserved.
* *
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
* Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
* *
...@@ -34,8 +35,6 @@ ...@@ -34,8 +35,6 @@
#include "cache_varnishd.h" #include "cache_varnishd.h"
#include <stdio.h>
#define WS_REDZONE_END '\x15' #define WS_REDZONE_END '\x15'
static const uintptr_t snap_overflowed = (uintptr_t)&snap_overflowed; static const uintptr_t snap_overflowed = (uintptr_t)&snap_overflowed;
...@@ -100,32 +99,6 @@ WS_Init(struct ws *ws, const char *id, void *space, unsigned len) ...@@ -100,32 +99,6 @@ WS_Init(struct ws *ws, const char *id, void *space, unsigned len)
WS_Assert(ws); WS_Assert(ws);
} }
void
WS_Id(const struct ws *ws, char *id)
{
WS_Assert(ws);
AN(id);
memcpy(id, ws->id, WS_ID_SIZE);
id[0] |= 0x20; // cheesy tolower()
}
void
WS_MarkOverflow(struct ws *ws)
{
CHECK_OBJ_NOTNULL(ws, WS_MAGIC);
ws->id[0] &= ~0x20; // cheesy toupper()
}
static void
ws_ClearOverflow(struct ws *ws)
{
CHECK_OBJ_NOTNULL(ws, WS_MAGIC);
ws->id[0] |= 0x20; // cheesy tolower()
}
/* /*
* Reset a WS to a cookie from WS_Snapshot * Reset a WS to a cookie from WS_Snapshot
* *
...@@ -156,24 +129,6 @@ WS_Reset(struct ws *ws, uintptr_t pp) ...@@ -156,24 +129,6 @@ WS_Reset(struct ws *ws, uintptr_t pp)
WS_Assert(ws); WS_Assert(ws);
} }
/*
* Reset the WS to a cookie or its start and clears any overflow
*
* for varnishd internal use only
*/
void
WS_Rollback(struct ws *ws, uintptr_t pp)
{
WS_Assert(ws);
if (pp == 0)
pp = (uintptr_t)ws->s;
ws_ClearOverflow(ws);
WS_Reset(ws, pp);
}
/* /*
* Make a reservation and optionally pipeline a memory region that may or * Make a reservation and optionally pipeline a memory region that may or
* may not originate from the same workspace. * may not originate from the same workspace.
...@@ -252,28 +207,6 @@ WS_Copy(struct ws *ws, const void *str, int len) ...@@ -252,28 +207,6 @@ WS_Copy(struct ws *ws, const void *str, int len)
return (r); return (r);
} }
const char *
WS_Printf(struct ws *ws, const char *fmt, ...)
{
unsigned u, v;
va_list ap;
char *p;
u = WS_ReserveAll(ws);
p = ws->f;
va_start(ap, fmt);
v = vsnprintf(p, u, fmt, ap);
va_end(ap);
if (v >= u) {
WS_Release(ws, 0);
WS_MarkOverflow(ws);
p = NULL;
} else {
WS_Release(ws, v + 1);
}
return (p);
}
uintptr_t uintptr_t
WS_Snapshot(struct ws *ws) WS_Snapshot(struct ws *ws)
{ {
...@@ -332,12 +265,6 @@ WS_ReserveSize(struct ws *ws, unsigned bytes) ...@@ -332,12 +265,6 @@ WS_ReserveSize(struct ws *ws, unsigned bytes)
return (bytes); return (bytes);
} }
unsigned
WS_ReserveLumps(struct ws *ws, size_t sz)
{
return (WS_ReserveAll(ws) / sz);
}
void void
WS_Release(struct ws *ws, unsigned bytes) WS_Release(struct ws *ws, unsigned bytes)
{ {
...@@ -364,17 +291,6 @@ WS_ReleaseP(struct ws *ws, const char *ptr) ...@@ -364,17 +291,6 @@ WS_ReleaseP(struct ws *ws, const char *ptr)
WS_Assert(ws); WS_Assert(ws);
} }
int
WS_Overflowed(const struct ws *ws)
{
CHECK_OBJ_NOTNULL(ws, WS_MAGIC);
AN(ws->id[0]);
if (ws->id[0] & 0x20) // cheesy islower()
return (0);
return (1);
}
void * void *
WS_AtOffset(const struct ws *ws, unsigned off, unsigned len) WS_AtOffset(const struct ws *ws, unsigned off, unsigned len)
{ {
...@@ -394,60 +310,6 @@ WS_ReservationOffset(const struct ws *ws) ...@@ -394,60 +310,6 @@ WS_ReservationOffset(const struct ws *ws)
return (ws->f - ws->s); return (ws->f - ws->s);
} }
/*---------------------------------------------------------------------
* Build a VSB on a workspace.
* Usage pattern:
*
* struct vsb vsb[1];
* char *p;
*
* WS_VSB_new(vsb, ctx->ws);
* VSB_printf(vsb, "blablabla");
* p = WS_VSB_finish(vsb, ctx->ws, NULL);
* if (p == NULL)
* return (FAILURE);
*/
void
WS_VSB_new(struct vsb *vsb, struct ws *ws)
{
unsigned u;
static char bogus[2]; // Smallest possible vsb
AN(vsb);
WS_Assert(ws);
u = WS_ReserveAll(ws);
if (WS_Overflowed(ws) || u < 2)
AN(VSB_init(vsb, bogus, sizeof bogus));
else
AN(VSB_init(vsb, WS_Reservation(ws), u));
}
char *
WS_VSB_finish(struct vsb *vsb, struct ws *ws, size_t *szp)
{
char *p;
AN(vsb);
WS_Assert(ws);
if (!VSB_finish(vsb)) {
p = VSB_data(vsb);
if (p == ws->f) {
WS_Release(ws, VSB_len(vsb) + 1);
if (szp != NULL)
*szp = VSB_len(vsb);
VSB_fini(vsb);
return (p);
}
}
WS_MarkOverflow(ws);
VSB_fini(vsb);
WS_Release(ws, 0);
if (szp)
*szp = 0;
return (NULL);
}
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
unsigned unsigned
......
/*-
* Copyright (c) 2006 Verdens Gang AS
* Copyright (c) 2006-2021 Varnish Software AS
* All rights reserved.
*
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
* Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* 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 <stdio.h>
#include "cache_varnishd.h"
void
WS_Id(const struct ws *ws, char *id)
{
WS_Assert(ws);
AN(id);
memcpy(id, ws->id, WS_ID_SIZE);
id[0] |= 0x20; // cheesy tolower()
}
void
WS_MarkOverflow(struct ws *ws)
{
CHECK_OBJ_NOTNULL(ws, WS_MAGIC);
ws->id[0] &= ~0x20; // cheesy toupper()
}
static void
ws_ClearOverflow(struct ws *ws)
{
CHECK_OBJ_NOTNULL(ws, WS_MAGIC);
ws->id[0] |= 0x20; // cheesy tolower()
}
int
WS_Overflowed(const struct ws *ws)
{
CHECK_OBJ_NOTNULL(ws, WS_MAGIC);
AN(ws->id[0]);
if (ws->id[0] & 0x20) // cheesy islower()
return (0);
return (1);
}
/*
* Reset the WS to a cookie or its start and clears any overflow
*
* for varnishd internal use only
*/
void
WS_Rollback(struct ws *ws, uintptr_t pp)
{
WS_Assert(ws);
if (pp == 0)
pp = (uintptr_t)ws->s;
ws_ClearOverflow(ws);
WS_Reset(ws, pp);
}
/*--------------------------------------------------------------------*/
const char *
WS_Printf(struct ws *ws, const char *fmt, ...)
{
unsigned u, v;
va_list ap;
char *p;
u = WS_ReserveAll(ws);
p = ws->f;
va_start(ap, fmt);
v = vsnprintf(p, u, fmt, ap);
va_end(ap);
if (v >= u) {
WS_Release(ws, 0);
WS_MarkOverflow(ws);
p = NULL;
} else {
WS_Release(ws, v + 1);
}
return (p);
}
/*---------------------------------------------------------------------
* Build a VSB on a workspace.
* Usage pattern:
*
* struct vsb vsb[1];
* char *p;
*
* WS_VSB_new(vsb, ctx->ws);
* VSB_printf(vsb, "blablabla");
* p = WS_VSB_finish(vsb, ctx->ws, NULL);
* if (p == NULL)
* return (FAILURE);
*/
void
WS_VSB_new(struct vsb *vsb, struct ws *ws)
{
unsigned u;
static char bogus[2]; // Smallest possible vsb
AN(vsb);
WS_Assert(ws);
u = WS_ReserveAll(ws);
if (WS_Overflowed(ws) || u < 2)
AN(VSB_init(vsb, bogus, sizeof bogus));
else
AN(VSB_init(vsb, WS_Reservation(ws), u));
}
char *
WS_VSB_finish(struct vsb *vsb, struct ws *ws, size_t *szp)
{
char *p;
AN(vsb);
WS_Assert(ws);
if (!VSB_finish(vsb)) {
p = VSB_data(vsb);
if (p == ws->f) {
WS_Release(ws, VSB_len(vsb) + 1);
if (szp != NULL)
*szp = VSB_len(vsb);
VSB_fini(vsb);
return (p);
}
}
WS_MarkOverflow(ws);
VSB_fini(vsb);
WS_Release(ws, 0);
if (szp)
*szp = 0;
return (NULL);
}
...@@ -39,7 +39,6 @@ ...@@ -39,7 +39,6 @@
#include "cache_varnishd.h" #include "cache_varnishd.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
struct ws_alloc { struct ws_alloc {
...@@ -182,32 +181,6 @@ WS_Init(struct ws *ws, const char *id, void *space, unsigned len) ...@@ -182,32 +181,6 @@ WS_Init(struct ws *ws, const char *id, void *space, unsigned len)
WS_Assert(ws); WS_Assert(ws);
} }
void
WS_Id(const struct ws *ws, char *id)
{
WS_Assert(ws);
AN(id);
memcpy(id, ws->id, WS_ID_SIZE);
id[0] |= 0x20; // cheesy tolower()
}
void
WS_MarkOverflow(struct ws *ws)
{
CHECK_OBJ_NOTNULL(ws, WS_MAGIC);
ws->id[0] &= ~0x20; // cheesy toupper()
}
static void
ws_ClearOverflow(struct ws *ws)
{
CHECK_OBJ_NOTNULL(ws, WS_MAGIC);
ws->id[0] |= 0x20; // cheesy tolower()
}
static void static void
ws_alloc_free(struct ws_emu *we, struct ws_alloc **wap) ws_alloc_free(struct ws_emu *we, struct ws_alloc **wap)
{ {
...@@ -248,18 +221,6 @@ WS_Reset(struct ws *ws, uintptr_t pp) ...@@ -248,18 +221,6 @@ WS_Reset(struct ws *ws, uintptr_t pp)
WS_Assert(ws); WS_Assert(ws);
} }
void
WS_Rollback(struct ws *ws, uintptr_t pp)
{
WS_Assert(ws);
if (pp == 0)
pp = (uintptr_t)ws->s;
ws_ClearOverflow(ws);
WS_Reset(ws, pp);
}
unsigned unsigned
WS_ReqPipeline(struct ws *ws, const void *b, const void *e) WS_ReqPipeline(struct ws *ws, const void *b, const void *e)
{ {
...@@ -375,28 +336,6 @@ WS_Copy(struct ws *ws, const void *str, int len) ...@@ -375,28 +336,6 @@ WS_Copy(struct ws *ws, const void *str, int len)
return (NULL); return (NULL);
} }
const char *
WS_Printf(struct ws *ws, const char *fmt, ...)
{
unsigned u, v;
va_list ap;
char *p;
u = WS_ReserveAll(ws);
p = ws->f;
va_start(ap, fmt);
v = vsnprintf(p, u, fmt, ap);
va_end(ap);
if (v >= u) {
WS_Release(ws, 0);
WS_MarkOverflow(ws);
p = NULL;
} else {
WS_Release(ws, v + 1);
}
return (p);
}
uintptr_t uintptr_t
WS_Snapshot(struct ws *ws) WS_Snapshot(struct ws *ws)
{ {
...@@ -464,12 +403,6 @@ WS_ReserveSize(struct ws *ws, unsigned bytes) ...@@ -464,12 +403,6 @@ WS_ReserveSize(struct ws *ws, unsigned bytes)
return (bytes); return (bytes);
} }
unsigned
WS_ReserveLumps(struct ws *ws, size_t sz)
{
return (WS_ReserveAll(ws) / sz);
}
static void static void
ws_release(struct ws *ws, unsigned bytes) ws_release(struct ws *ws, unsigned bytes)
{ {
...@@ -520,17 +453,6 @@ WS_ReleaseP(struct ws *ws, const char *ptr) ...@@ -520,17 +453,6 @@ WS_ReleaseP(struct ws *ws, const char *ptr)
DSL(DBG_WORKSPACE, 0, "WS_ReleaseP(%p, %p (%u))", ws, ptr, l); DSL(DBG_WORKSPACE, 0, "WS_ReleaseP(%p, %p (%u))", ws, ptr, l);
} }
int
WS_Overflowed(const struct ws *ws)
{
CHECK_OBJ_NOTNULL(ws, WS_MAGIC);
AN(ws->id[0]);
if (ws->id[0] & 0x20) // cheesy islower()
return (0);
return (1);
}
void * void *
WS_AtOffset(const struct ws *ws, unsigned off, unsigned len) WS_AtOffset(const struct ws *ws, unsigned off, unsigned len)
{ {
...@@ -566,46 +488,6 @@ WS_ReservationOffset(const struct ws *ws) ...@@ -566,46 +488,6 @@ WS_ReservationOffset(const struct ws *ws)
return (wa->off); return (wa->off);
} }
void
WS_VSB_new(struct vsb *vsb, struct ws *ws)
{
unsigned u;
static char bogus[2]; // Smallest possible vsb
AN(vsb);
WS_Assert(ws);
u = WS_ReserveAll(ws);
if (WS_Overflowed(ws) || u < 2)
AN(VSB_init(vsb, bogus, sizeof bogus));
else
AN(VSB_init(vsb, WS_Reservation(ws), u));
}
char *
WS_VSB_finish(struct vsb *vsb, struct ws *ws, size_t *szp)
{
char *p;
AN(vsb);
WS_Assert(ws);
if (!VSB_finish(vsb)) {
p = VSB_data(vsb);
if (p == ws->f) {
WS_Release(ws, VSB_len(vsb) + 1);
if (szp != NULL)
*szp = VSB_len(vsb);
VSB_fini(vsb);
return (p);
}
}
WS_MarkOverflow(ws);
VSB_fini(vsb);
WS_Release(ws, 0);
if (szp)
*szp = 0;
return (NULL);
}
unsigned unsigned
WS_Dump(const struct ws *ws, char where, size_t off, void *buf, size_t len) WS_Dump(const struct ws *ws, char where, size_t off, void *buf, size_t len)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment