1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* Memory handler for a plain memory divided in slot.
18 * This one uses plain memory.
21 #include "ap_slotmem.h"
24 char *name; /* per segment name */
25 void *base; /* data set start */
26 apr_size_t size; /* size of each memory slot */
27 unsigned int num; /* number of mem slots */
28 apr_pool_t *gpool; /* per segment global pool */
29 apr_global_mutex_t *smutex; /* mutex */
30 struct ap_slotmem_t *next; /* location of next allocated segment */
34 /* global pool and list of slotmem we are handling */
35 static struct ap_slotmem_t *globallistmem = NULL;
36 static apr_pool_t *gpool = NULL;
38 static apr_status_t slotmem_do(ap_slotmem_t *mem, ap_slotmem_callback_fn_t *func, void *data, apr_pool_t *pool)
44 return APR_ENOSHMAVAIL;
47 for (i = 0; i < mem->num; i++) {
48 ptr = ptr + mem->size;
49 func((void *) ptr, data, pool);
54 static apr_status_t slotmem_create(ap_slotmem_t **new, const char *name, apr_size_t item_size, unsigned int item_num, int type, apr_pool_t *pool)
57 ap_slotmem_t *next = globallistmem;
64 fname = ap_server_root_relative(pool, name);
66 /* first try to attach to existing slotmem */
69 if (strcmp(next->name, fname) == 0) {
70 /* we already have it */
83 /* create the memory using the gpool */
84 res = (ap_slotmem_t *) apr_pcalloc(gpool, sizeof(ap_slotmem_t));
85 res->base = apr_pcalloc(gpool, item_size * item_num);
87 return APR_ENOSHMAVAIL;
89 /* For the chained slotmem stuff */
90 res->name = apr_pstrdup(gpool, fname);
91 res->size = item_size;
94 if (globallistmem == NULL)
103 static apr_status_t slotmem_attach(ap_slotmem_t **new, const char *name, apr_size_t *item_size, unsigned int *item_num, apr_pool_t *pool)
105 ap_slotmem_t *next = globallistmem;
112 fname = ap_server_root_relative(pool, name);
115 return APR_ENOSHMAVAIL;
117 /* first try to attach to existing slotmem */
120 if (strcmp(next->name, fname) == 0) {
121 /* we already have it */
123 *item_size = next->size;
124 *item_num = next->num;
133 return APR_ENOSHMAVAIL;
136 static apr_status_t slotmem_mem(ap_slotmem_t *score, unsigned int id, void **mem)
142 return APR_ENOSHMAVAIL;
143 if (id < 0 || id > score->num)
144 return APR_ENOSHMAVAIL;
146 ptr = score->base + score->size * id;
148 return APR_ENOSHMAVAIL;
153 static apr_status_t slotmem_get(ap_slotmem_t *slot, unsigned int id, unsigned char *dest, apr_size_t dest_len)
159 ret = slotmem_mem(slot, id, &ptr);
160 if (ret != APR_SUCCESS) {
163 memcpy(dest, ptr, dest_len); /* bounds check? */
167 static apr_status_t slotmem_put(ap_slotmem_t *slot, unsigned int id, unsigned char *src, apr_size_t src_len)
173 ret = slotmem_mem(slot, id, &ptr);
174 if (ret != APR_SUCCESS) {
177 memcpy(ptr, src, src_len); /* bounds check? */
181 static const ap_slotmem_storage_method storage = {
191 static int pre_config(apr_pool_t *p, apr_pool_t *plog,
198 static void ap_plainmem_register_hook(apr_pool_t *p)
200 /* XXX: static const char * const prePos[] = { "mod_slotmem.c", NULL }; */
201 ap_register_provider(p, AP_SLOTMEM_STORAGE, "plain", "0", &storage);
202 ap_hook_pre_config(pre_config, NULL, NULL, APR_HOOK_MIDDLE);
205 module AP_MODULE_DECLARE_DATA plainmem_module = {
206 STANDARD20_MODULE_STUFF,
207 NULL, /* create per-directory config structure */
208 NULL, /* merge per-directory config structures */
209 NULL, /* create per-server config structure */
210 NULL, /* merge per-server config structures */
211 NULL, /* command apr_table_t */
212 ap_plainmem_register_hook /* register hooks */