]> git.donarmstrong.com Git - lilypond.git/blob - lib/mapped-file-storage.cc
release: 0.1.43
[lilypond.git] / lib / mapped-file-storage.cc
1 #ifdef HAIRY_STUFF
2
3 /*
4   file-storage.cc -- implement Mapped_file_storage
5
6   source file of the GNU LilyPond music typesetter
7
8   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
9   Jan Nieuwenhuizen <jan@digicash.com>.
10
11   Nextstep fixes by tiggr@ics.ele.tue.nl
12 */
13
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
23
24
25
26 #ifdef __NeXT__
27 #include <mach/mach.h>
28 #include <mach/mach_traps.h>
29 #include <mach/mach_error.h>
30 #endif
31
32 #include "string.hh"
33 #include "proto.hh"
34 #include "warn.hh"
35 #include "file-storage.hh"
36
37 Mapped_file_storage::Mapped_file_storage (String s)
38 {
39   data_caddr_ = 0;
40   fildes_i_ = 0;
41   size_off_ = 0;
42   open (s);
43 }
44
45 char const*
46 Mapped_file_storage::ch_C () const
47 {
48   return (char const*)data_caddr_;
49 }
50
51 void
52 Mapped_file_storage::map ()
53 {
54   if (fildes_i_ == -1)
55     return;
56   
57 #ifdef __NeXT__
58    /* Should be #if !HAVE_MMAP && HAVE_MAP_FD...  */
59    {
60      vm_offset_t address;
61      kern_return_t r;
62  
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));
66      else
67        data_caddr_ = (char *) address;
68    }
69 #else
70
71   data_caddr_ = (caddr_t)mmap ((void*)0, size_off_, PROT_READ, MAP_SHARED, fildes_i_, 0);
72
73   if ((int)data_caddr_ == -1)
74     warning (String (_ ("can't map: error no: ")) + strerror (errno));
75
76 #endif
77 }
78
79
80 void
81 Mapped_file_storage::open (String name_str)
82 {
83   fildes_i_ = ::open (name_str.ch_C (), O_RDONLY);
84
85   if (fildes_i_ == -1)
86     {
87       warning (String (_ ("can't open: ")) + name_str + String (": ") + strerror (errno));
88       return;
89     }
90
91   struct stat file_stat;
92   fstat (fildes_i_, &file_stat);
93   size_off_ = file_stat.st_size;
94   map ();
95 }
96
97 void
98 Mapped_file_storage::unmap ()
99 {
100   if (data_caddr_)
101     {
102 #ifdef __NeXT__
103        kern_return_t r;
104  
105        r = vm_deallocate (task_self (), (vm_address_t) data_caddr_, 
106 size_off_);
107        if (r != KERN_SUCCESS)
108        warning (String (_ ("vm_deallocate: ")) + mach_error_string (r));
109 #else
110        munmap (data_caddr_, size_off_);
111 #endif
112        
113       data_caddr_ = 0;
114       size_off_ = 0;
115     }
116 }
117
118 void
119 Mapped_file_storage::close ()
120 {
121   unmap ();
122   if (fildes_i_)
123     {
124       ::close (fildes_i_);
125       fildes_i_ = 0;
126     }
127 }
128
129 int
130 Mapped_file_storage::length_i () const
131 {
132   return size_off_;
133 }
134
135 Mapped_file_storage::~Mapped_file_storage ()
136 {
137   close ();
138 }
139 #endif