source: bin/varnishd/stevedore.c @ 1cc48fa

Revision 1cc48fa, 3.9 KB checked in by Dag Erling Smørgrav <des@…>, 5 years ago (diff)

Update Linpro copyright (about 3 months late). Someone should
probably s/Linpro/Redpill Linpro/ at some point.

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

  • Property mode set to 100644
Line 
1/*-
2 * Copyright (c) 2007-2009 Linpro AS
3 * All rights reserved.
4 *
5 * Author: Dag-Erling Smørgav <des@des.no>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $Id$
29 */
30
31#include "config.h"
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
37#include "cache.h"
38#include "stevedore.h"
39
40static VTAILQ_HEAD(, stevedore) stevedores =
41    VTAILQ_HEAD_INITIALIZER(stevedores);
42
43static const struct stevedore * volatile stv_next;
44
45struct storage *
46STV_alloc(struct sess *sp, size_t size)
47{
48        struct storage *st;
49        struct stevedore *stv = NULL;
50        unsigned fail = 0;
51
52        /*
53         * Always try the stevedore which allocated the object in order to
54         * not needlessly split an object across multiple stevedores.
55         */
56        if (sp->obj != NULL) {
57                CHECK_OBJ_NOTNULL(sp->obj, OBJECT_MAGIC);
58                if (sp->obj->objstore != NULL) {
59                        stv = sp->obj->objstore->stevedore;
60                        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
61                }
62        }
63
64        for (;;) {
65                if (stv == NULL) {
66                        /* pick a stevedore and bump the head along */
67                        stv = VTAILQ_NEXT(stv_next, list);
68                        if (stv == NULL)
69                                stv = VTAILQ_FIRST(&stevedores);
70                        AN(stv);
71                        AN(stv->name);
72                        stv_next = stv;
73                        fail = 0;
74                }
75
76                /* try to allocate from it */
77                AN(stv->alloc);
78                st = stv->alloc(stv, size);
79                if (st != NULL)
80                        break;
81
82                /* no luck; try to free some space and keep trying */
83                if (EXP_NukeOne(sp) == -1)
84                        break;
85
86                /* Enough is enough: try another if we have one */
87                if (++fail == 50)
88                        stv = NULL;
89        }
90        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
91        return (st);
92}
93
94void
95STV_trim(struct storage *st, size_t size)
96{
97
98        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
99        AN(st->stevedore);
100        if (st->stevedore->trim)
101                st->stevedore->trim(st, size);
102}
103
104void
105STV_free(struct storage *st)
106{
107
108        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
109        AN(st->stevedore);
110        AN(st->stevedore->free);
111        st->stevedore->free(st);
112}
113
114void
115STV_add(const struct stevedore *stv2, int ac, char * const *av)
116{
117        struct stevedore *stv;
118
119        CHECK_OBJ_NOTNULL(stv2, STEVEDORE_MAGIC);
120        ALLOC_OBJ(stv, STEVEDORE_MAGIC);
121        AN(stv);
122
123        *stv = *stv2;
124        AN(stv->name);
125        AN(stv->alloc);
126
127        if (stv->init != NULL)
128                stv->init(stv, ac, av);
129        else if (ac != 0)
130                ARGV_ERR("(-s%s) too many arguments\n", stv->name);
131
132        VTAILQ_INSERT_TAIL(&stevedores, stv, list);
133
134        if (!stv_next)
135                stv_next = VTAILQ_FIRST(&stevedores);
136}
137
138void
139STV_open(void)
140{
141        struct stevedore *stv;
142
143        VTAILQ_FOREACH(stv, &stevedores, list) {
144                if (stv->open != NULL)
145                        stv->open(stv);
146        }
147}
148
149void
150STV_close(void)
151{
152        struct stevedore *stv;
153
154        VTAILQ_FOREACH(stv, &stevedores, list) {
155                if (stv->close != NULL)
156                        stv->close(stv);
157        }
158}
159
160const struct choice STV_choice[] = {
161        { "file",       &smf_stevedore },
162        { "malloc",     &sma_stevedore },
163        { "persistent", &smp_stevedore },
164#ifdef HAVE_LIBUMEM
165        { "umem",       &smu_stevedore },
166#endif
167        { NULL,         NULL }
168};
Note: See TracBrowser for help on using the repository browser.