eventmachine 0.12.10-x86-mswin32-60 → 1.0.0.beta.2-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. data/.gitignore +2 -0
  2. data/Gemfile +1 -0
  3. data/README +80 -81
  4. data/Rakefile +7 -370
  5. data/docs/COPYING +60 -60
  6. data/docs/ChangeLog +211 -211
  7. data/docs/DEFERRABLES +246 -133
  8. data/docs/EPOLL +141 -141
  9. data/docs/GNU +281 -281
  10. data/docs/INSTALL +13 -13
  11. data/docs/KEYBOARD +42 -38
  12. data/docs/LEGAL +25 -25
  13. data/docs/LIGHTWEIGHT_CONCURRENCY +130 -70
  14. data/docs/PURE_RUBY +75 -75
  15. data/docs/RELEASE_NOTES +94 -94
  16. data/docs/SMTP +4 -2
  17. data/docs/SPAWNED_PROCESSES +148 -89
  18. data/docs/TODO +8 -8
  19. data/eventmachine.gemspec +19 -26
  20. data/examples/ex_channel.rb +42 -42
  21. data/examples/ex_queue.rb +2 -2
  22. data/examples/ex_tick_loop_array.rb +15 -0
  23. data/examples/ex_tick_loop_counter.rb +32 -0
  24. data/examples/helper.rb +1 -1
  25. data/ext/binder.cpp +0 -1
  26. data/ext/cmain.cpp +36 -25
  27. data/ext/ed.cpp +104 -113
  28. data/ext/ed.h +24 -30
  29. data/ext/em.cpp +349 -283
  30. data/ext/em.h +25 -29
  31. data/ext/eventmachine.h +5 -4
  32. data/ext/extconf.rb +58 -49
  33. data/ext/fastfilereader/extconf.rb +5 -3
  34. data/ext/fastfilereader/mapper.cpp +214 -214
  35. data/ext/fastfilereader/mapper.h +59 -59
  36. data/ext/fastfilereader/rubymain.cpp +127 -127
  37. data/ext/kb.cpp +1 -3
  38. data/ext/page.cpp +107 -107
  39. data/ext/page.h +51 -51
  40. data/ext/pipe.cpp +9 -11
  41. data/ext/project.h +12 -8
  42. data/ext/rubymain.cpp +138 -104
  43. data/java/.classpath +8 -8
  44. data/java/.project +17 -17
  45. data/java/src/com/rubyeventmachine/EmReactor.java +1 -0
  46. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -40
  47. data/lib/em/buftok.rb +138 -138
  48. data/lib/em/callback.rb +25 -25
  49. data/lib/em/channel.rb +1 -1
  50. data/lib/em/connection.rb +6 -1
  51. data/lib/em/deferrable.rb +16 -2
  52. data/lib/em/file_watch.rb +53 -53
  53. data/lib/em/future.rb +61 -61
  54. data/lib/em/iterator.rb +270 -0
  55. data/lib/em/messages.rb +66 -66
  56. data/lib/em/process_watch.rb +43 -43
  57. data/lib/em/protocols.rb +1 -1
  58. data/lib/em/protocols/header_and_content.rb +138 -138
  59. data/lib/em/protocols/httpclient.rb +267 -262
  60. data/lib/em/protocols/line_protocol.rb +28 -0
  61. data/lib/em/protocols/memcache.rb +322 -322
  62. data/lib/em/protocols/postgres3.rb +247 -247
  63. data/lib/em/protocols/saslauth.rb +175 -175
  64. data/lib/em/protocols/smtpserver.rb +640 -547
  65. data/lib/em/protocols/stomp.rb +200 -200
  66. data/lib/em/protocols/tcptest.rb +52 -52
  67. data/lib/{pr_eventmachine.rb → em/pure_ruby.rb} +1013 -1022
  68. data/lib/em/queue.rb +1 -0
  69. data/lib/em/spawnable.rb +85 -85
  70. data/lib/em/streamer.rb +130 -130
  71. data/lib/em/tick_loop.rb +85 -0
  72. data/lib/em/timers.rb +2 -1
  73. data/lib/em/version.rb +1 -1
  74. data/lib/eventmachine.rb +40 -84
  75. data/lib/jeventmachine.rb +2 -1
  76. data/lib/rubyeventmachine.rb +2 -0
  77. data/setup.rb +1585 -1585
  78. data/tasks/doc.rake +30 -0
  79. data/tasks/package.rake +85 -0
  80. data/tasks/test.rake +6 -0
  81. data/tests/client.crt +31 -31
  82. data/tests/client.key +51 -51
  83. data/tests/test_attach.rb +13 -3
  84. data/tests/test_basic.rb +60 -95
  85. data/tests/test_channel.rb +3 -2
  86. data/tests/test_defer.rb +49 -47
  87. data/tests/test_deferrable.rb +35 -0
  88. data/tests/test_error_handler.rb +35 -35
  89. data/tests/test_errors.rb +82 -82
  90. data/tests/test_exc.rb +55 -55
  91. data/tests/test_file_watch.rb +49 -49
  92. data/tests/test_futures.rb +198 -198
  93. data/tests/test_handler_check.rb +36 -36
  94. data/tests/test_hc.rb +190 -218
  95. data/tests/test_httpclient.rb +227 -218
  96. data/tests/test_httpclient2.rb +3 -2
  97. data/tests/test_inactivity_timeout.rb +3 -3
  98. data/tests/test_kb.rb +60 -60
  99. data/tests/test_ltp.rb +13 -5
  100. data/tests/test_ltp2.rb +317 -317
  101. data/tests/test_next_tick.rb +1 -1
  102. data/tests/test_object_protocol.rb +36 -36
  103. data/tests/test_pending_connect_timeout.rb +2 -2
  104. data/tests/test_process_watch.rb +50 -48
  105. data/tests/test_proxy_connection.rb +52 -0
  106. data/tests/test_pure.rb +134 -125
  107. data/tests/test_queue.rb +44 -44
  108. data/tests/test_running.rb +42 -42
  109. data/tests/test_sasl.rb +72 -72
  110. data/tests/test_send_file.rb +251 -242
  111. data/tests/test_servers.rb +76 -76
  112. data/tests/test_smtpclient.rb +83 -83
  113. data/tests/test_smtpserver.rb +85 -85
  114. data/tests/test_spawn.rb +322 -322
  115. data/tests/test_ssl_methods.rb +49 -49
  116. data/tests/test_ssl_verify.rb +82 -82
  117. data/tests/test_tick_loop.rb +59 -0
  118. data/tests/test_timers.rb +13 -15
  119. data/tests/test_ud.rb +36 -36
  120. data/tests/testem.rb +31 -31
  121. metadata +66 -51
  122. data/ext/cplusplus.cpp +0 -202
  123. data/ext/emwin.cpp +0 -300
  124. data/ext/emwin.h +0 -94
  125. data/ext/epoll.cpp +0 -26
  126. data/ext/epoll.h +0 -25
  127. data/ext/eventmachine_cpp.h +0 -96
  128. data/ext/files.cpp +0 -94
  129. data/ext/files.h +0 -65
  130. data/ext/sigs.cpp +0 -89
  131. data/ext/sigs.h +0 -32
  132. data/java/src/com/rubyeventmachine/application/Application.java +0 -194
  133. data/java/src/com/rubyeventmachine/application/Connection.java +0 -74
  134. data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +0 -37
  135. data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +0 -46
  136. data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +0 -38
  137. data/java/src/com/rubyeventmachine/application/Timer.java +0 -54
  138. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +0 -109
  139. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +0 -148
  140. data/java/src/com/rubyeventmachine/tests/EMTest.java +0 -80
  141. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +0 -53
  142. data/java/src/com/rubyeventmachine/tests/TestServers.java +0 -75
  143. data/java/src/com/rubyeventmachine/tests/TestTimers.java +0 -90
  144. data/lib/evma.rb +0 -32
  145. data/lib/evma/callback.rb +0 -32
  146. data/lib/evma/container.rb +0 -75
  147. data/lib/evma/factory.rb +0 -77
  148. data/lib/evma/protocol.rb +0 -87
  149. data/lib/evma/reactor.rb +0 -48
  150. data/web/whatis +0 -7
data/ext/em.h CHANGED
@@ -17,16 +17,6 @@ See the file COPYING for complete licensing information.
17
17
 
18
18
  *****************************************************************************/
19
19
 
20
-
21
-
22
- #ifdef OS_WIN32
23
- #include "emwin.h"
24
- #endif
25
-
26
-
27
- // THIS ENTIRE FILE WILL EVENTUALLY BE FOR UNIX BUILDS ONLY.
28
- //#ifdef OS_UNIX
29
-
30
20
  #ifndef __EventMachine__H_
31
21
  #define __EventMachine__H_
32
22
 
@@ -57,16 +47,6 @@ See the file COPYING for complete licensing information.
57
47
  #define EmSelect select
58
48
  #endif
59
49
 
60
-
61
- #ifdef OS_UNIX
62
- typedef long long Int64;
63
- #endif
64
- #ifdef OS_WIN32
65
- typedef __int64 Int64;
66
- #endif
67
-
68
- extern Int64 gCurrentLoopTime;
69
-
70
50
  class EventableDescriptor;
71
51
  class InotifyDescriptor;
72
52
 
@@ -82,7 +62,7 @@ class EventMachine_t
82
62
  static void SetMaxTimerCount (int);
83
63
 
84
64
  public:
85
- EventMachine_t (void(*event_callback)(const unsigned long, int, const char*, const unsigned long));
65
+ EventMachine_t (EMCallback);
86
66
  virtual ~EventMachine_t();
87
67
 
88
68
  void Run();
@@ -95,7 +75,6 @@ class EventMachine_t
95
75
  const unsigned long CreateTcpServer (const char *, int);
96
76
  const unsigned long OpenDatagramSocket (const char *, int);
97
77
  const unsigned long CreateUnixDomainServer (const char*);
98
- const unsigned long _OpenFileForWriting (const char*);
99
78
  const unsigned long OpenKeyboard();
100
79
  //const char *Popen (const char*, const char*);
101
80
  const unsigned long Socketpair (char* const*);
@@ -137,6 +116,8 @@ class EventMachine_t
137
116
  void _HandleKqueuePidEvent (struct kevent*);
138
117
  #endif
139
118
 
119
+ uint64_t GetCurrentLoopTime() { return MyCurrentLoopTime; }
120
+
140
121
  // Temporary:
141
122
  void _UseEpoll();
142
123
  void _UseKqueue();
@@ -144,6 +125,11 @@ class EventMachine_t
144
125
  bool UsingKqueue() { return bKqueue; }
145
126
  bool UsingEpoll() { return bEpoll; }
146
127
 
128
+ void QueueHeartbeat(EventableDescriptor*);
129
+ void ClearHeartbeat(uint64_t);
130
+
131
+ uint64_t GetRealTime();
132
+
147
133
  private:
148
134
  bool _RunOnce();
149
135
  bool _RunTimers();
@@ -151,12 +137,16 @@ class EventMachine_t
151
137
  void _AddNewDescriptors();
152
138
  void _ModifyDescriptors();
153
139
  void _InitializeLoopBreaker();
140
+ void _CleanupSockets();
154
141
 
155
142
  bool _RunSelectOnce();
156
143
  bool _RunEpollOnce();
157
144
  bool _RunKqueueOnce();
158
145
 
159
146
  void _ModifyEpollEvent (EventableDescriptor*);
147
+ void _DispatchHeartbeats();
148
+ timeval _TimeTilNextEvent();
149
+ void _CleanBadDescriptors();
160
150
 
161
151
  public:
162
152
  void _ReadLoopBreaker();
@@ -168,19 +158,20 @@ class EventMachine_t
168
158
  MaxEvents = 4096
169
159
  };
170
160
  int HeartbeatInterval;
171
- void (*EventCallback)(const unsigned long, int, const char*, const unsigned long);
161
+ EMCallback EventCallback;
172
162
 
173
163
  class Timer_t: public Bindable_t {
174
164
  };
175
165
 
176
- multimap<Int64, Timer_t> Timers;
166
+ multimap<uint64_t, Timer_t> Timers;
167
+ multimap<uint64_t, EventableDescriptor*> Heartbeats;
177
168
  map<int, Bindable_t*> Files;
178
169
  map<int, Bindable_t*> Pids;
179
170
  vector<EventableDescriptor*> Descriptors;
180
171
  vector<EventableDescriptor*> NewDescriptors;
181
172
  set<EventableDescriptor*> ModifiedDescriptors;
182
173
 
183
- Int64 NextHeartbeatTime;
174
+ uint64_t NextHeartbeatTime;
184
175
 
185
176
  int LoopBreakerReader;
186
177
  int LoopBreakerWriter;
@@ -190,7 +181,16 @@ class EventMachine_t
190
181
 
191
182
  timeval Quantum;
192
183
 
184
+ uint64_t MyCurrentLoopTime;
185
+
186
+ #ifdef OS_WIN32
187
+ unsigned TickCountTickover;
188
+ unsigned LastTickCount;
189
+ #endif
190
+
193
191
  private:
192
+ bool bTerminateSignalReceived;
193
+
194
194
  bool bEpoll;
195
195
  int epfd; // Epoll file-descriptor
196
196
  #ifdef HAVE_EPOLL
@@ -225,8 +225,4 @@ struct SelectData_t
225
225
  int nSockets;
226
226
  };
227
227
 
228
-
229
-
230
228
  #endif // __EventMachine__H_
231
-
232
- //#endif // OS_UNIX
@@ -35,11 +35,12 @@ extern "C" {
35
35
  EM_CONNECTION_NOTIFY_WRITABLE = 107,
36
36
  EM_SSL_HANDSHAKE_COMPLETED = 108,
37
37
  EM_SSL_VERIFY = 109,
38
- EM_PROXY_TARGET_UNBOUND = 110
38
+ EM_PROXY_TARGET_UNBOUND = 110,
39
+ EM_PROXY_COMPLETED = 111
39
40
 
40
41
  };
41
42
 
42
- void evma_initialize_library (void(*)(const unsigned long, int, const char*, const unsigned long));
43
+ void evma_initialize_library (EMCallback);
43
44
  void evma_run_machine();
44
45
  void evma_release_library();
45
46
  const unsigned long evma_install_oneshot_timer (int seconds);
@@ -96,7 +97,6 @@ extern "C" {
96
97
  float evma_get_heartbeat_interval();
97
98
  int evma_set_heartbeat_interval(float);
98
99
 
99
- const unsigned long evma__write_file (const char *filename);
100
100
  const unsigned long evma_popen (char * const*cmd_strings);
101
101
 
102
102
  const unsigned long evma_watch_filename (const char *fname);
@@ -105,7 +105,7 @@ extern "C" {
105
105
  const unsigned long evma_watch_pid (int);
106
106
  void evma_unwatch_pid (const unsigned long);
107
107
 
108
- void evma_start_proxy(const unsigned long, const unsigned long, const unsigned long);
108
+ void evma_start_proxy(const unsigned long, const unsigned long, const unsigned long, const unsigned long);
109
109
  void evma_stop_proxy(const unsigned long);
110
110
 
111
111
  int evma_set_rlimit_nofile (int n_files);
@@ -113,6 +113,7 @@ extern "C" {
113
113
  void evma_set_epoll (int use);
114
114
  void evma_set_kqueue (int use);
115
115
 
116
+ uint64_t evma_get_current_loop_time();
116
117
  #if __cplusplus
117
118
  }
118
119
  #endif
@@ -1,3 +1,4 @@
1
+ require 'fileutils'
1
2
  require 'mkmf'
2
3
 
3
4
  def check_libs libs = [], fatal = false
@@ -12,12 +13,63 @@ def add_define(name)
12
13
  $defs.push("-D#{name}")
13
14
  end
14
15
 
16
+ ##
17
+ # OpenSSL:
18
+
19
+ # override append_library, so it actually appends (instead of prepending)
20
+ # this fixes issues with linking ssl, since libcrypto depends on symbols in libssl
21
+ def append_library(libs, lib)
22
+ libs + " " + format(LIBARG, lib)
23
+ end
24
+
25
+ def manual_ssl_config
26
+ ssl_libs_heads_args = {
27
+ :unix => [%w[ssl crypto], %w[openssl/ssl.h openssl/err.h]],
28
+ :mswin => [%w[ssleay32 libeay32], %w[openssl/ssl.h openssl/err.h]],
29
+ }
30
+
31
+ dc_flags = ['ssl']
32
+ dc_flags += ["#{ENV['OPENSSL']}/include", ENV['OPENSSL']] if /linux/ =~ RUBY_PLATFORM and ENV['OPENSSL']
33
+
34
+ libs, heads = case RUBY_PLATFORM
35
+ when /mswin/ ; ssl_libs_heads_args[:mswin]
36
+ else ssl_libs_heads_args[:unix]
37
+ end
38
+ dir_config(*dc_flags)
39
+ check_libs(libs) and check_heads(heads)
40
+ end
41
+
42
+ if ENV['CROSS_COMPILING']
43
+ openssl_dir = File.expand_path("~/.rake-compiler/builds/openssl-1.0.0a/")
44
+ if File.exists?(openssl_dir)
45
+ FileUtils.mkdir_p Dir.pwd+"/openssl/"
46
+ FileUtils.cp Dir[openssl_dir+"/include/openssl/*.h"], Dir.pwd+"/openssl/", :verbose => true
47
+ FileUtils.cp Dir[openssl_dir+"/lib*.a"], Dir.pwd, :verbose => true
48
+ $INCFLAGS << " -I#{Dir.pwd}" # for the openssl headers
49
+ else
50
+ STDERR.puts
51
+ STDERR.puts "**************************************************************************************"
52
+ STDERR.puts "**** Cross-compiled OpenSSL not found"
53
+ STDERR.puts "**** Run: hg clone http://bitbucket.org/ged/ruby-pg && cd ruby-pg && rake openssl_libs"
54
+ STDERR.puts "**************************************************************************************"
55
+ STDERR.puts
56
+ end
57
+ end
58
+
59
+ # Try to use pkg_config first, fixes #73
60
+ if pkg_config('openssl') || manual_ssl_config
61
+ add_define "WITH_SSL"
62
+ else
63
+ add_define "WITHOUT_SSL"
64
+ end
65
+
15
66
  add_define 'BUILD_FOR_RUBY'
16
67
  add_define 'HAVE_RBTRAP' if have_var('rb_trap_immediate', ['ruby.h', 'rubysig.h'])
17
68
  add_define "HAVE_TBR" if have_func('rb_thread_blocking_region')# and have_macro('RUBY_UBF_IO', 'ruby.h')
18
69
  add_define "HAVE_INOTIFY" if inotify = have_func('inotify_init', 'sys/inotify.h')
19
70
  add_define "HAVE_OLD_INOTIFY" if !inotify && have_macro('__NR_inotify_init', 'sys/syscall.h')
20
71
  add_define 'HAVE_WRITEV' if have_func('writev', 'sys/uio.h')
72
+
21
73
  have_func('rb_thread_check_ints')
22
74
  have_func('rb_time_new')
23
75
 
@@ -53,11 +105,13 @@ when /solaris/
53
105
  add_define 'OS_SOLARIS8'
54
106
  check_libs(%w[nsl socket], true)
55
107
 
56
- # Patch by Tim Pease, fixes SUNWspro compile problems.
57
- if CONFIG['CC'] == 'cc'
108
+ if CONFIG['CC'] == 'cc' and `cc -flags 2>&1` =~ /Sun/ # detect SUNWspro compiler
58
109
  # SUN CHAIN
59
- $CFLAGS = CONFIG['CFLAGS'] = "-KPIC -G"
110
+ add_define 'CC_SUNWspro'
111
+ $preload = ["\nCXX = CC"] # hack a CXX= line into the makefile
112
+ $CFLAGS = CONFIG['CFLAGS'] = "-KPIC"
60
113
  CONFIG['CCDLFLAGS'] = "-KPIC"
114
+ CONFIG['LDSHARED'] = "$(CXX) -G -KPIC -lCstd"
61
115
  else
62
116
  # GNU CHAIN
63
117
  # on Unix we need a g++ link, not gcc.
@@ -79,19 +133,6 @@ when /darwin/
79
133
  when /linux/
80
134
  add_define 'HAVE_EPOLL' if have_func('epoll_create', 'sys/epoll.h')
81
135
 
82
- # Original epoll test is inadequate because 2.4 kernels have the header
83
- # but not the code.
84
- # add_define 'HAVE_EPOLL' if have_header('sys/epoll.h')
85
- # if have_header('sys/epoll.h')
86
- # File.open("hasEpollTest.c", "w") {|f|
87
- # f.puts "#include <sys/epoll.h>"
88
- # f.puts "int main() { epoll_create(1024); return 0;}"
89
- # }
90
- # (e = system( "gcc hasEpollTest.c -o hasEpollTest " )) and (e = $?.to_i)
91
- # `rm -f hasEpollTest.c hasEpollTest`
92
- # add_define 'HAVE_EPOLL' if e == 0
93
- # end
94
-
95
136
  # on Unix we need a g++ link, not gcc.
96
137
  CONFIG['LDSHARED'] = "$(CXX) -shared"
97
138
 
@@ -103,38 +144,6 @@ else
103
144
  CONFIG['LDSHARED'] = "$(CXX) -shared"
104
145
  end
105
146
 
106
- # OpenSSL:
107
-
108
- def manual_ssl_config
109
- ssl_libs_heads_args = {
110
- :unix => [%w[ssl crypto], %w[openssl/ssl.h openssl/err.h]],
111
- :darwin => [%w[ssl crypto C], %w[openssl/ssl.h openssl/err.h]],
112
- # openbsd and linux:
113
- :crypto_hack => [%w[crypto ssl crypto], %w[openssl/ssl.h openssl/err.h]],
114
- :mswin => [%w[ssleay32 libeay32], %w[openssl/ssl.h openssl/err.h]],
115
- }
116
-
117
- dc_flags = ['ssl']
118
- dc_flags += ["#{ENV['OPENSSL']}/include", ENV['OPENSSL']] if /linux/ =~ RUBY_PLATFORM
119
-
120
- libs, heads = case RUBY_PLATFORM
121
- when /mswin/ ; ssl_libs_heads_args[:mswin]
122
- when /mingw/ ; ssl_libs_heads_args[:unix]
123
- when /darwin/ ; ssl_libs_heads_args[:darwin]
124
- when /openbsd/ ; ssl_libs_heads_args[:crypto_hack]
125
- when /linux/ ; ssl_libs_heads_args[:crypto_hack]
126
- else ssl_libs_heads_args[:unix]
127
- end
128
- dir_config(*dc_flags)
129
- check_libs(libs) and check_heads(heads)
130
- end
131
-
132
- # Try to use pkg_config first, fixes #73
133
- if pkg_config('openssl') || manual_ssl_config
134
- add_define "WITH_SSL"
135
- else
136
- add_define "WITHOUT_SSL"
137
- end
138
147
 
139
148
  # solaris c++ compiler doesn't have make_pair()
140
149
  TRY_LINK.sub!('$(CC)', '$(CXX)')
@@ -145,4 +154,4 @@ add_define 'HAVE_MAKE_PAIR' if try_link(<<SRC, '-lstdc++')
145
154
  SRC
146
155
  TRY_LINK.sub!('$(CXX)', '$(CC)')
147
156
 
148
- create_makefile "rubyeventmachine"
157
+ create_makefile "rubyeventmachine"
@@ -44,11 +44,13 @@ when /solaris/
44
44
  add_define 'OS_SOLARIS8'
45
45
  check_libs(%w[nsl socket], true)
46
46
 
47
- # Patch by Tim Pease, fixes SUNWspro compile problems.
48
- if CONFIG['CC'] == 'cc'
47
+ if CONFIG['CC'] == 'cc' and `cc -flags 2>&1` =~ /Sun/ # detect SUNWspro compiler
49
48
  # SUN CHAIN
50
- $CFLAGS = CONFIG['CFLAGS'] = "-KPIC -G"
49
+ add_define 'CC_SUNWspro'
50
+ $preload = ["\nCXX = CC"] # hack a CXX= line into the makefile
51
+ $CFLAGS = CONFIG['CFLAGS'] = "-KPIC"
51
52
  CONFIG['CCDLFLAGS'] = "-KPIC"
53
+ CONFIG['LDSHARED'] = "$(CXX) -G -KPIC -lCstd"
52
54
  else
53
55
  # GNU CHAIN
54
56
  # on Unix we need a g++ link, not gcc.
@@ -1,214 +1,214 @@
1
- /*****************************************************************************
2
-
3
- $Id: mapper.cpp 4527 2007-07-04 10:21:34Z francis $
4
-
5
- File: mapper.cpp
6
- Date: 02Jul07
7
-
8
- Copyright (C) 2007 by Francis Cianfrocca. All Rights Reserved.
9
- Gmail: garbagecat10
10
-
11
- This program is free software; you can redistribute it and/or modify
12
- it under the terms of either: 1) the GNU General Public License
13
- as published by the Free Software Foundation; either version 2 of the
14
- License, or (at your option) any later version; or 2) Ruby's License.
15
-
16
- See the file COPYING for complete licensing information.
17
-
18
- *****************************************************************************/
19
-
20
-
21
- //////////////////////////////////////////////////////////////////////
22
- // UNIX implementation
23
- //////////////////////////////////////////////////////////////////////
24
-
25
-
26
- #ifdef OS_UNIX
27
-
28
- #include <sys/types.h>
29
- #include <sys/stat.h>
30
- #include <sys/mman.h>
31
- #include <fcntl.h>
32
- #include <errno.h>
33
-
34
- #include <iostream>
35
- #include "unistd.h"
36
- #include <string>
37
- #include <cstring>
38
- #include <stdexcept>
39
- using namespace std;
40
-
41
- #include "mapper.h"
42
-
43
- /******************
44
- Mapper_t::Mapper_t
45
- ******************/
46
-
47
- Mapper_t::Mapper_t (const string &filename)
48
- {
49
- /* We ASSUME we can open the file.
50
- * (More precisely, we assume someone else checked before we got here.)
51
- */
52
-
53
- Fd = open (filename.c_str(), O_RDONLY);
54
- if (Fd < 0)
55
- throw runtime_error (strerror (errno));
56
-
57
- struct stat st;
58
- if (fstat (Fd, &st))
59
- throw runtime_error (strerror (errno));
60
- FileSize = st.st_size;
61
-
62
- #ifdef OS_WIN32
63
- MapPoint = (char*) mmap (0, FileSize, PROT_READ, MAP_SHARED, Fd, 0);
64
- #else
65
- MapPoint = (const char*) mmap (0, FileSize, PROT_READ, MAP_SHARED, Fd, 0);
66
- #endif
67
- if (MapPoint == MAP_FAILED)
68
- throw runtime_error (strerror (errno));
69
- }
70
-
71
-
72
- /*******************
73
- Mapper_t::~Mapper_t
74
- *******************/
75
-
76
- Mapper_t::~Mapper_t()
77
- {
78
- Close();
79
- }
80
-
81
-
82
- /***************
83
- Mapper_t::Close
84
- ***************/
85
-
86
- void Mapper_t::Close()
87
- {
88
- // Can be called multiple times.
89
- // Calls to GetChunk are invalid after a call to Close.
90
- if (MapPoint) {
91
- #ifdef OS_SOLARIS8
92
- munmap ((char*)MapPoint, FileSize);
93
- #else
94
- munmap ((void*)MapPoint, FileSize);
95
- #endif
96
- MapPoint = NULL;
97
- }
98
- if (Fd >= 0) {
99
- close (Fd);
100
- Fd = -1;
101
- }
102
- }
103
-
104
- /******************
105
- Mapper_t::GetChunk
106
- ******************/
107
-
108
- const char *Mapper_t::GetChunk (unsigned start)
109
- {
110
- return MapPoint + start;
111
- }
112
-
113
-
114
-
115
- #endif // OS_UNIX
116
-
117
-
118
- //////////////////////////////////////////////////////////////////////
119
- // WINDOWS implementation
120
- //////////////////////////////////////////////////////////////////////
121
-
122
- #ifdef OS_WIN32
123
-
124
- #include <windows.h>
125
-
126
- #include <iostream>
127
- #include <string>
128
- #include <stdexcept>
129
- using namespace std;
130
-
131
- #include "mapper.h"
132
-
133
- /******************
134
- Mapper_t::Mapper_t
135
- ******************/
136
-
137
- Mapper_t::Mapper_t (const string &filename)
138
- {
139
- /* We ASSUME we can open the file.
140
- * (More precisely, we assume someone else checked before we got here.)
141
- */
142
-
143
- hFile = INVALID_HANDLE_VALUE;
144
- hMapping = NULL;
145
- MapPoint = NULL;
146
- FileSize = 0;
147
-
148
- hFile = CreateFile (filename.c_str(), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
149
-
150
- if (hFile == INVALID_HANDLE_VALUE)
151
- throw runtime_error ("File not found");
152
-
153
- BY_HANDLE_FILE_INFORMATION i;
154
- if (GetFileInformationByHandle (hFile, &i))
155
- FileSize = i.nFileSizeLow;
156
-
157
- hMapping = CreateFileMapping (hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
158
- if (!hMapping)
159
- throw runtime_error ("File not mapped");
160
-
161
- #ifdef OS_WIN32
162
- MapPoint = (char*) MapViewOfFile (hMapping, FILE_MAP_WRITE, 0, 0, 0);
163
- #else
164
- MapPoint = (const char*) MapViewOfFile (hMapping, FILE_MAP_WRITE, 0, 0, 0);
165
- #endif
166
- if (!MapPoint)
167
- throw runtime_error ("Mappoint not read");
168
- }
169
-
170
-
171
- /*******************
172
- Mapper_t::~Mapper_t
173
- *******************/
174
-
175
- Mapper_t::~Mapper_t()
176
- {
177
- Close();
178
- }
179
-
180
- /***************
181
- Mapper_t::Close
182
- ***************/
183
-
184
- void Mapper_t::Close()
185
- {
186
- // Can be called multiple times.
187
- // Calls to GetChunk are invalid after a call to Close.
188
- if (MapPoint) {
189
- UnmapViewOfFile (MapPoint);
190
- MapPoint = NULL;
191
- }
192
- if (hMapping != NULL) {
193
- CloseHandle (hMapping);
194
- hMapping = NULL;
195
- }
196
- if (hFile != INVALID_HANDLE_VALUE) {
197
- CloseHandle (hFile);
198
- hMapping = INVALID_HANDLE_VALUE;
199
- }
200
- }
201
-
202
-
203
- /******************
204
- Mapper_t::GetChunk
205
- ******************/
206
-
207
- const char *Mapper_t::GetChunk (unsigned start)
208
- {
209
- return MapPoint + start;
210
- }
211
-
212
-
213
-
214
- #endif // OS_WINDOWS
1
+ /*****************************************************************************
2
+
3
+ $Id: mapper.cpp 4527 2007-07-04 10:21:34Z francis $
4
+
5
+ File: mapper.cpp
6
+ Date: 02Jul07
7
+
8
+ Copyright (C) 2007 by Francis Cianfrocca. All Rights Reserved.
9
+ Gmail: garbagecat10
10
+
11
+ This program is free software; you can redistribute it and/or modify
12
+ it under the terms of either: 1) the GNU General Public License
13
+ as published by the Free Software Foundation; either version 2 of the
14
+ License, or (at your option) any later version; or 2) Ruby's License.
15
+
16
+ See the file COPYING for complete licensing information.
17
+
18
+ *****************************************************************************/
19
+
20
+
21
+ //////////////////////////////////////////////////////////////////////
22
+ // UNIX implementation
23
+ //////////////////////////////////////////////////////////////////////
24
+
25
+
26
+ #ifdef OS_UNIX
27
+
28
+ #include <sys/types.h>
29
+ #include <sys/stat.h>
30
+ #include <sys/mman.h>
31
+ #include <fcntl.h>
32
+ #include <errno.h>
33
+
34
+ #include <iostream>
35
+ #include "unistd.h"
36
+ #include <string>
37
+ #include <cstring>
38
+ #include <stdexcept>
39
+ using namespace std;
40
+
41
+ #include "mapper.h"
42
+
43
+ /******************
44
+ Mapper_t::Mapper_t
45
+ ******************/
46
+
47
+ Mapper_t::Mapper_t (const string &filename)
48
+ {
49
+ /* We ASSUME we can open the file.
50
+ * (More precisely, we assume someone else checked before we got here.)
51
+ */
52
+
53
+ Fd = open (filename.c_str(), O_RDONLY);
54
+ if (Fd < 0)
55
+ throw runtime_error (strerror (errno));
56
+
57
+ struct stat st;
58
+ if (fstat (Fd, &st))
59
+ throw runtime_error (strerror (errno));
60
+ FileSize = st.st_size;
61
+
62
+ #ifdef OS_WIN32
63
+ MapPoint = (char*) mmap (0, FileSize, PROT_READ, MAP_SHARED, Fd, 0);
64
+ #else
65
+ MapPoint = (const char*) mmap (0, FileSize, PROT_READ, MAP_SHARED, Fd, 0);
66
+ #endif
67
+ if (MapPoint == MAP_FAILED)
68
+ throw runtime_error (strerror (errno));
69
+ }
70
+
71
+
72
+ /*******************
73
+ Mapper_t::~Mapper_t
74
+ *******************/
75
+
76
+ Mapper_t::~Mapper_t()
77
+ {
78
+ Close();
79
+ }
80
+
81
+
82
+ /***************
83
+ Mapper_t::Close
84
+ ***************/
85
+
86
+ void Mapper_t::Close()
87
+ {
88
+ // Can be called multiple times.
89
+ // Calls to GetChunk are invalid after a call to Close.
90
+ if (MapPoint) {
91
+ #ifdef CC_SUNWspro
92
+ munmap ((char*)MapPoint, FileSize);
93
+ #else
94
+ munmap ((void*)MapPoint, FileSize);
95
+ #endif
96
+ MapPoint = NULL;
97
+ }
98
+ if (Fd >= 0) {
99
+ close (Fd);
100
+ Fd = -1;
101
+ }
102
+ }
103
+
104
+ /******************
105
+ Mapper_t::GetChunk
106
+ ******************/
107
+
108
+ const char *Mapper_t::GetChunk (unsigned start)
109
+ {
110
+ return MapPoint + start;
111
+ }
112
+
113
+
114
+
115
+ #endif // OS_UNIX
116
+
117
+
118
+ //////////////////////////////////////////////////////////////////////
119
+ // WINDOWS implementation
120
+ //////////////////////////////////////////////////////////////////////
121
+
122
+ #ifdef OS_WIN32
123
+
124
+ #include <windows.h>
125
+
126
+ #include <iostream>
127
+ #include <string>
128
+ #include <stdexcept>
129
+ using namespace std;
130
+
131
+ #include "mapper.h"
132
+
133
+ /******************
134
+ Mapper_t::Mapper_t
135
+ ******************/
136
+
137
+ Mapper_t::Mapper_t (const string &filename)
138
+ {
139
+ /* We ASSUME we can open the file.
140
+ * (More precisely, we assume someone else checked before we got here.)
141
+ */
142
+
143
+ hFile = INVALID_HANDLE_VALUE;
144
+ hMapping = NULL;
145
+ MapPoint = NULL;
146
+ FileSize = 0;
147
+
148
+ hFile = CreateFile (filename.c_str(), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
149
+
150
+ if (hFile == INVALID_HANDLE_VALUE)
151
+ throw runtime_error ("File not found");
152
+
153
+ BY_HANDLE_FILE_INFORMATION i;
154
+ if (GetFileInformationByHandle (hFile, &i))
155
+ FileSize = i.nFileSizeLow;
156
+
157
+ hMapping = CreateFileMapping (hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
158
+ if (!hMapping)
159
+ throw runtime_error ("File not mapped");
160
+
161
+ #ifdef OS_WIN32
162
+ MapPoint = (char*) MapViewOfFile (hMapping, FILE_MAP_WRITE, 0, 0, 0);
163
+ #else
164
+ MapPoint = (const char*) MapViewOfFile (hMapping, FILE_MAP_WRITE, 0, 0, 0);
165
+ #endif
166
+ if (!MapPoint)
167
+ throw runtime_error ("Mappoint not read");
168
+ }
169
+
170
+
171
+ /*******************
172
+ Mapper_t::~Mapper_t
173
+ *******************/
174
+
175
+ Mapper_t::~Mapper_t()
176
+ {
177
+ Close();
178
+ }
179
+
180
+ /***************
181
+ Mapper_t::Close
182
+ ***************/
183
+
184
+ void Mapper_t::Close()
185
+ {
186
+ // Can be called multiple times.
187
+ // Calls to GetChunk are invalid after a call to Close.
188
+ if (MapPoint) {
189
+ UnmapViewOfFile (MapPoint);
190
+ MapPoint = NULL;
191
+ }
192
+ if (hMapping != NULL) {
193
+ CloseHandle (hMapping);
194
+ hMapping = NULL;
195
+ }
196
+ if (hFile != INVALID_HANDLE_VALUE) {
197
+ CloseHandle (hFile);
198
+ hMapping = INVALID_HANDLE_VALUE;
199
+ }
200
+ }
201
+
202
+
203
+ /******************
204
+ Mapper_t::GetChunk
205
+ ******************/
206
+
207
+ const char *Mapper_t::GetChunk (unsigned start)
208
+ {
209
+ return MapPoint + start;
210
+ }
211
+
212
+
213
+
214
+ #endif // OS_WINDOWS