4 file-storage.cc -- implement Mapped_file_storage
6 source file of the GNU LilyPond music typesetter
8 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 Jan Nieuwenhuizen <janneke@gnu.org>.
11 Nextstep fixes by tiggr@ics.ele.tue.nl
14 #include <sys/types.h> // open, mmap
15 #include <sys/stat.h> // open
16 #include <sys/mman.h> // mmap
17 #include <limits.h> // INT_MAX
18 #include <fcntl.h> // open
19 #include <unistd.h> // close, stat
20 #include <stdio.h> // fdopen
21 #include <string.h> // strerror
22 #include <errno.h> // errno
27 #include <mach/mach.h>
28 #include <mach/mach_traps.h>
29 #include <mach/mach_error.h>
35 #include "file-storage.hh"
37 Mapped_file_storage::Mapped_file_storage (String s
)
46 Mapped_file_storage::ch_C () const
48 return (char const*)data_caddr_
;
52 Mapped_file_storage::map ()
58 /* Should be #if !HAVE_MMAP && HAVE_MAP_FD... */
63 r
= map_fd (fildes_i_
, (vm_offset_t
) 0, &address
, TRUE
, size_off_
);
64 if (r
!= KERN_SUCCESS
)
65 warning (String ("map_fd: ") + mach_error_string (r
));
67 data_caddr_
= (char *) address
;
71 data_caddr_
= (caddr_t
)mmap ((void*)0, size_off_
, PROT_READ
, MAP_SHARED
, fildes_i_
, 0);
73 if ((int)data_caddr_
== -1)
74 warning (_ ("Can't map file") + ": " + strerror (errno
));
81 Mapped_file_storage::open (String name_str
)
83 fildes_i_
= ::open (name_str
.ch_C (), O_RDONLY
);
87 warning (_f ("Can't open file: `%s'", name_str
)
88 + ": " + strerror (errno
));
92 struct stat file_stat
;
93 fstat (fildes_i_
, &file_stat
);
94 size_off_
= file_stat
.st_size
;
99 Mapped_file_storage::unmap ()
106 r
= vm_deallocate (task_self (), (vm_address_t
) data_caddr_
,
108 if (r
!= KERN_SUCCESS
)
109 warning (String ("vm_deallocate: ") + mach_error_string (r
));
111 munmap (data_caddr_
, size_off_
);
120 Mapped_file_storage::close ()
131 Mapped_file_storage::length_i () const
136 Mapped_file_storage::~Mapped_file_storage ()