apr_allocator.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 APR_ALLOCATOR_H
  17. #define APR_ALLOCATOR_H
  18. /**
  19. * @file apr_allocator.h
  20. * @brief APR Internal Memory Allocation
  21. */
  22. #include "apr.h"
  23. #include "apr_errno.h"
  24. #define APR_WANT_MEMFUNC /**< For no good reason? */
  25. #include "apr_want.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /**
  30. * @defgroup apr_allocator Internal Memory Allocation
  31. * @ingroup APR
  32. * @{
  33. */
  34. /** the allocator structure */
  35. typedef struct apr_allocator_t apr_allocator_t;
  36. /** the structure which holds information about the allocation */
  37. typedef struct apr_memnode_t apr_memnode_t;
  38. /** basic memory node structure
  39. * @note The next, ref and first_avail fields are available for use by the
  40. * caller of apr_allocator_alloc(), the remaining fields are read-only.
  41. * The next field has to be used with caution and sensibly set when the
  42. * memnode is passed back to apr_allocator_free(). See apr_allocator_free()
  43. * for details.
  44. * The ref and first_avail fields will be properly restored by
  45. * apr_allocator_free().
  46. */
  47. struct apr_memnode_t {
  48. apr_memnode_t *next; /**< next memnode */
  49. apr_memnode_t **ref; /**< reference to self */
  50. apr_uint32_t index; /**< size */
  51. apr_uint32_t free_index; /**< how much free */
  52. char *first_avail; /**< pointer to first free memory */
  53. char *endp; /**< pointer to end of free memory */
  54. };
  55. /** The base size of a memory node - aligned. */
  56. #define APR_MEMNODE_T_SIZE APR_ALIGN_DEFAULT(sizeof(apr_memnode_t))
  57. /** Symbolic constants */
  58. #define APR_ALLOCATOR_MAX_FREE_UNLIMITED 0
  59. /**
  60. * Create a new allocator
  61. * @param allocator The allocator we have just created.
  62. *
  63. */
  64. APR_DECLARE(apr_status_t) apr_allocator_create(apr_allocator_t **allocator)
  65. __attribute__((nonnull(1)));
  66. /**
  67. * Destroy an allocator
  68. * @param allocator The allocator to be destroyed
  69. * @remark Any memnodes not given back to the allocator prior to destroying
  70. * will _not_ be free()d.
  71. */
  72. APR_DECLARE(void) apr_allocator_destroy(apr_allocator_t *allocator)
  73. __attribute__((nonnull(1)));
  74. /**
  75. * Allocate a block of mem from the allocator
  76. * @param allocator The allocator to allocate from
  77. * @param size The size of the mem to allocate (excluding the
  78. * memnode structure)
  79. */
  80. APR_DECLARE(apr_memnode_t *) apr_allocator_alloc(apr_allocator_t *allocator,
  81. apr_size_t size)
  82. __attribute__((nonnull(1)));
  83. /**
  84. * Free a list of blocks of mem, giving them back to the allocator.
  85. * The list is typically terminated by a memnode with its next field
  86. * set to NULL.
  87. * @param allocator The allocator to give the mem back to
  88. * @param memnode The memory node to return
  89. */
  90. APR_DECLARE(void) apr_allocator_free(apr_allocator_t *allocator,
  91. apr_memnode_t *memnode)
  92. __attribute__((nonnull(1,2)));
  93. #include "apr_pools.h"
  94. /**
  95. * Set the owner of the allocator
  96. * @param allocator The allocator to set the owner for
  97. * @param pool The pool that is to own the allocator
  98. * @remark Typically pool is the highest level pool using the allocator
  99. */
  100. /*
  101. * XXX: see if we can come up with something a bit better. Currently
  102. * you can make a pool an owner, but if the pool doesn't use the allocator
  103. * the allocator will never be destroyed.
  104. */
  105. APR_DECLARE(void) apr_allocator_owner_set(apr_allocator_t *allocator,
  106. apr_pool_t *pool)
  107. __attribute__((nonnull(1)));
  108. /**
  109. * Get the current owner of the allocator
  110. * @param allocator The allocator to get the owner from
  111. */
  112. APR_DECLARE(apr_pool_t *) apr_allocator_owner_get(apr_allocator_t *allocator)
  113. __attribute__((nonnull(1)));
  114. /**
  115. * Set the current threshold at which the allocator should start
  116. * giving blocks back to the system.
  117. * @param allocator The allocator to set the threshold on
  118. * @param size The threshold. 0 == unlimited.
  119. */
  120. APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator,
  121. apr_size_t size)
  122. __attribute__((nonnull(1)));
  123. #include "apr_thread_mutex.h"
  124. #if APR_HAS_THREADS
  125. /**
  126. * Set a mutex for the allocator to use
  127. * @param allocator The allocator to set the mutex for
  128. * @param mutex The mutex
  129. */
  130. APR_DECLARE(void) apr_allocator_mutex_set(apr_allocator_t *allocator,
  131. apr_thread_mutex_t *mutex)
  132. __attribute__((nonnull(1)));
  133. /**
  134. * Get the mutex currently set for the allocator
  135. * @param allocator The allocator
  136. */
  137. APR_DECLARE(apr_thread_mutex_t *) apr_allocator_mutex_get(
  138. apr_allocator_t *allocator)
  139. __attribute__((nonnull(1)));
  140. #endif /* APR_HAS_THREADS */
  141. /** @} */
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif /* !APR_ALLOCATOR_H */