Commit 61ee9da8 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Move VSM allocation to common code



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@4959 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 6c54c13d
......@@ -73,6 +73,9 @@ const void *pick(const struct choice *cp, const char *which, const char *kind);
extern struct vsm_head *vsm_head;
extern void *vsm_end;
void *VSM_Alloc(unsigned size, const char *class, const char *type,
const char *ident);
struct vsm_chunk *vsm_iter_0(void);
void vsm_iter_n(struct vsm_chunk **pp);
......
......@@ -511,7 +511,7 @@ mgt_cli_secret(const char *S_arg)
/* Save in shmem */
i = strlen(S_arg);
p = mgt_SHM_Alloc(i + 1, "Arg", "-S", "");
p = VSM_Alloc(i + 1, "Arg", "-S", "");
AN(p);
strcpy(p, S_arg);
......@@ -546,7 +546,7 @@ mgt_cli_telnet(const char *T_arg)
/* Save in shmem */
i = strlen(T_arg);
p = mgt_SHM_Alloc(i + 1, "Arg", "-T", "");
p = VSM_Alloc(i + 1, "Arg", "-T", "");
AN(p);
strcpy(p, T_arg);
......
......@@ -118,58 +118,6 @@ struct vsm_head *loghead;
static int vsl_fd = -1;
/*--------------------------------------------------------------------*/
void *
mgt_SHM_Alloc(unsigned size, const char *class, const char *type, const char *ident)
{
struct vsm_chunk *sha, *sha2;
unsigned seq;
ASSERT_MGT();
AN(loghead);
/* Round up to pointersize */
size += sizeof(sha) - 1;
size &= ~(sizeof(sha) - 1);
size += sizeof *sha;
sha = &loghead->head;
while (1) {
CHECK_OBJ_NOTNULL(sha, VSM_CHUNK_MAGIC);
if (strcmp(sha->class, "Free")) {
sha = VSM_NEXT(sha);
continue;
}
assert(size <= sha->len);
sha2 = (void*)((uintptr_t)sha + size);
seq = loghead->alloc_seq;
loghead->alloc_seq = 0;
VWMB();
memset(sha2, 0, sizeof *sha2);
sha2->magic = VSM_CHUNK_MAGIC;
sha2->len = sha->len - size;
bprintf(sha2->class, "%s", "Free");
sha->len = size;
bprintf(sha->class, "%s", class);
bprintf(sha->type, "%s", type);
bprintf(sha->ident, "%s", ident);
VWMB();
if (seq != 0)
do
loghead->alloc_seq = seq++;
while (loghead->alloc_seq == 0);
return (VSM_PTR(sha));
}
return (NULL);
}
/*--------------------------------------------------------------------
* Check that we are not started with the same -n argument as an already
* running varnishd
......@@ -342,16 +290,16 @@ mgt_SHM_Init(const char *l_arg)
vsm_head = loghead;
vsm_end = (uint8_t*)loghead + size;
VSL_stats = mgt_SHM_Alloc(sizeof *VSL_stats,
VSL_stats = VSM_Alloc(sizeof *VSL_stats,
VSC_CLASS, VSC_TYPE_MAIN, "");
AN(VSL_stats);
pp = mgt_SHM_Alloc(sizeof *pp, "Params", "", "");
pp = VSM_Alloc(sizeof *pp, "Params", "", "");
AN(pp);
*pp = *params;
params = pp;
vsl_log_start = mgt_SHM_Alloc(s1, VSL_CLASS, "", "");
vsl_log_start = VSM_Alloc(s1, VSL_CLASS, "", "");
AN(vsl_log_start);
vsl_log_start[1] = VSL_ENDMARKER;
VWMB();
......
......@@ -187,7 +187,7 @@ sma_ready(struct stevedore *st)
struct sma_sc *sma_sc;
CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC);
sma_sc->stats = mgt_SHM_Alloc(sizeof *sma_sc->stats,
sma_sc->stats = VSM_Alloc(sizeof *sma_sc->stats,
VSC_CLASS, VSC_TYPE_SMA, st->ident);
memset(sma_sc->stats, 0, sizeof *sma_sc->stats);
}
......
......@@ -35,11 +35,14 @@
SVNID("$Id$")
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include "miniobj.h"
#include "libvarnish.h"
#include "common.h"
#include "vsm.h"
#include "vmb.h"
struct vsm_head *vsm_head;
void *vsm_end;
......@@ -68,3 +71,55 @@ vsm_iter_n(struct vsm_chunk **pp)
}
CHECK_OBJ_NOTNULL(*pp, VSM_CHUNK_MAGIC);
}
/*--------------------------------------------------------------------*/
void *
VSM_Alloc(unsigned size, const char *class, const char *type, const char *ident)
{
struct vsm_chunk *sha, *sha2;
unsigned seq;
CHECK_OBJ_NOTNULL(vsm_head, VSM_HEAD_MAGIC);
/* Round up to pointersize */
size += sizeof(void *) - 1;
size &= ~(sizeof(void *) - 1);
size += sizeof *sha; /* Make space for the header */
VSM_ITER(sha) {
CHECK_OBJ_NOTNULL(sha, VSM_CHUNK_MAGIC);
if (strcmp(sha->class, "Free"))
continue;
xxxassert(size <= sha->len);
sha2 = (void*)((uintptr_t)sha + size);
/* Mark as inconsistent while we write string fields */
seq = vsm_head->alloc_seq;
vsm_head->alloc_seq = 0;
VWMB();
memset(sha2, 0, sizeof *sha2);
sha2->magic = VSM_CHUNK_MAGIC;
sha2->len = sha->len - size;
bprintf(sha2->class, "%s", "Free");
sha->len = size;
bprintf(sha->class, "%s", class);
bprintf(sha->type, "%s", type);
bprintf(sha->ident, "%s", ident);
VWMB();
if (seq != 0)
do
loghead->alloc_seq = seq++;
while (loghead->alloc_seq == 0);
return (VSM_PTR(sha));
}
return (NULL);
}
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