Project

General

Profile

Feature #4015 ยป io-advise.patch

runpaint (Run Paint Run Run), 11/08/2010 09:48 PM

View differences:

configure.in
1266 1266
fi
1267 1267
AC_CHECK_FUNCS(fmod killpg wait4 waitpid fork spawnv syscall chroot getcwd eaccess\
1268 1268
	      truncate ftruncate chsize times utimes utimensat fcntl lockf lstat\
1269
	      link symlink readlink readdir_r fsync fdatasync fchown\
1269
	      link symlink readlink readdir_r fsync fdatasync fchown posix_fadvise\
1270 1270
	      setitimer setruid seteuid setreuid setresuid setproctitle socketpair\
1271 1271
	      setrgid setegid setregid setresgid issetugid pause lchown lchmod\
1272 1272
	      getpgrp setpgrp getpgid setpgid initgroups getgroups setgroups\
io.c
1

  
1 2
/**********************************************************************
2 3

  
3 4
  io.c -
......
1413 1414
#define rb_io_fdatasync rb_f_notimplement
1414 1415
#endif
1415 1416

  
1417
static VALUE sym_normal,   sym_sequential, sym_random,
1418
             sym_willneed, sym_dontneed, sym_noreuse;
1419
/*
1420
 *  call-seq:
1421
 *     ios.advise(advice, offset=0, len=0) -> nil
1422
 *
1423
 *  Announce an intention to access data from the current file in a
1424
 *  specific pattern. On platforms that do not support the
1425
 *  <em>posix_fadvise(2)</em> system call, this method is a no-op.
1426
 *
1427
 * _advice_ is one of the following symbols:
1428
 *
1429
 *  * :normal - No advice to give; the default assumption for an open file.
1430
 *  * :sequential - The data will be accessed sequentially:
1431
 *     with lower offsets read before higher ones.
1432
 *  * :random - The data will be accessed in random order.
1433
 *  * :willneed - The data will be accessed in the near future.
1434
 *  * :dontneed - The data will not be accessed in the near future.
1435
 *  * :noreuse - The data will only be accessed once.
1436
 *
1437
 *  "data" means the region of the current file that begins
1438
 *  at _offset_ and extends for _len_ bytes. By default, both _offset_
1439
 *  and _len_ are 0, meaning that the advice applies to the entire
1440
 *  file.
1441
 *
1442
 *  If an error occurs, one of the following exceptions will be raised:
1443
 *
1444
 *  * <code>IOError</code> - The <code>IO</code> stream is closed.
1445
 *  * <code>Errno::EBADF</code> - The file descriptor of the current file is
1446
      invalid.
1447
 *  * <code>Errno::EINVAL</code> - An invalid value for _advice_ was given.
1448
 *  * <code>Errno::ESPIPE</code> - The file descriptor of the current
1449
 *  * file refers to a FIFO or pipe. (Linux raises <code>Errno::EINVAL</code>
1450
 *  * in this case).
1451
 *  * <code>TypeError</code> - Either _advice_ was not a Symbol, or one of the
1452
      other arguments was not an <code>Integer</code>.
1453
 *  * <code>RangeError</code> - One of the arguments given was too big/small.
1454
 */
1455

  
1456
static VALUE
1457
rb_io_advise(int argc, VALUE *argv, VALUE io)
1458
{
1459
  off_t offset, len;
1460
  int advice, rv;
1461
  VALUE initadvice, initoffset, initlen;
1462
  rb_io_t *fptr;
1463

  
1464
  rb_scan_args(argc, argv, "12", &initadvice, &initoffset, &initlen);
1465
  if (TYPE(initadvice) != T_SYMBOL)
1466
      rb_raise(rb_eTypeError, "advice must be a Symbol");
1467

  
1468
  offset = NIL_P(initoffset) ? 0 : NUM2OFFT(initoffset);
1469
  len    = NIL_P(initlen)    ? 0 : NUM2OFFT(initlen);
1470
  advice = 0;
1471

  
1472
  if      (initadvice == sym_normal)     {
1473
#ifdef POSIX_FADV_NORMAL
1474
      advice = POSIX_FADV_NORMAL;
1475
#endif
1476
  }
1477
  else if (initadvice == sym_random)     {
1478
#ifdef POSIX_FADV_RANDOM
1479
      advice = POSIX_FADV_RANDOM;
1480
#endif
1481
  }
1482
  else if (initadvice == sym_sequential) {
1483
#ifdef POSIX_FADV_SEQUENTIAL
1484
      advice = POSIX_FADV_SEQUENTIAL;
1485
#endif
1486
  }
1487
  else if (initadvice == sym_willneed)   {
1488
#ifdef POSIX_FADV_WILLNEED
1489
      advice = POSIX_FADV_WILLNEED;
1490
#endif
1491
  }
1492
  else if (initadvice == sym_dontneed)   {
1493
#ifdef POSIX_FADV_DONTNEED
1494
      advice = POSIX_FADV_DONTNEED;
1495
#endif
1496
  }
1497
  else if (initadvice == sym_noreuse)    {
1498
#ifdef POSIX_FADV_NOREUSE
1499
      advice = POSIX_FADV_NOREUSE;
1500
#endif
1501
  }
1502
  else
1503
      rb_raise(rb_eArgError, "Invalid advice: :%s",
1504
	       RSTRING_PTR(rb_id2str(SYM2ID(initadvice))));
1505

  
1506
#ifdef HAVE_POSIX_FADVISE
1507
  io = GetWriteIO(io);
1508
  GetOpenFile(io, fptr);
1509

  
1510
  if (rv = posix_fadvise(fptr->fd, offset, len, advice)) {
1511
    /* posix_fadvise(2) doesn't set errno. On success it returns 0; otherwise
1512
       it returns the error code. */
1513
    errno = rv;
1514
    rb_sys_fail_path(fptr->pathv);
1515
  }
1516
#endif
1517
  return Qnil;
1518
}
1519

  
1416 1520
/*
1417 1521
 *  call-seq:
1418 1522
 *     ios.fileno    -> fixnum
......
10052 10156
    rb_define_method(rb_cIO, "binmode",  rb_io_binmode_m, 0);
10053 10157
    rb_define_method(rb_cIO, "binmode?", rb_io_binmode_p, 0);
10054 10158
    rb_define_method(rb_cIO, "sysseek", rb_io_sysseek, -1);
10159
    rb_define_method(rb_cIO, "advise", rb_io_advise, -1);
10055 10160

  
10056 10161
    rb_define_method(rb_cIO, "ioctl", rb_io_ioctl, -1);
10057 10162
    rb_define_method(rb_cIO, "fcntl", rb_io_fcntl, -1);
......
10220 10325
    sym_textmode = ID2SYM(rb_intern("textmode"));
10221 10326
    sym_binmode = ID2SYM(rb_intern("binmode"));
10222 10327
    sym_autoclose = ID2SYM(rb_intern("autoclose"));
10328
    sym_normal = ID2SYM(rb_intern("normal"));
10329
    sym_sequential = ID2SYM(rb_intern("sequential"));
10330
    sym_random = ID2SYM(rb_intern("random"));
10331
    sym_willneed = ID2SYM(rb_intern("willneed"));
10332
    sym_dontneed = ID2SYM(rb_intern("dontneed"));
10333
    sym_noreuse = ID2SYM(rb_intern("noreuse"));
10223 10334
}