[Bugfix] Fix allocation & free logic of SingleWriterShmRingBuffer (#27117)
Signed-off-by: Kero Liang <kerorek@outlook.com> Signed-off-by: Roger Wang <hey@rogerw.io> Co-authored-by: donglu <donglu@cohere.com> Co-authored-by: Roger Wang <hey@rogerw.io>
This commit is contained in:
@@ -127,9 +127,7 @@ class SingleWriterShmRingBuffer:
|
||||
|
||||
if create:
|
||||
# we are creating a buffer
|
||||
self.metadata = {
|
||||
self.monotonic_id_end: self.data_buffer_end
|
||||
} # monotonic_id -> start address
|
||||
self.metadata: dict[int, int] = {} # monotonic_id -> start address
|
||||
self.shared_memory = shared_memory.SharedMemory(
|
||||
create=True, size=self.data_buffer_size, name=name
|
||||
)
|
||||
@@ -288,7 +286,15 @@ class SingleWriterShmRingBuffer:
|
||||
self.monotonic_id_start = (
|
||||
self.monotonic_id_start + 1
|
||||
) % self.ID_MAX
|
||||
self.data_buffer_start = address
|
||||
if self.monotonic_id_start in self.metadata:
|
||||
# pointing to the start addr of next allocation
|
||||
self.data_buffer_start += (
|
||||
self.metadata[self.monotonic_id_start]
|
||||
- self.data_buffer_start
|
||||
) % self.data_buffer_size
|
||||
else:
|
||||
# no remaining allocation, reset to zero
|
||||
self.data_buffer_start = self.data_buffer_end = 0
|
||||
freed_bytes += metadata[1]
|
||||
else:
|
||||
# there are still readers, we cannot free the buffer
|
||||
|
||||
Reference in New Issue
Block a user