|
@@ -925,7 +925,7 @@ def memoize(function):
|
|
|
|
|
|
class Buffer:
|
|
class Buffer:
|
|
"""
|
|
"""
|
|
- provide a thread-local buffer
|
|
|
|
|
|
+ Provides a managed, resizable buffer.
|
|
"""
|
|
"""
|
|
|
|
|
|
class MemoryLimitExceeded(Error, OSError):
|
|
class MemoryLimitExceeded(Error, OSError):
|
|
@@ -938,13 +938,12 @@ class Buffer:
|
|
"""
|
|
"""
|
|
assert callable(allocator), 'must give alloc(size) function as first param'
|
|
assert callable(allocator), 'must give alloc(size) function as first param'
|
|
assert limit is None or size <= limit, 'initial size must be <= limit'
|
|
assert limit is None or size <= limit, 'initial size must be <= limit'
|
|
- self._thread_local = threading.local()
|
|
|
|
self.allocator = allocator
|
|
self.allocator = allocator
|
|
self.limit = limit
|
|
self.limit = limit
|
|
self.resize(size, init=True)
|
|
self.resize(size, init=True)
|
|
|
|
|
|
def __len__(self):
|
|
def __len__(self):
|
|
- return len(self._thread_local.buffer)
|
|
|
|
|
|
+ return len(self.buffer)
|
|
|
|
|
|
def resize(self, size, init=False):
|
|
def resize(self, size, init=False):
|
|
"""
|
|
"""
|
|
@@ -956,7 +955,7 @@ class Buffer:
|
|
if self.limit is not None and size > self.limit:
|
|
if self.limit is not None and size > self.limit:
|
|
raise Buffer.MemoryLimitExceeded(size, self.limit)
|
|
raise Buffer.MemoryLimitExceeded(size, self.limit)
|
|
if init or len(self) < size:
|
|
if init or len(self) < size:
|
|
- self._thread_local.buffer = self.allocator(size)
|
|
|
|
|
|
+ self.buffer = self.allocator(size)
|
|
|
|
|
|
def get(self, size=None, init=False):
|
|
def get(self, size=None, init=False):
|
|
"""
|
|
"""
|
|
@@ -965,7 +964,7 @@ class Buffer:
|
|
"""
|
|
"""
|
|
if size is not None:
|
|
if size is not None:
|
|
self.resize(size, init)
|
|
self.resize(size, init)
|
|
- return self._thread_local.buffer
|
|
|
|
|
|
+ return self.buffer
|
|
|
|
|
|
|
|
|
|
@memoize
|
|
@memoize
|