Skip to content

Commit a18692c

Browse files
gengjiawentargos
authored andcommitted
src: extract common macro to util.h
PR-URL: #27512 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 172fa63 commit a18692c

File tree

6 files changed

+19
-45
lines changed

6 files changed

+19
-45
lines changed

src/env-inl.h

-8
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@
3838

3939
#include <utility>
4040

41-
#ifdef _WIN32
42-
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
43-
#define CWD_BUFSIZE (MAX_PATH * 4)
44-
#else
45-
#include <climits> // PATH_MAX on Solaris.
46-
#define CWD_BUFSIZE (PATH_MAX)
47-
#endif
48-
4941
namespace node {
5042

5143
inline v8::Isolate* IsolateData::isolate() const {

src/inspector_profiler.cc

+3-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "node_file.h"
66
#include "node_internals.h"
77
#include "v8-inspector.h"
8+
#include "util.h"
89

910
namespace node {
1011
namespace profiler {
@@ -23,16 +24,6 @@ using v8::Value;
2324

2425
using v8_inspector::StringView;
2526

26-
#ifdef _WIN32
27-
const char* const kPathSeparator = "\\/";
28-
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
29-
#define CWD_BUFSIZE (MAX_PATH * 4)
30-
#else
31-
#include <climits> // PATH_MAX on Solaris.
32-
const char* const kPathSeparator = "/";
33-
#define CWD_BUFSIZE (PATH_MAX)
34-
#endif
35-
3627
V8ProfilerConnection::V8ProfilerConnection(Environment* env)
3728
: session_(env->inspector_agent()->Connect(
3829
std::make_unique<V8ProfilerConnection::V8ProfilerSessionDelegate>(
@@ -284,8 +275,8 @@ void EndStartedProfilers(Environment* env) {
284275
}
285276

286277
std::string GetCwd() {
287-
char cwd[CWD_BUFSIZE];
288-
size_t size = CWD_BUFSIZE;
278+
char cwd[PATH_MAX_BYTES];
279+
size_t size = PATH_MAX_BYTES;
289280
int err = uv_cwd(cwd, &size);
290281
// This can fail if the cwd is deleted.
291282
// TODO(joyeecheung): store this in the Environment during Environment

src/node_process_methods.cc

+2-9
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,6 @@ Mutex umask_mutex;
5959
// used in Hrtime() and Uptime() below
6060
#define NANOS_PER_SEC 1000000000
6161

62-
#ifdef _WIN32
63-
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
64-
#define CHDIR_BUFSIZE (MAX_PATH * 4)
65-
#else
66-
#define CHDIR_BUFSIZE (PATH_MAX)
67-
#endif
68-
6962
static void Abort(const FunctionCallbackInfo<Value>& args) {
7063
Abort();
7164
}
@@ -81,7 +74,7 @@ static void Chdir(const FunctionCallbackInfo<Value>& args) {
8174
if (err) {
8275
// Also include the original working directory, since that will usually
8376
// be helpful information when debugging a `chdir()` failure.
84-
char buf[CHDIR_BUFSIZE];
77+
char buf[PATH_MAX_BYTES];
8578
size_t cwd_len = sizeof(buf);
8679
uv_cwd(buf, &cwd_len);
8780
return env->ThrowUVException(err, "chdir", nullptr, buf, *path);
@@ -119,7 +112,7 @@ static void CPUUsage(const FunctionCallbackInfo<Value>& args) {
119112
static void Cwd(const FunctionCallbackInfo<Value>& args) {
120113
Environment* env = Environment::GetCurrent(args);
121114
CHECK(env->has_run_bootstrapping_code());
122-
char buf[CHDIR_BUFSIZE];
115+
char buf[PATH_MAX_BYTES];
123116
size_t cwd_len = sizeof(buf);
124117
int err = uv_cwd(buf, &cwd_len);
125118
if (err)

src/node_report.cc

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
21
#include "node_report.h"
32
#include "debug_utils.h"
43
#include "node_internals.h"
54
#include "node_metadata.h"
5+
#include "util.h"
66

77
#ifdef _WIN32
88
#include <Windows.h>
@@ -17,14 +17,6 @@
1717
#include <cwctype>
1818
#include <fstream>
1919
#include <iomanip>
20-
#include <climits> // PATH_MAX
21-
22-
#ifdef _WIN32
23-
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
24-
#define PATH_MAX_BYTES (MAX_PATH * 4)
25-
#else
26-
#define PATH_MAX_BYTES (PATH_MAX)
27-
#endif
2820

2921
#ifndef _WIN32
3022
extern char** environ;
@@ -110,7 +102,7 @@ std::string TriggerNodeReport(Isolate* isolate,
110102
// Regular file. Append filename to directory path if one was specified
111103
if (env != nullptr && options->report_directory.length() > 0) {
112104
std::string pathname = options->report_directory;
113-
pathname += PATHSEP;
105+
pathname += node::kPathSeparator;
114106
pathname += filename;
115107
outfile.open(pathname, std::ios::out | std::ios::binary);
116108
} else {

src/node_report.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "node_buffer.h"
55
#include "uv.h"
66
#include "v8.h"
7+
#include "util.h"
78

89
#ifndef _WIN32
910
#include <sys/types.h>
@@ -26,12 +27,6 @@
2627

2728
namespace report {
2829

29-
#ifdef _WIN32
30-
#define PATHSEP "\\"
31-
#else // UNIX, OSX
32-
#define PATHSEP "/"
33-
#endif
34-
3530
// Function declarations - functions in src/node_report.cc
3631
std::string TriggerNodeReport(v8::Isolate* isolate,
3732
node::Environment* env,

src/util.h

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "v8.h"
2828

2929
#include <cassert>
30+
#include <climits> // PATH_MAX
3031
#include <csignal>
3132
#include <cstddef>
3233
#include <cstdio>
@@ -43,6 +44,16 @@
4344

4445
namespace node {
4546

47+
// Maybe remove kPathSeparator when cpp17 is ready
48+
#ifdef _WIN32
49+
constexpr char kPathSeparator = '\\';
50+
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
51+
#define PATH_MAX_BYTES (MAX_PATH * 4)
52+
#else
53+
constexpr char kPathSeparator = '/';
54+
#define PATH_MAX_BYTES (PATH_MAX)
55+
#endif
56+
4657
// These should be used in our code as opposed to the native
4758
// versions as they abstract out some platform and or
4859
// compiler version specific functionality

0 commit comments

Comments
 (0)