1 /* 2 * Copyright 2019, Ryan Leavengood. 3 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de. 4 * Copyright 2002, Marcus Overhagen. All Rights Reserved. 5 * Distributed under the terms of the MIT License. 6 */ 7 8 9 //! A cache for BBuffers to be received by BBufferConsumer::BufferReceived(). 10 11 12 #include "BufferCache.h" 13 14 #include <Buffer.h> 15 16 #include "MediaDebug.h" 17 18 19 namespace BPrivate { 20 21 22 BufferCache::BufferCache() 23 { 24 } 25 26 27 BufferCache::~BufferCache() 28 { 29 BufferMap::Iterator iterator = fMap.GetIterator(); 30 while (iterator.HasNext()) { 31 BufferMap::Entry entry = iterator.Next(); 32 delete entry.value.buffer; 33 } 34 } 35 36 37 BBuffer* 38 BufferCache::GetBuffer(media_buffer_id id, port_id port) 39 { 40 if (id <= 0) 41 return NULL; 42 43 buffer_cache_entry* existing; 44 if (fMap.Get(id, existing)) 45 return existing->buffer; 46 47 buffer_clone_info info; 48 info.buffer = id; 49 BBuffer* buffer = new(std::nothrow) BBuffer(info); 50 if (buffer == NULL || buffer->ID() <= 0 51 || buffer->Data() == NULL) { 52 delete buffer; 53 return NULL; 54 } 55 56 if (buffer->ID() != id) 57 debugger("BufferCache::GetBuffer: IDs mismatch"); 58 59 buffer_cache_entry entry; 60 entry.buffer = buffer; 61 entry.port = port; 62 status_t error = fMap.Put(id, entry); 63 if (error != B_OK) { 64 delete buffer; 65 return NULL; 66 } 67 68 return buffer; 69 } 70 71 72 void 73 BufferCache::FlushCacheForPort(port_id port) 74 { 75 BufferMap::Iterator iterator = fMap.GetIterator(); 76 while (iterator.HasNext()) { 77 BufferMap::Entry entry = iterator.Next(); 78 if (entry.value.port == port) { 79 // Delete the buffer 80 delete entry.value.buffer; 81 // Then remove it from the map 82 fMap.Remove(iterator); 83 } 84 } 85 } 86 87 88 } // namespace BPrivate 89