00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <MemoryMapAgent.h>
00023 #include <pthread.h>
00024
00025
00026
00027
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
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
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
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 }