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.
20 /* Memory handler for a shared memory divided in slot.
24 * @brief Memory Slot Extension Storage Module for Apache
27 * @ingroup APACHE_MODS
32 #include "http_config.h"
34 #include "ap_provider.h"
37 #include "apr_strings.h"
38 #include "apr_pools.h"
40 #include "apr_global_mutex.h"
41 #include "apr_file_io.h"
43 #ifdef AP_NEED_SET_MUTEX_PERMS
48 #include <unistd.h> /* for getpid() */
55 #define AP_SLOTMEM_PROVIDER_GROUP "slotmem"
56 #define AP_SLOTMEM_PROVIDER_VERSION 0
58 typedef unsigned int ap_slotmem_type_t;
61 * AP_SLOTMEM_TYPE_PERSIST: For transitory providers, persist
62 * the data on the file-system
64 * AP_SLOTMEM_TYPE_NOTMPSAFE:
66 * AP_SLOTMEM_TYPE_PREALLOC: Access to slots require they be grabbed 1st
68 #define AP_SLOTMEM_TYPE_PERSIST (1 << 0)
69 #define AP_SLOTMEM_TYPE_NOTMPSAFE (1 << 1)
70 #define AP_SLOTMEM_TYPE_PREGRAB (1 << 2)
72 typedef struct ap_slotmem_instance_t ap_slotmem_instance_t;
75 * callback function used for slotmem.
76 * @param mem is the memory associated with a worker.
77 * @param data is what is passed to slotmem.
78 * @param pool is pool used
79 * @return APR_SUCCESS if all went well
81 typedef apr_status_t ap_slotmem_callback_fn_t(void* mem, void *data, apr_pool_t *pool);
83 struct ap_slotmem_provider_t {
85 * Name of the provider method
89 * call the callback on all worker slots
90 * @param s ap_slotmem_t to use.
91 * @param funct callback function to call for each element.
92 * @param data parameter for the callback function.
93 * @param pool is pool used
94 * @return APR_SUCCESS if all went well
96 apr_status_t (* doall)(ap_slotmem_instance_t *s, ap_slotmem_callback_fn_t *func, void *data, apr_pool_t *pool);
98 * create a new slotmem with each item size is item_size.
99 * This would create shared memory, basically.
100 * @param name is a key used for debugging and in mod_status output or allow another process to share this space.
101 * @param item_size size of each item
102 * @param item_num number of item to create.
103 * @param type type of slotmem.
104 * @param pool is pool used
105 * @return APR_SUCCESS if all went well
107 apr_status_t (* create)(ap_slotmem_instance_t **new, const char *name, apr_size_t item_size, unsigned int item_num, ap_slotmem_type_t type, apr_pool_t *pool);
109 * attach to an existing slotmem.
110 * This would attach to shared memory, basically.
111 * @param name is a key used for debugging and in mod_status output or allow another process to share this space.
112 * @param item_size size of each item
113 * @param item_num max number of item.
114 * @param pool is pool to memory allocate.
115 * @return APR_SUCCESS if all went well
117 apr_status_t (* attach)(ap_slotmem_instance_t **new, const char *name, apr_size_t *item_size, unsigned int *item_num, apr_pool_t *pool);
119 * get the memory ptr associated with this worker slot.
120 * @param s ap_slotmem_t to use.
121 * @param item_id item to return for 0 to item_num
122 * @param mem address to store the pointer to the slot
123 * @return APR_SUCCESS if all went well
125 apr_status_t (* dptr)(ap_slotmem_instance_t *s, unsigned int item_id, void**mem);
127 * retrieve the memory associated with this worker slot.
128 * @param s ap_slotmem_t to use.
129 * @param item_id item to return for 0 to item_num
130 * @param dest address to store the data
131 * @param dest_len length of dataset to retrieve
132 * @return APR_SUCCESS if all went well
134 apr_status_t (* get)(ap_slotmem_instance_t *s, unsigned int item_id, unsigned char *dest, apr_size_t dest_len);
136 * store the memory associated with this worker slot.
137 * @param s ap_slotmem_t to use.
138 * @param item_id item to return for 0 to item_num
139 * @param src address of the data to store in the slot
140 * @param src_len length of dataset to store in the slot
141 * @return APR_SUCCESS if all went well
143 apr_status_t (* put)(ap_slotmem_instance_t *slot, unsigned int item_id, unsigned char *src, apr_size_t src_len);
145 * return number of slots allocated for this entry.
146 * @param s ap_slotmem_t to use.
147 * @return number of slots
149 unsigned int (* num_slots)(ap_slotmem_instance_t *s);
151 * return slot size allocated for this entry.
152 * @param s ap_slotmem_t to use.
153 * @return size of slot
155 apr_size_t (* slot_size)(ap_slotmem_instance_t *s);
157 * grab (or alloc) the slot associated with this item_id
158 * @param s ap_slotmem_t to use.
159 * @param item_id item allocate and mark as in-use
160 * @return APR_SUCCESS if all went well
162 apr_status_t (* grab)(ap_slotmem_instance_t *s, unsigned int item_id);
164 * release (or free) the slot associated with this item_id
165 * @param s ap_slotmem_t to use.
166 * @param item_id item free and mark as no longer in-use
167 * @return APR_SUCCESS if all went well
169 apr_status_t (* release)(ap_slotmem_instance_t *s, unsigned int item_id);
172 typedef struct ap_slotmem_provider_t ap_slotmem_provider_t;