NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
File.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 #include "io/Stream.hpp"
30 #include "base/DLLImports.hpp"
31 
32 namespace FW
33 {
34 
35 //------------------------------------------------------------------------
36 
37 class File : public InputStream, public OutputStream
38 {
39 public:
40  enum
41  {
42  MaxBytesPerSysCall = 16 << 20,
45  };
46 
47  enum Mode
48  {
49  Read, // must exist - cannot be written
50  Create, // created or truncated - can be read or written
51  Modify // opened or created - can be read or written
52  };
53 
54  class AsyncOp
55  {
56  friend class File;
57 
58  public:
59  ~AsyncOp (void);
60 
61  bool isDone (void);
62  bool hasFailed (void) const { return m_failed; }
63  void wait (void);
64  int getNumBytes (void) const { FW_ASSERT(m_done); return (m_failed) ? 0 : m_userBytes; }
65 
66  private:
67  AsyncOp (HANDLE fileHandle);
68 
69  void done (void);
70  void failed (void) { m_failed = true; done(); }
71 
72  private:
73  AsyncOp (const AsyncOp&); // forbidden
74  AsyncOp& operator= (const AsyncOp&); // forbidden
75 
76  private:
77  S64 m_offset;
78  S32 m_numBytes; // Number of bytes to read or write.
79  S32 m_expectedBytes; // Number of bytes to expect.
80  S32 m_userBytes; // Number of bytes to return to the user.
81  void* m_readPtr; // Buffer to read to.
82  const void* m_writePtr; // Buffer to write from.
83 
84  S32 m_copyBytes; // Number of bytes to copy afterwards.
85  const void* m_copySrc; // Pointer to copy from.
86  void* m_copyDst; // Pointer to copy to.
87  void* m_freePtr; // Pointer to free afterwards.
88 
89  HANDLE m_fileHandle;
90  OVERLAPPED m_overlapped;
91  bool m_done;
92  bool m_failed;
93  };
94 
95 public:
96  File (const String& name, Mode mode, bool disableCache = false);
97  virtual ~File (void);
98 
99  const String& getName (void) const { return m_name; }
100  Mode getMode (void) const { return m_mode; }
101  int getAlign (void) const { return m_align; } // 1 unless disableCache = true.
102  bool checkWritable (void) const;
103 
104  S64 getSize (void) const { return m_size; }
105  S64 getOffset (void) const { return m_offset; }
106  void seek (S64 ofs);
107  void setSize (S64 size);
108  void allocateSpace (S64 size);
109 
110  virtual int read (void* ptr, int size);
111  virtual void write (const void* ptr, int size);
112  virtual void flush (void);
113 
114  AsyncOp* readAsync (void* ptr, int size);
115  AsyncOp* writeAsync (const void* ptr, int size);
116 
117 private:
118  File (const File&); // forbidden
119  File& operator= (const File&); // forbidden
120 
121  void fixSize (void);
122  void startOp (AsyncOp* op);
123 
124  void* allocAligned (void*& base, int size);
125  bool readAligned (S64 ofs, void* ptr, int size);
126 
127 private:
128  String m_name;
129  Mode m_mode;
130  bool m_disableCache;
131  HANDLE m_handle;
132  S32 m_align;
133 
134  S64 m_size;
135  S64 m_actualSize;
136  S64 m_offset;
137 };
138 
139 //------------------------------------------------------------------------
140 }
void seek(S64 ofs)
Definition: File.cpp:229
virtual int read(void *ptr, int size)
Definition: File.cpp:267
int getNumBytes(void) const
Definition: File.hpp:64
const char * name
Definition: DLLImports.cpp:42
void ** ptr
Definition: DLLImports.cpp:74
bool hasFailed(void) const
Definition: File.hpp:62
int getAlign(void) const
Definition: File.hpp:101
const String & getName(void) const
Definition: File.hpp:99
AsyncOp * writeAsync(const void *ptr, int size)
Definition: File.cpp:347
virtual void flush(void)
Definition: File.cpp:285
File(const String &name, Mode mode, bool disableCache=false)
Definition: File.cpp:123
S64 getOffset(void) const
Definition: File.hpp:105
AsyncOp * readAsync(void *ptr, int size)
Definition: File.cpp:299
bool isDone(void)
Definition: File.cpp:42
S64 getSize(void) const
Definition: File.hpp:104
bool checkWritable(void) const
Definition: File.cpp:219
void wait(void)
Definition: File.cpp:56
void allocateSpace(S64 size)
Definition: File.cpp:251
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
~AsyncOp(void)
Definition: File.cpp:34
signed __int64 S64
Definition: Defs.hpp:98
void setSize(S64 size)
Definition: File.cpp:239
virtual void write(const void *ptr, int size)
Definition: File.cpp:278
Mode getMode(void) const
Definition: File.hpp:100
virtual ~File(void)
Definition: File.cpp:207
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
Definition: DLLImports.inl:319