PageRenderTime 125ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/byterun/io.h

http://github.com/colinbenner/ocamlllvm
C Header | 126 lines | 78 code | 24 blank | 24 comment | 7 complexity | 092257d2e0bcb2734b785d3428da4dd8 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /***********************************************************************/
  2. /* */
  3. /* Objective Caml */
  4. /* */
  5. /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
  6. /* */
  7. /* Copyright 1996 Institut National de Recherche en Informatique et */
  8. /* en Automatique. All rights reserved. This file is distributed */
  9. /* under the terms of the GNU Library General Public License, with */
  10. /* the special exception on linking described in file ../LICENSE. */
  11. /* */
  12. /***********************************************************************/
  13. /* $Id$ */
  14. /* Buffered input/output */
  15. #ifndef CAML_IO_H
  16. #define CAML_IO_H
  17. #include "misc.h"
  18. #include "mlvalues.h"
  19. #ifndef IO_BUFFER_SIZE
  20. #define IO_BUFFER_SIZE 4096
  21. #endif
  22. #if defined(_WIN32)
  23. typedef __int64 file_offset;
  24. extern __int64 _lseeki64(int, __int64, int);
  25. #define lseek(fd,d,m) _lseeki64(fd,d,m)
  26. #elif defined(HAS_OFF_T)
  27. #include <sys/types.h>
  28. typedef off_t file_offset;
  29. #else
  30. typedef long file_offset;
  31. #endif
  32. struct channel {
  33. int fd; /* Unix file descriptor */
  34. file_offset offset; /* Absolute position of fd in the file */
  35. char * end; /* Physical end of the buffer */
  36. char * curr; /* Current position in the buffer */
  37. char * max; /* Logical end of the buffer (for input) */
  38. void * mutex; /* Placeholder for mutex (for systhreads) */
  39. struct channel * next, * prev;/* Double chaining of channels (flush_all) */
  40. int revealed; /* For Cash only */
  41. int old_revealed; /* For Cash only */
  42. int refcount; /* For flush_all and for Cash */
  43. int flags; /* Bitfield */
  44. char buff[IO_BUFFER_SIZE]; /* The buffer itself */
  45. };
  46. enum {
  47. CHANNEL_FLAG_FROM_SOCKET = 1 /* For Windows */
  48. };
  49. /* For an output channel:
  50. [offset] is the absolute position of the beginning of the buffer [buff].
  51. For an input channel:
  52. [offset] is the absolute position of the logical end of the buffer, [max].
  53. */
  54. /* Functions and macros that can be called from C. Take arguments of
  55. type struct channel *. No locking is performed. */
  56. #define putch(channel, ch) do{ \
  57. if ((channel)->curr >= (channel)->end) caml_flush_partial(channel); \
  58. *((channel)->curr)++ = (ch); \
  59. }while(0)
  60. #define getch(channel) \
  61. ((channel)->curr >= (channel)->max \
  62. ? caml_refill(channel) \
  63. : (unsigned char) *((channel)->curr)++)
  64. CAMLextern struct channel * caml_open_descriptor_in (int);
  65. CAMLextern struct channel * caml_open_descriptor_out (int);
  66. CAMLextern void caml_close_channel (struct channel *);
  67. CAMLextern int caml_channel_binary_mode (struct channel *);
  68. CAMLextern value caml_alloc_channel(struct channel *chan);
  69. CAMLextern int caml_flush_partial (struct channel *);
  70. CAMLextern void caml_flush (struct channel *);
  71. CAMLextern void caml_putword (struct channel *, uint32);
  72. CAMLextern int caml_putblock (struct channel *, char *, intnat);
  73. CAMLextern void caml_really_putblock (struct channel *, char *, intnat);
  74. CAMLextern unsigned char caml_refill (struct channel *);
  75. CAMLextern uint32 caml_getword (struct channel *);
  76. CAMLextern int caml_getblock (struct channel *, char *, intnat);
  77. CAMLextern int caml_really_getblock (struct channel *, char *, intnat);
  78. /* Extract a struct channel * from the heap object representing it */
  79. #define Channel(v) (*((struct channel **) (Data_custom_val(v))))
  80. /* The locking machinery */
  81. CAMLextern void (*caml_channel_mutex_free) (struct channel *);
  82. CAMLextern void (*caml_channel_mutex_lock) (struct channel *);
  83. CAMLextern void (*caml_channel_mutex_unlock) (struct channel *);
  84. CAMLextern void (*caml_channel_mutex_unlock_exn) (void);
  85. CAMLextern struct channel * caml_all_opened_channels;
  86. #define Lock(channel) \
  87. if (caml_channel_mutex_lock != NULL) (*caml_channel_mutex_lock)(channel)
  88. #define Unlock(channel) \
  89. if (caml_channel_mutex_unlock != NULL) (*caml_channel_mutex_unlock)(channel)
  90. #define Unlock_exn() \
  91. if (caml_channel_mutex_unlock_exn != NULL) (*caml_channel_mutex_unlock_exn)()
  92. /* Conversion between file_offset and int64 */
  93. #ifdef ARCH_INT64_TYPE
  94. #define Val_file_offset(fofs) caml_copy_int64(fofs)
  95. #define File_offset_val(v) ((file_offset) Int64_val(v))
  96. #else
  97. CAMLextern value caml_Val_file_offset(file_offset fofs);
  98. CAMLextern file_offset caml_File_offset_val(value v);
  99. #define Val_file_offset caml_Val_file_offset
  100. #define File_offset_val caml_File_offset_val
  101. #endif
  102. #endif /* CAML_IO_H */