Skip to content

Commit e0a207c

Browse files
committed
Begin moving static vars into struct for isolates.
Only node.cc, stream_wrap.cc, and tcp_wrap.cc have been done. The rest still need work.
1 parent ebe0fb6 commit e0a207c

File tree

6 files changed

+195
-78
lines changed

6 files changed

+195
-78
lines changed

node.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
'src/cares_wrap.cc',
7373
'src/handle_wrap.cc',
7474
'src/node.cc',
75+
'src/node_vars.cc',
7576
'src/node_buffer.cc',
7677
'src/node_constants.cc',
7778
'src/node_extensions.cc',
@@ -94,6 +95,7 @@
9495
# headers to make for a more pleasant IDE experience
9596
'src/handle_wrap.h',
9697
'src/node.h',
98+
'src/node_vars.h',
9799
'src/node_buffer.h',
98100
'src/node_constants.h',
99101
'src/node_crypto.h',

src/node.cc

Lines changed: 37 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -95,76 +95,51 @@ using namespace v8;
9595
extern char **environ;
9696
# endif
9797

98-
namespace node {
99-
100-
static Persistent<Object> process;
101-
102-
static Persistent<String> errno_symbol;
103-
static Persistent<String> syscall_symbol;
104-
static Persistent<String> errpath_symbol;
105-
static Persistent<String> code_symbol;
106-
107-
static Persistent<String> rss_symbol;
108-
static Persistent<String> heap_total_symbol;
109-
static Persistent<String> heap_used_symbol;
110-
111-
static Persistent<String> listeners_symbol;
112-
static Persistent<String> uncaught_exception_symbol;
113-
static Persistent<String> emit_symbol;
11498

99+
#include <node_vars.h>
100+
101+
// We do the following to minimize the detal between v0.6 branch. We want to
102+
// use the variables as they were being used before.
103+
#define check_tick_watcher NODE_VAR(check_tick_watcher)
104+
#define code_symbol NODE_VAR(code_symbol)
105+
#define debug_port NODE_VAR(debug_port)
106+
#define debug_wait_connect NODE_VAR(debug_wait_connect)
107+
#define emit_symbol NODE_VAR(emit_symbol)
108+
#define errno_symbol NODE_VAR(errno_symbol)
109+
#define errpath_symbol NODE_VAR(errpath_symbol)
110+
#define eval_string NODE_VAR(eval_string)
111+
#define gc_check NODE_VAR(gc_check)
112+
#define gc_idle NODE_VAR(gc_idle)
113+
#define gc_timer NODE_VAR(gc_timer)
114+
#define getbuf NODE_VAR(getbuf)
115+
#define heap_total_symbol NODE_VAR(heap_total_symbol)
116+
#define heap_used_symbol NODE_VAR(heap_used_symbol)
117+
#define listeners_symbol NODE_VAR(listeners_symbol)
118+
#define max_stack_size NODE_VAR(max_stack_size)
119+
#define need_tick_cb NODE_VAR(need_tick_cb)
120+
#define option_end_index NODE_VAR(option_end_index)
121+
#define prepare_tick_watcher NODE_VAR(prepare_tick_watcher)
122+
#define print_eval NODE_VAR(print_eval)
123+
#define process NODE_VAR(process)
124+
#define rss_symbol NODE_VAR(rss_symbol)
125+
#define syscall_symbol NODE_VAR(syscall_symbol)
126+
#define tick_callback_sym NODE_VAR(tick_callback_sym)
127+
#define tick_spinner NODE_VAR(tick_spinner)
128+
#define tick_time_head NODE_VAR(tick_time_head)
129+
#define tick_times NODE_VAR(tick_times)
130+
#define uncaught_exception_symbol NODE_VAR(uncaught_exception_symbol)
131+
#define use_debug_agent NODE_VAR(use_debug_agent)
132+
#define use_npn NODE_VAR(use_npn)
133+
#define use_sni NODE_VAR(use_sni)
115134

116-
static bool print_eval = false;
117-
static char *eval_string = NULL;
118-
static int option_end_index = 0;
119-
static bool use_debug_agent = false;
120-
static bool debug_wait_connect = false;
121-
static int debug_port=5858;
122-
static int max_stack_size = 0;
123135

124-
static uv_check_t check_tick_watcher;
125-
static uv_prepare_t prepare_tick_watcher;
126-
static uv_idle_t tick_spinner;
127-
static bool need_tick_cb;
128-
static Persistent<String> tick_callback_sym;
129-
130-
131-
#ifdef OPENSSL_NPN_NEGOTIATED
132-
static bool use_npn = true;
133-
#else
134-
static bool use_npn = false;
135-
#endif
136+
namespace node {
136137

137-
#ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
138-
static bool use_sni = true;
139-
#else
140-
static bool use_sni = false;
141-
#endif
142138

143-
#ifdef __POSIX__
144-
// Buffer for getpwnam_r(), getgrpam_r() and other misc callers; keep this
145-
// scoped at file-level rather than method-level to avoid excess stack usage.
146-
static char getbuf[PATH_MAX + 1];
147-
#endif
148139

149-
// We need to notify V8 when we're idle so that it can run the garbage
150-
// collector. The interface to this is V8::IdleNotification(). It returns
151-
// true if the heap hasn't be fully compacted, and needs to be run again.
152-
// Returning false means that it doesn't have anymore work to do.
153-
//
154-
// A rather convoluted algorithm has been devised to determine when Node is
155-
// idle. You'll have to figure it out for yourself.
156-
static uv_check_t gc_check;
157-
static uv_idle_t gc_idle;
158-
static uv_timer_t gc_timer;
159-
bool need_gc;
140+
#define TICK_TIME(n) tick_times[(tick_time_head - (n)) % RPM_SAMPLES]
160141

161142

162-
#define FAST_TICK 700.
163-
#define GC_WAIT_TIME 5000.
164-
#define RPM_SAMPLES 100
165-
#define TICK_TIME(n) tick_times[(tick_time_head - (n)) % RPM_SAMPLES]
166-
static int64_t tick_times[RPM_SAMPLES];
167-
static int tick_time_head;
168143

169144
static void CheckStatus(uv_timer_t* watcher, int status);
170145

src/node_vars.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <node_vars.h>
2+
#if HAVE_OPENSSL
3+
# include <node_crypto.h>
4+
#endif
5+
#include <string.h>
6+
7+
namespace node {
8+
9+
// For now we just statically initialize the globals structure. Later there
10+
// will be one struct globals for each isolate.
11+
12+
static struct globals g_struct;
13+
static struct globals* g_ptr;
14+
15+
16+
static void globals_init(struct globals* g) {
17+
memset(g, 0, sizeof(struct globals));
18+
g->debug_port = 5858;
19+
20+
#ifdef OPENSSL_NPN_NEGOTIATED
21+
g->use_npn = true;
22+
#else
23+
g->use_npn = false;
24+
#endif
25+
26+
#ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
27+
g->use_sni = true;
28+
#else
29+
g->use_sni = false;
30+
#endif
31+
}
32+
33+
34+
struct globals* globals_get() {
35+
if (!g_ptr) {
36+
g_ptr = &g_struct;
37+
globals_init(g_ptr);
38+
}
39+
return g_ptr;
40+
}
41+
42+
} // namespace node

src/node_vars.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#ifndef NODE_VARS_H
2+
#define NODE_VARS_H
3+
4+
// This file contains all Isolate-local variables. We allow people to
5+
// compile Node either with Isolates or without. In the case that they
6+
// compile without isolates, these will be static variables.
7+
8+
#include <v8.h>
9+
#include <uv.h>
10+
11+
#ifndef PATH_MAX
12+
# define PATH_MAX 4096
13+
#endif
14+
15+
namespace node {
16+
17+
18+
#define NODE_VAR(x) (globals_get()->x)
19+
20+
struct globals {
21+
// node.cc
22+
v8::Persistent<v8::Object> process;
23+
v8::Persistent<v8::String> errno_symbol;
24+
v8::Persistent<v8::String> syscall_symbol;
25+
v8::Persistent<v8::String> errpath_symbol;
26+
v8::Persistent<v8::String> code_symbol;
27+
v8::Persistent<v8::String> rss_symbol;
28+
v8::Persistent<v8::String> heap_total_symbol;
29+
v8::Persistent<v8::String> heap_used_symbol;
30+
v8::Persistent<v8::String> listeners_symbol;
31+
v8::Persistent<v8::String> uncaught_exception_symbol;
32+
v8::Persistent<v8::String> emit_symbol;
33+
34+
// stream_wrap.cc
35+
size_t slab_used;
36+
uv_stream_t* handle_that_last_alloced;
37+
v8::Persistent<v8::String> slab_sym;
38+
v8::Persistent<v8::String> buffer_sym;
39+
v8::Persistent<v8::String> write_queue_size_sym;
40+
bool stream_wrap_initialized;
41+
42+
// tcp_wrap.cc
43+
v8::Persistent<v8::Function> tcpConstructor;
44+
v8::Persistent<v8::String> family_symbol;
45+
v8::Persistent<v8::String> address_symbol;
46+
v8::Persistent<v8::String> port_symbol;
47+
48+
bool print_eval;
49+
char *eval_string;
50+
int option_end_index;
51+
bool use_debug_agent;
52+
bool debug_wait_connect;
53+
int debug_port;
54+
int max_stack_size;
55+
56+
uv_check_t check_tick_watcher;
57+
uv_prepare_t prepare_tick_watcher;
58+
uv_idle_t tick_spinner;
59+
bool need_tick_cb;
60+
v8::Persistent<v8::String> tick_callback_sym;
61+
62+
bool use_npn;
63+
bool use_sni;
64+
65+
// Buffer for getpwnam_r(), getgrpam_r() and other misc callers; keep this
66+
// scoped at file-level rather than method-level to avoid excess stack usage.
67+
char getbuf[PATH_MAX + 1];
68+
69+
// We need to notify V8 when we're idle so that it can run the garbage
70+
// collector. The interface to this is V8::IdleNotification(). It returns
71+
// true if the heap hasn't be fully compacted, and needs to be run again.
72+
// Returning false means that it doesn't have anymore work to do.
73+
//
74+
// A rather convoluted algorithm has been devised to determine when Node is
75+
// idle. You'll have to figure it out for yourself.
76+
uv_check_t gc_check;
77+
uv_idle_t gc_idle;
78+
uv_timer_t gc_timer;
79+
bool need_gc;
80+
81+
# define FAST_TICK 700.
82+
# define GC_WAIT_TIME 5000.
83+
# define RPM_SAMPLES 100
84+
85+
int64_t tick_times[RPM_SAMPLES];
86+
int tick_time_head;
87+
};
88+
89+
struct globals* globals_get();
90+
91+
} // namespace node
92+
#endif // NODE_VARS_H

src/stream_wrap.cc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626
#include <tcp_wrap.h>
2727
#include <req_wrap.h>
2828

29+
#include <node_vars.h>
30+
31+
// We do the following to minimize the detal between v0.6 branch. We want to
32+
// use the variables as they were being used before.
33+
#define slab_used NODE_VAR(slab_used)
34+
#define slab_sym NODE_VAR(slab_sym)
35+
#define handle_that_last_alloced NODE_VAR(handle_that_last_alloced)
36+
#define buffer_sym NODE_VAR(buffer_sym)
37+
#define write_queue_size_sym NODE_VAR(write_queue_size_sym)
38+
#define stream_wrap_initialized NODE_VAR(stream_wrap_initialized)
39+
2940

3041
namespace node {
3142

@@ -66,19 +77,11 @@ typedef class ReqWrap<uv_shutdown_t> ShutdownWrap;
6677
typedef class ReqWrap<uv_write_t> WriteWrap;
6778

6879

69-
static size_t slab_used;
70-
static uv_stream_t* handle_that_last_alloced;
71-
static Persistent<String> slab_sym;
72-
static Persistent<String> buffer_sym;
73-
static Persistent<String> write_queue_size_sym;
74-
static bool initialized;
75-
76-
7780
void StreamWrap::Initialize(Handle<Object> target) {
78-
if (initialized) {
81+
if (stream_wrap_initialized) {
7982
return;
8083
} else {
81-
initialized = true;
84+
stream_wrap_initialized = true;
8285
}
8386

8487
HandleScope scope;

src/tcp_wrap.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@
5555
return scope.Close(Integer::New(-1)); \
5656
}
5757

58+
#include <node_vars.h>
59+
60+
// We do the following to minimize the detal between v0.6 branch. We want to
61+
// use the variables as they were being used before.
62+
#define tcpConstructor NODE_VAR(tcpConstructor)
63+
#define family_symbol NODE_VAR(family_symbol)
64+
#define address_symbol NODE_VAR(address_symbol)
65+
#define port_symbol NODE_VAR(port_symbol)
66+
5867
namespace node {
5968

6069
using v8::Object;
@@ -72,12 +81,6 @@ using v8::Arguments;
7281
using v8::Integer;
7382
using v8::Undefined;
7483

75-
static Persistent<Function> tcpConstructor;
76-
static Persistent<String> family_symbol;
77-
static Persistent<String> address_symbol;
78-
static Persistent<String> port_symbol;
79-
80-
8184
typedef class ReqWrap<uv_connect_t> ConnectWrap;
8285

8386

0 commit comments

Comments
 (0)