1 //===- MemoryAreaFactory.cpp ----------------------------------------------===//
2 //
3 //                     The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include <mcld/Support/MemoryAreaFactory.h>
10 #include <mcld/Support/MsgHandling.h>
11 #include <mcld/Support/SystemUtils.h>
12 
13 using namespace mcld;
14 
15 //===----------------------------------------------------------------------===//
16 // MemoryAreaFactory
17 //===----------------------------------------------------------------------===//
MemoryAreaFactory(size_t pNum)18 MemoryAreaFactory::MemoryAreaFactory(size_t pNum)
19   : GCFactory<MemoryArea, 0>(pNum) {
20 }
21 
~MemoryAreaFactory()22 MemoryAreaFactory::~MemoryAreaFactory()
23 {
24 }
25 
produce(const sys::fs::Path & pPath,FileHandle::OpenMode pMode)26 MemoryArea* MemoryAreaFactory::produce(const sys::fs::Path& pPath,
27                                        FileHandle::OpenMode pMode)
28 {
29   llvm::StringRef name(pPath.native());
30   if (m_AreaMap.find(name) == m_AreaMap.end()) {
31     MemoryArea* result = allocate();
32     new (result) MemoryArea(name);
33     m_AreaMap[name] = result;
34     return result;
35   }
36 
37   return m_AreaMap[name];
38 }
39 
produce(const sys::fs::Path & pPath,FileHandle::OpenMode pMode,FileHandle::Permission pPerm)40 MemoryArea* MemoryAreaFactory::produce(const sys::fs::Path& pPath,
41                                        FileHandle::OpenMode pMode,
42                                        FileHandle::Permission pPerm)
43 {
44   llvm::StringRef name(pPath.native());
45   if (m_AreaMap.find(name) == m_AreaMap.end()) {
46     MemoryArea* result = allocate();
47     new (result) MemoryArea(name);
48     m_AreaMap[name] = result;
49     return result;
50   }
51 
52   return m_AreaMap[name];
53 }
54 
produce(void * pMemBuffer,size_t pSize)55 MemoryArea* MemoryAreaFactory::produce(void* pMemBuffer, size_t pSize)
56 {
57   const char* base = reinterpret_cast<const char*>(pMemBuffer);
58   llvm::StringRef name(base, pSize);
59   if (m_AreaMap.find(name) == m_AreaMap.end()) {
60     MemoryArea* result = allocate();
61     new (result) MemoryArea(base, pSize);
62     m_AreaMap[name] = result;
63     return result;
64   }
65 
66   return m_AreaMap[name];
67 }
68 
produce(int pFD,FileHandle::OpenMode pMode)69 MemoryArea* MemoryAreaFactory::produce(int pFD, FileHandle::OpenMode pMode)
70 {
71   // TODO
72   return NULL;
73 }
74 
destruct(MemoryArea * pArea)75 void MemoryAreaFactory::destruct(MemoryArea* pArea)
76 {
77   destroy(pArea);
78   deallocate(pArea);
79 }
80