Class: ProcessShared::SharedMemoryIO

Inherits:
Object
  • Object
show all
Defined in:
lib/process_shared/shared_memory_io.rb

Overview

Does some bounds checking for EOF, but assumes underlying memory object (FFI::Pointer) will do bounds checking, in particular the #_putbytes method relies on this.

Note: an unbounded FFI::Pointer may be converted into a bounded pointer using ptr.slice(0, size).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mem) ⇒ SharedMemoryIO

Returns a new instance of SharedMemoryIO.



13
14
15
16
17
# File 'lib/process_shared/shared_memory_io.rb', line 13

def initialize(mem)
  @mem = mem
  @pos = 0
  @closed = false           # TODO: actually pay attention to this
end

Instance Attribute Details

#memObject (readonly)

Returns the value of attribute mem.



11
12
13
# File 'lib/process_shared/shared_memory_io.rb', line 11

def mem
  @mem
end

#posObject Also known as: tell

Returns the value of attribute pos.



10
11
12
# File 'lib/process_shared/shared_memory_io.rb', line 10

def pos
  @pos
end

Instance Method Details

#<<(*args) ⇒ Object

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/process_shared/shared_memory_io.rb', line 19

def <<(*args)
  raise NotImplementedError
end

#advise(*args) ⇒ Object



23
24
25
# File 'lib/process_shared/shared_memory_io.rb', line 23

def advise(*args)
  # no-op
end

#autoclose=(*args) ⇒ Object

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/process_shared/shared_memory_io.rb', line 27

def autoclose=(*args)
  raise NotImplementedError
end

#autoclose?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


31
32
33
# File 'lib/process_shared/shared_memory_io.rb', line 31

def autoclose?
  raise NotImplementedError
end

#binmodeObject



35
36
37
# File 'lib/process_shared/shared_memory_io.rb', line 35

def binmode
  # no-op; always in binmode
end

#binmode?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/process_shared/shared_memory_io.rb', line 39

def binmode?
  true
end

#bytesObject Also known as: each_byte



43
44
45
46
47
48
49
50
51
# File 'lib/process_shared/shared_memory_io.rb', line 43

def bytes
  if block_given?
    until eof?
      yield _getbyte
    end
  else
    raise NotImplementedError
  end
end

#charsObject Also known as: each_char

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/process_shared/shared_memory_io.rb', line 54

def chars
  raise NotImplementedError
end

#closeObject



59
60
61
# File 'lib/process_shared/shared_memory_io.rb', line 59

def close
  @closed = true
end

#close_on_exec=(bool) ⇒ Object

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/process_shared/shared_memory_io.rb', line 63

def close_on_exec=(bool)
  raise NotImplementedError
end

#close_on_exec?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


67
68
69
# File 'lib/process_shared/shared_memory_io.rb', line 67

def close_on_exec?
  raise NotImplementedError
end

#close_readObject

Raises:

  • (NotImplementedError)


71
72
73
# File 'lib/process_shared/shared_memory_io.rb', line 71

def close_read
  raise NotImplementedError
end

#close_writeObject

Raises:

  • (NotImplementedError)


75
76
77
# File 'lib/process_shared/shared_memory_io.rb', line 75

def close_write
  raise NotImplementedError
end

#closed?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/process_shared/shared_memory_io.rb', line 79

def closed?
  @closed
end

#codepointsObject Also known as: each_codepoint

Raises:

  • (NotImplementedError)


83
84
85
# File 'lib/process_shared/shared_memory_io.rb', line 83

def codepoints
  raise NotImplementedError
end

#eachObject Also known as: each_line, lines

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/process_shared/shared_memory_io.rb', line 88

def each
  raise NotImplementedError
end

#eof?Boolean Also known as: eof

Returns:

  • (Boolean)


94
95
96
# File 'lib/process_shared/shared_memory_io.rb', line 94

def eof?
  pos == mem.size
end

#external_encodingObject

Raises:

  • (NotImplementedError)


99
100
101
# File 'lib/process_shared/shared_memory_io.rb', line 99

def external_encoding
  raise NotImplementedError
end

#fcntlObject

Raises:

  • (NotImplementedError)


103
104
105
# File 'lib/process_shared/shared_memory_io.rb', line 103

def fcntl
  raise NotImplementedError
end

#fdatasyncObject

Raises:

  • (NotImplementedError)


107
108
109
# File 'lib/process_shared/shared_memory_io.rb', line 107

def fdatasync
  raise NotImplementedError
end

#filenoObject Also known as: to_i

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/process_shared/shared_memory_io.rb', line 111

def fileno
  raise NotImplementedError
end

#flushObject



116
117
118
# File 'lib/process_shared/shared_memory_io.rb', line 116

def flush
  # no-op
end

#fsyncObject

Raises:

  • (NotImplementedError)


120
121
122
# File 'lib/process_shared/shared_memory_io.rb', line 120

def fsync
  raise NotImplementedError
end

#getbyteObject



124
125
126
127
# File 'lib/process_shared/shared_memory_io.rb', line 124

def getbyte
  return nil if eof?
  _getbyte
end

#getc19Object Also known as: getc

#getc in Ruby 1.9 returns String or nil. In 1.8, it returned Fixnum of nil (identical to getbyte).

FIXME: should this be encoding/character aware?



133
134
135
136
137
# File 'lib/process_shared/shared_memory_io.rb', line 133

def getc19
  if b = getbyte
    '' << b
  end
end

#getsObject

Raises:

  • (NotImplementedError)


145
146
147
# File 'lib/process_shared/shared_memory_io.rb', line 145

def gets
  raise NotImplementedError
end

#internal_encodingObject

Raises:

  • (NotImplementedError)


149
150
151
# File 'lib/process_shared/shared_memory_io.rb', line 149

def internal_encoding
  raise NotImplementedError
end

#ioctlObject

Raises:

  • (NotImplementedError)


153
154
155
# File 'lib/process_shared/shared_memory_io.rb', line 153

def ioctl
  raise NotImplementedError
end

#linenoObject

Raises:

  • (NotImplementedError)


162
163
164
# File 'lib/process_shared/shared_memory_io.rb', line 162

def lineno
  raise NotImplementedError
end

#lineno=Object

Raises:

  • (NotImplementedError)


166
167
168
# File 'lib/process_shared/shared_memory_io.rb', line 166

def lineno=
  raise NotImplementedError
end

#pidObject

Raises:

  • (NotImplementedError)


174
175
176
# File 'lib/process_shared/shared_memory_io.rb', line 174

def pid
  raise NotImplementedError
end

Raises:

  • (NotImplementedError)


180
181
182
# File 'lib/process_shared/shared_memory_io.rb', line 180

def print(*args)
  raise NotImplementedError
end

#printf(*args) ⇒ Object

Raises:

  • (NotImplementedError)


183
184
185
# File 'lib/process_shared/shared_memory_io.rb', line 183

def printf(*args)
  raise NotImplementedError
end

#putc(arg) ⇒ Object

Raises:

  • (NotImplementedError)


187
188
189
# File 'lib/process_shared/shared_memory_io.rb', line 187

def putc(arg)
  raise NotImplementedError
end

#puts(*args) ⇒ Object

Raises:

  • (NotImplementedError)


191
192
193
# File 'lib/process_shared/shared_memory_io.rb', line 191

def puts(*args)
  raise NotImplementedError
end

#read(length = nil, buffer = nil) ⇒ Object

FIXME: this doesn’t match IO#read exactly (corner cases about EOF and whether length was nil or not), but it’s enough for Marshal::load.



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/process_shared/shared_memory_io.rb', line 198

def read(length = nil, buffer = nil)
  length ||= (mem.size - pos)
  buffer ||= ''
  buffer.force_encoding('ASCII-8BIT') unless RUBY_VERSION.start_with?('1.8')
  
  actual_length = [(mem.size - pos), length].min
  actual_length.times do
    buffer << _getbyte
  end
  buffer
end

#read_nonblock(*args) ⇒ Object

Raises:

  • (NotImplementedError)


210
211
212
# File 'lib/process_shared/shared_memory_io.rb', line 210

def read_nonblock(*args)
  raise NotImplementedError
end

#readbyteObject

Raises:

  • (EOFError)


214
215
216
217
# File 'lib/process_shared/shared_memory_io.rb', line 214

def readbyte
  raise EOFError if eof?
  _getbyte
end

#readcharObject

Raises:

  • (NotImplementedError)


219
220
221
# File 'lib/process_shared/shared_memory_io.rb', line 219

def readchar
  raise NotImplementedError
end

#readlineObject

Raises:

  • (NotImplementedError)


223
224
225
# File 'lib/process_shared/shared_memory_io.rb', line 223

def readline
  raise NotImplementedError
end

#readlinesObject

Raises:

  • (NotImplementedError)


227
228
229
# File 'lib/process_shared/shared_memory_io.rb', line 227

def readlines
  raise NotImplementedError
end

#readpartialObject

Raises:

  • (NotImplementedError)


231
232
233
# File 'lib/process_shared/shared_memory_io.rb', line 231

def readpartial
  raise NotImplementedError
end

#reopenObject

Raises:

  • (NotImplementedError)


235
236
237
# File 'lib/process_shared/shared_memory_io.rb', line 235

def reopen
  raise NotImplementedError
end

#rewindObject



239
240
241
# File 'lib/process_shared/shared_memory_io.rb', line 239

def rewind
  pos = 0
end

#seek(amount, whence = IO::SEEK_SET) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/process_shared/shared_memory_io.rb', line 243

def seek(amount, whence = IO::SEEK_SET)
  case whence
  when IO::SEEK_CUR
    self.pos += amount
  when IO::SEEK_END
    self.pos = (mem.size + amount)
  when IO::SEEK_SET
    self.pos = amount
  else
    raise ArgumentError, "bad seek whence #{whence}"
  end
end

#set_encodingObject

Raises:

  • (NotImplementedError)


256
257
258
# File 'lib/process_shared/shared_memory_io.rb', line 256

def set_encoding
  raise NotImplementedError
end

#statObject

Raises:

  • (NotImplementedError)


260
261
262
# File 'lib/process_shared/shared_memory_io.rb', line 260

def stat
  raise NotImplementedError
end

#syncObject



264
265
266
# File 'lib/process_shared/shared_memory_io.rb', line 264

def sync
  true
end

#sync=Object

Raises:

  • (NotImplementedError)


268
269
270
# File 'lib/process_shared/shared_memory_io.rb', line 268

def sync=
  raise NotImplementedError
end

#sysread(*args) ⇒ Object

Raises:

  • (NotImplementedError)


272
273
274
# File 'lib/process_shared/shared_memory_io.rb', line 272

def sysread(*args)
  raise NotImplementedError
end

#sysseek(*args) ⇒ Object

Raises:

  • (NotImplementedError)


276
277
278
# File 'lib/process_shared/shared_memory_io.rb', line 276

def sysseek(*args)
  raise NotImplementedError
end

#syswrite(*args) ⇒ Object

Raises:

  • (NotImplementedError)


280
281
282
# File 'lib/process_shared/shared_memory_io.rb', line 280

def syswrite(*args)
  raise NotImplementedError
end

#to_ioObject

Raises:

  • (NotImplementedError)


284
285
286
# File 'lib/process_shared/shared_memory_io.rb', line 284

def to_io
  raise NotImplementedError
end

#tty?Boolean Also known as: isatty

Returns:

  • (Boolean)


157
158
159
# File 'lib/process_shared/shared_memory_io.rb', line 157

def tty?
  false
end

#ungetbyteObject

Raises:

  • (IOError)


288
289
290
291
# File 'lib/process_shared/shared_memory_io.rb', line 288

def ungetbyte
  raise IOError if pos == 0
  pos -= 1
end

#ungetcObject

Raises:

  • (NotImplementedError)


293
294
295
# File 'lib/process_shared/shared_memory_io.rb', line 293

def ungetc
  raise NotImplementedError
end

#write(str) ⇒ Object



297
298
299
300
301
# File 'lib/process_shared/shared_memory_io.rb', line 297

def write(str)
  s = str.to_s
  _putbytes(s)
  s.size
end

#write_nonblock(str) ⇒ Object

Raises:

  • (NotImplementedError)


303
304
305
# File 'lib/process_shared/shared_memory_io.rb', line 303

def write_nonblock(str)
  raise NotImplementedError
end