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).



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
519
520
521
522
523
524
# File 'ext/mmap_ruby/mmap_ruby.c', line 252

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->len && offset) size -= 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;
      }

      char ipc_template[1024];
      ipc_template[0] = '\0';

      if ((int)mmap->key <= 0) {
        mode |= IPC_CREAT;
        strcpy(ipc_template, "/tmp/ruby_mmap.XXXXXX");
        if (mkstemp(ipc_template) == -1) {
          rb_sys_fail("mkstemp()");
        }
        if ((key = ftok(ipc_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) && ipc_template[0]) {
        mmap->template = ALLOC_N(char, strlen(ipc_template) + 1);
        strcpy(mmap->template, ipc_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)


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

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)


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

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)


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

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)


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

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)


1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
# File 'ext/mmap_ruby/mmap_ruby.c', line 1274

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)


650
651
652
653
654
655
656
657
658
659
660
661
662
# File 'ext/mmap_ruby/mmap_ruby.c', line 650

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. Only compares with other Mmap objects.

Overloads:

  • #==(other) ⇒ Boolean

    Returns:

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

    Returns:

    • (Boolean)


619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# File 'ext/mmap_ruby/mmap_ruby.c', line 619

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. Only compares with other Mmap objects.

Overloads:

  • #==(other) ⇒ Boolean

    Returns:

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

    Returns:

    • (Boolean)


619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# File 'ext/mmap_ruby/mmap_ruby.c', line 619

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)


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
722
723
724
725
726
727
728
729
# File 'ext/mmap_ruby/mmap_ruby.c', line 695

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)


912
913
914
915
916
# File 'ext/mmap_ruby/mmap_ruby.c', line 912

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.



1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
# File 'ext/mmap_ruby/mmap_ruby.c', line 1080

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)


2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
# File 'ext/mmap_ruby/mmap_ruby.c', line 2276

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)


1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
# File 'ext/mmap_ruby/mmap_ruby.c', line 1716

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, 1);
    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)


673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'ext/mmap_ruby/mmap_ruby.c', line 673

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)


2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
# File 'ext/mmap_ruby/mmap_ruby.c', line 2036

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, 1);
    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)


1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
# File 'ext/mmap_ruby/mmap_ruby.c', line 1938

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, 1);
    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)


1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
# File 'ext/mmap_ruby/mmap_ruby.c', line 1274

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)


1184
1185
1186
1187
1188
# File 'ext/mmap_ruby/mmap_ruby.c', line 1184

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)


2200
2201
2202
2203
2204
# File 'ext/mmap_ruby/mmap_ruby.c', line 2200

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)


2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
# File 'ext/mmap_ruby/mmap_ruby.c', line 2096

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, 1);
    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)


1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
# File 'ext/mmap_ruby/mmap_ruby.c', line 1650

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, 1);
    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)


873
874
875
876
877
878
879
880
881
# File 'ext/mmap_ruby/mmap_ruby.c', line 873

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)


593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'ext/mmap_ruby/mmap_ruby.c', line 593

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)


2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
# File 'ext/mmap_ruby/mmap_ruby.c', line 2475

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)


2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
# File 'ext/mmap_ruby/mmap_ruby.c', line 2392

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)


1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
# File 'ext/mmap_ruby/mmap_ruby.c', line 1532

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, 1);
    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)


574
575
576
577
578
579
580
581
582
583
584
# File 'ext/mmap_ruby/mmap_ruby.c', line 574

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)


1145
1146
1147
1148
1149
# File 'ext/mmap_ruby/mmap_ruby.c', line 1145

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)


1158
1159
1160
1161
1162
# File 'ext/mmap_ruby/mmap_ruby.c', line 1158

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)


1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
# File 'ext/mmap_ruby/mmap_ruby.c', line 1208

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)


2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
# File 'ext/mmap_ruby/mmap_ruby.c', line 2552

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)


858
859
860
861
862
863
864
865
# File 'ext/mmap_ruby/mmap_ruby.c', line 858

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)


2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
# File 'ext/mmap_ruby/mmap_ruby.c', line 2425

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)


2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
# File 'ext/mmap_ruby/mmap_ruby.c', line 2276

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)


845
846
847
848
849
# File 'ext/mmap_ruby/mmap_ruby.c', line 845

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)


2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
# File 'ext/mmap_ruby/mmap_ruby.c', line 2425

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)


2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
# File 'ext/mmap_ruby/mmap_ruby.c', line 2214

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)


2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
# File 'ext/mmap_ruby/mmap_ruby.c', line 2392

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)


2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
# File 'ext/mmap_ruby/mmap_ruby.c', line 2452

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)


2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
# File 'ext/mmap_ruby/mmap_ruby.c', line 2496

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

  GET_MMAP(self, mmap, 0);
  mmap_lock(mmap, 1);
  munmap(mmap->addr, mmap->len);
  mmap->addr = NULL;
  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)


2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
# File 'ext/mmap_ruby/mmap_ruby.c', line 2214

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)


1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
# File 'ext/mmap_ruby/mmap_ruby.c', line 1836

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, 1);
    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)


1171
1172
1173
1174
1175
# File 'ext/mmap_ruby/mmap_ruby.c', line 1171

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)


2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
# File 'ext/mmap_ruby/mmap_ruby.c', line 2524

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

  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)


858
859
860
861
862
863
864
865
# File 'ext/mmap_ruby/mmap_ruby.c', line 858

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)


912
913
914
915
916
# File 'ext/mmap_ruby/mmap_ruby.c', line 912

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)


1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'ext/mmap_ruby/mmap_ruby.c', line 1115

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)


2187
2188
2189
2190
2191
# File 'ext/mmap_ruby/mmap_ruby.c', line 2187

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)


2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
# File 'ext/mmap_ruby/mmap_ruby.c', line 2156

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, 1);
    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)


1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
# File 'ext/mmap_ruby/mmap_ruby.c', line 1866

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, 1);
  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)


1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
# File 'ext/mmap_ruby/mmap_ruby.c', line 1413

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, 1);
    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)


1196
1197
1198
1199
1200
# File 'ext/mmap_ruby/mmap_ruby.c', line 1196

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)


1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
# File 'ext/mmap_ruby/mmap_ruby.c', line 1778

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, 1);
    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)


2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
# File 'ext/mmap_ruby/mmap_ruby.c', line 2392

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.



561
562
563
564
565
# File 'ext/mmap_ruby/mmap_ruby.c', line 561

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)


2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
# File 'ext/mmap_ruby/mmap_ruby.c', line 2452

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)


2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
# File 'ext/mmap_ruby/mmap_ruby.c', line 2496

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

  GET_MMAP(self, mmap, 0);
  mmap_lock(mmap, 1);
  munmap(mmap->addr, mmap->len);
  mmap->addr = NULL;
  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)


1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
# File 'ext/mmap_ruby/mmap_ruby.c', line 1591

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, 1);
    res = rb_ensure(mmap_upcase_bang_int, (VALUE)&bang_st, mmap_vunlock, self);
  }
  else {
    res = mmap_upcase_bang_int((VALUE)&bang_st);
  }

  return res;
}