sylvester 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --drb
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Gem dependencies
4
+
5
+ # Development dependencies
6
+ group :development do
7
+ gem "rspec", "~> 2.8.0"
8
+ gem "rdoc", "~> 3.12"
9
+ gem "bundler", "~> 1.1.0"
10
+ gem "jeweler", "~> 1.8.4"
11
+ gem "simplecov", "~> 0.6.4"
12
+ gem "spork", "~> 0.9.2"
13
+ gem "pry"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ coderay (1.0.7)
5
+ diff-lcs (1.1.3)
6
+ git (1.2.5)
7
+ jeweler (1.8.4)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rdoc
12
+ json (1.7.4)
13
+ method_source (0.8)
14
+ multi_json (1.3.6)
15
+ pry (0.9.10)
16
+ coderay (~> 1.0.5)
17
+ method_source (~> 0.8)
18
+ slop (~> 3.3.1)
19
+ rake (0.9.2.2)
20
+ rdoc (3.12)
21
+ json (~> 1.4)
22
+ rspec (2.8.0)
23
+ rspec-core (~> 2.8.0)
24
+ rspec-expectations (~> 2.8.0)
25
+ rspec-mocks (~> 2.8.0)
26
+ rspec-core (2.8.0)
27
+ rspec-expectations (2.8.0)
28
+ diff-lcs (~> 1.1.2)
29
+ rspec-mocks (2.8.0)
30
+ simplecov (0.6.4)
31
+ multi_json (~> 1.0)
32
+ simplecov-html (~> 0.5.3)
33
+ simplecov-html (0.5.3)
34
+ slop (3.3.2)
35
+ spork (0.9.2)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ bundler (~> 1.1.0)
42
+ jeweler (~> 1.8.4)
43
+ pry
44
+ rdoc (~> 3.12)
45
+ rspec (~> 2.8.0)
46
+ simplecov (~> 0.6.4)
47
+ spork (~> 0.9.2)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Jeremy Holland
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = sylvester
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to sylvester
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Jeremy Holland. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ require 'pry'
6
+
7
+ begin
8
+ Bundler.setup(:default, :development)
9
+ rescue Bundler::BundlerError => e
10
+ $stderr.puts e.message
11
+ $stderr.puts "Run `bundle install` to install missing gems"
12
+ exit e.status_code
13
+ end
14
+ require 'rake'
15
+
16
+ require 'jeweler'
17
+ Jeweler::Tasks.new do |gem|
18
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
19
+ gem.name = "sylvester"
20
+ gem.homepage = "http://github.com/awebneck/sylvester"
21
+ gem.license = "MIT"
22
+ gem.summary = %Q{A ruby library for interacting with, storing, and reading basic ruby objects from System V shared memory}
23
+ gem.description = %Q{The beginnings of a handy ruby interface for System V shared memory, making it accessible and easy to use directly from ruby. Plans are to simply treat it as a string to begin with, thereafter building more complex data structures that will implement the interfaces (where appropriate) of their stack-based counterparts.}
24
+ gem.email = "jeremy@jeremypholland.com"
25
+ gem.authors = ["Jeremy Holland"]
26
+ # dependencies defined in Gemfile
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rspec/core'
31
+ require 'rspec/core/rake_task'
32
+ RSpec::Core::RakeTask.new(:spec) do |spec|
33
+ spec.pattern = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
37
+ spec.pattern = 'spec/**/*_spec.rb'
38
+ spec.rcov = true
39
+ end
40
+
41
+ task :default => :spec
42
+
43
+ require 'rdoc/task'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "sylvester #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
52
+
53
+ desc "Compile for testing"
54
+ task :buildext do
55
+ puts `/usr/bin/env ruby #{File.dirname(__FILE__)}/ext/sylvester/extconf.rb`
56
+ puts `/usr/bin/make`
57
+ puts `rm *.o > /dev/null 2>&1`
58
+ puts `rm mkmf.log > /dev/null 2>&1`
59
+ puts `rm Makefile > /dev/null 2>&1`
60
+ puts `mkdir -p #{File.dirname(__FILE__)}/spec/build/sylvester > /dev/null 2>&1`
61
+ Kernel.exec "mv sylvester.{bundle,so} #{File.dirname(__FILE__)}/lib > /dev/null 2>&1"
62
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,43 @@
1
+ require "mkmf"
2
+
3
+ HEADER_DIRS = [
4
+ # First search /opt/local for macports
5
+ '/opt/local/include',
6
+ # Then search /usr/local for people that installed from source or homebrew
7
+ '/usr/local/include',
8
+ # Finally fall back to /usr
9
+ '/usr/include',
10
+ ]
11
+
12
+ LIB_DIRS = [
13
+ # First search /opt/local for macports
14
+ '/opt/local/lib',
15
+ # Then search /usr/local for people that installed from source or homebrew
16
+ '/usr/local/lib',
17
+ # Finally fall back to /usr
18
+ '/usr/lib',
19
+ ]
20
+
21
+ headers = [
22
+ "stdarg.h",
23
+ "time.h",
24
+ "sys/types.h",
25
+ "sys/ipc.h",
26
+ "sys/shm.h",
27
+ ]
28
+
29
+ libraries = [
30
+ ]
31
+
32
+ puts ">> check require libraries..."
33
+
34
+ libraries.each{|lib|
35
+ raise "lib#{lib} not found." unless have_library(lib)
36
+ }
37
+
38
+ puts ">> check require headers..."
39
+ headers.each{|header|
40
+ raise "#{header} not found." unless have_header(header)
41
+ }
42
+
43
+ create_makefile("sylvester/sylvester")
@@ -0,0 +1,28 @@
1
+ #include <sylvester.h>
2
+
3
+ /**
4
+ * Sylvester system initializer
5
+ */
6
+
7
+ /* ================= EXTERNAL VARIABLES ================== */
8
+
9
+ /**
10
+ * module Sylvester
11
+ */
12
+ VALUE mSylvester;
13
+
14
+ /* ================ RUBY ROOT INITIALIZER ================ */
15
+
16
+ void Init_sylvester(void) {
17
+
18
+ /**
19
+ * Define namespace modules
20
+ */
21
+ mSylvester = rb_define_module("Sylvester");
22
+
23
+ /**
24
+ * Initialize all systems in order of dependency
25
+ */
26
+ sylvester_init_errors();
27
+ sylvester_init_share();
28
+ }
@@ -0,0 +1,21 @@
1
+ #ifndef SYLVESTER
2
+ #define SYLVESTER
3
+
4
+ #include <sys/types.h>
5
+ #include <sys/ipc.h>
6
+ #include <sys/shm.h>
7
+ #include <stdio.h>
8
+ #include <time.h>
9
+ #include <string.h>
10
+ #include <errno.h>
11
+ #include <math.h>
12
+ #include "ruby.h"
13
+
14
+ #include <sylvester_errors.h>
15
+ #include <sylvester_share.h>
16
+
17
+ /* ====================== EXTERNALS ====================== */
18
+
19
+ extern VALUE mSylvester;
20
+
21
+ #endif
@@ -0,0 +1,31 @@
1
+ #include <sylvester_errors.h>
2
+
3
+ /**
4
+ * Contains all Sylvester system error classes
5
+ */
6
+
7
+ /* ================= EXTERNAL VARIABLES ================== */
8
+
9
+ /**
10
+ * Sylvester error classes
11
+ */
12
+ VALUE cSylvesterAttachmentError;
13
+ VALUE cSylvesterDetachmentError;
14
+ VALUE cSylvesterMetadataError;
15
+
16
+ /* ================== RUBY INITIALIZER =================== */
17
+
18
+ void sylvester_init_errors(void) {
19
+
20
+ /**
21
+ * Acquire namespace module
22
+ */
23
+ VALUE sylvester = rb_define_module("Sylvester");
24
+
25
+ /**
26
+ * Define error classes
27
+ */
28
+ cSylvesterAttachmentError = rb_define_class_under(sylvester, "AttachmentError", rb_eException);
29
+ cSylvesterDetachmentError = rb_define_class_under(sylvester, "DetachmentError", rb_eException);
30
+ cSylvesterMetadataError = rb_define_class_under(sylvester, "MetadataError", rb_eException);
31
+ }
@@ -0,0 +1,14 @@
1
+ #ifndef SYLVESTER_ERRORS
2
+ #define SYLVESTER_ERRORS
3
+
4
+ #include <sylvester.h>
5
+
6
+ /* ====================== EXTERNALS ====================== */
7
+
8
+ extern VALUE cSylvesterAttachmentError;
9
+ extern VALUE cSylvesterDetachmentError;
10
+ extern VALUE cSylvesterMetadataError;
11
+
12
+ extern void sylvester_init_errors(void);
13
+
14
+ #endif
@@ -0,0 +1,284 @@
1
+ #include <sylvester_share.h>
2
+ #include <sylvester_errors.h>
3
+
4
+ /**
5
+ * A Sylvester::Share is an attachment to shared memory
6
+ */
7
+
8
+ /* ================= EXTERNAL VARIABLES ================== */
9
+
10
+ /**
11
+ * class Sylvester::Share
12
+ */
13
+ VALUE cSylvesterShare;
14
+
15
+ /* ================== C-LEVEL FUNCTIONS ================== */
16
+
17
+ /**
18
+ * Allocates and returns new Sshare struct
19
+ */
20
+ static SsharePtr sshare_new_sshare(void) {
21
+ return (SsharePtr) malloc(sizeof(Sshare));
22
+ }
23
+
24
+ /**
25
+ * Checks validity of key
26
+ */
27
+ static VALUE sshare_validate_key(VALUE key) {
28
+ if (!rb_obj_is_kind_of(key, rb_cInteger) || FIX2INT(key) < 0)
29
+ rb_raise(rb_eArgError, "Shared memory key not a positive integer!");
30
+ }
31
+
32
+ /**
33
+ * Checks validity of key
34
+ */
35
+ static VALUE sshare_validate_size(VALUE size) {
36
+ if (!rb_obj_is_kind_of(size, rb_cInteger) || FIX2INT(size) < 0)
37
+ rb_raise(rb_eArgError, "Specified size not a positive integer!");
38
+ }
39
+
40
+ /**
41
+ * Returns the integer share reference for a given key
42
+ */
43
+ int sshare_get_share_reference(key_t key, int sz) {
44
+ int shmid;
45
+
46
+ if ((shmid = shmget(key, sz, IPC_CREAT | 0666)) < 0)
47
+ rb_raise(cSylvesterAttachmentError, "Error acquiring id for shared memory for key %d of size %d: [%d] %s", key, sz, errno, strerror(errno));
48
+ return shmid;
49
+ }
50
+
51
+ /**
52
+ * Attaches a unit of shared memory at the specified
53
+ * key and of the specified size to the current process,
54
+ * returning a pointer thereinto for readonly purposes
55
+ */
56
+ void sshare_attach_share(key_t key, int sz, int shmid, void **shmaddr, int *attached) {
57
+ if (!*attached) {
58
+ if ((*shmaddr = shmat(shmid, NULL, 0)) == (int *)-1)
59
+ rb_raise(cSylvesterAttachmentError, "Error attaching shared memory for key %d of size %d: [%d] %s", key, sz, errno, strerror(errno));
60
+ *attached = 1;
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Detaches a unit of shared memory at the specified
66
+ * key and of the specified size from the current process.
67
+ */
68
+ void sshare_detach_share(key_t key, int sz, void *shmaddr, int *attached) {
69
+ if (*attached) {
70
+ if ((shmdt(shmaddr)) == -1)
71
+ rb_raise(cSylvesterDetachmentError, "Error detaching shared memory for key %d of size %d: [%d] %s", (unsigned int)key, sz, errno, strerror(errno));
72
+ *attached = 0;
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Destroys a unit of shared memory at the specified
78
+ * key and of the specified size from the current process.
79
+ */
80
+ void sshare_destroy_share(key_t key, int sz, int shmid, void *shmaddr, int *attached) {
81
+ struct shmid_ds *databuf;
82
+
83
+ sshare_detach_share(key, sz, shmaddr, attached);
84
+ databuf = malloc(sizeof(struct shmid_ds));
85
+ if ((shmctl(shmid, IPC_RMID, databuf)) == -1)
86
+ rb_raise(cSylvesterMetadataError, "Error destroying shared memory for key %d of size %d: [%d] %s", (unsigned int)key, sz, errno, strerror(errno));
87
+ free(databuf);
88
+ //printf("DESTROYED!\n");
89
+ }
90
+
91
+ /**
92
+ * Returns the number of currently attached processes
93
+ */
94
+ int sshare_get_attachment_count(key_t key, int sz, int shmid) {
95
+ struct shmid_ds *databuf;
96
+ int count;
97
+
98
+ databuf = malloc(sizeof(struct shmid_ds));
99
+ if ((shmctl(shmid, IPC_STAT, databuf)) == -1)
100
+ rb_raise(cSylvesterMetadataError, "Error accessing shared memory metadata for key %d of size %d: [%d] %s", (unsigned int)key, sz, errno, strerror(errno));
101
+ count = databuf->shm_nattch;
102
+ free(databuf);
103
+ return count;
104
+ //printf("DESTROYED!\n");
105
+ }
106
+
107
+ /* ================ RUBY SYSTEM FUNCTIONS ================ */
108
+
109
+ /**
110
+ * Class free function
111
+ */
112
+ static void free_sshare(SsharePtr sshare) {
113
+ if (sshare->attached)
114
+ sshare_detach_share(sshare->key, sshare->sz, sshare->ptr, &sshare->attached);
115
+ free(sshare);
116
+ }
117
+
118
+ /**
119
+ * Class allocation function
120
+ */
121
+ static VALUE sshare_alloc(VALUE klass) {
122
+ SsharePtr sshare;
123
+ VALUE obj;
124
+
125
+ sshare = sshare_new_sshare();
126
+ sshare->attached = 0;
127
+ sshare->shmid = 0;
128
+ sshare->key = (key_t)0;
129
+ sshare->sz = 0;
130
+ sshare->autoattach = 1;
131
+ obj = Data_Wrap_Struct(klass, 0, free_sshare, sshare);
132
+ return obj;
133
+ }
134
+
135
+ /**
136
+ * Parse initialization options
137
+ */
138
+ static void sshare_parse_init_opts(SsharePtr sshare, VALUE opts) {
139
+ if (opts != Qnil) {
140
+ if (st_lookup(RHASH(opts)->ntbl, ID2SYM(rb_intern("autoattach")), 0) && RTEST(rb_hash_aref(opts, ID2SYM(rb_intern("autoattach")))) == 0)
141
+ sshare->autoattach = 0;
142
+ }
143
+ }
144
+
145
+ /* ================ RUBY INSTANCE METHODS ================ */
146
+
147
+ /**
148
+ * Constructor:
149
+ * Takes 1 argument, being the refkey for shared
150
+ * memory. Attaches such memory to the process, allowing
151
+ * it to be queried
152
+ */
153
+ static VALUE sshare_init(int argc, VALUE *argv, VALUE self) {
154
+ VALUE key, shmsz, opts, autoattach;
155
+ SsharePtr sshare;
156
+
157
+ rb_scan_args(argc, argv, "21", &key, &shmsz, &opts);
158
+ Data_Get_Struct(self, Sshare, sshare);
159
+
160
+ sshare_validate_key(key);
161
+ sshare_validate_size(shmsz);
162
+
163
+ sshare->key = (key_t)FIX2INT(key);
164
+ sshare->sz = FIX2INT(shmsz);
165
+ sshare->shmid = sshare_get_share_reference((key_t)sshare->key, sshare->sz);
166
+
167
+ sshare_parse_init_opts(sshare, opts);
168
+
169
+ if (sshare->autoattach) {
170
+ // printf("ATTACHING: %d\n", sshare->attached);
171
+ sshare_attach_share(sshare->key, sshare->sz, sshare->shmid, &sshare->ptr, &sshare->attached);
172
+ // printf("ATTACHED AFTER CALL: %d\n", sshare->attached);
173
+ }
174
+
175
+ return self;
176
+ }
177
+
178
+ /**
179
+ * name: :attached?
180
+ * Returns true if the share is currently attached to the current process, false otherwise
181
+ */
182
+ static VALUE sshare_attached_qm(VALUE self) {
183
+ SsharePtr sshare;
184
+
185
+ Data_Get_Struct(self, Sshare, sshare);
186
+ // printf("IS ATTACHED?: %d\n", sshare->attached);
187
+ return (sshare->attached) ? Qtrue : Qfalse;
188
+ }
189
+
190
+ /**
191
+ * name: :attach!
192
+ * Attaches the share (if it is not already).
193
+ */
194
+ static VALUE sshare_attach_ep(VALUE self) {
195
+ SsharePtr sshare;
196
+
197
+ Data_Get_Struct(self, Sshare, sshare);
198
+ sshare_attach_share(sshare->key, sshare->sz, sshare->shmid, &sshare->ptr, &sshare->attached);
199
+ return Qnil;
200
+ }
201
+
202
+ /**
203
+ * name: :detach!
204
+ * Detaches the share (if it is not already).
205
+ */
206
+ static VALUE sshare_detach_ep(VALUE self) {
207
+ SsharePtr sshare;
208
+
209
+ Data_Get_Struct(self, Sshare, sshare);
210
+ sshare_detach_share(sshare->key, sshare->sz, sshare->ptr, &sshare->attached);
211
+ sshare->ptr = NULL;
212
+ return Qnil;
213
+ }
214
+
215
+ /**
216
+ * name: :destroy!
217
+ * Detaches the share (if attached) and destroys the reference.
218
+ */
219
+ static VALUE sshare_destroy_ep(VALUE self) {
220
+ SsharePtr sshare;
221
+
222
+ Data_Get_Struct(self, Sshare, sshare);
223
+ sshare_destroy_share(sshare->key, sshare->sz, sshare->shmid, sshare->ptr, &sshare->attached);
224
+ sshare->ptr = NULL;
225
+ return Qnil;
226
+ }
227
+
228
+ /**
229
+ * name: :key
230
+ * Returns the key for the share.
231
+ */
232
+ static VALUE sshare_key(VALUE self) {
233
+ SsharePtr sshare;
234
+
235
+ Data_Get_Struct(self, Sshare, sshare);
236
+ return INT2FIX((unsigned int)sshare->key);
237
+ }
238
+
239
+ /**
240
+ * name: :size
241
+ * Returns the size of the share.
242
+ */
243
+ static VALUE sshare_size(VALUE self) {
244
+ SsharePtr sshare;
245
+
246
+ Data_Get_Struct(self, Sshare, sshare);
247
+ return INT2FIX(sshare->sz);
248
+ }
249
+
250
+ /**
251
+ * name: :size
252
+ * Returns the current number of attached processes.
253
+ */
254
+ static VALUE sshare_attachment_count(VALUE self) {
255
+ SsharePtr sshare;
256
+
257
+ Data_Get_Struct(self, Sshare, sshare);
258
+ return INT2FIX(sshare_get_attachment_count(sshare->key, sshare->sz, sshare->shmid));
259
+ }
260
+
261
+
262
+ /* ================== RUBY INITIALIZER =================== */
263
+
264
+ void sylvester_init_share(void) {
265
+
266
+ /**
267
+ * Acquire namespace module
268
+ */
269
+ VALUE sylvester = rb_define_module("Sylvester");
270
+
271
+ /**
272
+ * Define and describe share class
273
+ */
274
+ cSylvesterShare = rb_define_class_under(sylvester, "Share", rb_cObject);
275
+ rb_define_alloc_func(cSylvesterShare, sshare_alloc);
276
+ rb_define_method(cSylvesterShare, "initialize", sshare_init, -1);
277
+ rb_define_method(cSylvesterShare, "attached?", sshare_attached_qm, 0);
278
+ rb_define_method(cSylvesterShare, "attach!", sshare_attach_ep, 0);
279
+ rb_define_method(cSylvesterShare, "detach!", sshare_detach_ep, 0);
280
+ rb_define_method(cSylvesterShare, "destroy!", sshare_destroy_ep, 0);
281
+ rb_define_method(cSylvesterShare, "key", sshare_key, 0);
282
+ rb_define_method(cSylvesterShare, "size", sshare_size, 0);
283
+ rb_define_method(cSylvesterShare, "attachment_count", sshare_attachment_count, 0);
284
+ }
@@ -0,0 +1,23 @@
1
+ #ifndef SYLVESTER_SHARE
2
+ #define SYLVESTER_SHARE
3
+
4
+ #include <sylvester.h>
5
+
6
+ /* ================ STRUCTS AND TYPEDEFS ================= */
7
+
8
+ struct _sshare {
9
+ unsigned int attached, shmid, sz, autoattach;
10
+ key_t key;
11
+ void *ptr;
12
+ };
13
+
14
+ typedef struct _sshare Sshare;
15
+ typedef Sshare * SsharePtr;
16
+
17
+ /* ====================== EXTERNALS ====================== */
18
+
19
+ extern VALUE cSylvesterShare;
20
+
21
+ extern void sylvester_init_share(void);
22
+
23
+ #endif
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'spork'
3
+ require 'pry'
4
+
5
+ #uncomment the following line to use spork with the debugger
6
+ #require 'spork/ext/ruby-debug'
7
+
8
+ Spork.prefork do
9
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
11
+ require 'rspec'
12
+ require 'sylvester'
13
+
14
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
15
+
16
+ RSpec.configure do |config|
17
+ end
18
+ end
19
+
20
+ Spork.each_run do
21
+ end
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ require "#{File.dirname(__FILE__)}/../bin_helper"
3
+
4
+ class Attacher
5
+ def initialize(key,size)
6
+ @share = Sylvester::Share.new ARGV[0].to_i, ARGV[1].to_i
7
+ @running = true
8
+ run
9
+ end
10
+
11
+ def handle_interrupt
12
+ Proc.new do
13
+ @running = false
14
+ @share.detach!
15
+ end
16
+ end
17
+
18
+ def run
19
+ while @running
20
+ trap("INT", &handle_interrupt)
21
+ trap("KILL", &handle_interrupt)
22
+ trap("QUIT", &handle_interrupt)
23
+ trap("TERM", &handle_interrupt)
24
+ trap("HUP", &handle_interrupt)
25
+ end
26
+ end
27
+ end
28
+
29
+ @attacher = Attacher.new ARGV[0].to_i, ARGV[1].to_i
30
+
@@ -0,0 +1,7 @@
1
+ def execute_binary(command)
2
+ `#{File.dirname(__FILE__)}/bin/#{command} > /dev/null & echo $!`.to_i
3
+ end
4
+
5
+ def kill_binary(pid)
6
+ `kill #{pid}`
7
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
2
+ require 'sylvester'
@@ -0,0 +1,4 @@
1
+ TEST_SHARES = [
2
+ {:key => 9991, :size => 1048576}, # 1Mb
3
+ {:key => 9992, :size => 1024} # 1Mb
4
+ ]
@@ -0,0 +1,161 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sylvester::Share do
4
+ let(:share_params) { TEST_SHARES[0] }
5
+ let(:share_params_2) { TEST_SHARES[1] }
6
+
7
+ it "should be a class" do
8
+ Sylvester::Share.should be_a Class
9
+ end
10
+
11
+ describe "constructor" do
12
+ it "should return a valid instance of the class when presented with a positive integer key and a positive integer size" do
13
+ share_params[:key].should be_a Integer
14
+ share_params[:size].should be_a Integer
15
+ share = Sylvester::Share.new share_params[:key], share_params[:size]
16
+ share.should be_a Sylvester::Share
17
+ share.destroy!
18
+ end
19
+
20
+ it "should throw an argument error when presented with a negative integer key" do
21
+ lambda {
22
+ share = Sylvester::Share.new -123, share_params[:size]
23
+ share.destroy!
24
+ }.should raise_error ArgumentError, "Shared memory key not a positive integer!"
25
+ end
26
+
27
+ it "should throw an argument error when presented with a noninteger key" do
28
+ lambda {
29
+ share = Sylvester::Share.new "test", share_params[:size]
30
+ share.destroy!
31
+ }.should raise_error ArgumentError, "Shared memory key not a positive integer!"
32
+ end
33
+
34
+ it "should throw an argument error when presented with a negative integer size" do
35
+ lambda {
36
+ share = Sylvester::Share.new share_params[:key], -123
37
+ share.destroy!
38
+ }.should raise_error ArgumentError, "Specified size not a positive integer!"
39
+ end
40
+
41
+ it "should be attached if the opts hash is omitted" do
42
+ share = Sylvester::Share.new share_params[:key], share_params[:size]
43
+ share.should be_attached
44
+ share.destroy!
45
+ end
46
+
47
+ it "should be attached if the opts hash key :autoattach is set to anything besides nil or false" do
48
+ share = Sylvester::Share.new share_params[:key], share_params[:size], :autoattach => true
49
+ share.should be_attached
50
+ share.destroy!
51
+ share = Sylvester::Share.new share_params_2[:key], share_params_2[:size], :autoattach => "hat"
52
+ share.should be_attached
53
+ share.destroy!
54
+ end
55
+
56
+ it "should be unattached if the opts hash key :autoattach is set to false or nil" do
57
+ share = Sylvester::Share.new share_params[:key], share_params[:size], :autoattach => false
58
+ share.should_not be_attached
59
+ share.destroy!
60
+ share = Sylvester::Share.new share_params_2[:key], share_params_2[:size], :autoattach => nil
61
+ share.should_not be_attached
62
+ share.destroy!
63
+ end
64
+ end
65
+
66
+ describe "instance methods" do
67
+ describe "attached?" do
68
+ it "should be true if the share is currently attached to the process" do
69
+ share = Sylvester::Share.new share_params[:key], share_params[:size], :autoattach => false
70
+ share.should_not be_attached
71
+ share.attach!
72
+ share.should be_attached
73
+ share.destroy!
74
+ end
75
+
76
+ it "should be false if the share is currently not attached to the process" do
77
+ share = Sylvester::Share.new share_params[:key], share_params[:size]
78
+ share.should be_attached
79
+ share.detach!
80
+ share.should_not be_attached
81
+ share.destroy!
82
+ end
83
+ end
84
+
85
+ describe "attach!" do
86
+ it "should attach the shared memory to the current process if it isn't already" do
87
+ share = Sylvester::Share.new share_params[:key], share_params[:size], :autoattach => false
88
+ share.attach!
89
+ share.should be_attached
90
+ share.attachment_count.should == 1
91
+ share.destroy!
92
+ end
93
+
94
+ it "should do nothing if the share is already attached" do
95
+ share = Sylvester::Share.new share_params[:key], share_params[:size]
96
+ share.should be_attached
97
+ share.attachment_count.should == 1
98
+ share.attach!
99
+ share.should be_attached
100
+ share.attachment_count.should == 1
101
+ share.destroy!
102
+ end
103
+ end
104
+
105
+ describe "detach!" do
106
+ it "should detach the shared memory from the current process if it isn't already" do
107
+ share = Sylvester::Share.new share_params[:key], share_params[:size]
108
+ share.detach!
109
+ share.should_not be_attached
110
+ share.attachment_count.should == 0
111
+ share.destroy!
112
+ end
113
+
114
+ it "should do nothing if the share is already attached" do
115
+ share = Sylvester::Share.new share_params[:key], share_params[:size], :autoattach => false
116
+ share.should_not be_attached
117
+ share.attachment_count.should == 0
118
+ share.detach!
119
+ share.should_not be_attached
120
+ share.attachment_count.should == 0
121
+ share.destroy!
122
+ end
123
+ end
124
+
125
+ describe "destroy!"
126
+
127
+ describe "attachment_count" do
128
+ it "should return the number of current attachments" do
129
+ share = Sylvester::Share.new share_params[:key], share_params[:size], :autoattach => false
130
+ share.attachment_count.should == 0
131
+ share.attach!
132
+ share.attachment_count.should == 1
133
+ pid = execute_binary "attach #{share_params[:key]} #{share_params[:size]}"
134
+ sleep 0.1
135
+ share.attachment_count.should == 2
136
+ share.detach!
137
+ share.attachment_count.should == 1
138
+ kill_binary pid
139
+ sleep 0.1
140
+ share.attachment_count.should == 0
141
+ share.destroy!
142
+ end
143
+ end
144
+
145
+ describe "key" do
146
+ it "should retrieve the share key" do
147
+ share = Sylvester::Share.new share_params[:key], share_params[:size]
148
+ share.key.should == share_params[:key]
149
+ share.destroy!
150
+ end
151
+ end
152
+
153
+ describe "size" do
154
+ it "should retrieve the size of the share" do
155
+ share = Sylvester::Share.new share_params[:key], share_params[:size]
156
+ share.size.should == share_params[:size]
157
+ share.destroy!
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sylvester do
4
+ it "should be a module" do
5
+ Sylvester.should be_a Module
6
+ end
7
+ end
data/sylvester.gemspec ADDED
@@ -0,0 +1,81 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "sylvester"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jeremy Holland"]
12
+ s.date = "2012-08-04"
13
+ s.description = "The beginnings of a handy ruby interface for System V shared memory, making it accessible and easy to use directly from ruby. Plans are to simply treat it as a string to begin with, thereafter building more complex data structures that will implement the interfaces (where appropriate) of their stack-based counterparts."
14
+ s.email = "jeremy@jeremypholland.com"
15
+ s.extensions = ["ext/sylvester/extconf.rb"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".rspec",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "ext/sylvester/extconf.rb",
30
+ "ext/sylvester/sylvester.c",
31
+ "ext/sylvester/sylvester.h",
32
+ "ext/sylvester/sylvester_errors.c",
33
+ "ext/sylvester/sylvester_errors.h",
34
+ "ext/sylvester/sylvester_share.c",
35
+ "ext/sylvester/sylvester_share.h",
36
+ "spec/spec_helper.rb",
37
+ "spec/support/bin/attach",
38
+ "spec/support/bin_commands.rb",
39
+ "spec/support/bin_helper.rb",
40
+ "spec/support/test_shares.rb",
41
+ "spec/sylvester/share_spec.rb",
42
+ "spec/sylvester_spec.rb",
43
+ "sylvester.gemspec"
44
+ ]
45
+ s.homepage = "http://github.com/awebneck/sylvester"
46
+ s.licenses = ["MIT"]
47
+ s.require_paths = ["lib"]
48
+ s.rubygems_version = "1.8.15"
49
+ s.summary = "A ruby library for interacting with, storing, and reading basic ruby objects from System V shared memory"
50
+
51
+ if s.respond_to? :specification_version then
52
+ s.specification_version = 3
53
+
54
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
55
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
56
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
57
+ s.add_development_dependency(%q<bundler>, ["~> 1.1.0"])
58
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
59
+ s.add_development_dependency(%q<simplecov>, ["~> 0.6.4"])
60
+ s.add_development_dependency(%q<spork>, ["~> 0.9.2"])
61
+ s.add_development_dependency(%q<pry>, [">= 0"])
62
+ else
63
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
64
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
65
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
66
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
67
+ s.add_dependency(%q<simplecov>, ["~> 0.6.4"])
68
+ s.add_dependency(%q<spork>, ["~> 0.9.2"])
69
+ s.add_dependency(%q<pry>, [">= 0"])
70
+ end
71
+ else
72
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
73
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
74
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
75
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
76
+ s.add_dependency(%q<simplecov>, ["~> 0.6.4"])
77
+ s.add_dependency(%q<spork>, ["~> 0.9.2"])
78
+ s.add_dependency(%q<pry>, [">= 0"])
79
+ end
80
+ end
81
+
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sylvester
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeremy Holland
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &18611940 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.8.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *18611940
25
+ - !ruby/object:Gem::Dependency
26
+ name: rdoc
27
+ requirement: &18633900 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.12'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *18633900
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &18630220 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.1.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *18630220
47
+ - !ruby/object:Gem::Dependency
48
+ name: jeweler
49
+ requirement: &18675840 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.8.4
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *18675840
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov
60
+ requirement: &18672080 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 0.6.4
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *18672080
69
+ - !ruby/object:Gem::Dependency
70
+ name: spork
71
+ requirement: &18720440 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 0.9.2
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *18720440
80
+ - !ruby/object:Gem::Dependency
81
+ name: pry
82
+ requirement: &18732660 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *18732660
91
+ description: The beginnings of a handy ruby interface for System V shared memory,
92
+ making it accessible and easy to use directly from ruby. Plans are to simply treat
93
+ it as a string to begin with, thereafter building more complex data structures that
94
+ will implement the interfaces (where appropriate) of their stack-based counterparts.
95
+ email: jeremy@jeremypholland.com
96
+ executables: []
97
+ extensions:
98
+ - ext/sylvester/extconf.rb
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.rdoc
102
+ files:
103
+ - .document
104
+ - .rspec
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ - Rakefile
110
+ - VERSION
111
+ - ext/sylvester/extconf.rb
112
+ - ext/sylvester/sylvester.c
113
+ - ext/sylvester/sylvester.h
114
+ - ext/sylvester/sylvester_errors.c
115
+ - ext/sylvester/sylvester_errors.h
116
+ - ext/sylvester/sylvester_share.c
117
+ - ext/sylvester/sylvester_share.h
118
+ - spec/spec_helper.rb
119
+ - spec/support/bin/attach
120
+ - spec/support/bin_commands.rb
121
+ - spec/support/bin_helper.rb
122
+ - spec/support/test_shares.rb
123
+ - spec/sylvester/share_spec.rb
124
+ - spec/sylvester_spec.rb
125
+ - sylvester.gemspec
126
+ homepage: http://github.com/awebneck/sylvester
127
+ licenses:
128
+ - MIT
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ segments:
140
+ - 0
141
+ hash: -1812158138877080485
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 1.8.15
151
+ signing_key:
152
+ specification_version: 3
153
+ summary: A ruby library for interacting with, storing, and reading basic ruby objects
154
+ from System V shared memory
155
+ test_files: []