xref: /haiku/src/kits/media/BufferCache.cpp (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
1 /*
2  * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3  * Copyright 2002, Marcus Overhagen. All Rights Reserved.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 //! A cache for BBuffers to be received by BBufferConsumer::BufferReceived().
9 
10 
11 #include "BufferCache.h"
12 
13 #include <Buffer.h>
14 
15 #include "debug.h"
16 
17 
18 namespace BPrivate {
19 
20 
21 BufferCache::BufferCache()
22 {
23 }
24 
25 
26 BufferCache::~BufferCache()
27 {
28 	for (BufferMap::iterator iterator = fMap.begin(); iterator != fMap.end();
29 			iterator++) {
30 		delete iterator->second;
31 	}
32 }
33 
34 
35 BBuffer*
36 BufferCache::GetBuffer(media_buffer_id id)
37 {
38 	BufferMap::iterator found = fMap.find(id);
39 	if (found != fMap.end())
40 		return found->second;
41 
42 	buffer_clone_info info;
43 	info.buffer = id;
44 	BBuffer* buffer = new(std::nothrow) BBuffer(info);
45 	if (buffer == NULL)
46 		return NULL;
47 
48 	try {
49 		fMap.insert(std::make_pair(id, buffer));
50 	} catch (std::bad_alloc& exception) {
51 		delete buffer;
52 		return NULL;
53 	}
54 
55 	return buffer;
56 }
57 
58 
59 }	// namespace BPrivate
60