MemoryMapAgent.cc

Go to the documentation of this file.
00001 /*
00002  * This file is part of openSDK.
00003  *
00004  * Copyright (C) 2006-2007 openSDK team
00005  *
00006  * openSDK is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; version 2.
00009  *
00010  * openSDK is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with openSDK; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  *     $Id: MemoryMapAgent.cc,v 1.6 2007/01/26 23:12:05 nuno-lopes Exp $
00020  */
00021 
00022 #include <MemoryMapAgent.h>
00023 #include <pthread.h>
00024 
00025 /*
00026         This file implements the database that maps the Memory Region IDs into real addresses
00027         It shouldn't be used for non-internals stuff
00028 */
00029 
00030 #define LOCK() pthread_mutex_lock(&mutex);
00031 #define UNLOCK() pthread_mutex_unlock(&mutex);
00032 
00033 static MemoryRegionID nextMemID = 0;
00034 static map<MemoryRegionID, void*> memIDsDB;
00035 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
00036 
00037 
00038 // temporary implement these functions, as they are automatically called form OSubject and OObject
00039 MemoryMapAgent::MemoryMapAgent() {}
00040 MemoryMapAgent::~MemoryMapAgent() {}
00041 
00042 
00045 OStatus GetMemID(MemoryRegionID memID, void** address)
00046 {
00047         map<MemoryRegionID, void*>::iterator it;
00048 
00049         LOCK();
00050         it = memIDsDB.find(memID);
00051 
00052         // memID not found
00053         if (it == memIDsDB.end()) {
00054                 UNLOCK();
00055                 return oFAIL;
00056         }
00057 
00058         *address = it->second;
00059         UNLOCK();
00060 
00061         return oSUCCESS;
00062 }
00063 
00064 
00067 OStatus FreeMemId(MemoryRegionID memID)
00068 {
00069         map<MemoryRegionID, void*>::iterator it;
00070 
00071         LOCK();
00072         it = memIDsDB.find(memID);
00073 
00074         // memID not found
00075         if (it == memIDsDB.end()) {
00076                 UNLOCK();
00077                 return oFAIL;
00078         }
00079 
00080         void *ptr = it->second;
00081         memIDsDB.erase(it);
00082         UNLOCK();
00083 
00084         free(ptr);
00085 
00086         return oSUCCESS;
00087 }
00088 
00089 
00091 MemoryRegionID RegisterNewMemID(void* addr)
00092 {
00093         LOCK();
00094         MemoryRegionID id = nextMemID++;
00095         memIDsDB[id] = addr;
00096         UNLOCK();
00097 
00098         return id;
00099 }

Generated on Sun Dec 2 23:04:29 2007 for openSDK by  doxygen 1.3.9.1