source: bin/varnishd/storage_malloc.c @ 407087

Revision 407087, 3.9 KB checked in by Poul-Henning Kamp <phk@…>, 5 years ago (diff)

Give the stevedores alloc method a flag that tells that we are allocating
an object structure.

git-svn-id:  http://www.varnish-cache.org/svn/trunk/varnish-cache@4266 d4fa192b-c00b-0410-8231-f00ffab90ce4

  • Property mode set to 100644
Line 
1/*-
2 * Copyright (c) 2006 Verdens Gang AS
3 * Copyright (c) 2006-2009 Linpro 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 * Storage method based on malloc(3)
30 */
31
32#include "config.h"
33
34#include "svnid.h"
35SVNID("$Id$")
36
37#include <sys/types.h>
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <pthread.h>
42
43#include "shmlog.h"
44#include "cache.h"
45#include "stevedore.h"
46
47static size_t                   sma_max = SIZE_MAX;
48static struct lock              sma_mtx;
49
50struct sma {
51        struct storage          s;
52        size_t                  sz;
53};
54
55static struct storage *
56sma_alloc(struct stevedore *st, size_t size, int isobj)
57{
58        struct sma *sma;
59
60        (void)isobj;
61        Lck_Lock(&sma_mtx);
62        VSL_stats->sma_nreq++;
63        if (VSL_stats->sma_nbytes + size > sma_max)
64                size = 0;
65        else {
66                VSL_stats->sma_nobj++;
67                VSL_stats->sma_nbytes += size;
68                VSL_stats->sma_balloc += size;
69        }
70        Lck_Unlock(&sma_mtx);
71
72        if (size == 0)
73                return (NULL);
74
75        sma = calloc(sizeof *sma, 1);
76        if (sma == NULL)
77                return (NULL);
78        sma->sz = size;
79        sma->s.priv = sma;
80        sma->s.ptr = malloc(size);
81        XXXAN(sma->s.ptr);
82        sma->s.len = 0;
83        sma->s.space = size;
84        sma->s.fd = -1;
85        sma->s.stevedore = st;
86        sma->s.magic = STORAGE_MAGIC;
87        return (&sma->s);
88}
89
90/*lint -e{818} not const-able */
91static void
92sma_free(struct storage *s)
93{
94        struct sma *sma;
95
96        CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC);
97        sma = s->priv;
98        assert(sma->sz == sma->s.space);
99        Lck_Lock(&sma_mtx);
100        VSL_stats->sma_nobj--;
101        VSL_stats->sma_nbytes -= sma->sz;
102        VSL_stats->sma_bfree += sma->sz;
103        Lck_Unlock(&sma_mtx);
104        free(sma->s.ptr);
105        free(sma);
106}
107
108static void
109sma_trim(struct storage *s, size_t size)
110{
111        struct sma *sma;
112        void *p;
113
114        CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC);
115        sma = s->priv;
116        assert(sma->sz == sma->s.space);
117        if ((p = realloc(sma->s.ptr, size)) != NULL) {
118                Lck_Lock(&sma_mtx);
119                VSL_stats->sma_nbytes -= (sma->sz - size);
120                VSL_stats->sma_bfree += sma->sz - size;
121                sma->sz = size;
122                Lck_Unlock(&sma_mtx);
123                sma->s.ptr = p;
124                s->space = size;
125        }
126}
127
128static void
129sma_init(struct stevedore *parent, int ac, char * const *av)
130{
131        const char *e;
132        uintmax_t u;
133
134        (void)parent;
135
136        AZ(av[ac]);
137        if (ac > 1)
138                ARGV_ERR("(-smalloc) too many arguments\n");
139
140        if (ac == 0 || *av[0] == '\0')
141                 return;
142
143        e = str2bytes(av[0], &u, 0);
144        if (e != NULL)
145                ARGV_ERR("(-smalloc) size \"%s\": %s\n", av[0], e);
146        if ((u != (uintmax_t)(size_t)u))
147                ARGV_ERR("(-smalloc) size \"%s\": too big\n", av[0]);
148
149        printf("storage_malloc: max size %ju MB.\n",
150            u / (1024 * 1024));
151        sma_max = u;
152}
153
154static void
155sma_open(const struct stevedore *st)
156{
157        (void)st;
158        Lck_New(&sma_mtx);
159}
160
161struct stevedore sma_stevedore = {
162        .magic  =       STEVEDORE_MAGIC,
163        .name   =       "malloc",
164        .init   =       sma_init,
165        .open   =       sma_open,
166        .alloc  =       sma_alloc,
167        .free   =       sma_free,
168        .trim   =       sma_trim,
169};
Note: See TracBrowser for help on using the repository browser.