pyarena.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "Python.h"
  2. #include "pycore_pyarena.h" // PyArena
  3. /* A simple arena block structure.
  4. Measurements with standard library modules suggest the average
  5. allocation is about 20 bytes and that most compiles use a single
  6. block.
  7. TODO(jhylton): Think about a realloc API, maybe just for the last
  8. allocation?
  9. */
  10. #define DEFAULT_BLOCK_SIZE 8192
  11. #define ALIGNMENT 8
  12. typedef struct _block {
  13. /* Total number of bytes owned by this block available to pass out.
  14. * Read-only after initialization. The first such byte starts at
  15. * ab_mem.
  16. */
  17. size_t ab_size;
  18. /* Total number of bytes already passed out. The next byte available
  19. * to pass out starts at ab_mem + ab_offset.
  20. */
  21. size_t ab_offset;
  22. /* An arena maintains a singly-linked, NULL-terminated list of
  23. * all blocks owned by the arena. These are linked via the
  24. * ab_next member.
  25. */
  26. struct _block *ab_next;
  27. /* Pointer to the first allocatable byte owned by this block. Read-
  28. * only after initialization.
  29. */
  30. void *ab_mem;
  31. } block;
  32. /* The arena manages two kinds of memory, blocks of raw memory
  33. and a list of PyObject* pointers. PyObjects are decrefed
  34. when the arena is freed.
  35. */
  36. struct _arena {
  37. /* Pointer to the first block allocated for the arena, never NULL.
  38. It is used only to find the first block when the arena is
  39. being freed.
  40. */
  41. block *a_head;
  42. /* Pointer to the block currently used for allocation. Its
  43. ab_next field should be NULL. If it is not-null after a
  44. call to block_alloc(), it means a new block has been allocated
  45. and a_cur should be reset to point it.
  46. */
  47. block *a_cur;
  48. /* A Python list object containing references to all the PyObject
  49. pointers associated with this arena. They will be DECREFed
  50. when the arena is freed.
  51. */
  52. PyObject *a_objects;
  53. #if defined(Py_DEBUG)
  54. /* Debug output */
  55. size_t total_allocs;
  56. size_t total_size;
  57. size_t total_blocks;
  58. size_t total_block_size;
  59. size_t total_big_blocks;
  60. #endif
  61. };
  62. static block *
  63. block_new(size_t size)
  64. {
  65. /* Allocate header and block as one unit.
  66. ab_mem points just past header. */
  67. block *b = (block *)PyMem_Malloc(sizeof(block) + size);
  68. if (!b)
  69. return NULL;
  70. b->ab_size = size;
  71. b->ab_mem = (void *)(b + 1);
  72. b->ab_next = NULL;
  73. b->ab_offset = (char *)_Py_ALIGN_UP(b->ab_mem, ALIGNMENT) -
  74. (char *)(b->ab_mem);
  75. return b;
  76. }
  77. static void
  78. block_free(block *b) {
  79. while (b) {
  80. block *next = b->ab_next;
  81. PyMem_Free(b);
  82. b = next;
  83. }
  84. }
  85. static void *
  86. block_alloc(block *b, size_t size)
  87. {
  88. void *p;
  89. assert(b);
  90. size = _Py_SIZE_ROUND_UP(size, ALIGNMENT);
  91. if (b->ab_offset + size > b->ab_size) {
  92. /* If we need to allocate more memory than will fit in
  93. the default block, allocate a one-off block that is
  94. exactly the right size. */
  95. /* TODO(jhylton): Think about space waste at end of block */
  96. block *newbl = block_new(
  97. size < DEFAULT_BLOCK_SIZE ?
  98. DEFAULT_BLOCK_SIZE : size);
  99. if (!newbl)
  100. return NULL;
  101. assert(!b->ab_next);
  102. b->ab_next = newbl;
  103. b = newbl;
  104. }
  105. assert(b->ab_offset + size <= b->ab_size);
  106. p = (void *)(((char *)b->ab_mem) + b->ab_offset);
  107. b->ab_offset += size;
  108. return p;
  109. }
  110. PyArena *
  111. _PyArena_New(void)
  112. {
  113. PyArena* arena = (PyArena *)PyMem_Malloc(sizeof(PyArena));
  114. if (!arena)
  115. return (PyArena*)PyErr_NoMemory();
  116. arena->a_head = block_new(DEFAULT_BLOCK_SIZE);
  117. arena->a_cur = arena->a_head;
  118. if (!arena->a_head) {
  119. PyMem_Free((void *)arena);
  120. return (PyArena*)PyErr_NoMemory();
  121. }
  122. arena->a_objects = PyList_New(0);
  123. if (!arena->a_objects) {
  124. block_free(arena->a_head);
  125. PyMem_Free((void *)arena);
  126. return (PyArena*)PyErr_NoMemory();
  127. }
  128. #if defined(Py_DEBUG)
  129. arena->total_allocs = 0;
  130. arena->total_size = 0;
  131. arena->total_blocks = 1;
  132. arena->total_block_size = DEFAULT_BLOCK_SIZE;
  133. arena->total_big_blocks = 0;
  134. #endif
  135. return arena;
  136. }
  137. void
  138. _PyArena_Free(PyArena *arena)
  139. {
  140. assert(arena);
  141. #if defined(Py_DEBUG)
  142. /*
  143. fprintf(stderr,
  144. "alloc=%zu size=%zu blocks=%zu block_size=%zu big=%zu objects=%zu\n",
  145. arena->total_allocs, arena->total_size, arena->total_blocks,
  146. arena->total_block_size, arena->total_big_blocks,
  147. PyList_Size(arena->a_objects));
  148. */
  149. #endif
  150. block_free(arena->a_head);
  151. /* This property normally holds, except when the code being compiled
  152. is sys.getobjects(0), in which case there will be two references.
  153. assert(arena->a_objects->ob_refcnt == 1);
  154. */
  155. Py_DECREF(arena->a_objects);
  156. PyMem_Free(arena);
  157. }
  158. void *
  159. _PyArena_Malloc(PyArena *arena, size_t size)
  160. {
  161. void *p = block_alloc(arena->a_cur, size);
  162. if (!p)
  163. return PyErr_NoMemory();
  164. #if defined(Py_DEBUG)
  165. arena->total_allocs++;
  166. arena->total_size += size;
  167. #endif
  168. /* Reset cur if we allocated a new block. */
  169. if (arena->a_cur->ab_next) {
  170. arena->a_cur = arena->a_cur->ab_next;
  171. #if defined(Py_DEBUG)
  172. arena->total_blocks++;
  173. arena->total_block_size += arena->a_cur->ab_size;
  174. if (arena->a_cur->ab_size > DEFAULT_BLOCK_SIZE)
  175. ++arena->total_big_blocks;
  176. #endif
  177. }
  178. return p;
  179. }
  180. int
  181. _PyArena_AddPyObject(PyArena *arena, PyObject *obj)
  182. {
  183. int r = PyList_Append(arena->a_objects, obj);
  184. if (r >= 0) {
  185. Py_DECREF(obj);
  186. }
  187. return r;
  188. }