home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / MachOViewer / Source / RCS / MappedFile.m,v < prev    next >
Encoding:
Text File  |  1994-02-10  |  3.4 KB  |  235 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ediger:1.2;
  6. comment  @@;
  7.  
  8.  
  9. 1.2
  10. date     94.02.10.22.20.08;  author ediger;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     94.02.07.21.38.37;  author ediger;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @Implementation of a simple object that represents a memory-mapped file.
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @added - (char *)errorString method to assist in error handling
  28. @
  29. text
  30. @//H+
  31. //    Implementation of a simple object representing a memory-mapped file.
  32. //H-
  33.  
  34. /* $Log:    MappedFile.m,v $
  35. Revision 1.1  94/02/07  21:38:37  ediger
  36. Initial revision
  37.  */
  38.  
  39. #import <MappedFile.h>
  40.  
  41. @@implementation MappedFile
  42.  
  43. static char MappedFileRcsIdent[] = "$Id: MappedFile.m,v 1.1 94/02/07 21:38:37 ediger Exp Locker: ediger $";
  44.  
  45. //F+  -init
  46. //F-
  47. - init
  48. {
  49.     [super init];
  50.  
  51.     mappedAddress = NULL;
  52.     lastErrno = 0;
  53.     lastKernReturn = KERN_SUCCESS;
  54.     FileName = NULL;
  55.     fd = -1;  // -1 signifies that file descriptor is not used.
  56.  
  57.     return self;
  58. }
  59.  
  60. //F+  -free
  61. //F-
  62. - free
  63. {
  64.     if (fd > -1 || mappedAddress != NULL)
  65.         [self unmap];
  66.  
  67.     if (FileName != NULL)
  68.         free(FileName);
  69.  
  70.     [super free];
  71.  
  72.     return self;
  73. }
  74.  
  75. //F+  -(BOOL)map
  76. //
  77. //    Returns TRUE if memory-mapping of file worked, FALSE if it failed.
  78. //
  79. //    Must set name of file to map first.
  80. //F-
  81. - (BOOL)map
  82. {
  83.     assert(FileName != NULL);
  84.  
  85.     if (fd > -1 || mappedAddress != NULL)
  86.         [self unmap];
  87.  
  88.     fd = open(FileName, O_RDONLY);
  89.     if (fd < 0)
  90.     {
  91.         lastErrno = errno;
  92.         return FALSE;
  93.     }
  94.  
  95.     if (fstat(fd, &statBuf) < 0)
  96.     {
  97.         lastErrno = errno;
  98.         close(fd);
  99.         return FALSE;
  100.     }
  101.  
  102.     lastKernReturn = map_fd(fd, (vm_offset_t)NULL,
  103.         (vm_offset_t *)&mappedAddress, TRUE, statBuf.st_size);
  104.  
  105.     if (lastKernReturn != KERN_SUCCESS)
  106.     {
  107.         close(fd);
  108.         return FALSE;
  109.     }
  110.  
  111.     return TRUE;
  112. }
  113.  
  114. //F+  -(BOOL)unmap
  115. //
  116. //    Returns TRUE if unmapping of file worked, FALSE if it failed.
  117. //
  118. //F-
  119. - (BOOL)unmap
  120. {
  121.     BOOL fSuccess;
  122.  
  123.     fSuccess = FALSE;
  124.  
  125.     if (mappedAddress == NULL || fd == -1)
  126.         return TRUE;  // allow repeated use of this message
  127.  
  128.     lastKernReturn = vm_deallocate(task_self(), (vm_address_t)mappedAddress,
  129.         statBuf.st_size);
  130.     if (lastKernReturn == KERN_SUCCESS)
  131.         fSuccess = TRUE;
  132.     else
  133.         mappedAddress = NULL;
  134.  
  135.     if (close(fd) < 0)
  136.     {
  137.         lastErrno = errno;
  138.         fSuccess = FALSE;
  139.     } else
  140.         fd = -1;
  141.  
  142.     return fSuccess;
  143. }
  144.  
  145. //F+    -(char *)address
  146. //F-
  147. - (char *)address
  148. {
  149.     return mappedAddress;
  150. }
  151.  
  152. //F+    -(char *)filename
  153. //F-
  154. - (char *)filename
  155. {
  156.     return FileName;
  157. }
  158.  
  159. //F+    -(int)size
  160. //F-
  161. - (int)size
  162. {
  163.     return statBuf.st_size;
  164. }
  165.  
  166. //F+    -(int)lastUnixError
  167. //F-
  168. - (int)lastUnixError
  169. {
  170.     return lastErrno;
  171. }
  172.  
  173. //F+    -(kern_return_t)lastMachError
  174. //F-
  175. - (kern_return_t)lastMachError
  176. {
  177.     return lastKernReturn;
  178. }
  179.  
  180. //F+    -filename:(char *)fileName
  181. //
  182. //    Set filename of file to be memory mapped.
  183. //    Possibly an unwise pseudo-method-name overloading.
  184. //
  185. //F-
  186. - filename:(char *)fileName
  187. {
  188.     if (fileName != NULL && strlen(fileName) > 0)
  189.     {
  190.         if (FileName != NULL)
  191.             free(FileName);
  192.  
  193.         FileName = malloc(strlen(fileName) + 1);
  194.  
  195.         if (FileName != NULL)
  196.             strcpy(FileName, fileName);
  197.     }
  198.  
  199.     return self;
  200. }
  201.  
  202. //F+
  203. //F-
  204. - (char *)errorString
  205. {
  206.     char *bprErrorString = NULL;
  207.  
  208.     if (lastErrno != 0)
  209.         bprErrorString = sys_errlist[lastErrno];
  210.     else if (lastKernReturn != KERN_SUCCESS)
  211.         bprErrorString = mach_error_string(lastKernReturn);
  212.     else
  213.         bprErrorString = "No error";
  214.  
  215.     return bprErrorString;
  216. }
  217.  
  218. @@end
  219. @
  220.  
  221.  
  222. 1.1
  223. log
  224. @Initial revision
  225. @
  226. text
  227. @d5 4
  228. a8 1
  229. /* $Log$ */
  230. d14 1
  231. a14 1
  232. static char MappedFileRcsIdent[] = "$Id$";
  233. d171 16
  234. @
  235.