1 #include "../sass.hpp"
2 #include "allocator.hpp"
3 #include "memory_pool.hpp"
4 
5 #if defined (_MSC_VER) // Visual studio
6 #define thread_local __declspec( thread )
7 #elif defined (__GCC__) // GCC
8 #define thread_local __thread
9 #endif
10 
11 namespace Sass {
12 
13 #ifdef SASS_CUSTOM_ALLOCATOR
14 
15   // Only use PODs for thread_local
16   // Objects get unpredictable init order
17   static thread_local MemoryPool* pool;
18   static thread_local size_t allocations;
19 
allocateMem(size_t size)20   void* allocateMem(size_t size)
21   {
22     if (pool == nullptr) {
23       pool = new MemoryPool();
24     }
25     allocations++;
26     return pool->allocate(size);
27   }
28 
deallocateMem(void * ptr,size_t size)29   void deallocateMem(void* ptr, size_t size)
30   {
31 
32     // It seems thread_local variable might be discharged!?
33     // But the destructors of e.g. static strings is still
34     // called, although their memory was discharged too.
35     // Fine with me as long as address sanitizer is happy.
36     if (pool == nullptr || allocations == 0) { return; }
37 
38     pool->deallocate(ptr);
39     if (--allocations == 0) {
40       delete pool;
41       pool = nullptr;
42     }
43 
44   }
45 
46 #endif
47 
48 }
49