mempool.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2002-2003, 2005 Hewlett-Packard Co
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. This file is part of libunwind.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  21. #include "libunwind_i.h"
  22. #define MAX_ALIGN (sizeof (long double))
  23. static char sos_memory[SOS_MEMORY_SIZE];
  24. static char *sos_memp;
  25. static size_t pg_size;
  26. HIDDEN void *
  27. sos_alloc (size_t size)
  28. {
  29. char *mem;
  30. #ifdef HAVE_CMPXCHG
  31. char *old_mem;
  32. size = (size + MAX_ALIGN - 1) & -MAX_ALIGN;
  33. if (!sos_memp)
  34. cmpxchg_ptr (&sos_memp, 0, sos_memory);
  35. do
  36. {
  37. old_mem = sos_memp;
  38. mem = (char *) (((unsigned long) old_mem + MAX_ALIGN - 1) & -MAX_ALIGN);
  39. mem += size;
  40. assert (mem < sos_memory + sizeof (sos_memory));
  41. }
  42. while (!cmpxchg_ptr (&sos_memp, old_mem, mem));
  43. #else
  44. static define_lock (sos_lock);
  45. intrmask_t saved_mask;
  46. size = (size + MAX_ALIGN - 1) & -MAX_ALIGN;
  47. lock_acquire (&sos_lock, saved_mask);
  48. {
  49. if (!sos_memp)
  50. sos_memp = sos_memory;
  51. mem = (char *) (((unsigned long) sos_memp + MAX_ALIGN - 1) & -MAX_ALIGN);
  52. mem += size;
  53. assert (mem < sos_memory + sizeof (sos_memory));
  54. sos_memp = mem;
  55. }
  56. lock_release (&sos_lock, saved_mask);
  57. #endif
  58. return mem;
  59. }
  60. /* Must be called while holding the mempool lock. */
  61. static void
  62. free_object (struct mempool *pool, void *object)
  63. {
  64. struct object *obj = object;
  65. obj->next = pool->free_list;
  66. pool->free_list = obj;
  67. ++pool->num_free;
  68. }
  69. static void
  70. add_memory (struct mempool *pool, char *mem, size_t size, size_t obj_size)
  71. {
  72. char *obj;
  73. for (obj = mem; obj <= mem + size - obj_size; obj += obj_size)
  74. free_object (pool, obj);
  75. }
  76. static void
  77. expand (struct mempool *pool)
  78. {
  79. size_t size;
  80. char *mem;
  81. size = pool->chunk_size;
  82. GET_MEMORY (mem, size);
  83. if (!mem)
  84. {
  85. size = (pool->obj_size + pg_size - 1) & -pg_size;
  86. GET_MEMORY (mem, size);
  87. if (!mem)
  88. {
  89. /* last chance: try to allocate one object from the SOS memory */
  90. size = pool->obj_size;
  91. mem = sos_alloc (size);
  92. }
  93. }
  94. add_memory (pool, mem, size, pool->obj_size);
  95. }
  96. HIDDEN void
  97. mempool_init (struct mempool *pool, size_t obj_size, size_t reserve)
  98. {
  99. if (pg_size == 0)
  100. pg_size = getpagesize ();
  101. memset (pool, 0, sizeof (*pool));
  102. lock_init (&pool->lock);
  103. /* round object-size up to integer multiple of MAX_ALIGN */
  104. obj_size = (obj_size + MAX_ALIGN - 1) & -MAX_ALIGN;
  105. if (!reserve)
  106. {
  107. reserve = pg_size / obj_size / 4;
  108. if (!reserve)
  109. reserve = 16;
  110. }
  111. pool->obj_size = obj_size;
  112. pool->reserve = reserve;
  113. pool->chunk_size = (2*reserve*obj_size + pg_size - 1) & -pg_size;
  114. expand (pool);
  115. }
  116. HIDDEN void *
  117. mempool_alloc (struct mempool *pool)
  118. {
  119. intrmask_t saved_mask;
  120. struct object *obj;
  121. lock_acquire (&pool->lock, saved_mask);
  122. {
  123. if (pool->num_free <= pool->reserve)
  124. expand (pool);
  125. assert (pool->num_free > 0);
  126. --pool->num_free;
  127. obj = pool->free_list;
  128. pool->free_list = obj->next;
  129. }
  130. lock_release (&pool->lock, saved_mask);
  131. return obj;
  132. }
  133. HIDDEN void
  134. mempool_free (struct mempool *pool, void *object)
  135. {
  136. intrmask_t saved_mask;
  137. lock_acquire (&pool->lock, saved_mask);
  138. {
  139. free_object (pool, object);
  140. }
  141. lock_release (&pool->lock, saved_mask);
  142. }