Skip to content

Commit 993d5a7

Browse files
committed
Avoid extern lifetime for thread local allocator
*Fingers crossed*
1 parent f05d58e commit 993d5a7

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

src/allocator.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ namespace Sass {
1717

1818
void deallocateMem(void* ptr, size_t size)
1919
{
20+
21+
// It seems thread_local variable might be discharged!?
22+
// But the destructors of e.g. static strings is still
23+
// called, although their memory was discharged too.
24+
// Fine with me as long as address sanitizer is happy.
25+
if (pool == nullptr) { return; }
26+
if (allocations == 0) { return; }
27+
2028
pool->deallocate(ptr);
29+
// ToDo: change to empty()
2130
if (--allocations == 0) {
2231
delete pool;
2332
pool = nullptr;
@@ -27,9 +36,10 @@ namespace Sass {
2736

2837
void* allocateMem(size_t size)
2938
{
30-
if (allocations++ == 0) {
39+
if (pool == nullptr) {
3140
pool = new MemoryPool();
3241
}
42+
allocations++;
3343
return pool->allocate(size);
3444
}
3545

src/error_handling.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,12 @@
88
#include "error_handling.hpp"
99

1010
#include <iostream>
11+
#include <string.h>
1112

1213
namespace Sass {
1314

1415
namespace Exception {
1516

16-
sass::string def_msg("Invalid sass detected");
17-
sass::string def_op_msg("Undefined operation");
18-
sass::string def_op_null_msg("Invalid null operation");
19-
sass::string def_nesting_limit("Code too deeply neested");
20-
21-
sass::string msg_recursion_limit =
22-
"Too deep recursion detected. This can be caused by too deep level nesting.\n"
23-
"LibSass will abort here in order to avoid a possible stack overflow.\n";
24-
2517
Base::Base(SourceSpan pstate, sass::string msg, Backtraces traces)
2618
: std::runtime_error(msg.c_str()), msg(msg),
2719
prefix("Error"), pstate(pstate), traces(traces)
@@ -100,7 +92,7 @@ namespace Sass {
10092
UndefinedOperation::UndefinedOperation(const Expression* lhs, const Expression* rhs, enum Sass_OP op)
10193
: OperationError(), lhs(lhs), rhs(rhs), op(op)
10294
{
103-
msg = def_op_msg + " \"" +
95+
msg = "Undefined operation \"" +
10496
lhs->to_string({ NESTED, 5 }) +
10597
" " + sass_op_separator(op) + " " +
10698
rhs->to_string({ TO_SASS, 5 }) +

src/error_handling.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ namespace Sass {
2121

2222
namespace Exception {
2323

24-
extern sass::string def_msg;
25-
extern sass::string def_op_msg;
26-
extern sass::string def_op_null_msg;
27-
extern sass::string def_nesting_limit;
24+
const sass::string def_msg("Invalid sass detected");
25+
const sass::string def_op_null_msg("Invalid null operation");
26+
const sass::string def_nesting_limit("Code too deeply neested");
2827

29-
extern sass::string msg_recursion_limit;
28+
const sass::string msg_recursion_limit =
29+
"Too deep recursion detected. This can be caused by too deep level nesting.\n"
30+
"LibSass will abort here in order to avoid a possible stack overflow.\n";
3031

3132
class Base : public std::runtime_error {
3233
protected:
@@ -137,7 +138,7 @@ namespace Sass {
137138
protected:
138139
sass::string msg;
139140
public:
140-
OperationError(sass::string msg = def_op_msg)
141+
OperationError(sass::string msg = sass::string("Undefined operation"))
141142
: std::runtime_error(msg.c_str()), msg(msg)
142143
{};
143144
public:

src/file.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
// __EXTENSIONS__ fix on Solaris.
33
#include "sass.hpp"
44

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+
511
#ifdef _WIN32
612
# ifdef __MINGW32__
713
# ifndef off64_t

src/memory_pool.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ namespace Sass {
112112
~MemoryPool() {
113113
// The memory will not be used anymore
114114
// If you kill the pool to early go figure
115-
// std::cerr << "MEMORY POOL DESTRUCTOR\n";
115+
// std::cerr << "MEMORY POOL DESTROYED\n";
116116
// Cant just throw them out, some might still want
117117
// to be destructed. Those will fail greatly.
118118
for (auto arena : arenas) {

0 commit comments

Comments
 (0)