Skip to content

Commit 1a6b23e

Browse files
author
Marcos Bracco
committed
update memory-safe-cpp and syntax fix for gcc-11
1 parent bab44cb commit 1a6b23e

File tree

11 files changed

+119
-57
lines changed

11 files changed

+119
-57
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ add_library(nodecpp_no_main STATIC
4242

4343
# target_compile_definitions(nodecpp_no_main PUBLIC NODECPP_ENABLE_CLUSTERING)
4444

45-
if(TARGET EASTL)
46-
target_compile_definitions(nodecpp_no_main PUBLIC NODECPP_USE_SAFE_MEMORY_CONTAINERS)
47-
endif()
45+
#if(TARGET EASTL)
46+
# target_compile_definitions(nodecpp_no_main PUBLIC NODECPP_USE_SAFE_MEMORY_CONTAINERS)
47+
#endif()
4848

4949
target_include_directories(nodecpp_no_main
5050
PUBLIC include
@@ -75,7 +75,7 @@ add_library(nodecpp STATIC
7575

7676

7777
target_link_libraries(nodecpp_no_main safememory)
78-
target_link_libraries(nodecpp_no_main_q_based_infra safememory)
78+
target_link_libraries(nodecpp_no_main_q_based_infra safememory global-mq)
7979
target_link_libraries(nodecpp nodecpp_no_main)
8080

8181
target_compile_definitions(nodecpp_no_main PUBLIC USING_T_SOCKETS)

include/nodecpp/basic_collections.h

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,84 @@ namespace nodecpp
100100

101101
using string = ::std::basic_string<char, std::char_traits<char>, safememory::detail::iiballocator<char>>;
102102

103-
class string_literal
103+
template<typename T>
104+
class basic_string_literal
104105
{
105-
const char* str;
106106
public:
107-
string_literal() : str( nullptr ) {}
108-
string_literal( const char* str_) : str( str_ ) {}
109-
string_literal( const string_literal& other ) : str( other.str ) {}
110-
string_literal& operator = ( const string_literal& other ) {str = other.str; return *this;}
111-
string_literal( string_literal&& other ) : str( other.str ) {}
112-
string_literal& operator = ( string_literal&& other ) {str = other.str; return *this;}
107+
typedef T value_type;
108+
typedef const T& const_reference;
109+
typedef std::size_t size_type;
113110

114-
bool operator == ( const string_literal& other ) const { return strcmp( str, other.str ) == 0; }
115-
bool operator != ( const string_literal& other ) const { return strcmp( str, other.str ) != 0; }
111+
private:
112+
const value_type* str = nullptr;
113+
size_type sz = 0;
116114

117-
// bool operator == ( const char* other ) const { return strcmp( str, other.str ) == 0; }
118-
// bool operator != ( const char* other ) const { return strcmp( str, other.str ) != 0; }
119-
120-
const char* c_str() const { return str; }
115+
public:
116+
basic_string_literal() {}
117+
118+
template<size_type N>
119+
basic_string_literal(const value_type (&ptr)[N]) : str(ptr), sz(N - 1) {
120+
static_assert(N >= 1);
121+
static_assert(N < std::numeric_limits<size_type>::max());
122+
}
123+
124+
basic_string_literal(const basic_string_literal& other) = default;
125+
basic_string_literal& operator=(const basic_string_literal& other) = default;
126+
basic_string_literal(basic_string_literal&& other) = default;
127+
basic_string_literal& operator=(basic_string_literal&& other) = default;
128+
129+
~basic_string_literal() = default; // string literals have infinite lifetime, no need to zero pointers
130+
131+
constexpr bool empty() const noexcept { return sz == 0; }
132+
constexpr size_type size() const noexcept { return sz; }
133+
134+
constexpr const T* c_str() const noexcept { return str; }
135+
constexpr const T* data() const noexcept { return str; }
136+
137+
std::basic_string_view<T> to_string_view_unsafe() const {
138+
return std::basic_string_view<T>(data(), size());
139+
}
121140
};
122141

142+
typedef basic_string_literal<char> string_literal;
143+
typedef basic_string_literal<wchar_t> wstring_literal;
144+
145+
/// string8 / string16 / string32
146+
// typedef basic_string_literal<char8_t> u8string_literal;
147+
typedef basic_string_literal<char16_t> u16string_literal;
148+
typedef basic_string_literal<char32_t> u32string_literal;
149+
150+
151+
template<class T>
152+
bool operator==( const basic_string_literal<T>& a, const basic_string_literal<T>& b ) {
153+
return std::operator==(a.to_string_view_unsafe(), b.to_string_view_unsafe());
154+
}
155+
156+
template<class T>
157+
bool operator!=( const basic_string_literal<T>& a, const basic_string_literal<T>& b ) {
158+
return std::operator!=(a.to_string_view_unsafe(), b.to_string_view_unsafe());
159+
}
160+
161+
template<class T>
162+
bool operator<( const basic_string_literal<T>& a, const basic_string_literal<T>& b ) {
163+
return std::operator<(a.to_string_view_unsafe(), b.to_string_view_unsafe());
164+
}
165+
166+
template<class T>
167+
bool operator<=( const basic_string_literal<T>& a, const basic_string_literal<T>& b ) {
168+
return std::operator<=(a.to_string_view_unsafe(), b.to_string_view_unsafe());
169+
}
170+
171+
template<class T>
172+
bool operator>( const basic_string_literal<T>& a, const basic_string_literal<T>& b ) {
173+
return std::operator>(a.to_string_view_unsafe(), b.to_string_view_unsafe());
174+
}
175+
176+
template<class T>
177+
bool operator>=( const basic_string_literal<T>& a, const basic_string_literal<T>& b ) {
178+
return std::operator>=(a.to_string_view_unsafe(), b.to_string_view_unsafe());
179+
}
180+
123181
#endif // NODECPP_USE_SAFE_MEMORY_CONTAINERS
124182

125183
// inline string_literal operator"" _s(const char* str, size_t len) noexcept { return {str}; }

include/nodecpp/event.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,71 +45,71 @@ namespace nodecpp
4545
struct Close
4646
{
4747
using callback = std::function<void(bool)>;
48-
static constexpr const char* name = "close";
48+
static constexpr const char name[] = "close";
4949
};
5050
static constexpr Close close = Close();
5151

5252
struct Connect
5353
{
5454
using callback = std::function<void()>;
55-
static constexpr const char* name = "connect";
55+
static constexpr const char name[] = "connect";
5656
};
5757
static constexpr Connect connect = Connect();
5858

5959
struct Data
6060
{
6161
using callback = std::function<void(const Buffer&)>;
62-
static constexpr const char* name = "data";
62+
static constexpr const char name[] = "data";
6363
};
6464
static constexpr Data data = Data();
6565

6666
struct Drain
6767
{
6868
using callback = std::function<void()>;
69-
static constexpr const char* name = "drain";
69+
static constexpr const char name[] = "drain";
7070
};
7171
static constexpr Drain drain = Drain();
7272

7373
struct End
7474
{
7575
using callback = std::function<void()>;
76-
static constexpr const char* name = "end";
76+
static constexpr const char name[] = "end";
7777
};
7878
static constexpr End end = End();
7979

8080
struct Accepted
8181
{
8282
using callback = std::function<void()>;
83-
static constexpr const char* name = "accepted";
83+
static constexpr const char name[] = "accepted";
8484
};
8585
static constexpr Accepted accepted = Accepted();
8686

8787
struct Error
8888
{
8989
using callback = std::function<void(nodecpp::Error&)>;
90-
static constexpr const char* name = "error";
90+
static constexpr const char name[] = "error";
9191
};
9292
static constexpr Error error = Error();
9393

9494

9595
struct Connection
9696
{
9797
using callback = std::function<void(soft_ptr<net::SocketBase>)>;
98-
static constexpr const char* name = "connection";
98+
static constexpr const char name[] = "connection";
9999
};
100100
static constexpr Connection connection = Connection();
101101

102102
struct Listening
103103
{
104104
using callback = std::function<void(size_t, net::Address)>;
105-
static constexpr const char* name = "listening";
105+
static constexpr const char name[] = "listening";
106106
};
107107
static constexpr Listening listening = Listening();
108108

109109
struct HttpRequest
110110
{
111111
using callback = std::function<void(nodecpp::net::IncomingHttpMessageAtServer&, nodecpp::net::HttpServerResponse&)>;
112-
static constexpr const char* name = "HttpRequest";
112+
static constexpr const char name[] = "HttpRequest";
113113
};
114114
static constexpr HttpRequest httpRequest = HttpRequest();
115115

include/nodecpp/http_server_common.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ namespace nodecpp {
329329

330330
EventEmitter<event::HttpRequest> eHttpRequest;
331331
void on(nodecpp::string_literal name, event::HttpRequest::callback cb NODECPP_MAY_EXTEND_TO_THIS) {
332-
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == event::HttpRequest::name);
332+
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == string_literal(event::HttpRequest::name));
333333
eHttpRequest.on(std::move(cb));
334334
}
335335

@@ -340,11 +340,11 @@ namespace nodecpp {
340340
{
341341
public:
342342
using DataParentType = DataParentT;
343-
HttpServer<DataParentT>() {};
343+
HttpServer() {};
344344
HttpServer(event::HttpRequest::callback cb NODECPP_MAY_EXTEND_TO_THIS) : HttpServerBase(std::move(cb)) {}
345-
HttpServer<DataParentT>(DataParentT* dataParent ) : HttpServerBase(), ::nodecpp::DataParent<DataParentT>( dataParent ) {};
346-
HttpServer<DataParentT>(DataParentT* dataParent, event::HttpRequest::callback cb NODECPP_MAY_EXTEND_TO_THIS ) : HttpServerBase(std::move(cb)), ::nodecpp::DataParent<DataParentT>( dataParent ) {};
347-
virtual ~HttpServer<DataParentT>() {}
345+
HttpServer(DataParentT* dataParent ) : HttpServerBase(), ::nodecpp::DataParent<DataParentT>( dataParent ) {};
346+
HttpServer(DataParentT* dataParent, event::HttpRequest::callback cb NODECPP_MAY_EXTEND_TO_THIS ) : HttpServerBase(std::move(cb)), ::nodecpp::DataParent<DataParentT>( dataParent ) {};
347+
virtual ~HttpServer() {}
348348
};
349349

350350
template<>

include/nodecpp/http_socket_at_server.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ namespace nodecpp {
227227
{
228228
public:
229229
using DataParentType = DataParentT;
230-
HttpSocket<RequestT, DataParentT>() {};
231-
HttpSocket<RequestT, DataParentT>(DataParentT* dataParent ) : HttpSocketBase(), ::nodecpp::DataParent<DataParentT>( dataParent ) {};
232-
virtual ~HttpSocket<RequestT, DataParentT>() {}
230+
HttpSocket() {};
231+
HttpSocket(DataParentT* dataParent ) : HttpSocketBase(), ::nodecpp::DataParent<DataParentT>( dataParent ) {};
232+
virtual ~HttpSocket() {}
233233
};
234234

235235
template<class RequestT>

include/nodecpp/server_common.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -950,63 +950,63 @@ namespace nodecpp {
950950
static_assert(!std::is_same< event::Close::callback, event::Connection::callback >::value);
951951
static_assert(!std::is_same< event::Close::callback, event::Listening::callback >::value);
952952
static_assert(!std::is_same< event::Close::callback, event::Error::callback >::value);
953-
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == event::Close::name);
953+
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == nodecpp::string_literal(event::Close::name));
954954
eClose.on(std::move(cb));
955955
}
956956

957957
void on(nodecpp::string_literal name, event::Connection::callback cb NODECPP_MAY_EXTEND_TO_THIS) {
958958
static_assert(!std::is_same< event::Connection::callback, event::Close::callback >::value);
959959
static_assert(!std::is_same< event::Connection::callback, event::Listening::callback >::value);
960960
static_assert(!std::is_same< event::Connection::callback, event::Error::callback >::value);
961-
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == event::Connection::name);
961+
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == nodecpp::string_literal(event::Connection::name));
962962
eConnection.on(std::move(cb));
963963
}
964964

965965
void on(nodecpp::string_literal name, event::Error::callback cb NODECPP_MAY_EXTEND_TO_THIS) {
966966
static_assert(!std::is_same< event::Error::callback, event::Close::callback >::value);
967967
static_assert(!std::is_same< event::Error::callback, event::Listening::callback >::value);
968968
static_assert(!std::is_same< event::Error::callback, event::Connection::callback >::value);
969-
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == event::Error::name);
969+
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == nodecpp::string_literal(event::Error::name));
970970
eError.on(std::move(cb));
971971
}
972972

973973
void on(nodecpp::string_literal name, event::Listening::callback cb NODECPP_MAY_EXTEND_TO_THIS) {
974974
static_assert(!std::is_same< event::Listening::callback, event::Close::callback >::value);
975975
static_assert(!std::is_same< event::Listening::callback, event::Connection::callback >::value);
976976
static_assert(!std::is_same< event::Listening::callback, event::Error::callback >::value);
977-
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == event::Listening::name);
977+
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == nodecpp::string_literal(event::Listening::name));
978978
eListening.on(std::move(cb));
979979
}
980980

981981
void once(nodecpp::string_literal name, event::Close::callback cb NODECPP_MAY_EXTEND_TO_THIS) {
982982
static_assert(!std::is_same< event::Close::callback, event::Connection::callback >::value);
983983
static_assert(!std::is_same< event::Close::callback, event::Listening::callback >::value);
984984
static_assert(!std::is_same< event::Close::callback, event::Error::callback >::value);
985-
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == event::Close::name);
985+
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == nodecpp::string_literal(event::Close::name));
986986
eClose.once(std::move(cb));
987987
}
988988

989989
void once(nodecpp::string_literal name, event::Connection::callback cb NODECPP_MAY_EXTEND_TO_THIS) {
990990
static_assert(!std::is_same< event::Connection::callback, event::Close::callback >::value);
991991
static_assert(!std::is_same< event::Connection::callback, event::Listening::callback >::value);
992992
static_assert(!std::is_same< event::Connection::callback, event::Error::callback >::value);
993-
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == event::Connection::name);
993+
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == nodecpp::string_literal(event::Connection::name));
994994
eConnection.once(std::move(cb));
995995
}
996996

997997
void once(nodecpp::string_literal name, event::Error::callback cb NODECPP_MAY_EXTEND_TO_THIS) {
998998
static_assert(!std::is_same< event::Error::callback, event::Close::callback >::value);
999999
static_assert(!std::is_same< event::Error::callback, event::Listening::callback >::value);
10001000
static_assert(!std::is_same< event::Error::callback, event::Connection::callback >::value);
1001-
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == event::Error::name);
1001+
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == nodecpp::string_literal(event::Error::name));
10021002
eError.once(std::move(cb));
10031003
}
10041004

10051005
void once(nodecpp::string_literal name, event::Listening::callback cb NODECPP_MAY_EXTEND_TO_THIS) {
10061006
static_assert(!std::is_same< event::Listening::callback, event::Close::callback >::value);
10071007
static_assert(!std::is_same< event::Listening::callback, event::Connection::callback >::value);
10081008
static_assert(!std::is_same< event::Listening::callback, event::Error::callback >::value);
1009-
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == event::Listening::name);
1009+
NODECPP_ASSERT( nodecpp::module_id, ::nodecpp::assert::AssertLevel::critical, name == nodecpp::string_literal(event::Listening::name));
10101010
eListening.once(std::move(cb));
10111011
}
10121012

@@ -1046,9 +1046,9 @@ namespace nodecpp {
10461046
{
10471047
public:
10481048
using DataParentType = DataParentT;
1049-
ServerSocket<DataParentT>() {};
1050-
ServerSocket<DataParentT>(DataParentT* dataParent ) : ServerBase(), ::nodecpp::DataParent<DataParentT>( dataParent ) {};
1051-
virtual ~ServerSocket<DataParentT>() {}
1049+
ServerSocket() {};
1050+
ServerSocket(DataParentT* dataParent ) : ServerBase(), ::nodecpp::DataParent<DataParentT>( dataParent ) {};
1051+
virtual ~ServerSocket() {}
10521052
};
10531053

10541054
template<>

include/nodecpp/socket_common.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,9 +1579,9 @@ namespace nodecpp {
15791579
{
15801580
public:
15811581
using DataParentType = DataParentT;
1582-
Socket<DataParentT>() {};
1583-
Socket<DataParentT>(DataParentT* dataParent ) : SocketBase(), ::nodecpp::DataParent<DataParentT>( dataParent ) {};
1584-
virtual ~Socket<DataParentT>() {}
1582+
Socket() {};
1583+
Socket(DataParentT* dataParent ) : SocketBase(), ::nodecpp::DataParent<DataParentT>( dataParent ) {};
1584+
virtual ~Socket() {}
15851585
};
15861586

15871587
template<>

safe_memory

src/infra_main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ int main( int argc, char *argv_[] )
8888
argv.push_back( argv_[i] );
8989

9090
#ifdef NODECPP_USE_IIBMALLOC
91-
using nodecpp::iibmalloc::g_AllocManager;
92-
g_AllocManager.initialize();
91+
nodecpp::iibmalloc::ThreadLocalAllocatorT allocManager;
92+
auto formerAlloc = nodecpp::iibmalloc::setCurrneAllocator( &allocManager );
9393
#endif
9494
nodecpp::log::Log log;
9595
log.level = nodecpp::log::LogLevel::info;
@@ -118,6 +118,10 @@ int main( int argc, char *argv_[] )
118118

119119
nodecpp::logging_impl::currentLog = nullptr; // TODO: this is thread-unsafe. Revise and make sure all other threads using nodecpp::logging_impl::currentLog has already exited
120120

121+
#ifdef NODECPP_USE_IIBMALLOC
122+
nodecpp::iibmalloc::setCurrneAllocator(formerAlloc);
123+
#endif
124+
121125
return 0;
122126
}
123127

src/infrastructure.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ class Runnable : public RunnableBase
469469
void internalRun()
470470
#endif
471471
{
472-
safememory::detail::interceptNewDeleteOperators(true);
472+
// safememory::detail::interceptNewDeleteOperators(true);
473473
{
474474
#ifdef NODECPP_THREADLOCAL_INIT_BUG_GCC_60702
475475
nodecpp::net::SocketBase::DataForCommandProcessing::userHandlerClassPattern.init();
@@ -537,7 +537,7 @@ class Runnable : public RunnableBase
537537
#endif // NODECPP_THREADLOCAL_INIT_BUG_GCC_60702
538538
}
539539
safememory::detail::killAllZombies();
540-
safememory::detail::interceptNewDeleteOperators(false);
540+
// safememory::detail::interceptNewDeleteOperators(false);
541541
}
542542

543543
public:

0 commit comments

Comments
 (0)