diff --git a/src/servers/media/BufferManager.cpp b/src/servers/media/BufferManager.cpp index 862a81cbe2..9116139493 100644 --- a/src/servers/media/BufferManager.cpp +++ b/src/servers/media/BufferManager.cpp @@ -2,167 +2,185 @@ * Copyright 2002, Marcus Overhagen. All rights reserved. * Distributed under the terms of the MIT License. */ -#include -#include + + #include "BufferManager.h" -#include "SharedBufferList.h" + +#include + #include "debug.h" +#include "SharedBufferList.h" + BufferManager::BufferManager() - : fSharedBufferList(_shared_buffer_list::Clone()), - fNextBufferId(1), - fLocker(new BLocker("buffer manager locker")), - fBufferInfoMap(new Map) + : + fSharedBufferList(_shared_buffer_list::Clone()), + fNextBufferID(1), + fLocker("buffer manager locker") { - fSharedBufferListId = area_for(fSharedBufferList); - ASSERT(fSharedBufferList!=NULL); - ASSERT(fSharedBufferListId > 0); + fSharedBufferListID = area_for(fSharedBufferList); } + BufferManager::~BufferManager() { fSharedBufferList->Unmap(); - delete fLocker; - delete fBufferInfoMap; } + area_id BufferManager::SharedBufferListID() { - return fSharedBufferListId; + return fSharedBufferListID; } status_t -BufferManager::RegisterBuffer(team_id teamid, media_buffer_id bufferid, - size_t *size, int32 *flags, size_t *offset, area_id *area) +BufferManager::RegisterBuffer(team_id team, media_buffer_id bufferID, + size_t* _size, int32* _flags, size_t* _offset, area_id* _area) { BAutolock lock(fLocker); - TRACE("RegisterBuffer team = %ld, bufferid = %ld\n", teamid, bufferid); - - buffer_info *info; - if (!fBufferInfoMap->Get(bufferid, &info)) { - ERROR("failed to register buffer! team = %ld, bufferid = %ld\n", teamid, bufferid); + TRACE("RegisterBuffer team = %ld, bufferid = %ld\n", team, bufferID); + + buffer_info* info; + if (!fBufferInfoMap.Get(bufferID, &info)) { + ERROR("failed to register buffer! team = %ld, bufferid = %ld\n", team, + bufferID); return B_ERROR; } - - info->teams.Insert(teamid); - - *area = info->area; - *offset = info->offset; - *size = info->size, - *flags = info->flags; + + info->teams.Insert(team); + + *_area = info->area; + *_offset = info->offset; + *_size = info->size, + *_flags = info->flags; + return B_OK; } + status_t -BufferManager::RegisterBuffer(team_id teamid, size_t size, int32 flags, size_t offset, area_id area, - media_buffer_id *bufferid) +BufferManager::RegisterBuffer(team_id team, size_t size, int32 flags, + size_t offset, area_id area, media_buffer_id* _bufferID) { BAutolock lock(fLocker); - TRACE("RegisterBuffer team = %ld, areaid = %ld, offset = %ld, size = %ld\n", teamid, area, offset, size); + TRACE("RegisterBuffer team = %ld, area = %ld, offset = %ld, size = %ld\n", + team, area, offset, size); - void *adr; - area_id newarea; - - newarea = clone_area("media_server cloned buffer", &adr, B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, area); - if (newarea <= B_OK) { - ERROR("RegisterBuffer: failed to clone buffer! error = %#lx, team = %ld, areaid = %ld, offset = %ld, size = %ld\n", newarea, teamid, area, offset, size); + void* address; + area_id clonedArea = clone_area("media_server cloned buffer", &address, + B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, area); + if (clonedArea < 0) { + ERROR("RegisterBuffer: failed to clone buffer! error = %#lx, team = " + "%ld, areaid = %ld, offset = %ld, size = %ld\n", clonedArea, team, + area, offset, size); return B_ERROR; } buffer_info info; - - *bufferid = fNextBufferId; - info.id = fNextBufferId; - info.area = newarea; + info.id = fNextBufferID++; + info.area = clonedArea; info.offset = offset; - info.size = size; - info.flags = flags; - info.teams.Insert(teamid); - fBufferInfoMap->Insert(fNextBufferId, info); + info.size = size; + info.flags = flags; + info.teams.Insert(team); - TRACE("RegisterBuffer: done, bufferid = %ld\n", fNextBufferId); + *_bufferID = info.id; + + fBufferInfoMap.Insert(info.id, info); + + TRACE("RegisterBuffer: done, bufferID = %ld\n", info.id); - fNextBufferId += 1; - return B_OK; } + status_t -BufferManager::UnregisterBuffer(team_id teamid, media_buffer_id bufferid) +BufferManager::UnregisterBuffer(team_id team, media_buffer_id bufferID) { BAutolock lock(fLocker); - TRACE("UnregisterBuffer: team = %ld, bufferid = %ld\n", teamid, bufferid); + TRACE("UnregisterBuffer: team = %ld, bufferid = %ld\n", team, bufferID); - buffer_info *info; - int index; - - if (!fBufferInfoMap->Get(bufferid, &info)) { - ERROR("UnregisterBuffer: failed to unregister buffer! team = %ld, bufferid = %ld\n", teamid, bufferid); + buffer_info* info; + if (!fBufferInfoMap.Get(bufferID, &info)) { + ERROR("UnregisterBuffer: failed to unregister buffer! team = %ld, " + "bufferid = %ld\n", team, bufferID); return B_ERROR; } - index = info->teams.Find(teamid); + int index = info->teams.Find(team); if (index < 0) { - ERROR("UnregisterBuffer: failed to find team = %ld belonging to bufferid = %ld\n", teamid, bufferid); + ERROR("UnregisterBuffer: failed to find team = %ld belonging to " + "bufferID = %ld\n", team, bufferID); return B_ERROR; } if (!info->teams.Remove(index)) { - ERROR("UnregisterBuffer: failed to remove team = %ld from bufferid = %ld\n", teamid, bufferid); + ERROR("UnregisterBuffer: failed to remove team = %ld from bufferID " + "= %ld\n", team, bufferID); return B_ERROR; } - TRACE("UnregisterBuffer: team = %ld removed from bufferid = %ld\n", teamid, bufferid); + + TRACE("UnregisterBuffer: team = %ld removed from bufferID = %ld\n", team, + bufferID); if (info->teams.IsEmpty()) { - - if (!fBufferInfoMap->Remove(bufferid)) { - ERROR("UnregisterBuffer: failed to remove bufferid = %ld\n", bufferid); + if (!fBufferInfoMap.Remove(bufferID)) { + ERROR("UnregisterBuffer: failed to remove bufferID = %ld\n", + bufferID); return B_ERROR; } - - TRACE("UnregisterBuffer: bufferid = %ld removed\n", bufferid); + + TRACE("UnregisterBuffer: bufferID = %ld removed\n", bufferID); } return B_OK; } + void BufferManager::CleanupTeam(team_id team) { BAutolock lock(fLocker); - buffer_info *info; - + TRACE("BufferManager::CleanupTeam: team %ld\n", team); - - for (fBufferInfoMap->Rewind(); fBufferInfoMap->GetNext(&info); ) { - team_id *otherteam; - for (info->teams.Rewind(); info->teams.GetNext(&otherteam); ) { - if (team == *otherteam) { - PRINT(1, "BufferManager::CleanupTeam: removing team %ld from buffer id %ld\n", team, info->id); + + buffer_info* info; + for (fBufferInfoMap.Rewind(); fBufferInfoMap.GetNext(&info); ) { + team_id* currentTeam; + for (info->teams.Rewind(); info->teams.GetNext(¤tTeam); ) { + if (team == *currentTeam) { + PRINT(1, "BufferManager::CleanupTeam: removing team %ld from " + "buffer id %ld\n", team, info->id); info->teams.RemoveCurrent(); } } if (info->teams.IsEmpty()) { - PRINT(1, "BufferManager::CleanupTeam: removing buffer id %ld that has no teams\n", info->id); - fBufferInfoMap->RemoveCurrent(); + PRINT(1, "BufferManager::CleanupTeam: removing buffer id %ld that " + "has no teams\n", info->id); + fBufferInfoMap.RemoveCurrent(); } } } + void BufferManager::Dump() { BAutolock lock(fLocker); - buffer_info *info; + printf("\n"); printf("BufferManager: list of buffers follows:\n"); - for (fBufferInfoMap->Rewind(); fBufferInfoMap->GetNext(&info); ) { - printf(" buffer-id %ld, area-id %ld, offset %ld, size %ld, flags %#08lx\n", - info->id, info->area, info->offset, info->size, info->flags); + + buffer_info *info; + for (fBufferInfoMap.Rewind(); fBufferInfoMap.GetNext(&info); ) { + printf(" buffer-id %ld, area-id %ld, offset %ld, size %ld, flags " + "%#08lx\n", info->id, info->area, info->offset, info->size, + info->flags); printf(" assigned teams: "); - team_id *team; + + team_id* team; for (info->teams.Rewind(); info->teams.GetNext(&team); ) { printf("%ld, ", *team); } diff --git a/src/servers/media/BufferManager.h b/src/servers/media/BufferManager.h index fc7970611c..63c2280974 100644 --- a/src/servers/media/BufferManager.h +++ b/src/servers/media/BufferManager.h @@ -2,46 +2,54 @@ * Copyright 2002, Marcus Overhagen. All rights reserved. * Distributed under the terms of the MIT License. */ -struct _shared_buffer_list; + + +#include +#include #include #include -class BufferManager -{ + +struct _shared_buffer_list; + + +class BufferManager { public: - BufferManager(); - ~BufferManager(); + BufferManager(); + ~BufferManager(); - area_id SharedBufferListID(); + area_id SharedBufferListID(); - status_t RegisterBuffer(team_id teamid, media_buffer_id bufferid, - size_t *size, int32 *flags, size_t *offset, area_id *area); + status_t RegisterBuffer(team_id team, + media_buffer_id bufferID, size_t* _size, + int32* _flags, size_t* _offset, area_id* _area); - status_t RegisterBuffer(team_id teamid, size_t size, int32 flags, size_t offset, area_id area, - media_buffer_id *bufferid); + status_t RegisterBuffer(team_id team, size_t size, + int32 flags, size_t offset, area_id area, + media_buffer_id* _bufferID); - status_t UnregisterBuffer(team_id teamid, media_buffer_id bufferid); - - void CleanupTeam(team_id teamid); + status_t UnregisterBuffer(team_id team, + media_buffer_id bufferID); - void Dump(); + void CleanupTeam(team_id team); + + void Dump(); private: - struct buffer_info - { - media_buffer_id id; - area_id area; - size_t offset; - size_t size; - int32 flags; - List teams; + struct buffer_info { + media_buffer_id id; + area_id area; + size_t offset; + size_t size; + int32 flags; + List teams; }; - - _shared_buffer_list * fSharedBufferList; - area_id fSharedBufferListId; - media_buffer_id fNextBufferId; - BLocker * fLocker; - Map *fBufferInfoMap; + + _shared_buffer_list* fSharedBufferList; + area_id fSharedBufferListID; + media_buffer_id fNextBufferID; + BLocker fLocker; + Map fBufferInfoMap; }; diff --git a/src/servers/media/media_server.cpp b/src/servers/media/media_server.cpp index 7c789b545b..28ab8924aa 100644 --- a/src/servers/media/media_server.cpp +++ b/src/servers/media/media_server.cpp @@ -708,7 +708,7 @@ ServerApp::HandleMessage(int32 code, void *data, size_t size) request->info.offset, request->info.area, &reply.info.buffer); } else { - reply.info = request->info; //buffer id is kept + reply.info = request->info; // buffer id is kept status = gBufferManager->RegisterBuffer(request->team, request->info.buffer, &reply.info.size, &reply.info.flags, &reply.info.offset, &reply.info.area); @@ -720,7 +720,7 @@ ServerApp::HandleMessage(int32 code, void *data, size_t size) case SERVER_UNREGISTER_BUFFER: { const server_unregister_buffer_command *cmd = reinterpret_cast< - const server_unregister_buffer_command *>(data); + const server_unregister_buffer_command *>(data); gBufferManager->UnregisterBuffer(cmd->team, cmd->bufferid); break; @@ -736,7 +736,7 @@ ServerApp::HandleMessage(int32 code, void *data, size_t size) rv = gMMediaFilesManager->RewindTypes( &types, &reply.count); - if(reply.count>0) { + if (reply.count > 0) { // we create an area here, and pass it to the library, // where it will be deleted. char *start_addr; @@ -780,7 +780,7 @@ ServerApp::HandleMessage(int32 code, void *data, size_t size) &items, &reply.count); // we create an area here, and pass it to the library, // where it will be deleted. - if(reply.count>0) { + if (reply.count > 0) { char *start_addr; size_t size = ((reply.count * B_MEDIA_NAME_LENGTH) + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1);