Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions Lib/multiprocessing/heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

__all__ = ['BufferWrapper']

#
# Module constants
#

_MAXSIZE = sys.maxsize

#
# Inheritable class which wraps an mmap, and from which blocks can be allocated
#
Expand Down Expand Up @@ -108,6 +114,17 @@ def rebuild_arena(size, dupfd):

reduction.register(Arena, reduce_arena)

#
# Validation helpers
#

def _validate_size(size):
"""Validate requested size; raise ValueError if < 0, OverflowError if >= _MAXSIZE."""
if size < 0:
raise ValueError("Size {0:n} out of range".format(size))
if _MAXSIZE <= size:
raise OverflowError("Size {0:n} too large".format(size))

#
# Class allowing allocation of chunks of memory from arenas
#
Expand Down Expand Up @@ -295,10 +312,7 @@ def free(self, block):

def malloc(self, size):
# return a block of right size (possibly rounded up)
if size < 0:
raise ValueError("Size {0:n} out of range".format(size))
if sys.maxsize <= size:
raise OverflowError("Size {0:n} too large".format(size))
_validate_size(size)
if os.getpid() != self._lastpid:
self.__init__() # reinitialize after fork
with self._lock:
Expand All @@ -324,10 +338,7 @@ class BufferWrapper(object):
_heap = Heap()

def __init__(self, size):
if size < 0:
raise ValueError("Size {0:n} out of range".format(size))
if sys.maxsize <= size:
raise OverflowError("Size {0:n} too large".format(size))
_validate_size(size)
block = BufferWrapper._heap.malloc(size)
self._state = (block, size)
util.Finalize(self, BufferWrapper._heap.free, args=(block,))
Expand Down
Loading