memarea.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Copyright (c) 2008, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /** \file memarea.c
  5. * \brief Implementation for memarea_t, an allocator for allocating lots of
  6. * small objects that will be freed all at once.
  7. */
  8. #include "orconfig.h"
  9. #include <stdlib.h>
  10. #include "memarea.h"
  11. #include "util.h"
  12. #include "compat.h"
  13. #include "log.h"
  14. /** All returned pointers should be aligned to the nearest multiple of this
  15. * value. */
  16. #define MEMAREA_ALIGN SIZEOF_VOID_P
  17. #if MEMAREA_ALIGN == 4
  18. #define MEMAREA_ALIGN_MASK 3lu
  19. #elif MEMAREA_ALIGN == 8
  20. #define MEMAREA_ALIGN_MASK 7lu
  21. #else
  22. #error "void* is neither 4 nor 8 bytes long. I don't know how to align stuff."
  23. #endif
  24. /* Increment <b>ptr</b> until it is aligned to MEMAREA_ALIGN. */
  25. static INLINE void *
  26. realign_pointer(void *ptr)
  27. {
  28. uintptr_t x = (uintptr_t)ptr;
  29. x = (x+MEMAREA_ALIGN_MASK) & ~MEMAREA_ALIGN_MASK;
  30. return (void*)x;
  31. }
  32. /** Implements part of a memarea. New memory is carved off from chunk->mem in
  33. * increasing order until a request is too big, at which point a new chunk is
  34. * allocated. */
  35. typedef struct memarea_chunk_t {
  36. /** Next chunk in this area. Only kept around so we can free it. */
  37. struct memarea_chunk_t *next_chunk;
  38. size_t mem_size; /**< How much RAM is available in u.mem, total? */
  39. char *next_mem; /**< Next position in u.mem to allocate data at. If it's
  40. * greater than or equal to mem+mem_size, this chunk is
  41. * full. */
  42. union {
  43. char mem[1]; /**< Memory space in this chunk. */
  44. void *_void_for_alignment; /**< Dummy; used to make sure mem is aligned. */
  45. } u;
  46. } memarea_chunk_t;
  47. #define CHUNK_HEADER_SIZE STRUCT_OFFSET(memarea_chunk_t, u)
  48. /** A memarea_t is an allocation region for a set of small memory requests
  49. * that will all be freed at once. */
  50. struct memarea_t {
  51. struct memarea_chunk_t *first; /**< Top of the chunk stack: never NULL. */
  52. size_t chunk_size; /**<Size to use when allocating chunks.*/
  53. };
  54. /** Helper: allocate a new memarea chunk of around <b>chunk_size</b> bytes. */
  55. static memarea_chunk_t *
  56. alloc_chunk(size_t chunk_size)
  57. {
  58. memarea_chunk_t *res = tor_malloc_roundup(&chunk_size);
  59. res->next_chunk = NULL;
  60. res->mem_size = chunk_size - CHUNK_HEADER_SIZE;
  61. res->next_mem = res->u.mem;
  62. return res;
  63. }
  64. /** Allocate and return new memarea, with chunks of approximately
  65. * <b>chunk_size</b> bytes. (There is indeed some overhead.) */
  66. memarea_t *
  67. memarea_new(size_t chunk_size)
  68. {
  69. memarea_t *head = tor_malloc(sizeof(memarea_t));
  70. head->first = alloc_chunk(chunk_size);
  71. head->chunk_size = chunk_size;
  72. return head;
  73. }
  74. /** Free <b>area</b>, invalidating all pointers returned from memarea_alloc()
  75. * and friends for this area */
  76. void
  77. memarea_drop_all(memarea_t *area)
  78. {
  79. memarea_chunk_t *chunk, *next;
  80. for (chunk = area->first; chunk; chunk = next) {
  81. next = chunk->next_chunk;
  82. tor_free(chunk);
  83. }
  84. area->first = NULL; /*fail fast on */
  85. tor_free(area);
  86. }
  87. /** Forget about having allocated anything in <b>area</b>, and free some of
  88. * the backing storage associated with it, as appropriate. Invalidates all
  89. * pointers returned from memarea_alloc() for this area. */
  90. void
  91. memarea_clear(memarea_t *area)
  92. {
  93. memarea_chunk_t *chunk, *next;
  94. if (area->first->next_chunk) {
  95. for (chunk = area->first->next_chunk; chunk; chunk = next) {
  96. next = chunk->next_chunk;
  97. tor_free(chunk);
  98. }
  99. area->first->next_chunk = NULL;
  100. }
  101. area->first->next_mem = area->first->u.mem;
  102. }
  103. /** Return true iff <b>p</b> is in a range that has been returned by an
  104. * allocation from <b>area</b>. */
  105. int
  106. memarea_owns_ptr(const memarea_t *area, const void *p)
  107. {
  108. memarea_chunk_t *chunk;
  109. const char *ptr = p;
  110. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  111. if (ptr >= chunk->u.mem && ptr < chunk->next_mem)
  112. return 1;
  113. }
  114. return 0;
  115. }
  116. /** Return a pointer to a chunk of memory in <b>area</b> of at least <b>sz</b>
  117. * bytes. <b>sz</b> should be significantly smaller than the area's chunk
  118. * size, though we can deal if it isn't. */
  119. void *
  120. memarea_alloc(memarea_t *area, size_t sz)
  121. {
  122. memarea_chunk_t *chunk = area->first;
  123. char *result;
  124. tor_assert(chunk);
  125. if (chunk->next_mem+sz > chunk->u.mem+chunk->mem_size) {
  126. if (sz+CHUNK_HEADER_SIZE >= area->chunk_size) {
  127. /* This allocation is too big. Stick it in a special chunk, and put
  128. * that chunk second in the list. */
  129. memarea_chunk_t *new_chunk = alloc_chunk(sz+CHUNK_HEADER_SIZE);
  130. new_chunk->next_chunk = chunk->next_chunk;
  131. chunk->next_chunk = new_chunk;
  132. chunk = new_chunk;
  133. } else {
  134. memarea_chunk_t *new_chunk = alloc_chunk(area->chunk_size);
  135. new_chunk->next_chunk = chunk;
  136. area->first = chunk = new_chunk;
  137. }
  138. tor_assert(chunk->mem_size >= sz);
  139. }
  140. result = chunk->next_mem;
  141. chunk->next_mem = realign_pointer(chunk->next_mem + sz);
  142. return result;
  143. }
  144. /** As memarea_alloc(), but clears the memory it returns. */
  145. void *
  146. memarea_alloc_zero(memarea_t *area, size_t sz)
  147. {
  148. void *result = memarea_alloc(area, sz);
  149. memset(result, 0, sz);
  150. return result;
  151. }
  152. /** As memdup, but returns the memory from <b>area</b>. */
  153. void *
  154. memarea_memdup(memarea_t *area, const void *s, size_t n)
  155. {
  156. char *result = memarea_alloc(area, n);
  157. memcpy(result, s, n);
  158. return result;
  159. }
  160. /** As strdup, but returns the memory from <b>area</b>. */
  161. char *
  162. memarea_strdup(memarea_t *area, const char *s)
  163. {
  164. return memarea_memdup(area, s, strlen(s)+1);
  165. }
  166. /** As strndup, but returns the memory from <b>area</b>. */
  167. char *
  168. memarea_strndup(memarea_t *area, const char *s, size_t n)
  169. {
  170. size_t ln;
  171. char *result;
  172. const char *cp, *end = s+n;
  173. for (cp = s; *cp && cp < end; ++cp)
  174. ;
  175. /* cp now points to s+n, or to the 0 in the string. */
  176. ln = cp-s;
  177. result = memarea_memdup(area, s, ln+1);
  178. result[ln]='\0';
  179. return result;
  180. }
  181. /** Assert that <b>area</b> is okay. */
  182. void
  183. memarea_assert_ok(memarea_t *area)
  184. {
  185. memarea_chunk_t *chunk;
  186. tor_assert(area->first);
  187. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  188. tor_assert(chunk->next_mem >= chunk->u.mem);
  189. tor_assert(chunk->next_mem <= chunk->u.mem+chunk->mem_size+MEMAREA_ALIGN);
  190. }
  191. }