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 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 
produce(const sys::fs::Path & pPath,FileHandle::OpenMode pMode)25 MemoryArea* MemoryAreaFactory::produce(const sys::fs::Path& pPath,
26                                        FileHandle::OpenMode pMode) {
27   llvm::StringRef name(pPath.native());
28   if (m_AreaMap.find(name) == m_AreaMap.end()) {
29     MemoryArea* result = allocate();
30     new (result) MemoryArea(name);
31     m_AreaMap[name] = result;
32     return result;
33   }
34 
35   return m_AreaMap[name];
36 }
37 
produce(const sys::fs::Path & pPath,FileHandle::OpenMode pMode,FileHandle::Permission pPerm)38 MemoryArea* MemoryAreaFactory::produce(const sys::fs::Path& pPath,
39                                        FileHandle::OpenMode pMode,
40                                        FileHandle::Permission pPerm) {
41   llvm::StringRef name(pPath.native());
42   if (m_AreaMap.find(name) == m_AreaMap.end()) {
43     MemoryArea* result = allocate();
44     new (result) MemoryArea(name);
45     m_AreaMap[name] = result;
46     return result;
47   }
48 
49   return m_AreaMap[name];
50 }
51 
produce(void * pMemBuffer,size_t pSize)52 MemoryArea* MemoryAreaFactory::produce(void* pMemBuffer, size_t pSize) {
53   const char* base = reinterpret_cast<const char*>(pMemBuffer);
54   llvm::StringRef name(base, pSize);
55   if (m_AreaMap.find(name) == m_AreaMap.end()) {
56     MemoryArea* result = allocate();
57     new (result) MemoryArea(base, pSize);
58     m_AreaMap[name] = result;
59     return result;
60   }
61 
62   return m_AreaMap[name];
63 }
64 
produce(int pFD,FileHandle::OpenMode pMode)65 MemoryArea* MemoryAreaFactory::produce(int pFD, FileHandle::OpenMode pMode) {
66   // TODO
67   return NULL;
68 }
69 
destruct(MemoryArea * pArea)70 void MemoryAreaFactory::destruct(MemoryArea* pArea) {
71   destroy(pArea);
72   deallocate(pArea);
73 }
74 
75 }  // namespace mcld
76