Commit e8e82456 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Give the malloc storage a proper "sc" structure and mini-obj'ify the

sma structure.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@4811 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 0f781bc7
...@@ -38,47 +38,65 @@ SVNID("$Id$") ...@@ -38,47 +38,65 @@ SVNID("$Id$")
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h>
#include "shmlog.h" #include "shmlog.h"
#include "cache.h" #include "cache.h"
#include "stevedore.h" #include "stevedore.h"
static size_t sma_max = SIZE_MAX; struct sma_sc {
static struct lock sma_mtx; unsigned magic;
#define SMA_SC_MAGIC 0x1ac8a345
struct lock sma_mtx;
size_t sma_max;
};
struct sma { struct sma {
unsigned magic;
#define SMA_MAGIC 0x69ae9bb9
struct storage s; struct storage s;
size_t sz; size_t sz;
struct sma_sc *sc;
}; };
static struct storage * static struct storage *
sma_alloc(struct stevedore *st, size_t size, struct objcore *oc) sma_alloc(struct stevedore *st, size_t size, struct objcore *oc)
{ {
struct sma_sc *sma_sc;
struct sma *sma; struct sma *sma;
CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC);
(void)oc; (void)oc;
Lck_Lock(&sma_mtx); Lck_Lock(&sma_sc->sma_mtx);
VSL_stats->sma_nreq++; VSL_stats->sma_nreq++;
if (VSL_stats->sma_nbytes + size > sma_max) if (VSL_stats->sma_nbytes + size > sma_sc->sma_max)
size = 0; size = 0;
else { else {
VSL_stats->sma_nobj++; VSL_stats->sma_nobj++;
VSL_stats->sma_nbytes += size; VSL_stats->sma_nbytes += size;
VSL_stats->sma_balloc += size; VSL_stats->sma_balloc += size;
} }
Lck_Unlock(&sma_mtx); Lck_Unlock(&sma_sc->sma_mtx);
if (size == 0) if (size == 0)
return (NULL); return (NULL);
sma = calloc(sizeof *sma, 1); /*
* Do not collaps the sma allocation with sma->s.ptr: it is not
* a good idea. Not only would it make ->trim impossible,
* performance-wise it would be a catastropy with chunksized
* allocations growing another full page, just to accomodate the sma.
*/
ALLOC_OBJ(sma, SMA_MAGIC);
if (sma == NULL) if (sma == NULL)
return (NULL); return (NULL); /* XXX: stats suffer */
sma->sc = sma_sc;
sma->sz = size; sma->sz = size;
sma->s.priv = sma; sma->s.priv = sma;
sma->s.ptr = malloc(size); sma->s.ptr = malloc(size);
XXXAN(sma->s.ptr); if (sma->s.ptr == NULL) {
free(sma);
return (NULL); /* XXX: stats suffer */
}
sma->s.len = 0; sma->s.len = 0;
sma->s.space = size; sma->s.space = size;
sma->s.fd = -1; sma->s.fd = -1;
...@@ -91,16 +109,18 @@ sma_alloc(struct stevedore *st, size_t size, struct objcore *oc) ...@@ -91,16 +109,18 @@ sma_alloc(struct stevedore *st, size_t size, struct objcore *oc)
static void static void
sma_free(struct storage *s) sma_free(struct storage *s)
{ {
struct sma_sc *sma_sc;
struct sma *sma; struct sma *sma;
CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC); CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC);
sma = s->priv; CAST_OBJ_NOTNULL(sma, s->priv, SMA_MAGIC);
sma_sc = sma->sc;
assert(sma->sz == sma->s.space); assert(sma->sz == sma->s.space);
Lck_Lock(&sma_mtx); Lck_Lock(&sma_sc->sma_mtx);
VSL_stats->sma_nobj--; VSL_stats->sma_nobj--;
VSL_stats->sma_nbytes -= sma->sz; VSL_stats->sma_nbytes -= sma->sz;
VSL_stats->sma_bfree += sma->sz; VSL_stats->sma_bfree += sma->sz;
Lck_Unlock(&sma_mtx); Lck_Unlock(&sma_sc->sma_mtx);
free(sma->s.ptr); free(sma->s.ptr);
free(sma); free(sma);
} }
...@@ -108,18 +128,22 @@ sma_free(struct storage *s) ...@@ -108,18 +128,22 @@ sma_free(struct storage *s)
static void static void
sma_trim(struct storage *s, size_t size) sma_trim(struct storage *s, size_t size)
{ {
struct sma_sc *sma_sc;
struct sma *sma; struct sma *sma;
void *p; void *p;
CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC); CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC);
sma = s->priv; CAST_OBJ_NOTNULL(sma, s->priv, SMA_MAGIC);
sma_sc = sma->sc;
assert(sma->sz == sma->s.space); assert(sma->sz == sma->s.space);
assert(size < sma->sz);
if ((p = realloc(sma->s.ptr, size)) != NULL) { if ((p = realloc(sma->s.ptr, size)) != NULL) {
Lck_Lock(&sma_mtx); Lck_Lock(&sma_sc->sma_mtx);
VSL_stats->sma_nbytes -= (sma->sz - size); VSL_stats->sma_nbytes -= (sma->sz - size);
VSL_stats->sma_bfree += sma->sz - size; VSL_stats->sma_bfree += sma->sz - size;
sma->sz = size; sma->sz = size;
Lck_Unlock(&sma_mtx); Lck_Unlock(&sma_sc->sma_mtx);
sma->s.ptr = p; sma->s.ptr = p;
s->space = size; s->space = size;
} }
...@@ -130,8 +154,12 @@ sma_init(struct stevedore *parent, int ac, char * const *av) ...@@ -130,8 +154,12 @@ sma_init(struct stevedore *parent, int ac, char * const *av)
{ {
const char *e; const char *e;
uintmax_t u; uintmax_t u;
struct sma_sc *sc;
(void)parent; ALLOC_OBJ(sc, SMA_SC_MAGIC);
AN(sc);
sc->sma_max = SIZE_MAX;
parent->priv = sc;
AZ(av[ac]); AZ(av[ac]);
if (ac > 1) if (ac > 1)
...@@ -148,14 +176,17 @@ sma_init(struct stevedore *parent, int ac, char * const *av) ...@@ -148,14 +176,17 @@ sma_init(struct stevedore *parent, int ac, char * const *av)
printf("storage_malloc: max size %ju MB.\n", printf("storage_malloc: max size %ju MB.\n",
u / (1024 * 1024)); u / (1024 * 1024));
sma_max = u; sc->sma_max = u;
} }
static void static void
sma_open(const struct stevedore *st) sma_open(const struct stevedore *st)
{ {
(void)st; struct sma_sc *sma_sc;
Lck_New(&sma_mtx);
CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC);
Lck_New(&sma_sc->sma_mtx);
} }
struct stevedore sma_stevedore = { struct stevedore sma_stevedore = {
......
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