4 Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
7 Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>
9 This file is part of libFoundation.
11 Permission to use, copy, modify, and distribute this software and its
12 documentation for any purpose and without fee is hereby granted, provided
13 that the above copyright notice appear in all copies and that both that
14 copyright notice and this permission notice appear in supporting
17 We disclaim all warranties with regard to this software, including all
18 implied warranties of merchantability and fitness, in no event shall
19 we be liable for any special, indirect or consequential damages or any
20 damages whatsoever resulting from loss of use, data or profits, whether in
21 an action of contract, negligence or other tortious action, arising out of
22 or in connection with the use or performance of this software.
25 #include <Foundation/common.h>
26 #include <Foundation/NSPosixFileDescriptor.h>
27 #include <Foundation/NSException.h>
28 #include <Foundation/exceptions/GeneralExceptions.h>
30 #ifdef NeXT /* NeXT Mach map_fd() */
31 # include <mach/mach.h>
33 #elif defined(HAVE_MMAP) /* Posix mmap() */
34 # include <sys/types.h>
35 # include <sys/mman.h>
37 #else /* No file mapping available */
40 #include "NSMappedData.h"
43 * Read-only mapped data
46 @implementation NSMappedData
48 - (id)initWithPosixFileDescriptor:(NSPosixFileDescriptor*)descriptor
51 file = RETAIN(descriptor);
53 if (range.location + range.length > [file fileLength])
54 [[[RangeException alloc]
55 initWithReason:@"invalid range to be mapped"
56 size:[file fileLength]
57 index:range.location+range.length] raise];
59 length = capacity = range.length;
60 [file seekToPosition:range.location];
66 /* NeXt Mach map_fd() */
70 r = map_fd([file fileDescriptor], (vm_offset_t)0,
71 (vm_offset_t*)(&bytes), TRUE, (vm_size_t)length);
72 if (r != KERN_SUCCESS) {
79 #elif defined(HAVE_MMAP)
82 bytes = mmap(0, length, PROT_READ, MAP_SHARED,
83 [file fileDescriptor], 0);
84 if ((long)bytes == -1L) {
86 (void)AUTORELEASE(self);
92 /* No file mapping available */
94 bytes = MallocAtomic (length);
95 [file readBytes:bytes range:range];
104 /* NeXt Mach map_fd() */
106 vm_deallocate(task_self(), (vm_address_t)bytes, length);
107 #elif defined(HAVE_MMAP)
110 munmap(bytes, length);
112 /* No file mapping available */
119 - (id)copyWithZone:(NSZone*)zone
129 - (unsigned int)length
134 @end /* NSMappedData */