Class: MmapRuby::Mmap

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/mmap-ruby/mmap.rb,
ext/mmap_ruby/mmap_ruby.c

Constant Summary collapse

MS_SYNC =
INT2FIX(MS_SYNC)
MS_ASYNC =
INT2FIX(MS_ASYNC)
MS_INVALIDATE =
INT2FIX(MS_INVALIDATE)
PROT_READ =
INT2FIX(PROT_READ)
PROT_WRITE =
INT2FIX(PROT_WRITE)
PROT_EXEC =
INT2FIX(PROT_EXEC)
PROT_NONE =
INT2FIX(PROT_NONE)
MAP_ANON =
INT2FIX(MAP_ANON)
MAP_ANONYMOUS =
INT2FIX(MAP_ANONYMOUS)
MAP_SHARED =
INT2FIX(MAP_SHARED)
MAP_PRIVATE =
INT2FIX(MAP_PRIVATE)
MADV_NORMAL =
INT2FIX(MADV_NORMAL)
MADV_RANDOM =
INT2FIX(MADV_RANDOM)
MADV_SEQUENTIAL =
INT2FIX(MADV_SEQUENTIAL)
MADV_WILLNEED =
INT2FIX(MADV_WILLNEED)
MADV_DONTNEED =
INT2FIX(MADV_DONTNEED)
MAP_DENYWRITE =
INT2FIX(MAP_DENYWRITE)
MAP_EXECUTABLE =
INT2FIX(MAP_EXECUTABLE)
MAP_NORESERVE =
INT2FIX(MAP_NORESERVE)
MAP_LOCKED =
INT2FIX(MAP_LOCKED)
MAP_GROWSDOWN =
INT2FIX(MAP_GROWSDOWN)
MAP_NOSYNC =
INT2FIX(MAP_NOSYNC)
MCL_CURRENT =
INT2FIX(MCL_CURRENT)
MCL_FUTURE =
INT2FIX(MCL_FUTURE)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(file, mode = "r", protection = Mmap::MAP_SHARED, options = {}) ⇒ Object

Returns a new MmapRuby object.

  • file

    Pathname of the file. If nil is given, an anonymous map is created (Mmap::MAP_ANON).

  • mode

    Mode to open the file. Can be “r”, “w”, “rw”, or “a”.

  • protection

    Specifies the nature of the mapping:

    • Mmap::MAP_SHARED

      Creates a mapping that’s shared with all other processes mapping the same areas of the file. This is the default value.

    • Mmap::MAP_PRIVATE

      Creates a private copy-on-write mapping, so changes to the contents of the mmap object will be private to this process.

  • options

    Hash. If one of the options length or offset is specified, it will not be possible to modify the size of the mapped file.

    length

    Maps length bytes from the file.

    offset

    The mapping begins at offset.

    advice

    The type of access (see #madvise).



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'ext/mmap_ruby/mmap_ruby.c', line 250

static VALUE
rb_cMmap_initialize(int argc, VALUE *argv, VALUE self)
{
  mmap_t *mmap;
  struct stat st;

  VALUE fname, vmode, scope, options;
  VALUE fdv = Qnil;

  const char *path = 0, *mode = 0;
  int fd = -1, perm = 0666;

  caddr_t addr;
  size_t size = 0;
  off_t offset = 0;
  int smode = 0, pmode = 0, vscope = 0;

  int anonymous = 0, init = 0;

  options = Qnil;
  argc = rb_scan_args(argc, argv, "12:", &fname, &vmode, &scope, &options);

  if (NIL_P(fname)) {
    vscope = MAP_ANON | MAP_SHARED;
    anonymous = 1;
  }
  else
  {
    if (rb_respond_to(fname, rb_intern("fileno"))) {
      fdv = rb_funcall2(fname, rb_intern("fileno"), 0, 0);
    }

    if (NIL_P(fdv)) {
      fname = rb_str_to_str(fname);
      StringValue(fname);
      path = StringValuePtr(fname);
    }
    else {
      fd = NUM2INT(fdv);
      if (fd < 0) {
        rb_raise(rb_eArgError, "invalid file descriptor %d", fd);
      }
    }

    if (!NIL_P(scope)) {
      vscope = NUM2INT(scope);
      if (vscope & MAP_ANON) {
        rb_raise(rb_eArgError, "filename specified for an anonymous map");
      }
    }
  }

  vscope |= NIL_P(scope) ? MAP_SHARED : NUM2INT(scope);

  if (!anonymous) {
    if (NIL_P(vmode)) {
      mode = "r";
    }
    else if (rb_respond_to(vmode, rb_intern("to_ary"))) {
      VALUE tmp;

      vmode = rb_convert_type(vmode, T_ARRAY, "Array", "to_ary");
      if (RARRAY_LEN(vmode) != 2) {
        rb_raise(rb_eArgError, "invalid length %ld (expected 2)",
                 RARRAY_LEN(vmode));
      }
      tmp = rb_ary_entry(vmode, 0);
      mode = StringValuePtr(tmp);
      perm = NUM2INT(rb_ary_entry(vmode, 1));
    }
    else {
      mode = StringValuePtr(vmode);
    }

    if (strcmp(mode, "r") == 0) {
      smode = O_RDONLY;
      pmode = PROT_READ;
    }
    else if (strcmp(mode, "w") == 0) {
      smode = O_RDWR | O_TRUNC;
      pmode = PROT_READ | PROT_WRITE;
    }
    else if (strcmp(mode, "rw") == 0 || strcmp(mode, "wr") == 0) {
      smode = O_RDWR;
      pmode = PROT_READ | PROT_WRITE;
    }
    else if (strcmp(mode, "a") == 0) {
      smode = O_RDWR | O_CREAT;
      pmode = PROT_READ | PROT_WRITE;
    }
    else {
      rb_raise(rb_eArgError, "invalid mode %s", mode);
    }

    if (NIL_P(fdv)) {
      if ((fd = open(path, smode, perm)) == -1) {
        rb_raise(rb_eArgError, "can't open %s", path);
      }
    }
    if (fstat(fd, &st) == -1) {
      rb_raise(rb_eArgError, "can't stat %s", path);
    }
    size = st.st_size;
  }
  else {
    fd = -1;
    if (!NIL_P(vmode) && TYPE(vmode) != T_STRING) {
      size = NUM2INT(vmode);
    }
  }

  TypedData_Get_Struct(self, mmap_t, &mmap_type, mmap);
  rb_check_frozen(self);
  mmap->shmid = 0;
  mmap->semid = 0;

  if (options != Qnil) {
    rb_funcall(self, rb_intern("process_options"), 1, options);
    if (path && (mmap->len + mmap->offset) > (size_t)st.st_size) {
      rb_raise(rb_eArgError, "invalid value for length (%ld) or offset (%ld)",
               (long)mmap->len, (long)mmap->offset);
    }
    if (mmap->len) size = mmap->len;
    offset = mmap->offset;

    if (mmap->flag & MMAP_RUBY_IPC) {
      key_t key;
      int shmid, semid, mode;
      union semun sem_val;
      struct shmid_ds buf;

      if (!(vscope & MAP_SHARED)) {
        rb_warning("Probably it will not do what you expect ...");
      }
      mmap->key = -1;
      mmap->semid = 0;
      if (TYPE(mmap->shmid) == T_HASH) {
        rb_block_call(mmap->shmid, rb_intern("each"), 0, NULL, mmap_ipc_initialize, self);
      }
      mmap->shmid = 0;

      if (mmap->semid) {
        mode = mmap->semid;
        mmap->semid = 0;
      }
      else {
        mode = 0644;
      }

      if ((int)mmap->key <= 0) {
        mode |= IPC_CREAT;
        strcpy(template, "/tmp/ruby_mmap.XXXXXX");
        if (mkstemp(template) == -1) {
          rb_sys_fail("mkstemp()");
        }
        if ((key = ftok(template, 'R')) == -1) {
          rb_sys_fail("ftok()");
        }
      }
      else {
        key = (key_t)mmap->key;
      }

      if ((shmid = shmget(key, sizeof(mmap_t), mode)) == -1) {
        rb_sys_fail("shmget()");
      }
      mmap = shmat(shmid, (void *)0, 0);
      if (mmap == (mmap_t *)-1) {
        rb_sys_fail("shmat()");
      }
      if (mmap->flag & MMAP_RUBY_TMP) {
        if (shmctl(shmid, IPC_RMID, &buf) == -1) {
          rb_sys_fail("shmctl()");
        }
      }

      if ((semid = semget(key, 1, mode)) == -1) {
        rb_sys_fail("semget()");
      }
      if (mode & IPC_CREAT) {
        sem_val.val = 1;
        if (semctl(semid, 0, SETVAL, sem_val) == -1) {
          rb_sys_fail("semctl()");
        }
      }

      mmap->key = key;
      mmap->semid = semid;
      mmap->shmid = shmid;
      if (mmap->flag & MMAP_RUBY_TMP) {
        mmap->template = ALLOC_N(char, strlen(template) + 1);
        strcpy(mmap->template, template);
      }
    }
  }

  if (anonymous) {
    if (size <= 0) {
      rb_raise(rb_eArgError, "length not specified for an anonymous map");
    }
    if (offset) {
      rb_warning("Ignoring offset for an anonymous map");
      offset = 0;
    }
    smode = O_RDWR;
    pmode = PROT_READ | PROT_WRITE;
    mmap->flag |= MMAP_RUBY_FIXED | MMAP_RUBY_ANON;
  }
  else {
    if (size == 0 && (smode & O_RDWR)) {
      if (lseek(fd, mmap->incr - 1, SEEK_END) == -1) {
        rb_raise(rb_eIOError, "can't lseek %lu", mmap->incr - 1);
      }
      if (write(fd, "\000", 1) != 1) {
        rb_raise(rb_eIOError, "can't extend %s", path);
      }
      init = 1;
      size = mmap->incr;
    }
    if (!NIL_P(fdv)) {
      mmap->flag |= MMAP_RUBY_FIXED;
    }
  }

  addr = mmap_func(0, size, pmode, vscope, fd, offset);
  if (NIL_P(fdv) && !anonymous) {
    close(fd);
  }
  if (addr == MAP_FAILED || !addr) {
    rb_raise(rb_eArgError, "mmap failed (%d)", errno);
  }

#ifdef MADV_NORMAL
  if (mmap->advice && madvise(addr, size, mmap->advice) == -1) {
    rb_raise(rb_eArgError, "madvise(%d)", errno);
  }
#endif

  if (anonymous && TYPE(options) == T_HASH) {
    VALUE val;
    char *ptr;

    val = rb_hash_aref(options, rb_str_new2("initialize"));
    if (NIL_P(val)) val = rb_hash_aref(options, ID2SYM(rb_intern("initialize")));
    if (!NIL_P(val)) {
      ptr = StringValuePtr(val);
      memset(addr, ptr[0], size);
    }
  }

  mmap->addr = addr;
  mmap->len = size;
  if (!init) mmap->real = size;
  mmap->pmode = pmode;
  mmap->vscope = vscope;
  mmap->smode = smode & ~O_TRUNC;
  mmap->path = (path) ? strdup(path) : (char *)(intptr_t)-1;

  if (smode == O_RDONLY) {
    self = rb_obj_freeze(self);
  }
  else {
    if (smode == O_WRONLY) {
      mmap->flag |= MMAP_RUBY_FIXED;
    }
  }

  return self;
}

Class Method Details

.lockall(flag) ⇒ nil .mlockall(flag) ⇒ nil

Disables paging of all pages mapped into the address space of the calling process. The flag parameter can be MCL_CURRENT (lock all currently mapped pages) or MCL_FUTURE (lock all pages that become mapped in the future).

Overloads:

  • .lockall(flag) ⇒ nil

    Returns:

    • (nil)
  • .mlockall(flag) ⇒ nil

    Returns:

    • (nil)


134
135
136
137
138
139
140
141
# File 'ext/mmap_ruby/mmap_ruby.c', line 134

static VALUE
rb_cMmap_mlockall(VALUE self, VALUE flag)
{
  if (mlockall(NUM2INT(flag)) == -1) {
    rb_raise(rb_eArgError, "mlockall(%d)", errno);
  }
  return Qnil;
}

.lockall(flag) ⇒ nil .mlockall(flag) ⇒ nil

Disables paging of all pages mapped into the address space of the calling process. The flag parameter can be MCL_CURRENT (lock all currently mapped pages) or MCL_FUTURE (lock all pages that become mapped in the future).

Overloads:

  • .lockall(flag) ⇒ nil

    Returns:

    • (nil)
  • .mlockall(flag) ⇒ nil

    Returns:

    • (nil)


134
135
136
137
138
139
140
141
# File 'ext/mmap_ruby/mmap_ruby.c', line 134

static VALUE
rb_cMmap_mlockall(VALUE self, VALUE flag)
{
  if (mlockall(NUM2INT(flag)) == -1) {
    rb_raise(rb_eArgError, "mlockall(%d)", errno);
  }
  return Qnil;
}

.unlockallnil .munlockallnil

Re-enables paging for all pages mapped into the address space of the calling process.

Overloads:

  • .unlockallnil

    Returns:

    • (nil)
  • .munlockallnil

    Returns:

    • (nil)


151
152
153
154
155
156
157
158
159
# File 'ext/mmap_ruby/mmap_ruby.c', line 151

static VALUE
rb_cMmap_munlockall(VALUE self)
{
  (void)self;
  if (munlockall() == -1) {
    rb_raise(rb_eArgError, "munlockall(%d)", errno);
  }
  return Qnil;
}

.unlockallnil .munlockallnil

Re-enables paging for all pages mapped into the address space of the calling process.

Overloads:

  • .unlockallnil

    Returns:

    • (nil)
  • .munlockallnil

    Returns:

    • (nil)


151
152
153
154
155
156
157
158
159
# File 'ext/mmap_ruby/mmap_ruby.c', line 151

static VALUE
rb_cMmap_munlockall(VALUE self)
{
  (void)self;
  if (munlockall() == -1) {
    rb_raise(rb_eArgError, "munlockall(%d)", errno);
  }
  return Qnil;
}

Instance Method Details

#concat(other) ⇒ self #<<(other) ⇒ self

Appends the contents of other to the mapped memory. If other is an integer between 0 and 255, it is treated as a byte value. Returns self.

Overloads:

  • #concat(other) ⇒ self

    Returns:

    • (self)
  • #<<(other) ⇒ self

    Returns:

    • (self)


1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
# File 'ext/mmap_ruby/mmap_ruby.c', line 1294

static VALUE
rb_cMmap_concat(VALUE self, VALUE other)
{
  if (FIXNUM_P(other)) {
    int i = FIX2INT(other);
    if (0 <= i && i <= 0xff) {
      char c = i;
      return mmap_cat(self, &c, 1);
    }
  }
  self = mmap_append(self, other);
  return self;
}

#<=>(other) ⇒ -1, ...

Compares the mapped memory with other. Returns -1 if the mapped memory is less than other, 0 if they are equal, 1 if the mapped memory is greater than other, or nil if the values are incomparable.

Returns:

  • (-1, 0, 1, nil)


642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'ext/mmap_ruby/mmap_ruby.c', line 642

static VALUE
rb_cMmap_cmp(VALUE self, VALUE other)
{
  VALUE self_str, other_str;
  VALUE result;

  self_str = mmap_str(self, MMAP_RUBY_ORIGIN);
  other_str = rb_str_to_str(other);
  result = rb_funcall2(self_str, rb_intern("<=>"), 1, &other_str);
  RB_GC_GUARD(self_str);
  RB_GC_GUARD(other_str);
  return result;
}

#==(other) ⇒ Boolean #===(other) ⇒ Boolean

Returns true if the content of the mapped memory is equal to other. Compares with other Mmap objects or strings.

Overloads:

  • #==(other) ⇒ Boolean

    Returns:

    • (Boolean)
  • #===(other) ⇒ Boolean

    Returns:

    • (Boolean)


611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'ext/mmap_ruby/mmap_ruby.c', line 611

static VALUE
rb_cMmap_equal(VALUE self, VALUE other)
{
  VALUE result;
  mmap_t *mmap, *other_mmap;

  if (self == other) return Qtrue;
  if (!rb_typeddata_is_kind_of(other, &mmap_type)) return Qfalse;

  GET_MMAP(self, mmap, 0);
  GET_MMAP(other, other_mmap, 0);
  if (mmap->real != other_mmap->real) {
    return Qfalse;
  }

  self = mmap_str(self, MMAP_RUBY_ORIGIN);
  other = mmap_str(other, MMAP_RUBY_ORIGIN);
  result = rb_funcall2(self, rb_intern("=="), 1, &other);
  RB_GC_GUARD(self);
  RB_GC_GUARD(other);
  return result;
}

#==(other) ⇒ Boolean #===(other) ⇒ Boolean

Returns true if the content of the mapped memory is equal to other. Compares with other Mmap objects or strings.

Overloads:

  • #==(other) ⇒ Boolean

    Returns:

    • (Boolean)
  • #===(other) ⇒ Boolean

    Returns:

    • (Boolean)


611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'ext/mmap_ruby/mmap_ruby.c', line 611

static VALUE
rb_cMmap_equal(VALUE self, VALUE other)
{
  VALUE result;
  mmap_t *mmap, *other_mmap;

  if (self == other) return Qtrue;
  if (!rb_typeddata_is_kind_of(other, &mmap_type)) return Qfalse;

  GET_MMAP(self, mmap, 0);
  GET_MMAP(other, other_mmap, 0);
  if (mmap->real != other_mmap->real) {
    return Qfalse;
  }

  self = mmap_str(self, MMAP_RUBY_ORIGIN);
  other = mmap_str(other, MMAP_RUBY_ORIGIN);
  result = rb_funcall2(self, rb_intern("=="), 1, &other);
  RB_GC_GUARD(self);
  RB_GC_GUARD(other);
  return result;
}

#=~(other) ⇒ Integer?

Returns the index of the first match of other in the mapped memory, or nil if no match is found. The other parameter can be a Regexp, String, or any object that responds to =~.

Returns:

  • (Integer, nil)


687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'ext/mmap_ruby/mmap_ruby.c', line 687

static VALUE
rb_cMmap_match(VALUE self, VALUE other)
{
  VALUE reg, res, self_str;
  long start;

  self_str = mmap_str(self, MMAP_RUBY_ORIGIN);
  if (rb_typeddata_is_kind_of(other, &mmap_type)) {
    other = rb_cMmap_to_str(other);
  }

  switch (TYPE(other)) {
    case T_REGEXP:
      res = rb_reg_match(other, self_str);
      break;

    case T_STRING:
      reg = rb_reg_regcomp(other);
      start = rb_reg_search(reg, self_str, 0, 0);
      if (start == -1) {
        res = Qnil;
      }
      else {
        res = LONG2NUM(start);
      }
      break;

    default:
      res = rb_funcall(other, rb_intern("=~"), 1, self_str);
      break;
  }

  RB_GC_GUARD(self_str);
  return res;
}

#[](nth) ⇒ String? #[](start, length) ⇒ String? #[](start..last) ⇒ String? #[](pattern) ⇒ String? #slice(nth) ⇒ String? #slice(start, length) ⇒ String? #slice(start..last) ⇒ String? #slice(pattern) ⇒ String?

Element reference with the following syntax:

self[nth]

Retrieves the nth character.

self[start..last]

Returns a substring from start to last.

self[start, length]

Returns a substring of length characters from start.

self[pattern]

Returns the first match of pattern (String or Regexp).

Overloads:

  • #[](nth) ⇒ String?

    Returns:

    • (String, nil)
  • #[](start, length) ⇒ String?

    Returns:

    • (String, nil)
  • #[](start..last) ⇒ String?

    Returns:

    • (String, nil)
  • #[](pattern) ⇒ String?

    Returns:

    • (String, nil)
  • #slice(nth) ⇒ String?

    Returns:

    • (String, nil)
  • #slice(start, length) ⇒ String?

    Returns:

    • (String, nil)
  • #slice(start..last) ⇒ String?

    Returns:

    • (String, nil)
  • #slice(pattern) ⇒ String?

    Returns:

    • (String, nil)


924
925
926
927
928
# File 'ext/mmap_ruby/mmap_ruby.c', line 924

static VALUE
rb_cMmap_aref(int argc, VALUE *argv, VALUE self)
{
  return mmap_bang_initialize(self, MMAP_RUBY_ORIGIN, rb_intern("[]"), argc, argv);
}

#[]=(nth, val) ⇒ Object #[]=(start, length, val) ⇒ Object #[]=(start..last, val) ⇒ Object #[]=(pattern, val) ⇒ Object #[]=(pattern, nth, val) ⇒ Object

Element assignment with the following syntax:

self[nth] = val

Changes the nth character with val.

self[start..last] = val

Changes substring from start to last with val.

self[start, length] = val

Replaces length characters from start with val.

self[pattern] = val

Replaces the first match of pattern with val.

self[pattern, nth] = val

Replaces the nth match of pattern with val.



1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
# File 'ext/mmap_ruby/mmap_ruby.c', line 1100

static VALUE
rb_cMmap_aset(int argc, VALUE *argv, VALUE self)
{
  mmap_t *mmap;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (argc == 3) {
    long beg, len;

    if (TYPE(argv[0]) == T_REGEXP) {
      mmap_subpat_set(self, argv[0], NUM2INT(argv[1]), argv[2]);
    }
    else {
      beg = NUM2INT(argv[0]);
      len = NUM2INT(argv[1]);
      mmap_update(mmap, beg, len, argv[2]);
    }
    return argv[2];
  }
  if (argc != 2) {
    rb_raise(rb_eArgError, "wrong # of arguments(%d for 2)", argc);
  }
  return mmap_aset_m(self, argv[0], argv[1]);
}

#madvise(advice) ⇒ nil #advise(advice) ⇒ nil

Gives advice to the kernel about how the mapped memory will be accessed. The advice parameter can be one of the following constants: MADV_NORMAL, MADV_RANDOM, MADV_SEQUENTIAL, MADV_WILLNEED, or MADV_DONTNEED.

Overloads:

  • #madvise(advice) ⇒ nil

    Returns:

    • (nil)
  • #advise(advice) ⇒ nil

    Returns:

    • (nil)


2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
# File 'ext/mmap_ruby/mmap_ruby.c', line 2317

static VALUE
rb_cMmap_madvise(VALUE self, VALUE advice)
{
  mmap_t *mmap;

  GET_MMAP(self, mmap, 0);
  if (madvise(mmap->addr, mmap->len, NUM2INT(advice)) == -1) {
    rb_raise(rb_eTypeError, "madvise(%d)", errno);
  }
  mmap->advice = NUM2INT(advice);
  return Qnil;
}

#capitalize!self

Changes the first character to uppercase letter in the mapped memory. Returns self.

Returns:

  • (self)


1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
# File 'ext/mmap_ruby/mmap_ruby.c', line 1730

static VALUE
rb_cMmap_capitalize_bang(VALUE self)
{
  VALUE res;
  mmap_bang bang_st;
  mmap_t *mmap;

  bang_st.argc = 0;
  bang_st.argv = NULL;
  bang_st.obj = self;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (mmap->flag & MMAP_RUBY_IPC) {
    mmap_lock(mmap, Qtrue);
    res = rb_ensure(mmap_capitalize_bang_int, (VALUE)&bang_st, mmap_vunlock, self);
  }
  else {
    res = mmap_capitalize_bang_int((VALUE)&bang_st);
  }

  return res;
}

#casecmp(other) ⇒ -1, ...

Performs a case-insensitive comparison of the mapped memory with other. Returns -1, 0, or 1 depending on whether the mapped memory is less than, equal to, or greater than other. Returns nil if the two values are incomparable.

Returns:

  • (-1, 0, 1, nil)


665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'ext/mmap_ruby/mmap_ruby.c', line 665

static VALUE
rb_cMmap_casecmp(VALUE self, VALUE other)
{
  VALUE result;
  VALUE self_str, other_str;

  self_str = mmap_str(self, MMAP_RUBY_ORIGIN);
  other_str = rb_str_to_str(other);
  result = rb_funcall2(self_str, rb_intern("casecmp"), 1, &other_str);
  RB_GC_GUARD(self_str);
  RB_GC_GUARD(other_str);
  return result;
}

#chomp!(rs = $/) ⇒ self

Chops off line ending character specified by rs in the mapped memory. Returns self.

Returns:

  • (self)


2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
# File 'ext/mmap_ruby/mmap_ruby.c', line 2050

static VALUE
rb_cMmap_chomp_bang(int argc, VALUE *argv, VALUE self)
{
  VALUE res;
  mmap_bang bang_st;
  mmap_t *mmap;

  bang_st.argc = argc;
  bang_st.argv = argv;
  bang_st.obj = self;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (mmap->flag & MMAP_RUBY_IPC) {
    mmap_lock(mmap, Qtrue);
    res = rb_ensure(mmap_chomp_bang_int, (VALUE)&bang_st, mmap_vunlock, self);
  }
  else {
    res = mmap_chomp_bang_int((VALUE)&bang_st);
  }

  return res;
}

#chop!self

Chops off the last character in the mapped memory. Returns self.

Returns:

  • (self)


1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
# File 'ext/mmap_ruby/mmap_ruby.c', line 1952

static VALUE
rb_cMmap_chop_bang(VALUE self)
{
  VALUE res;
  mmap_bang bang_st;
  mmap_t *mmap;

  bang_st.argc = 0;
  bang_st.argv = NULL;
  bang_st.obj = self;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (mmap->flag & MMAP_RUBY_IPC) {
    mmap_lock(mmap, Qtrue);
    res = rb_ensure(mmap_chop_bang_int, (VALUE)&bang_st, mmap_vunlock, self);
  }
  else {
    res = mmap_chop_bang_int((VALUE)&bang_st);
  }

  return res;
}

#cloneObject

:nodoc:

Raises:

  • (TypeError)


8
9
10
# File 'lib/mmap-ruby/mmap.rb', line 8

def clone # :nodoc:
  raise TypeError, "can't clone instance of #{self.class}"
end

#concat(other) ⇒ self #<<(other) ⇒ self

Appends the contents of other to the mapped memory. If other is an integer between 0 and 255, it is treated as a byte value. Returns self.

Overloads:

  • #concat(other) ⇒ self

    Returns:

    • (self)
  • #<<(other) ⇒ self

    Returns:

    • (self)


1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
# File 'ext/mmap_ruby/mmap_ruby.c', line 1294

static VALUE
rb_cMmap_concat(VALUE self, VALUE other)
{
  if (FIXNUM_P(other)) {
    int i = FIX2INT(other);
    if (0 <= i && i <= 0xff) {
      char c = i;
      return mmap_cat(self, &c, 1);
    }
  }
  self = mmap_append(self, other);
  return self;
}

#count(o1, *args) ⇒ Integer

Each parameter defines a set of characters to count in the mapped memory. Returns the total count.

Returns:

  • (Integer)


1204
1205
1206
1207
1208
# File 'ext/mmap_ruby/mmap_ruby.c', line 1204

static VALUE
rb_cMmap_count(int argc, VALUE *argv, VALUE self)
{
  return mmap_bang_initialize(self, MMAP_RUBY_ORIGIN, rb_intern("count"), argc, argv);
}

#crypt(salt) ⇒ String

Encrypts the mapped memory using the standard Unix crypt function with the given salt. Returns the encrypted string.

Returns:

  • (String)


2241
2242
2243
2244
2245
# File 'ext/mmap_ruby/mmap_ruby.c', line 2241

static VALUE
rb_cMmap_crypt(VALUE self, VALUE salt)
{
  return mmap_bang_initialize(self, MMAP_RUBY_ORIGIN, rb_intern("crypt"), 1, &salt);
}

#delete!(str) ⇒ self

Deletes every character included in str from the mapped memory. Returns self.

Returns:

  • (self)


2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
# File 'ext/mmap_ruby/mmap_ruby.c', line 2110

static VALUE
rb_cMmap_delete_bang(int argc, VALUE *argv, VALUE self)
{
  VALUE res;
  mmap_bang bang_st;
  mmap_t *mmap;

  bang_st.argc = argc;
  bang_st.argv = argv;
  bang_st.obj = self;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (mmap->flag & MMAP_RUBY_IPC) {
    mmap_lock(mmap, Qtrue);
    res = rb_ensure(mmap_delete_bang_int, (VALUE)&bang_st, mmap_vunlock, self);
  }
  else {
    res = mmap_delete_bang_int((VALUE)&bang_st);
  }

  return res;
}

#downcase!self

Changes all uppercase characters to lowercase characters in the mapped memory. Returns self.

Returns:

  • (self)


1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
# File 'ext/mmap_ruby/mmap_ruby.c', line 1664

static VALUE
rb_cMmap_downcase_bang(VALUE self)
{
  VALUE res;
  mmap_bang bang_st;
  mmap_t *mmap;

  bang_st.argc = 0;
  bang_st.argv = NULL;
  bang_st.obj = self;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (mmap->flag & MMAP_RUBY_IPC) {
    mmap_lock(mmap, Qtrue);
    res = rb_ensure(mmap_downcase_bang_int, (VALUE)&bang_st, mmap_vunlock, self);
  }
  else {
    res = mmap_downcase_bang_int((VALUE)&bang_st);
  }

  return res;
}

#dupObject

:nodoc:

Raises:

  • (TypeError)


12
13
14
# File 'lib/mmap-ruby/mmap.rb', line 12

def dup # :nodoc:
  raise TypeError, "can't dup instance of #{self.class}"
end

#each_byteObject Also known as: each



17
18
19
# File 'lib/mmap-ruby/mmap.rb', line 17

def each_byte(...)
  to_str.each_byte(...)
end

#each_lineObject



23
24
25
# File 'lib/mmap-ruby/mmap.rb', line 23

def each_line(...)
  to_str.each_line(...)
end

#empty?Boolean

Returns true if the file is empty, false otherwise.

Returns:

  • (Boolean)


885
886
887
888
889
890
891
892
893
# File 'ext/mmap_ruby/mmap_ruby.c', line 885

static VALUE
rb_cMmap_empty(VALUE self)
{
  mmap_t *mmap;

  GET_MMAP(self, mmap, 0);
  if (mmap->real == 0) return Qtrue;
  return Qfalse;
}

#eql?(other) ⇒ Boolean

Returns true if the content and type of the mapped memory is equal to other. Unlike ==, this method only returns true for other Mmap objects, not strings.

Returns:

  • (Boolean)


585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'ext/mmap_ruby/mmap_ruby.c', line 585

static VALUE
rb_cMmap_eql(VALUE self, VALUE other)
{
  mmap_t *mmap, *other_mmap;

  if (self == other) return Qtrue;
  if (!rb_typeddata_is_kind_of(other, &mmap_type)) return Qfalse;

  GET_MMAP(self, mmap, 0);
  GET_MMAP(other, other_mmap, 0);
  if (mmap->real != other_mmap->real) {
    return Qfalse;
  }

  if (memcmp(mmap->addr, other_mmap->addr, mmap->real) == 0) return Qtrue;
  return Qfalse;
}

#extend(count) ⇒ Integer

Adds count bytes to the file (i.e. pre-extends the file). Returns the new size of the mapped memory.

Returns:

  • (Integer)


2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
# File 'ext/mmap_ruby/mmap_ruby.c', line 2515

static VALUE
rb_cMmap_extend(VALUE self, VALUE count)
{
  mmap_t *mmap;
  long len;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  len = NUM2LONG(count);
  if (len > 0) {
    mmap_expandf(mmap, mmap->len + len);
  }
  return SIZET2NUM(mmap->len);
}

#msync(flag = MS_SYNC) ⇒ self #sync(flag = MS_SYNC) ⇒ self #flush(flag = MS_SYNC) ⇒ self

Flushes the mapped memory to the underlying file. The flag parameter controls the synchronization behavior (MS_SYNC, MS_ASYNC, or MS_INVALIDATE). Returns self.

Overloads:

  • #msync(flag = MS_SYNC) ⇒ self

    Returns:

    • (self)
  • #sync(flag = MS_SYNC) ⇒ self

    Returns:

    • (self)
  • #flush(flag = MS_SYNC) ⇒ self

    Returns:

    • (self)


2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
# File 'ext/mmap_ruby/mmap_ruby.c', line 2432

static VALUE
rb_cMmap_msync(int argc, VALUE *argv, VALUE self)
{
  mmap_t *mmap;
  VALUE oflag;
  int ret;
  int flag = MS_SYNC;

  if (argc) {
    rb_scan_args(argc, argv, "01", &oflag);
    flag = NUM2INT(oflag);
  }

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if ((ret = msync(mmap->addr, mmap->len, flag)) != 0) {
    rb_raise(rb_eArgError, "msync(%d)", ret);
  }

  if (mmap->real < mmap->len && mmap->vscope != MAP_PRIVATE) {
    mmap_expandf(mmap, mmap->real);
  }

  return self;
}

#gsub!(pattern, replacement) ⇒ self? #gsub!(pattern) {|match| ... } ⇒ self?

Performs global substitution on the mapped memory. Returns self if any substitutions were made, or nil if no substitutions occurred.

Overloads:

  • #gsub!(pattern, replacement) ⇒ self?

    Returns:

    • (self, nil)
  • #gsub!(pattern) {|match| ... } ⇒ self?

    Yields:

    Returns:

    • (self, nil)


1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
# File 'ext/mmap_ruby/mmap_ruby.c', line 1546

static VALUE
rb_cMmap_gsub_bang(int argc, VALUE *argv, VALUE self)
{
  VALUE res;
  mmap_bang bang_st;
  mmap_t *mmap;

  bang_st.argc = argc;
  bang_st.argv = argv;
  bang_st.obj = self;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (mmap->flag & MMAP_RUBY_IPC) {
    mmap_lock(mmap, Qtrue);
    res = rb_ensure(mmap_gsub_bang_int, (VALUE)&bang_st, mmap_vunlock, self);
  }
  else {
    res = mmap_gsub_bang_int((VALUE)&bang_st);
  }

  return res;
}

#hashInteger

Returns the hash value for the mapped memory content. Objects with the same content will have the same hash value.

Returns:

  • (Integer)


566
567
568
569
570
571
572
573
574
575
576
# File 'ext/mmap_ruby/mmap_ruby.c', line 566

static VALUE
rb_cMmap_hash(VALUE self)
{
  VALUE str;
  VALUE result;

  str = mmap_str(self, MMAP_RUBY_ORIGIN);
  result = rb_funcall(str, rb_intern("hash"), 0);
  RB_GC_GUARD(str);
  return result;
}

#include?(other) ⇒ Boolean

Returns true if other is found in the mapped memory, false otherwise. The other parameter can be a string or regular expression.

Returns:

  • (Boolean)


1165
1166
1167
1168
1169
# File 'ext/mmap_ruby/mmap_ruby.c', line 1165

static VALUE
rb_cMmap_include(VALUE self, VALUE other)
{
  return mmap_bang_initialize(self, MMAP_RUBY_ORIGIN, rb_intern("include?"), 1, &other);
}

#index(substr) ⇒ Integer? #index(pattern) ⇒ Integer?

Returns the index of substr or pattern, or nil if not found.

Overloads:

  • #index(substr) ⇒ Integer?

    Returns:

    • (Integer, nil)
  • #index(pattern) ⇒ Integer?

    Returns:

    • (Integer, nil)


1178
1179
1180
1181
1182
# File 'ext/mmap_ruby/mmap_ruby.c', line 1178

static VALUE
rb_cMmap_index(int argc, VALUE *argv, VALUE self)
{
  return mmap_bang_initialize(self, MMAP_RUBY_ORIGIN, rb_intern("index"), argc, argv);
}

#insert(index, str) ⇒ self

Inserts str at index. Returns self.

Returns:

  • (self)


1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
# File 'ext/mmap_ruby/mmap_ruby.c', line 1228

static VALUE
rb_cMmap_insert(VALUE self, VALUE idx, VALUE str)
{
  mmap_t *mmap;
  long pos = NUM2LONG(idx);

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (pos == -1) {
    pos = mmap->real;
  }
  else if (pos < 0) {
    pos++;
  }
  mmap_update(mmap, pos, 0, str);
  return self;
}

#ipc_keyInteger

Gets the IPC key.

Returns:

  • (Integer)


2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
# File 'ext/mmap_ruby/mmap_ruby.c', line 2593

static VALUE
rb_cMmap_ipc_key(VALUE self)
{
  mmap_t *mmap;

  GET_MMAP(self, mmap, 0);
  if (mmap->flag & MMAP_RUBY_IPC) {
    return INT2NUM((int)mmap->key);
  }
  return INT2NUM(-1);
}

#lengthInteger #sizeInteger

Returns the size of the file.

Overloads:

  • #lengthInteger

    Returns:

    • (Integer)
  • #sizeInteger

    Returns:

    • (Integer)


870
871
872
873
874
875
876
877
# File 'ext/mmap_ruby/mmap_ruby.c', line 870

static VALUE
rb_cMmap_size(VALUE self)
{
  mmap_t *mmap;

  GET_MMAP(self, mmap, 0);
  return SIZET2NUM(mmap->real);
}

#lockself #mlockself

Disables paging for the mapped memory, locking it in physical memory. Returns self.

Overloads:

  • #lockself

    Returns:

    • (self)
  • #mlockself

    Returns:

    • (self)


2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
# File 'ext/mmap_ruby/mmap_ruby.c', line 2465

static VALUE
rb_cMmap_mlock(VALUE self)
{
  mmap_t *mmap;

  TypedData_Get_Struct(self, mmap_t, &mmap_type, mmap);
  if (mmap->flag & MMAP_RUBY_LOCK) {
    return self;
  }
  if (mmap->flag & MMAP_RUBY_ANON) {
    rb_raise(rb_eArgError, "mlock(anonymous)");
  }
  if (mlock(mmap->addr, mmap->len) == -1) {
    rb_raise(rb_eArgError, "mlock(%d)", errno);
  }
  mmap->flag |= MMAP_RUBY_LOCK;
  return self;
}

#madvise(advice) ⇒ nil #advise(advice) ⇒ nil

Gives advice to the kernel about how the mapped memory will be accessed. The advice parameter can be one of the following constants: MADV_NORMAL, MADV_RANDOM, MADV_SEQUENTIAL, MADV_WILLNEED, or MADV_DONTNEED.

Overloads:

  • #madvise(advice) ⇒ nil

    Returns:

    • (nil)
  • #advise(advice) ⇒ nil

    Returns:

    • (nil)


2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
# File 'ext/mmap_ruby/mmap_ruby.c', line 2317

static VALUE
rb_cMmap_madvise(VALUE self, VALUE advice)
{
  mmap_t *mmap;

  GET_MMAP(self, mmap, 0);
  if (madvise(mmap->addr, mmap->len, NUM2INT(advice)) == -1) {
    rb_raise(rb_eTypeError, "madvise(%d)", errno);
  }
  mmap->advice = NUM2INT(advice);
  return Qnil;
}

#match(pattern) ⇒ MatchData?

Converts pattern to a Regexp (if it isn’t already one) and returns a MatchData object describing the match, or nil if there was no match. This is equivalent to calling pattern.match on the mapped memory content.

Returns:

  • (MatchData, nil)


857
858
859
860
861
# File 'ext/mmap_ruby/mmap_ruby.c', line 857

static VALUE
rb_cMmap_match_m(VALUE self, VALUE pattern)
{
  return mmap_bang_initialize(self, MMAP_RUBY_ORIGIN, rb_intern("match"), 1, &pattern);
}

#lockself #mlockself

Disables paging for the mapped memory, locking it in physical memory. Returns self.

Overloads:

  • #lockself

    Returns:

    • (self)
  • #mlockself

    Returns:

    • (self)


2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
# File 'ext/mmap_ruby/mmap_ruby.c', line 2465

static VALUE
rb_cMmap_mlock(VALUE self)
{
  mmap_t *mmap;

  TypedData_Get_Struct(self, mmap_t, &mmap_type, mmap);
  if (mmap->flag & MMAP_RUBY_LOCK) {
    return self;
  }
  if (mmap->flag & MMAP_RUBY_ANON) {
    rb_raise(rb_eArgError, "mlock(anonymous)");
  }
  if (mlock(mmap->addr, mmap->len) == -1) {
    rb_raise(rb_eArgError, "mlock(%d)", errno);
  }
  mmap->flag |= MMAP_RUBY_LOCK;
  return self;
}

#mprotect(mode) ⇒ self #protect(mode) ⇒ self

Changes the memory protection mode. The mode value must be “r”, “w”, “rw”, or an integer representing protection flags. Returns self.

Overloads:

  • #mprotect(mode) ⇒ self

    Returns:

    • (self)
  • #protect(mode) ⇒ self

    Returns:

    • (self)


2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
# File 'ext/mmap_ruby/mmap_ruby.c', line 2255

static VALUE
rb_cMmap_mprotect(VALUE self, VALUE mode)
{
  mmap_t *mmap;
  int ret, pmode;
  const char *smode;

  GET_MMAP(self, mmap, 0);
  if (TYPE(mode) == T_STRING) {
    smode = StringValuePtr(mode);
    if (strcmp(smode, "r") == 0) {
      pmode = PROT_READ;
    }
    else if (strcmp(smode, "w") == 0) {
      pmode = PROT_WRITE;
    }
    else if (strcmp(smode, "rw") == 0 || strcmp(smode, "wr") == 0) {
      pmode = PROT_READ | PROT_WRITE;
    }
    else {
      rb_raise(rb_eArgError, "invalid mode %s", smode);
    }
  }
  else {
    pmode = NUM2INT(mode);
  }

  if ((pmode & PROT_WRITE) && RB_OBJ_FROZEN(self)) {
    rb_check_frozen(self);
  }

  if ((ret = mprotect(mmap->addr, mmap->len, pmode | PROT_READ)) != 0) {
    rb_raise(rb_eArgError, "mprotect(%d)", ret);
  }

  mmap->pmode = pmode;
  if (pmode & PROT_READ) {
    if (pmode & PROT_WRITE) {
      mmap->smode = O_RDWR;
    }
    else {
      mmap->smode = O_RDONLY;
      self = rb_obj_freeze(self);
    }
  }
  else if (pmode & PROT_WRITE) {
    mmap->flag |= MMAP_RUBY_FIXED;
    mmap->smode = O_WRONLY;
  }

  return self;
}

#msync(flag = MS_SYNC) ⇒ self #sync(flag = MS_SYNC) ⇒ self #flush(flag = MS_SYNC) ⇒ self

Flushes the mapped memory to the underlying file. The flag parameter controls the synchronization behavior (MS_SYNC, MS_ASYNC, or MS_INVALIDATE). Returns self.

Overloads:

  • #msync(flag = MS_SYNC) ⇒ self

    Returns:

    • (self)
  • #sync(flag = MS_SYNC) ⇒ self

    Returns:

    • (self)
  • #flush(flag = MS_SYNC) ⇒ self

    Returns:

    • (self)


2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
# File 'ext/mmap_ruby/mmap_ruby.c', line 2432

static VALUE
rb_cMmap_msync(int argc, VALUE *argv, VALUE self)
{
  mmap_t *mmap;
  VALUE oflag;
  int ret;
  int flag = MS_SYNC;

  if (argc) {
    rb_scan_args(argc, argv, "01", &oflag);
    flag = NUM2INT(oflag);
  }

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if ((ret = msync(mmap->addr, mmap->len, flag)) != 0) {
    rb_raise(rb_eArgError, "msync(%d)", ret);
  }

  if (mmap->real < mmap->len && mmap->vscope != MAP_PRIVATE) {
    mmap_expandf(mmap, mmap->real);
  }

  return self;
}

#unlockself #munlockself

Re-enables paging for the mapped memory. Returns self.

Overloads:

  • #unlockself

    Returns:

    • (self)
  • #munlockself

    Returns:

    • (self)


2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
# File 'ext/mmap_ruby/mmap_ruby.c', line 2492

static VALUE
rb_cMmap_munlock(VALUE self)
{
  mmap_t *mmap;

  TypedData_Get_Struct(self, mmap_t, &mmap_type, mmap);
  if (!(mmap->flag & MMAP_RUBY_LOCK)) {
    return self;
  }
  if (munlock(mmap->addr, mmap->len) == -1) {
    rb_raise(rb_eArgError, "munlock(%d)", errno);
  }
  mmap->flag &= ~MMAP_RUBY_LOCK;
  return self;
}

#munmapnil #unmapnil

Terminates the association between the mapped memory and the file.

Overloads:

  • #munmapnil

    Returns:

    • (nil)
  • #unmapnil

    Returns:

    • (nil)


2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
# File 'ext/mmap_ruby/mmap_ruby.c', line 2536

static VALUE
rb_cMmap_unmap(VALUE self)
{
  mmap_t *mmap;

  GET_MMAP(self, mmap, 0);
  if (mmap->path) {
    mmap_lock(mmap, Qtrue);
    munmap(mmap->addr, mmap->len);
    if (mmap->path != (char *)(intptr_t)-1) {
      if (mmap->real < mmap->len &&
          mmap->vscope != MAP_PRIVATE &&
          truncate(mmap->path, mmap->real) == -1) {
        rb_raise(rb_eTypeError, "truncate");
      }
      free(mmap->path);
    }
    mmap->path = NULL;
    mmap_unlock(mmap);
  }
  return Qnil;
}

#mprotect(mode) ⇒ self #protect(mode) ⇒ self

Changes the memory protection mode. The mode value must be “r”, “w”, “rw”, or an integer representing protection flags. Returns self.

Overloads:

  • #mprotect(mode) ⇒ self

    Returns:

    • (self)
  • #protect(mode) ⇒ self

    Returns:

    • (self)


2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
# File 'ext/mmap_ruby/mmap_ruby.c', line 2255

static VALUE
rb_cMmap_mprotect(VALUE self, VALUE mode)
{
  mmap_t *mmap;
  int ret, pmode;
  const char *smode;

  GET_MMAP(self, mmap, 0);
  if (TYPE(mode) == T_STRING) {
    smode = StringValuePtr(mode);
    if (strcmp(smode, "r") == 0) {
      pmode = PROT_READ;
    }
    else if (strcmp(smode, "w") == 0) {
      pmode = PROT_WRITE;
    }
    else if (strcmp(smode, "rw") == 0 || strcmp(smode, "wr") == 0) {
      pmode = PROT_READ | PROT_WRITE;
    }
    else {
      rb_raise(rb_eArgError, "invalid mode %s", smode);
    }
  }
  else {
    pmode = NUM2INT(mode);
  }

  if ((pmode & PROT_WRITE) && RB_OBJ_FROZEN(self)) {
    rb_check_frozen(self);
  }

  if ((ret = mprotect(mmap->addr, mmap->len, pmode | PROT_READ)) != 0) {
    rb_raise(rb_eArgError, "mprotect(%d)", ret);
  }

  mmap->pmode = pmode;
  if (pmode & PROT_READ) {
    if (pmode & PROT_WRITE) {
      mmap->smode = O_RDWR;
    }
    else {
      mmap->smode = O_RDONLY;
      self = rb_obj_freeze(self);
    }
  }
  else if (pmode & PROT_WRITE) {
    mmap->flag |= MMAP_RUBY_FIXED;
    mmap->smode = O_WRONLY;
  }

  return self;
}

#reverse!self

Reverses the characters in the mapped memory in place. Returns self.

Returns:

  • (self)


1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
# File 'ext/mmap_ruby/mmap_ruby.c', line 1850

static VALUE
rb_cMmap_reverse_bang(VALUE self)
{
  VALUE res;
  mmap_bang bang_st;
  mmap_t *mmap;

  bang_st.argc = 0;
  bang_st.argv = NULL;
  bang_st.obj = self;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (mmap->flag & MMAP_RUBY_IPC) {
    mmap_lock(mmap, Qtrue);
    res = rb_ensure(mmap_reverse_bang_int, (VALUE)&bang_st, mmap_vunlock, self);
  }
  else {
    res = mmap_reverse_bang_int((VALUE)&bang_st);
  }

  return res;
}

#rindex(substr, pos = nil) ⇒ Integer? #rindex(pattern, pos = nil) ⇒ Integer?

Returns the index of the last occurrence of substr or pattern, or nil if not found.

Overloads:

  • #rindex(substr, pos = nil) ⇒ Integer?

    Returns:

    • (Integer, nil)
  • #rindex(pattern, pos = nil) ⇒ Integer?

    Returns:

    • (Integer, nil)


1191
1192
1193
1194
1195
# File 'ext/mmap_ruby/mmap_ruby.c', line 1191

static VALUE
rb_cMmap_rindex(int argc, VALUE *argv, VALUE self)
{
  return mmap_bang_initialize(self, MMAP_RUBY_ORIGIN, rb_intern("rindex"), argc, argv);
}

#scanObject



28
29
30
# File 'lib/mmap-ruby/mmap.rb', line 28

def scan(...)
  to_str.scan(...)
end

#semlockself

Creates a lock.

Returns:

  • (self)


2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
# File 'ext/mmap_ruby/mmap_ruby.c', line 2565

static VALUE
rb_cMmap_semlock(int argc, VALUE *argv, VALUE self)
{
  mmap_t *mmap;
  VALUE a;
  int wait_lock = Qtrue;

  GET_MMAP(self, mmap, 0);
  if (!(mmap->flag & MMAP_RUBY_IPC)) {
    rb_warning("useless use of #semlock");
    rb_yield(self);
  }
  else {
    if (rb_scan_args(argc, argv, "01", &a)) {
      wait_lock = RTEST(a);
    }
    mmap_lock(mmap, wait_lock);
    rb_ensure(rb_yield, self, mmap_vunlock, self);
  }
  return Qnil;
}

#lengthInteger #sizeInteger

Returns the size of the file.

Overloads:

  • #lengthInteger

    Returns:

    • (Integer)
  • #sizeInteger

    Returns:

    • (Integer)


870
871
872
873
874
875
876
877
# File 'ext/mmap_ruby/mmap_ruby.c', line 870

static VALUE
rb_cMmap_size(VALUE self)
{
  mmap_t *mmap;

  GET_MMAP(self, mmap, 0);
  return SIZET2NUM(mmap->real);
}

#[](nth) ⇒ String? #[](start, length) ⇒ String? #[](start..last) ⇒ String? #[](pattern) ⇒ String? #slice(nth) ⇒ String? #slice(start, length) ⇒ String? #slice(start..last) ⇒ String? #slice(pattern) ⇒ String?

Element reference with the following syntax:

self[nth]

Retrieves the nth character.

self[start..last]

Returns a substring from start to last.

self[start, length]

Returns a substring of length characters from start.

self[pattern]

Returns the first match of pattern (String or Regexp).

Overloads:

  • #[](nth) ⇒ String?

    Returns:

    • (String, nil)
  • #[](start, length) ⇒ String?

    Returns:

    • (String, nil)
  • #[](start..last) ⇒ String?

    Returns:

    • (String, nil)
  • #[](pattern) ⇒ String?

    Returns:

    • (String, nil)
  • #slice(nth) ⇒ String?

    Returns:

    • (String, nil)
  • #slice(start, length) ⇒ String?

    Returns:

    • (String, nil)
  • #slice(start..last) ⇒ String?

    Returns:

    • (String, nil)
  • #slice(pattern) ⇒ String?

    Returns:

    • (String, nil)


924
925
926
927
928
# File 'ext/mmap_ruby/mmap_ruby.c', line 924

static VALUE
rb_cMmap_aref(int argc, VALUE *argv, VALUE self)
{
  return mmap_bang_initialize(self, MMAP_RUBY_ORIGIN, rb_intern("[]"), argc, argv);
}

#slice!(nth) ⇒ String? #slice!(start, length) ⇒ String? #slice!(start..last) ⇒ String? #slice!(pattern) ⇒ String?

Deletes the specified portion of the mapped memory and returns it. Returns nil if the portion is not found.

Overloads:

  • #slice!(nth) ⇒ String?

    Returns:

    • (String, nil)
  • #slice!(start, length) ⇒ String?

    Returns:

    • (String, nil)
  • #slice!(start..last) ⇒ String?

    Returns:

    • (String, nil)
  • #slice!(pattern) ⇒ String?

    Returns:

    • (String, nil)


1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
# File 'ext/mmap_ruby/mmap_ruby.c', line 1135

static VALUE
rb_cMmap_slice_bang(int argc, VALUE *argv, VALUE self)
{
  VALUE result;
  VALUE buf[3];
  int i;

  if (argc < 1 || 2 < argc) {
    rb_raise(rb_eArgError, "wrong # of arguments(%d for 1)", argc);
  }

  for (i = 0; i < argc; i++) {
    buf[i] = argv[i];
  }
  buf[i] = rb_str_new(0, 0);
  result = rb_cMmap_aref(argc, buf, self);
  if (!NIL_P(result)) {
    rb_cMmap_aset(argc + 1, buf, self);
  }

  return result;
}

#split(sep = $/, limit = 0) ⇒ Array

Splits the mapped memory into an array of strings and returns this array. The sep parameter specifies the separator pattern (String or Regexp). The limit parameter controls the number of splits.

Returns:

  • (Array)


2228
2229
2230
2231
2232
# File 'ext/mmap_ruby/mmap_ruby.c', line 2228

static VALUE
rb_cMmap_split(int argc, VALUE *argv, VALUE self)
{
  return mmap_bang_initialize(self, MMAP_RUBY_ORIGIN, rb_intern("split"), argc, argv);
}

#squeeze!(str) ⇒ self

Squeezes sequences of the same characters that are included in str. Returns self.

Returns:

  • (self)


2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
# File 'ext/mmap_ruby/mmap_ruby.c', line 2197

static VALUE
rb_cMmap_squeeze_bang(int argc, VALUE *argv, VALUE self)
{
  VALUE res;
  mmap_bang bang_st;
  mmap_t *mmap;

  bang_st.argc = argc;
  bang_st.argv = argv;
  bang_st.obj = self;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (mmap->flag & MMAP_RUBY_IPC) {
    mmap_lock(mmap, Qtrue);
    res = rb_ensure(mmap_squeeze_bang_int, (VALUE)&bang_st, mmap_vunlock, self);
  }
  else {
    res = mmap_squeeze_bang_int((VALUE)&bang_st);
  }

  return res;
}

#strip!self

Removes leading and trailing whitespace from the mapped memory. Returns self.

Returns:

  • (self)


1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
# File 'ext/mmap_ruby/mmap_ruby.c', line 1880

static VALUE
rb_cMmap_strip_bang(VALUE self)
{
  char *s, *t, *e;
  mmap_t *mmap;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  mmap_lock(mmap, Qtrue);
  s = (char *)mmap->addr;
  e = t = s + mmap->real;
  while (s < t && ISSPACE(*s)) s++;
  t--;
  while (s <= t && ISSPACE(*t)) t--;
  t++;

  if (mmap->real != (size_t)(t - s) && (mmap->flag & MMAP_RUBY_FIXED)) {
    mmap_unlock(mmap);
    rb_raise(rb_eTypeError, "can't change the size of a fixed map");
  }
  mmap->real = t - s;
  if (s > (char *)mmap->addr) {
    memmove(mmap->addr, s, mmap->real);
    ((char *)mmap->addr)[mmap->real] = '\0';
  }
  else if (t < e) {
    ((char *)mmap->addr)[mmap->real] = '\0';
  }
  else {
    self = Qnil;
  }
  mmap_unlock(mmap);
  return self;
}

#sub!(pattern, replacement) ⇒ self? #sub!(pattern) {|match| ... } ⇒ self?

Performs substitution on the mapped memory. Returns self if a substitution was made, or nil if no substitution occurred.

Overloads:

  • #sub!(pattern, replacement) ⇒ self?

    Returns:

    • (self, nil)
  • #sub!(pattern) {|match| ... } ⇒ self?

    Yields:

    Returns:

    • (self, nil)


1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
# File 'ext/mmap_ruby/mmap_ruby.c', line 1428

static VALUE
rb_cMmap_sub_bang(int argc, VALUE *argv, VALUE self)
{
  VALUE res;
  mmap_bang bang_st;
  mmap_t *mmap;

  bang_st.argc = argc;
  bang_st.argv = argv;
  bang_st.obj = self;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (mmap->flag & MMAP_RUBY_IPC) {
    mmap_lock(mmap, Qtrue);
    res = rb_ensure(mmap_sub_bang_int, (VALUE)&bang_st, mmap_vunlock, self);
  }
  else {
    res = mmap_sub_bang_int((VALUE)&bang_st);
  }

  return res;
}

#sum(bits = 16) ⇒ Integer

Returns a checksum for the mapped memory content.

Returns:

  • (Integer)


1216
1217
1218
1219
1220
# File 'ext/mmap_ruby/mmap_ruby.c', line 1216

static VALUE
rb_cMmap_sum(int argc, VALUE *argv, VALUE self)
{
  return mmap_bang_initialize(self, MMAP_RUBY_ORIGIN, rb_intern("sum"), argc, argv);
}

#swapcase!self

Replaces lowercase to uppercase and vice-versa in the mapped memory. Returns self.

Returns:

  • (self)


1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
# File 'ext/mmap_ruby/mmap_ruby.c', line 1792

static VALUE
rb_cMmap_swapcase_bang(VALUE self)
{
  VALUE res;
  mmap_bang bang_st;
  mmap_t *mmap;

  bang_st.argc = 0;
  bang_st.argv = NULL;
  bang_st.obj = self;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (mmap->flag & MMAP_RUBY_IPC) {
    mmap_lock(mmap, Qtrue);
    res = rb_ensure(mmap_swapcase_bang_int, (VALUE)&bang_st, mmap_vunlock, self);
  }
  else {
    res = mmap_swapcase_bang_int((VALUE)&bang_st);
  }

  return res;
}

#msync(flag = MS_SYNC) ⇒ self #sync(flag = MS_SYNC) ⇒ self #flush(flag = MS_SYNC) ⇒ self

Flushes the mapped memory to the underlying file. The flag parameter controls the synchronization behavior (MS_SYNC, MS_ASYNC, or MS_INVALIDATE). Returns self.

Overloads:

  • #msync(flag = MS_SYNC) ⇒ self

    Returns:

    • (self)
  • #sync(flag = MS_SYNC) ⇒ self

    Returns:

    • (self)
  • #flush(flag = MS_SYNC) ⇒ self

    Returns:

    • (self)


2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
# File 'ext/mmap_ruby/mmap_ruby.c', line 2432

static VALUE
rb_cMmap_msync(int argc, VALUE *argv, VALUE self)
{
  mmap_t *mmap;
  VALUE oflag;
  int ret;
  int flag = MS_SYNC;

  if (argc) {
    rb_scan_args(argc, argv, "01", &oflag);
    flag = NUM2INT(oflag);
  }

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if ((ret = msync(mmap->addr, mmap->len, flag)) != 0) {
    rb_raise(rb_eArgError, "msync(%d)", ret);
  }

  if (mmap->real < mmap->len && mmap->vscope != MAP_PRIVATE) {
    mmap_expandf(mmap, mmap->real);
  }

  return self;
}

#to_strObject

Convert object to a string.



553
554
555
556
557
# File 'ext/mmap_ruby/mmap_ruby.c', line 553

static VALUE
rb_cMmap_to_str(VALUE self)
{
  return mmap_str(self, MMAP_RUBY_ORIGIN);
}

#unlockself #munlockself

Re-enables paging for the mapped memory. Returns self.

Overloads:

  • #unlockself

    Returns:

    • (self)
  • #munlockself

    Returns:

    • (self)


2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
# File 'ext/mmap_ruby/mmap_ruby.c', line 2492

static VALUE
rb_cMmap_munlock(VALUE self)
{
  mmap_t *mmap;

  TypedData_Get_Struct(self, mmap_t, &mmap_type, mmap);
  if (!(mmap->flag & MMAP_RUBY_LOCK)) {
    return self;
  }
  if (munlock(mmap->addr, mmap->len) == -1) {
    rb_raise(rb_eArgError, "munlock(%d)", errno);
  }
  mmap->flag &= ~MMAP_RUBY_LOCK;
  return self;
}

#munmapnil #unmapnil

Terminates the association between the mapped memory and the file.

Overloads:

  • #munmapnil

    Returns:

    • (nil)
  • #unmapnil

    Returns:

    • (nil)


2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
# File 'ext/mmap_ruby/mmap_ruby.c', line 2536

static VALUE
rb_cMmap_unmap(VALUE self)
{
  mmap_t *mmap;

  GET_MMAP(self, mmap, 0);
  if (mmap->path) {
    mmap_lock(mmap, Qtrue);
    munmap(mmap->addr, mmap->len);
    if (mmap->path != (char *)(intptr_t)-1) {
      if (mmap->real < mmap->len &&
          mmap->vscope != MAP_PRIVATE &&
          truncate(mmap->path, mmap->real) == -1) {
        rb_raise(rb_eTypeError, "truncate");
      }
      free(mmap->path);
    }
    mmap->path = NULL;
    mmap_unlock(mmap);
  }
  return Qnil;
}

#upcase!self

Replaces all lowercase characters to uppercase characters in the mapped memory. Returns self.

Returns:

  • (self)


1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
# File 'ext/mmap_ruby/mmap_ruby.c', line 1605

static VALUE
rb_cMmap_upcase_bang(VALUE self)
{
  VALUE res;
  mmap_bang bang_st;
  mmap_t *mmap;

  bang_st.argc = 0;
  bang_st.argv = NULL;
  bang_st.obj = self;

  GET_MMAP(self, mmap, MMAP_RUBY_MODIFY);
  if (mmap->flag & MMAP_RUBY_IPC) {
    mmap_lock(mmap, Qtrue);
    res = rb_ensure(mmap_upcase_bang_int, (VALUE)&bang_st, mmap_vunlock, self);
  }
  else {
    res = mmap_upcase_bang_int((VALUE)&bang_st);
  }

  return res;
}