ap_slotmem.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #ifndef SLOTMEM_H
  17. #define SLOTMEM_H
  18. /* Memory handler for a shared memory divided in slot.
  19. */
  20. /**
  21. * @file ap_slotmem.h
  22. * @brief Memory Slot Extension Storage Module for Apache
  23. *
  24. * @defgroup MEM mem
  25. * @ingroup APACHE_MODS
  26. * @{
  27. */
  28. #include "httpd.h"
  29. #include "http_config.h"
  30. #include "http_log.h"
  31. #include "ap_provider.h"
  32. #include "apr.h"
  33. #include "apr_strings.h"
  34. #include "apr_pools.h"
  35. #include "apr_shm.h"
  36. #include "apr_global_mutex.h"
  37. #include "apr_file_io.h"
  38. #include "apr_md5.h"
  39. #if APR_HAVE_UNISTD_H
  40. #include <unistd.h> /* for getpid() */
  41. #endif
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #define AP_SLOTMEM_PROVIDER_GROUP "slotmem"
  46. #define AP_SLOTMEM_PROVIDER_VERSION "0"
  47. typedef unsigned int ap_slotmem_type_t;
  48. /*
  49. * Values for ap_slotmem_type_t::
  50. *
  51. * AP_SLOTMEM_TYPE_PERSIST: For transitory providers, persist
  52. * the data on the file-system
  53. *
  54. * AP_SLOTMEM_TYPE_NOTMPSAFE:
  55. *
  56. * AP_SLOTMEM_TYPE_PREALLOC: Access to slots require they be grabbed 1st
  57. *
  58. * AP_SLOTMEM_TYPE_CLEARINUSE: If persisting, clear 'inuse' array before
  59. * storing
  60. */
  61. #define AP_SLOTMEM_TYPE_PERSIST (1 << 0)
  62. #define AP_SLOTMEM_TYPE_NOTMPSAFE (1 << 1)
  63. #define AP_SLOTMEM_TYPE_PREGRAB (1 << 2)
  64. #define AP_SLOTMEM_TYPE_CLEARINUSE (1 << 3)
  65. typedef struct ap_slotmem_instance_t ap_slotmem_instance_t;
  66. /**
  67. * callback function used for slotmem doall.
  68. * @param mem is the memory associated with a worker.
  69. * @param data is what is passed to slotmem.
  70. * @param pool is pool used
  71. * @return APR_SUCCESS if all went well
  72. */
  73. typedef apr_status_t ap_slotmem_callback_fn_t(void* mem, void *data, apr_pool_t *pool);
  74. struct ap_slotmem_provider_t {
  75. /*
  76. * Name of the provider method
  77. */
  78. const char *name;
  79. /**
  80. * call the callback on all worker slots
  81. * @param s ap_slotmem_instance_t to use.
  82. * @param funct callback function to call for each element.
  83. * @param data parameter for the callback function.
  84. * @param pool is pool used
  85. * @return APR_SUCCESS if all went well
  86. */
  87. apr_status_t (* doall)(ap_slotmem_instance_t *s, ap_slotmem_callback_fn_t *func, void *data, apr_pool_t *pool);
  88. /**
  89. * create a new slotmem with each item size is item_size.
  90. * This would create shared memory, basically.
  91. * @param inst where to store pointer to slotmem
  92. * @param name a key used for debugging and in mod_status output or allow another process to share this space.
  93. * @param item_size size of each item
  94. * @param item_num number of item to create.
  95. * @param type type of slotmem.
  96. * @param pool is pool used
  97. * @return APR_SUCCESS if all went well
  98. */
  99. apr_status_t (* create)(ap_slotmem_instance_t **inst, const char *name, apr_size_t item_size, unsigned int item_num, ap_slotmem_type_t type, apr_pool_t *pool);
  100. /**
  101. * attach to an existing slotmem.
  102. * This would attach to shared memory, basically.
  103. * @param inst where to store pointer to slotmem
  104. * @param name a key used for debugging and in mod_status output or allow another process to share this space.
  105. * @param item_size size of each item
  106. * @param item_num max number of item.
  107. * @param pool is pool to memory allocate.
  108. * @return APR_SUCCESS if all went well
  109. */
  110. apr_status_t (* attach)(ap_slotmem_instance_t **inst, const char *name, apr_size_t *item_size, unsigned int *item_num, apr_pool_t *pool);
  111. /**
  112. * get the memory ptr associated with this worker slot.
  113. * @param s ap_slotmem_instance_t to use.
  114. * @param item_id item to return for 0 to item_num
  115. * @param mem address to store the pointer to the slot
  116. * @return APR_SUCCESS if all went well
  117. */
  118. apr_status_t (* dptr)(ap_slotmem_instance_t *s, unsigned int item_id, void**mem);
  119. /**
  120. * get/read the data associated with this worker slot.
  121. * @param s ap_slotmem_instance_t to use.
  122. * @param item_id item to return for 0 to item_num
  123. * @param dest address to store the data
  124. * @param dest_len length of dataset to retrieve
  125. * @return APR_SUCCESS if all went well
  126. */
  127. apr_status_t (* get)(ap_slotmem_instance_t *s, unsigned int item_id, unsigned char *dest, apr_size_t dest_len);
  128. /**
  129. * put/write the data associated with this worker slot.
  130. * @param s ap_slotmem_instance_t to use.
  131. * @param item_id item to return for 0 to item_num
  132. * @param src address of the data to store in the slot
  133. * @param src_len length of dataset to store in the slot
  134. * @return APR_SUCCESS if all went well
  135. */
  136. apr_status_t (* put)(ap_slotmem_instance_t *slot, unsigned int item_id, unsigned char *src, apr_size_t src_len);
  137. /**
  138. * return number of slots allocated for this entry.
  139. * @param s ap_slotmem_instance_t to use.
  140. * @return number of slots
  141. */
  142. unsigned int (* num_slots)(ap_slotmem_instance_t *s);
  143. /**
  144. * return number of free (not used) slots allocated for this entry.
  145. * Valid for slots which are AP_SLOTMEM_TYPE_PREGRAB as well as
  146. * any which use get/release.
  147. * @param s ap_slotmem_instance_t to use.
  148. * @return number of slots
  149. */
  150. unsigned int (* num_free_slots)(ap_slotmem_instance_t *s);
  151. /**
  152. * return slot size allocated for this entry.
  153. * @param s ap_slotmem_instance_t to use.
  154. * @return size of slot
  155. */
  156. apr_size_t (* slot_size)(ap_slotmem_instance_t *s);
  157. /**
  158. * grab (or alloc) a free slot
  159. * @param s ap_slotmem_instance_t to use.
  160. * @param item_id ptr to the available slot id and marked as in-use
  161. * @return APR_SUCCESS if all went well
  162. */
  163. apr_status_t (* grab)(ap_slotmem_instance_t *s, unsigned int *item_id);
  164. /**
  165. * release (or free) the slot associated with this item_id
  166. * @param s ap_slotmem_instance_t to use.
  167. * @param item_id slot id to free and mark as no longer in-use
  168. * @return APR_SUCCESS if all went well
  169. */
  170. apr_status_t (* release)(ap_slotmem_instance_t *s, unsigned int item_id);
  171. /**
  172. * forced grab (or alloc) a slot associated with this item_id
  173. * @param s ap_slotmem_instance_t to use.
  174. * @param item_id to the specified slot id and marked as in-use
  175. * @return APR_SUCCESS if all went well
  176. */
  177. apr_status_t (* fgrab)(ap_slotmem_instance_t *s, unsigned int item_id);
  178. };
  179. typedef struct ap_slotmem_provider_t ap_slotmem_provider_t;
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif
  184. /** @} */