少年修仙传客户端基础资源
hch
2024-04-11 4c71d74b77c9eb62a0323698c9a0db3b641a917e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "il2cpp-config.h"
#include "MarshalAlloc.h"
#include "os/MarshalAlloc.h"
#include "os/ThreadLocalValue.h"
#include "vm/Exception.h"
#include <deque>
 
namespace il2cpp
{
namespace vm
{
#if _DEBUG
    static os::ThreadLocalValue s_Allocations;
 
    static os::FastMutex s_AllocationStorageMutex;
    static std::deque<std::vector<std::map<void*, size_t> > > s_AllocationStorage;
 
    static std::vector<std::map<void*, size_t> >& GetAllocationsForCurrentThread()
    {
        std::vector<std::map<void*, size_t> >* ptr = NULL;
        s_Allocations.GetValue(reinterpret_cast<void**>(&ptr));
        if (ptr == NULL)
        {
            os::FastAutoLock lock(&s_AllocationStorageMutex);
            s_AllocationStorage.push_back(std::vector<std::map<void*, size_t> >());
            ptr = &s_AllocationStorage.back();
            s_Allocations.SetValue(ptr);
        }
 
        return *ptr;
    }
 
    static std::map<void*, size_t>* GetAllocationsForCurrentFrame()
    {
        std::vector<std::map<void*, size_t> >& currentThreadAllocations = GetAllocationsForCurrentThread();
        if (currentThreadAllocations.size() > 0)
            return &currentThreadAllocations.back();
 
        return NULL;
    }
 
#endif
 
    void* MarshalAlloc::Allocate(size_t size)
    {
        void* ptr = os::MarshalAlloc::Allocate(size);
 
#if _DEBUG
        std::map<void*, size_t>* allocations = GetAllocationsForCurrentFrame();
        if (allocations != NULL)
            (*allocations)[ptr] = size;
#endif
 
        return ptr;
    }
 
    void* MarshalAlloc::ReAlloc(void* ptr, size_t size)
    {
        void* realloced = os::MarshalAlloc::ReAlloc(ptr, size);
 
#if _DEBUG
        std::map<void*, size_t>* allocations = GetAllocationsForCurrentFrame();
        if (allocations != NULL)
        {
            if (ptr != NULL && ptr != realloced)
            {
                std::map<void*, size_t>::iterator found = allocations->find(ptr);
                IL2CPP_ASSERT(found != allocations->end() && "Invalid call to MarshalAlloc::ReAlloc. The pointer is not in the allocation list.");
                allocations->erase(found);
            }
 
            (*allocations)[realloced] = size;
        }
#endif
 
        return realloced;
    }
 
    void MarshalAlloc::Free(void* ptr)
    {
#if _DEBUG
        std::map<void*, size_t>* allocations = GetAllocationsForCurrentFrame();
        if (allocations != NULL)
        {
            std::map<void*, size_t>::iterator found = allocations->find(ptr);
            if (found != allocations->end()) // It might not be necessarily allocated by us, e.g. we might be freeing memory that's returned from native P/Invoke call
                allocations->erase(found);
        }
#endif
 
        os::MarshalAlloc::Free(ptr);
    }
 
    void* MarshalAlloc::AllocateHGlobal(size_t size)
    {
        return os::MarshalAlloc::AllocateHGlobal(size);
    }
 
    void* MarshalAlloc::ReAllocHGlobal(void* ptr, size_t size)
    {
        return os::MarshalAlloc::ReAllocHGlobal(ptr, size);
    }
 
    void MarshalAlloc::FreeHGlobal(void* ptr)
    {
        os::MarshalAlloc::FreeHGlobal(ptr);
    }
 
#if _DEBUG
 
    void MarshalAlloc::PushAllocationFrame()
    {
        GetAllocationsForCurrentThread().push_back(std::map<void*, size_t>());
    }
 
    void MarshalAlloc::PopAllocationFrame()
    {
        GetAllocationsForCurrentThread().pop_back();
    }
 
    bool MarshalAlloc::HasUnfreedAllocations()
    {
        std::map<void*, size_t>* allocations = GetAllocationsForCurrentFrame();
        return allocations != NULL && allocations->size() > 0;
    }
 
    void MarshalAlloc::ClearAllTrackedAllocations()
    {
        std::map<void*, size_t>* allocations = GetAllocationsForCurrentFrame();
        if (allocations != NULL)
            allocations->clear();
    }
 
#endif
} /* namespace vm */
} /* namespace il2cpp */