* The ChunkCache is now using the real time memory functions.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34420 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2009-12-01 17:22:28 +00:00
parent ccedee2295
commit ebe97b7cee
4 changed files with 31 additions and 21 deletions

View File

@ -24,7 +24,7 @@ chunk_buffer::chunk_buffer()
chunk_buffer::~chunk_buffer() chunk_buffer::~chunk_buffer()
{ {
free(buffer); rtm_free(buffer);
} }
@ -34,23 +34,26 @@ chunk_buffer::~chunk_buffer()
ChunkCache::ChunkCache(sem_id waitSem, size_t maxBytes) ChunkCache::ChunkCache(sem_id waitSem, size_t maxBytes)
: :
BLocker("media chunk cache"), BLocker("media chunk cache"),
fWaitSem(waitSem), fWaitSem(waitSem)
fMaxBytes(maxBytes),
fBytes(0)
{ {
rtm_create_pool(&fRealTimePool, maxBytes, "media chunk cache");
fMaxBytes = rtm_available(fRealTimePool);
} }
ChunkCache::~ChunkCache() ChunkCache::~ChunkCache()
{ {
while (chunk_buffer* chunk = fChunks.RemoveHead()) rtm_delete_pool(fRealTimePool);
delete chunk; }
while (chunk_buffer* chunk = fUnusedChunks.RemoveHead())
delete chunk;
while (chunk_buffer* chunk = fInFlightChunks.RemoveHead()) status_t
delete chunk; ChunkCache::InitCheck() const
{
if (fRealTimePool == NULL)
return B_NO_MEMORY;
return B_OK;
} }
@ -60,7 +63,6 @@ ChunkCache::MakeEmpty()
ASSERT(IsLocked()); ASSERT(IsLocked());
fUnusedChunks.MoveFrom(&fChunks); fUnusedChunks.MoveFrom(&fChunks);
fBytes = 0;
release_sem(fWaitSem); release_sem(fWaitSem);
} }
@ -71,7 +73,7 @@ ChunkCache::SpaceLeft() const
{ {
ASSERT(IsLocked()); ASSERT(IsLocked());
return fBytes < fMaxBytes; return sizeof(chunk_buffer) + 2048 < rtm_available(fRealTimePool);
} }
@ -82,7 +84,6 @@ ChunkCache::NextChunk()
chunk_buffer* chunk = fChunks.RemoveHead(); chunk_buffer* chunk = fChunks.RemoveHead();
if (chunk != NULL) { if (chunk != NULL) {
fBytes -= chunk->capacity;
fInFlightChunks.Add(chunk); fInFlightChunks.Add(chunk);
release_sem(fWaitSem); release_sem(fWaitSem);
} }
@ -113,10 +114,11 @@ ChunkCache::ReadNextChunk(Reader* reader, void* cookie)
chunk_buffer* chunk = fUnusedChunks.RemoveHead(); chunk_buffer* chunk = fUnusedChunks.RemoveHead();
if (chunk == NULL) { if (chunk == NULL) {
// allocate a new one // allocate a new one
chunk = new(std::nothrow) chunk_buffer; chunk = (chunk_buffer*)rtm_alloc(fRealTimePool, sizeof(chunk_buffer));
if (chunk == NULL) if (chunk == NULL)
return false; return false;
new(chunk) chunk_buffer;
} }
const void* buffer; const void* buffer;
@ -126,18 +128,17 @@ ChunkCache::ReadNextChunk(Reader* reader, void* cookie)
if (chunk->status == B_OK) { if (chunk->status == B_OK) {
if (chunk->capacity < bufferSize) { if (chunk->capacity < bufferSize) {
// adapt buffer size // adapt buffer size
free(chunk->buffer); rtm_free(chunk->buffer);
chunk->capacity = (bufferSize + 2047) & ~2047; chunk->capacity = (bufferSize + 2047) & ~2047;
chunk->buffer = malloc(chunk->capacity); chunk->buffer = rtm_alloc(fRealTimePool, chunk->capacity);
if (chunk->buffer == NULL) { if (chunk->buffer == NULL) {
delete chunk; rtm_free(chunk);
return false; return false;
} }
} }
memcpy(chunk->buffer, buffer, bufferSize); memcpy(chunk->buffer, buffer, bufferSize);
chunk->size = bufferSize; chunk->size = bufferSize;
fBytes += chunk->capacity;
} }
fChunks.Add(chunk); fChunks.Add(chunk);

View File

@ -8,10 +8,12 @@
#include <Locker.h> #include <Locker.h>
#include <MediaDefs.h> #include <MediaDefs.h>
#include "ReaderPlugin.h" #include <RealtimeAlloc.h>
#include <kernel/util/DoublyLinkedList.h> #include <kernel/util/DoublyLinkedList.h>
#include "ReaderPlugin.h"
namespace BPrivate { namespace BPrivate {
namespace media { namespace media {
@ -37,6 +39,8 @@ public:
ChunkCache(sem_id waitSem, size_t maxBytes); ChunkCache(sem_id waitSem, size_t maxBytes);
~ChunkCache(); ~ChunkCache();
status_t InitCheck() const;
void MakeEmpty(); void MakeEmpty();
bool SpaceLeft() const; bool SpaceLeft() const;
@ -45,9 +49,9 @@ public:
bool ReadNextChunk(Reader* reader, void* cookie); bool ReadNextChunk(Reader* reader, void* cookie);
private: private:
rtm_pool* fRealTimePool;
sem_id fWaitSem; sem_id fWaitSem;
size_t fMaxBytes; size_t fMaxBytes;
size_t fBytes;
ChunkList fChunks; ChunkList fChunks;
ChunkList fUnusedChunks; ChunkList fUnusedChunks;
ChunkList fInFlightChunks; ChunkList fInFlightChunks;

View File

@ -73,7 +73,7 @@ SharedLibrary libmedia.so :
OldMediaModule.cpp OldMediaModule.cpp
OldSoundFile.cpp OldSoundFile.cpp
OldSubscriber.cpp OldSubscriber.cpp
# Codec Plugin API # Codec Plugin API
ChunkCache.cpp ChunkCache.cpp
DecoderPlugin.cpp DecoderPlugin.cpp

View File

@ -90,6 +90,11 @@ MediaExtractor::MediaExtractor(BDataIO* source, int32 flags)
fStreamInfo[i].lastChunk = NULL; fStreamInfo[i].lastChunk = NULL;
memset(&fStreamInfo[i].encodedFormat, 0, memset(&fStreamInfo[i].encodedFormat, 0,
sizeof(fStreamInfo[i].encodedFormat)); sizeof(fStreamInfo[i].encodedFormat));
if (fStreamInfo[i].chunkCache->InitCheck() != B_OK) {
fInitStatus = B_NO_MEMORY;
return;
}
} }
// create all stream cookies // create all stream cookies