Skip to content

Commit c8c1f96

Browse files
committed
src: avoid manual memory management in inspector
Make the inspector code easier to reason about by restructuring it to avoid manual memory allocation and copying as much as possible. An amusing side effect is that it reduces the total amount of memory used in the test suite. Before: $ valgrind ./out/Release/cctest 2>&1 | grep 'total heap' | cut -c31- 1,017 allocs, 1,017 frees, 21,695,456 allocated After: $ valgrind ./out/Release/cctest 2>&1 | grep 'total heap' | cut -c31- 869 allocs, 869 frees, 14,484,641 bytes allocated PR-URL: #7906 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 0190db4 commit c8c1f96

File tree

4 files changed

+161
-228
lines changed

4 files changed

+161
-228
lines changed

src/inspector_agent.cc

+14-17
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ void PrintDebuggerReadyMessage(int port) {
4848
port, DEVTOOLS_HASH, port);
4949
}
5050

51-
bool AcceptsConnection(inspector_socket_t* socket, const char* path) {
52-
return strncmp(DEVTOOLS_PATH, path, sizeof(DEVTOOLS_PATH)) == 0;
51+
bool AcceptsConnection(inspector_socket_t* socket, const std::string& path) {
52+
return 0 == path.compare(0, sizeof(DEVTOOLS_PATH) - 1, DEVTOOLS_PATH);
5353
}
5454

5555
void DisposeInspector(inspector_socket_t* socket, int status) {
@@ -63,10 +63,7 @@ void DisconnectAndDisposeIO(inspector_socket_t* socket) {
6363
}
6464

6565
void OnBufferAlloc(uv_handle_t* handle, size_t len, uv_buf_t* buf) {
66-
if (len > 0) {
67-
buf->base = static_cast<char*>(malloc(len));
68-
CHECK_NE(buf->base, nullptr);
69-
}
66+
buf->base = new char[len];
7067
buf->len = len;
7168
}
7269

@@ -133,18 +130,19 @@ void SendTargentsListResponse(inspector_socket_t* socket, int port) {
133130
SendHttpResponse(socket, buffer, len);
134131
}
135132

136-
bool RespondToGet(inspector_socket_t* socket, const char* path, int port) {
133+
bool RespondToGet(inspector_socket_t* socket, const std::string& path,
134+
int port) {
137135
const char PATH[] = "/json";
138136
const char PATH_LIST[] = "/json/list";
139137
const char PATH_VERSION[] = "/json/version";
140138
const char PATH_ACTIVATE[] = "/json/activate/";
141-
if (!strncmp(PATH_VERSION, path, sizeof(PATH_VERSION))) {
139+
if (0 == path.compare(0, sizeof(PATH_VERSION) - 1, PATH_VERSION)) {
142140
SendVersionResponse(socket);
143-
} else if (!strncmp(PATH_LIST, path, sizeof(PATH_LIST)) ||
144-
!strncmp(PATH, path, sizeof(PATH))) {
141+
} else if (0 == path.compare(0, sizeof(PATH_LIST) - 1, PATH_LIST) ||
142+
0 == path.compare(0, sizeof(PATH) - 1, PATH)) {
145143
SendTargentsListResponse(socket, port);
146-
} else if (!strncmp(path, PATH_ACTIVATE, sizeof(PATH_ACTIVATE) - 1) &&
147-
atoi(path + (sizeof(PATH_ACTIVATE) - 1)) == getpid()) {
144+
} else if (0 == path.compare(0, sizeof(PATH_ACTIVATE) - 1, PATH_ACTIVATE) &&
145+
atoi(path.substr(sizeof(PATH_ACTIVATE) - 1).c_str()) == getpid()) {
148146
const char TARGET_ACTIVATED[] = "Target activated";
149147
SendHttpResponse(socket, TARGET_ACTIVATED, sizeof(TARGET_ACTIVATED) - 1);
150148
} else {
@@ -181,7 +179,7 @@ class AgentImpl {
181179
static void OnSocketConnectionIO(uv_stream_t* server, int status);
182180
static bool OnInspectorHandshakeIO(inspector_socket_t* socket,
183181
enum inspector_handshake_event state,
184-
const char* path);
182+
const std::string& path);
185183
static void WriteCbIO(uv_async_t* async);
186184

187185
void WorkerRunIO();
@@ -388,7 +386,6 @@ void AgentImpl::ThreadCbIO(void* agent) {
388386
void AgentImpl::OnSocketConnectionIO(uv_stream_t* server, int status) {
389387
if (status == 0) {
390388
inspector_socket_t* socket = new inspector_socket_t();
391-
memset(socket, 0, sizeof(*socket));
392389
socket->data = server->data;
393390
if (inspector_accept(server, socket,
394391
AgentImpl::OnInspectorHandshakeIO) != 0) {
@@ -399,8 +396,8 @@ void AgentImpl::OnSocketConnectionIO(uv_stream_t* server, int status) {
399396

400397
// static
401398
bool AgentImpl::OnInspectorHandshakeIO(inspector_socket_t* socket,
402-
enum inspector_handshake_event state,
403-
const char* path) {
399+
enum inspector_handshake_event state,
400+
const std::string& path) {
404401
AgentImpl* agent = static_cast<AgentImpl*>(socket->data);
405402
switch (state) {
406403
case kInspectorHandshakeHttpGet:
@@ -443,7 +440,7 @@ void AgentImpl::OnRemoteDataIO(inspector_socket_t* socket,
443440
DisconnectAndDisposeIO(socket);
444441
}
445442
if (buf) {
446-
free(buf->base);
443+
delete[] buf->base;
447444
}
448445
pause_cond_.Broadcast(scoped_lock);
449446
}

0 commit comments

Comments
 (0)