Skip to content

Commit 9d45569

Browse files
bnoordhuiscjihrig
authored andcommitted
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 c565c17 commit 9d45569

File tree

4 files changed

+161
-228
lines changed

4 files changed

+161
-228
lines changed

src/inspector_agent.cc

Lines changed: 14 additions & 17 deletions
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 {
@@ -180,7 +178,7 @@ class AgentImpl {
180178
static void OnSocketConnectionIO(uv_stream_t* server, int status);
181179
static bool OnInspectorHandshakeIO(inspector_socket_t* socket,
182180
enum inspector_handshake_event state,
183-
const char* path);
181+
const std::string& path);
184182
static void WriteCbIO(uv_async_t* async);
185183

186184
void WorkerRunIO();
@@ -387,7 +385,6 @@ void AgentImpl::ThreadCbIO(void* agent) {
387385
void AgentImpl::OnSocketConnectionIO(uv_stream_t* server, int status) {
388386
if (status == 0) {
389387
inspector_socket_t* socket = new inspector_socket_t();
390-
memset(socket, 0, sizeof(*socket));
391388
socket->data = server->data;
392389
if (inspector_accept(server, socket,
393390
AgentImpl::OnInspectorHandshakeIO) != 0) {
@@ -398,8 +395,8 @@ void AgentImpl::OnSocketConnectionIO(uv_stream_t* server, int status) {
398395

399396
// static
400397
bool AgentImpl::OnInspectorHandshakeIO(inspector_socket_t* socket,
401-
enum inspector_handshake_event state,
402-
const char* path) {
398+
enum inspector_handshake_event state,
399+
const std::string& path) {
403400
AgentImpl* agent = static_cast<AgentImpl*>(socket->data);
404401
switch (state) {
405402
case kInspectorHandshakeHttpGet:
@@ -442,7 +439,7 @@ void AgentImpl::OnRemoteDataIO(inspector_socket_t* socket,
442439
DisconnectAndDisposeIO(socket);
443440
}
444441
if (buf) {
445-
free(buf->base);
442+
delete[] buf->base;
446443
}
447444
pause_cond_.Broadcast(scoped_lock);
448445
}

0 commit comments

Comments
 (0)