File tree Expand file tree Collapse file tree 6 files changed +103
-34
lines changed Expand file tree Collapse file tree 6 files changed +103
-34
lines changed Original file line number Diff line number Diff line change @@ -159,18 +159,19 @@ SOURCES = \
159
159
constants.cpp \
160
160
context.cpp \
161
161
cssize.cpp \
162
- listize .cpp \
162
+ emitter .cpp \
163
163
error_handling.cpp \
164
164
eval.cpp \
165
165
expand.cpp \
166
166
extend.cpp \
167
167
file.cpp \
168
168
functions.cpp \
169
169
inspect.cpp \
170
+ json.cpp \
170
171
lexer.cpp \
172
+ listize.cpp \
173
+ memory_manager.cpp \
171
174
node.cpp \
172
- json.cpp \
173
- emitter.cpp \
174
175
output.cpp \
175
176
parser.cpp \
176
177
plugins.cpp \
@@ -308,4 +309,4 @@ lib-opts-shared:
308
309
install install-static install-shared \
309
310
lib-opts lib-opts-shared lib-opts-static \
310
311
lib-file lib-file-shared lib-file-static
311
- .DELETE_ON_ERROR :
312
+ .DELETE_ON_ERROR :
Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ lib_LTLIBRARIES = libsass.la
31
31
32
32
libsass_la_SOURCES = \
33
33
ast_fwd_decl.hpp ast_def_macros.hpp \
34
- kwd_arg_macros.hpp memory_manager.hpp \
34
+ kwd_arg_macros.hpp \
35
35
position.cpp position.hpp \
36
36
operation.hpp \
37
37
subset_map.hpp mapping.hpp \
@@ -56,6 +56,7 @@ libsass_la_SOURCES = \
56
56
functions.cpp functions.hpp \
57
57
inspect.cpp inspect.hpp \
58
58
lexer.cpp lexer.hpp \
59
+ memory_manager.cpp memory_manager.hpp \
59
60
node.cpp node.hpp \
60
61
json.cpp json.hpp \
61
62
emitter.cpp emitter.hpp \
Original file line number Diff line number Diff line change
1
+ #include " ast.hpp"
2
+ #include " memory_manager.hpp"
3
+
4
+ namespace Sass {
5
+ using namespace std ;
6
+
7
+ template <typename T>
8
+ Memory_Manager<T>::Memory_Manager(size_t size)
9
+ : nodes(vector<T*>())
10
+ {
11
+ size_t init = size;
12
+ if (init < 8 ) init = 8 ;
13
+ // reserve some space
14
+ nodes.reserve (init);
15
+ }
16
+
17
+ template <typename T>
18
+ Memory_Manager<T>::~Memory_Manager ()
19
+ {
20
+ // release memory for all controlled nodes
21
+ // avoid calling erase for every single node
22
+ for (size_t i = 0 , S = nodes.size (); i < S; ++i) {
23
+ deallocate (nodes[i]);
24
+ }
25
+ // just in case
26
+ nodes.clear ();
27
+ }
28
+
29
+ template <typename T>
30
+ T* Memory_Manager<T>::operator ()(T* np)
31
+ {
32
+ // add to pool
33
+ nodes.push_back (np);
34
+ // return resource
35
+ return np;
36
+ }
37
+
38
+ template <typename T>
39
+ bool Memory_Manager<T>::has(T* np)
40
+ {
41
+ // check if the pointer is controlled under our pool
42
+ return find (nodes.begin (), nodes.end (), np) != nodes.end ();
43
+ }
44
+
45
+ template <typename T>
46
+ T* Memory_Manager<T>::allocate(size_t size)
47
+ {
48
+ return static_cast <T*>(operator new (size));
49
+ }
50
+
51
+ template <typename T>
52
+ void Memory_Manager<T>::deallocate(T* np)
53
+ {
54
+ delete np;
55
+ }
56
+
57
+ template <typename T>
58
+ void Memory_Manager<T>::remove(T* np)
59
+ {
60
+ // remove node from pool (no longer active)
61
+ nodes.erase (find (nodes.begin (), nodes.end (), np));
62
+ // you are now in control of the memory
63
+ }
64
+
65
+ template <typename T>
66
+ void Memory_Manager<T>::destroy(T* np)
67
+ {
68
+ // remove from pool
69
+ remove (np);
70
+ // release memory
71
+ delete np;
72
+ }
73
+
74
+ // compile implementation for AST_Node
75
+ template class Memory_Manager <AST_Node>;
76
+
77
+ }
78
+
Original file line number Diff line number Diff line change @@ -18,40 +18,25 @@ namespace Sass {
18
18
vector<T*> nodes;
19
19
20
20
public:
21
- Memory_Manager (size_t size = 0 ) : nodes(vector<T*>())
22
- { nodes.reserve (size); }
23
-
24
- ~Memory_Manager ()
25
- {
26
- for (size_t i = 0 , S = nodes.size (); i < S; ++i) {
27
- // cout << "deleting " << typeid(*nodes[i]).name() << endl;
28
- delete nodes[i];
29
- }
30
- }
31
-
32
- T* operator ()(T* np)
33
- {
34
- nodes.push_back (np);
35
- // cout << "registering " << typeid(*np).name() << endl;
36
- return np;
37
- }
38
-
39
- void remove (T* np)
40
- {
41
- nodes.erase (find (nodes.begin (), nodes.end (), np));
42
- }
21
+ Memory_Manager (size_t size = 0 );
22
+ ~Memory_Manager ();
23
+
24
+ bool has (T* np);
25
+ T* allocate (size_t size);
26
+ void deallocate (T* np);
27
+ void remove (T* np);
28
+ void destroy (T* np);
29
+ T* operator ()(T* np);
30
+
43
31
};
44
32
}
45
33
46
34
template <typename T>
47
- inline void * operator new (size_t size, Sass::Memory_Manager<T>& mem_mgr )
48
- { return mem_mgr ( static_cast <T*>( operator new ( size)) ); }
35
+ inline void * operator new (size_t size, Sass::Memory_Manager<T>& mem )
36
+ { return mem. allocate ( size); }
49
37
50
38
template <typename T>
51
- inline void operator delete (void *np, Sass::Memory_Manager<T>& mem_mgr)
52
- {
53
- mem_mgr.remove (reinterpret_cast <T*>(np));
54
- operator delete (np);
55
- }
39
+ inline void operator delete (void *np, Sass::Memory_Manager<T>& mem)
40
+ { mem.destroy (reinterpret_cast <T*>(np)); }
56
41
57
42
#endif
Original file line number Diff line number Diff line change 63
63
<ClCompile Include =" ..\inspect.cpp" >
64
64
<Filter >Source Files</Filter >
65
65
</ClCompile >
66
+ <ClCompile Include =" ..\memory_manager.cpp" >
67
+ <Filter >Source Files</Filter >
68
+ </ClCompile >
66
69
<ClCompile Include =" ..\node.cpp" >
67
70
<Filter >Source Files</Filter >
68
71
</ClCompile >
Original file line number Diff line number Diff line change 177
177
<ClCompile Include =" ..\functions.cpp" />
178
178
<ClCompile Include =" ..\inspect.cpp" />
179
179
<ClCompile Include =" ..\json.cpp" />
180
+ <ClCompile Include =" ..\memory_manager.cpp" />
180
181
<ClCompile Include =" ..\node.cpp" />
181
182
<ClCompile Include =" ..\emitter.cpp" />
182
183
<ClCompile Include =" ..\output.cpp" />
You can’t perform that action at this time.
0 commit comments