~ubuntu-branches/debian/sid/ocaml/sid

« back to all changes in this revision

Viewing changes to otherlibs/bigarray/mmap_unix.c

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu
  • Date: 2011-04-21 21:35:08 UTC
  • mfrom: (1.1.11 upstream) (12.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110421213508-kg34453aqmb0moha
* Fixes related to -output-obj with g++ (in debian/patches):
  - add Declare-primitive-name-table-as-const-char
  - add Avoid-multiple-declarations-in-generated-.c-files-in
  - fix Embed-bytecode-in-C-object-when-using-custom: the closing
    brace for extern "C" { ... } was missing in some cases

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
/*                                                                     */
12
12
/***********************************************************************/
13
13
 
14
 
/* $Id: mmap_unix.c 8754 2008-01-04 15:01:48Z xleroy $ */
 
14
/* $Id: mmap_unix.c 10223 2010-04-01 07:36:49Z shinwell $ */
15
15
 
16
16
#include <stddef.h>
17
17
#include <string.h>
65
65
      caml_invalid_argument("Bigarray.create: negative dimension");
66
66
  }
67
67
  /* Determine file size */
 
68
  caml_enter_blocking_section();
68
69
  currpos = lseek(fd, 0, SEEK_CUR);
69
 
  if (currpos == -1) caml_sys_error(NO_ARG);
 
70
  if (currpos == -1) {
 
71
    caml_leave_blocking_section();
 
72
    caml_sys_error(NO_ARG);
 
73
  }
70
74
  file_size = lseek(fd, 0, SEEK_END);
71
 
  if (file_size == -1) caml_sys_error(NO_ARG);
 
75
  if (file_size == -1) {
 
76
    caml_leave_blocking_section();
 
77
    caml_sys_error(NO_ARG);
 
78
  }
72
79
  /* Determine array size in bytes (or size of array without the major
73
80
     dimension if that dimension wasn't specified) */
74
81
  array_size = caml_ba_element_size[flags & CAML_BA_KIND_MASK];
77
84
  /* Check if the major dimension is unknown */
78
85
  if (dim[major_dim] == -1) {
79
86
    /* Determine major dimension from file size */
80
 
    if (file_size < startpos)
 
87
    if (file_size < startpos) {
 
88
      caml_leave_blocking_section();
81
89
      caml_failwith("Bigarray.mmap: file position exceeds file size");
 
90
    }
82
91
    data_size = file_size - startpos;
83
92
    dim[major_dim] = (uintnat) (data_size / array_size);
84
93
    array_size = dim[major_dim] * array_size;
85
 
    if (array_size != data_size)
 
94
    if (array_size != data_size) {
 
95
      caml_leave_blocking_section();
86
96
      caml_failwith("Bigarray.mmap: file size doesn't match array dimensions");
 
97
    }
87
98
  } else {
88
99
    /* Check that file is large enough, and grow it otherwise */
89
100
    if (file_size < startpos + array_size) {
90
 
      if (lseek(fd, startpos + array_size - 1, SEEK_SET) == -1)
 
101
      if (lseek(fd, startpos + array_size - 1, SEEK_SET) == -1) {
 
102
        caml_leave_blocking_section();
91
103
        caml_sys_error(NO_ARG);
 
104
      }
92
105
      c = 0;
93
 
      if (write(fd, &c, 1) != 1) caml_sys_error(NO_ARG);
 
106
      if (write(fd, &c, 1) != 1) {
 
107
        caml_leave_blocking_section();
 
108
        caml_sys_error(NO_ARG);
 
109
      }
94
110
    }
95
111
  }
96
112
  /* Restore original file position */
102
118
  shared = Bool_val(vshared) ? MAP_SHARED : MAP_PRIVATE;
103
119
  addr = mmap(NULL, array_size + delta, PROT_READ | PROT_WRITE,
104
120
              shared, fd, startpos - delta);
 
121
  caml_leave_blocking_section();
105
122
  if (addr == (void *) MAP_FAILED) caml_sys_error(NO_ARG);
106
123
  addr = (void *) ((uintnat) addr + delta);
107
124
  /* Build and return the Caml bigarray */