vfs: check userland buffers before reading them.
[haiku.git] / src / kits / media / BufferCache.cpp
blob02ea7213f2651e5aae9c1d0bcff4c2c85880a744
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 */
8 //! A cache for BBuffers to be received by BBufferConsumer::BufferReceived().
11 #include "BufferCache.h"
13 #include <Buffer.h>
15 #include "debug.h"
18 namespace BPrivate {
21 BufferCache::BufferCache()
26 BufferCache::~BufferCache()
28 for (BufferMap::iterator iterator = fMap.begin(); iterator != fMap.end();
29 iterator++) {
30 delete iterator->second;
35 BBuffer*
36 BufferCache::GetBuffer(media_buffer_id id)
38 if (id <= 0)
39 return NULL;
41 BufferMap::iterator found = fMap.find(id);
42 if (found != fMap.end())
43 return found->second;
45 buffer_clone_info info;
46 info.buffer = id;
47 BBuffer* buffer = new(std::nothrow) BBuffer(info);
48 if (buffer == NULL || buffer->ID() <= 0
49 || buffer->Data() == NULL) {
50 delete buffer;
51 return NULL;
54 if (buffer->ID() != id)
55 debugger("BufferCache::GetBuffer: IDs mismatch");
57 try {
58 fMap.insert(std::make_pair(id, buffer));
59 } catch (std::bad_alloc& exception) {
60 delete buffer;
61 return NULL;
64 return buffer;
68 } // namespace BPrivate