Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Copyright (c) 2017 DIVIDE-Studio
Copyright (c) 2009 Ionut Cava

This file is part of DIVIDE Framework.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software
and associated documentation files (the "Software"), to deal in the Software
without restriction,
including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

#ifndef _MEMORY_TRACKER_H_
#define _MEMORY_TRACKER_H_

#include "Platform/Headers/PlatformDefines.h"
#include "Core/Headers/StringHelper.h"

namespace Divide {
// not totally compatible with smart pointers. Oh well ... -Ionut
namespace MemoryManager {
/// From https://www.relisoft.com/book/tech/9new.html
class MemoryTracker {
   private:
    class Entry {
       public:
        Entry(char const* file, size_t line, size_t size)
            : _file(file),
              _line(line),
              _size(size)
        {
        }

        Entry()
            : Entry("", 0, 0)
        {
        }

        inline char const* File() const {
            return _file;
        }

        inline size_t Line() const {
            return _line;
        }

        inline size_t Size() const {
            return _size;
        }

       private:
        char const* _file;
        size_t _line;
        size_t _size;
    };

    class Lock {
       public:
        Lock(MemoryTracker& tracer) 
            : _tracer(tracer)
        {
            _tracer.lock();
        }
        ~Lock()
        {
            _tracer.unlock();
        }

       private:
        MemoryTracker& _tracer;
    };

    typedef hashMapImplBest<void*, Entry>::iterator iterator;
    typedef hashMapImplBest<void*, Entry>::const_iterator citerator;
    friend class Lock;

   public:
    MemoryTracker()
        : _locked(false)
    {
        Ready = true;
    }

    ~MemoryTracker()
    {
        Ready = false;
        bool leakDetected = false;
        size_t sizeLeaked = 0;
        Dump(leakDetected, sizeLeaked);
    }

    inline void Add(void* p, size_t size, char const* file, size_t line) {
        if (!_locked) {
            WriteLock w_lock(_mutex);
            MemoryTracker::Lock lock(*this);
            hashAlg::emplace(_map, p, Entry(file, line, size));
        }
    }

    inline void Remove(void* p) {
        if (!_locked) {
            if (!MemoryTracker::LogAllAllocations) {
                WriteLock w_lock(_mutex);
                MemoryTracker::Lock lock(*this);
                iterator it = _map.find(p);
                if (it != std::cend(_map)) {
                    _map.erase(it);
                } else {
                    assert(false && "Called custom delete on non-custom allocated pointer!");
                }
            }
        }
    }

    inline stringImpl Dump(bool& leakDetected, size_t& sizeLeaked) {
        stringImpl output;
        sizeLeaked = 0;
        leakDetected = false;

        MemoryTracker::Lock lock(*this);
        WriteLock w_lock(_mutex);
        if (!_map.empty()) {
            stringImpl msg = "";
            if (MemoryTracker::LogAllAllocations) {
                msg = "memory allocations detected";
            } else {
                msg = "memory leaks detected";
            }
            output.append(Util::StringFormat("%d %s\n", _map.size(), msg.c_str()));
            size_t totalUsage = 0;
            for (citerator it = std::cbegin(_map); 
                           it != std::cend(_map);
                           ++it)
            {
                const Entry& entry = it->second;
                size_t crtSize = entry.Size();
                totalUsage += crtSize;

                output.append(entry.File());
                output.append(", ");
                output.append(to_stringImpl(entry.Line()).c_str());
                output.append(Util::StringFormat("( %d bytes / %d bytes (%5.2f Mb)) \n", crtSize, totalUsage, (totalUsage / 1024.0f) / 1024));
                sizeLeaked += entry.Size();
            }
            leakDetected = true;
            _map.clear();
        }
        if (!MemoryTracker::LogAllAllocations && sizeLeaked > 0) {
            output.append("Total leaked bytes: ");
            output.append(to_stringImpl(sizeLeaked).c_str());
            output.append("\n");
        } 
        return output;
    }

    static bool Ready;
    static bool LogAllAllocations;

   private:
    inline void lock() {
        _locked = true;
    }

    inline void unlock() {
        _locked = false;
    }

   private:
    mutable SharedLock _mutex;
    hashMapImplBest<void*, Entry> _map;
    std::atomic_bool _locked;
};

extern MemoryTracker AllocTracer;
};  // namespace MemoryManager
};  // namespace Divide
#endif  //_MEMORY_TRACKER_H_

Commits for Divide-Framework/trunk/Source Code/Utility/Headers/MemoryTracker.h

Diff revisions: vs.
Revision Author Commited Message
836 Diff Diff IonutCava picture IonutCava Fri 27 Jan, 2017 14:59:56 +0000

[IonutCava]
- Update copyright notice

833 Diff Diff IonutCava picture IonutCava Thu 26 Jan, 2017 22:04:33 +0000

[IonutCava]
- Fix most build errors. Divide-Server does not build for now.

830 Diff Diff IonutCava picture IonutCava Wed 25 Jan, 2017 17:29:44 +0000

[IonutCava]
- Add a new Platform element: File
— Move all predefined paths to FileManagement System
- Split MathHelper into MathHelper and StringHelper

788 Diff Diff IonutCava picture IonutCava Fri 21 Oct, 2016 16:11:37 +0000

[IonutCava]
- Added support for the Arena Allocator by Mamasha Knows (http://www.codeproject.com/Articles/44850/Arena-Allocator-DTOR-and-Embedded-Preallocated-Buf)
— Used for GFX Related objects: Textures, shaders, etc

719 Diff Diff IonutCava picture IonutCava Fri 03 Jun, 2016 16:20:08 +0000

[IonutCava]
- More SceneManager – ScenePool splitting of responsibilities
- DefaultScene will be the first scene loaded by the application. If nothing is specified in XML, it will default to "DefaultScene"

709 Diff Diff IonutCava picture IonutCava Thu 19 May, 2016 16:21:46 +0000

[IonutCava]
- Massive rewrite of the resource system:
— ResourceCache now uses a map of weak pointers and passes shared pointers to the call point of CreateResource
— RemoveResource is no longer needed, but resource based smart pointers use a custom deleter that calls unload()
— Keep a shared_ptr of the resource in the entity that needs it and pass shared_ptr references from getter methods
-— All of this removed the need for calls to RemoveResource, REGISTER_TRACKED_DEPENDENCY and UNREGISTER_TRACKED_DEPENDENCY reducing the number of mutex locks and atomic exchanges
- Singleton cleanup:
— Removed ShaderManager singleton and merged it’s data and responsibilities in static methods in both ShaderProgram and Shader classes.
Explanation: Due to the complex interdependency chains in the system, copying, updating, tracking the cache in a thread safe way became a lot more slower than a straight forward smart pointer based implementation. (e.g. scene graph nodes have 3d objects and material instances, 3d objects have materials, materials have textures and shaders, etc)

691 Diff Diff IonutCava picture IonutCava Tue 26 Apr, 2016 16:14:43 +0000

[IonutCava]
- Cleanup hash map template code (and allow creation of hash maps using the default allocator)
- Speedup text label multi-line parsing (now done on setText instead of drawText)
- Fix some reflection-related material crashes

674 Diff Diff IonutCava picture IonutCava Mon 11 Apr, 2016 16:17:10 +0000

[IonutCava]
- Some profile guided optimizations:
— Remove the task state hashmap and replace with flat array
— Allow PhysX to load collision meshes from a RAM cache first -> then file -> then recompute
- Improve memory logger output

671 Diff Diff IonutCava picture IonutCava Fri 08 Apr, 2016 16:26:49 +0000

[Ionut]
- Custom memory allocation system part 1:
— Add http://www.codeproject.com/Articles/1089905/A-Custom-STL-std-allocator-Replacement-Improves-Pe
— Add custom allocators to strings and vectors
— Add custom allocators to a few test classes
— No support for aligned memory yet, so vec4<F32> and mat4<F32> vectors use the old allocation system
- Replace more std::async call with our custom task system

658 IonutCava picture IonutCava Tue 15 Mar, 2016 21:51:58 +0000

[IonutCava]
- Unify variable argument string formatting (GUI now uses the Util::StringFormat method)
— Console does not use the general purpose formatting for performance reasons (uses internally statically allocated buffer instead)
- Re-enabled allocation tracking in debug builds
— Added (and enabled) option to log all memory allocations, not just leaks.
- Added a macro that returns the current function signature (on all platforms)
- Fixed minor 3 memory leaks