MCPcopy Index your code
hub / github.com/python/cpython / Heap

Class Heap

Lib/multiprocessing/heap.py:115–316  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

113#
114
115class Heap(object):
116
117 # Minimum malloc() alignment
118 _alignment = 8
119
120 _DISCARD_FREE_SPACE_LARGER_THAN = 4 * 1024 ** 2 # 4 MB
121 _DOUBLE_ARENA_SIZE_UNTIL = 4 * 1024 ** 2
122
123 def __init__(self, size=mmap.PAGESIZE):
124 self._lastpid = os.getpid()
125 self._lock = threading.Lock()
126 # Current arena allocation size
127 self._size = size
128 # A sorted list of available block sizes in arenas
129 self._lengths = []
130
131 # Free block management:
132 # - map each block size to a list of `(Arena, start, stop)` blocks
133 self._len_to_seq = {}
134 # - map `(Arena, start)` tuple to the `(Arena, start, stop)` block
135 # starting at that offset
136 self._start_to_block = {}
137 # - map `(Arena, stop)` tuple to the `(Arena, start, stop)` block
138 # ending at that offset
139 self._stop_to_block = {}
140
141 # Map arenas to their `(Arena, start, stop)` blocks in use
142 self._allocated_blocks = defaultdict(set)
143 self._arenas = []
144
145 # List of pending blocks to free - see comment in free() below
146 self._pending_free_blocks = []
147
148 # Statistics
149 self._n_mallocs = 0
150 self._n_frees = 0
151
152 @staticmethod
153 def _roundup(n, alignment):
154 # alignment must be a power of 2
155 mask = alignment - 1
156 return (n + mask) & ~mask
157
158 def _new_arena(self, size):
159 # Create a new arena with at least the given *size*
160 length = self._roundup(max(self._size, size), mmap.PAGESIZE)
161 # We carve larger and larger arenas, for efficiency, until we
162 # reach a large-ish size (roughly L3 cache-sized)
163 if self._size < self._DOUBLE_ARENA_SIZE_UNTIL:
164 self._size *= 2
165 util.info('allocating a new mmap of length %d', length)
166 arena = Arena(length)
167 self._arenas.append(arena)
168 return (arena, 0, length)
169
170 def _discard_arena(self, arena):
171 # Possibly delete the given (unused) arena
172 length = arena.size

Callers 1

BufferWrapperClass · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…