37 CloseHandle(m_overlapped.hEvent);
47 if (!HasOverlappedIoCompleted(&m_overlapped))
62 if (!GetOverlappedResult(m_fileHandle, &m_overlapped, &numBytes, TRUE))
64 setError(
"GetOverlappedResult() failed!");
67 else if ((
int)numBytes != m_expectedBytes)
69 setError(
"GetOverlappedResult() returned %d bytes, expected %d!", numBytes, m_expectedBytes);
80 File::AsyncOp::AsyncOp(HANDLE fileHandle)
93 m_fileHandle (fileHandle),
97 memset(&m_overlapped, 0,
sizeof(m_overlapped));
102 m_overlapped.hEvent = CreateEvent(
NULL, TRUE, FALSE,
NULL);
103 if (!m_overlapped.hEvent)
109 void File::AsyncOp::done(
void)
115 if (m_copyBytes && !m_failed)
116 memcpy(m_copyDst, m_copySrc, m_copyBytes);
126 m_disableCache (disableCache),
133 static bool privilegeSet =
false;
139 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
142 tp.PrivilegeCount = 1;
143 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
145 if (LookupPrivilegeValue(
NULL, SE_MANAGE_VOLUME_NAME, &tp.Privileges[0].Luid))
146 AdjustTokenPrivileges(token, FALSE, &tp, 0,
NULL,
NULL);
154 const char* modeName;
160 case Read: modeName =
"read"; access = GENERIC_READ; creation = OPEN_EXISTING;
break;
161 case Create: modeName =
"create"; access = GENERIC_READ | GENERIC_WRITE; creation = CREATE_ALWAYS;
break;
162 case Modify: modeName =
"modify"; access = GENERIC_READ | GENERIC_WRITE; creation = OPEN_ALWAYS;
break;
168 DWORD flags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED;
170 flags |= FILE_FLAG_NO_BUFFERING;
172 m_handle = CreateFile(
181 if (m_handle == INVALID_HANDLE_VALUE)
182 setError(
"Cannot open file '%s' for %s!", m_name.
getPtr(), modeName);
188 if (m_handle && !GetFileSizeEx(m_handle, &size))
190 m_size = size.QuadPart;
191 m_actualSize = size.QuadPart;
197 DWORD bytesPerSector = 1;
198 if (!GetDiskFreeSpace(NULL, NULL, &bytesPerSector, NULL, NULL))
200 m_align = bytesPerSector;
202 FW_ASSERT((m_align & (m_align - 1)) == 0);
214 CloseHandle(m_handle);
231 if (ofs >= 0 && ofs <= m_size)
246 setError(
"Tried to set negative size for '%s'!", m_name.
getPtr());
253 if (m_mode ==
Read || !m_handle || m_actualSize >= size)
257 ofs.QuadPart = (size + m_align - 1) & -m_align;
258 if (SetFilePointerEx(m_handle, ofs,
NULL, FILE_BEGIN) && SetEndOfFile(m_handle))
260 SetFileValidData(m_handle, ofs.QuadPart);
261 m_actualSize = ofs.QuadPart;
287 if (m_mode ==
Read || !m_handle)
291 if (!FlushFileBuffers(m_handle))
307 op->m_userBytes =
max((
int)
min((
S64)size, m_size - m_offset), 0);
311 int mask = m_align - 1;
312 op->m_numBytes =
min((op->m_userBytes + mask) & ~mask,
max(size, 0));
316 if (!op->m_numBytes || (((
UPTR)ptr & (
UPTR)mask) == 0 && (op->m_numBytes & mask) == 0))
318 op->m_offset = m_offset;
326 op->m_offset = m_offset & ~(
S64)mask;
327 op->m_numBytes = (((
S32)m_offset & mask) + op->m_userBytes +
mask) & ~(
S64)
mask;
328 op->m_readPtr = allocAligned(op->m_freePtr, op->m_numBytes);
329 op->m_copyBytes = op->m_userBytes;
330 op->m_copySrc = (
U8*)op->m_readPtr + ((
S32)m_offset &
mask);
336 op->m_expectedBytes = (
S32)
min((
S64)op->m_numBytes, m_actualSize - op->m_offset);
339 m_offset += op->m_userBytes;
354 int mask = m_align - 1;
356 if (m_actualSize < sizeNeeded)
360 m_actualSize =
max(m_actualSize, sizeNeeded);
366 op->m_userBytes =
size;
372 if (!op->m_userBytes || (((
UPTR)ptr & (
UPTR)mask) == 0 && (op->m_userBytes & mask) == 0))
374 op->m_offset = m_offset;
375 op->m_numBytes = op->m_userBytes;
376 op->m_writePtr =
ptr;
383 S64 start = m_offset & ~(
S64)mask;
385 op->m_offset = start;
386 op->m_numBytes = (
S32)(end - start);
387 U8*
buffer = (
U8*)allocAligned(op->m_freePtr, op->m_numBytes);
392 if (start != m_offset && start < m_size)
393 if (!readAligned(start, buffer, m_align))
398 if (end != m_offset + op->m_userBytes && end - m_align < m_size && (start == m_offset || end > start + m_align))
399 if (!readAligned(end - m_align, buffer + end - start - m_align, m_align))
404 memcpy(buffer + m_offset - start, ptr, op->m_userBytes);
407 op->m_expectedBytes = op->m_numBytes;
411 m_offset += op->m_userBytes;
412 m_size =
max(m_size, m_offset);
421 void File::fixSize(
void)
423 if (m_mode ==
Read || !m_handle || m_actualSize == m_size)
430 bool reopen = ((m_size & (m_align - 1)) != 0);
433 CloseHandle(m_handle);
434 m_handle = CreateFile(
440 FILE_ATTRIBUTE_NORMAL,
450 ofs.QuadPart = m_size;
451 if (!SetFilePointerEx(m_handle, ofs, NULL, FILE_BEGIN))
453 else if (!SetEndOfFile(m_handle))
456 m_actualSize = m_size;
462 CloseHandle(m_handle);
463 m_handle = CreateFile(
465 GENERIC_READ | GENERIC_WRITE,
469 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING,
481 void File::startOp(AsyncOp* op)
487 S32 numBytes = op->m_numBytes;
488 S32 expectedBytes = op->m_expectedBytes;
489 U8* readPtr = (
U8*)op->m_readPtr;
490 U8* writePtr = (
U8*)op->m_writePtr;
508 AsyncOp* blockOp = op;
510 blockOp =
new AsyncOp(m_handle);
512 blockOp->m_offset = offset + pos;
514 blockOp->m_expectedBytes =
min(expectedBytes - pos, blockOp->m_numBytes);
515 blockOp->m_readPtr = (readPtr) ? readPtr + pos : NULL;
516 blockOp->m_writePtr = (writePtr) ? writePtr + pos : NULL;
521 const char* funcName;
522 blockOp->m_overlapped.Offset = (DWORD)blockOp->m_offset;
523 blockOp->m_overlapped.OffsetHigh = (DWORD)(blockOp->m_offset >> 32);
527 ok = ReadFile(m_handle, blockOp->m_readPtr, blockOp->m_numBytes, NULL, &blockOp->m_overlapped);
528 funcName =
"ReadFile";
532 ok = WriteFile(m_handle, blockOp->m_writePtr, blockOp->m_numBytes, NULL, &blockOp->m_overlapped);
533 funcName =
"WriteFile";
540 else if (GetLastError() != ERROR_IO_PENDING)
553 pos += blockOp->m_numBytes;
555 bool failed = blockOp->hasFailed();
568 void* File::allocAligned(
void*& base,
int size)
570 base = (
U8*)
malloc(size + m_align - 1);
571 U8*
ptr = (
U8*)base + m_align - 1;
572 ptr -= (
UPTR)ptr & (
UPTR)(m_align - 1);
578 bool File::readAligned(
S64 ofs,
void* ptr,
int size)
580 AsyncOp* op =
new AsyncOp(m_handle);
582 op->m_numBytes =
size;
585 op->m_expectedBytes = (
S32)
min((
S64)
size, m_actualSize - ofs);
588 bool ok = (!op->hasFailed());
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 const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat GLfloat GLfloat z GLuint GLint GLenum GLboolean GLsizei const GLvoid pointer GLuint GLuint const GLchar name GLenum GLsizei GLenum GLsizei GLsizei height GLenum GLuint renderbuffer GLenum GLenum GLint params GLuint GLsizei range GLuint GLsizei const GLubyte GLsizei GLenum const GLvoid coords GLuint GLsizei GLsizei GLsizei const GLubyte GLsizei GLenum const GLvoid coords GLuint GLenum GLsizei const GLvoid pathString GLuint GLenum const GLvoid GLbitfield GLuint GLsizei GLenum GLuint GLfloat emScale GLuint GLuint srcPath GLuint GLuint GLenum const GLfloat transformValues GLuint GLenum GLint value GLuint GLenum GLfloat value GLenum GLint GLuint mask
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 offset
virtual int read(void *ptr, int size)
const char * getPtr(void) const
void * malloc(size_t size)
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
int getNumBytes(void) const
void setError(const char *fmt,...)
bool hasFailed(void) const
void failWin32Error(const char *funcName)
AsyncOp * writeAsync(const void *ptr, int size)
File(const String &name, Mode mode, bool disableCache=false)
AsyncOp * readAsync(void *ptr, int size)
bool checkWritable(void) const
FW_CUDA_FUNC T min(const VectorBase< T, L, S > &v)
FW_CUDA_FUNC T max(const VectorBase< T, L, S > &v)
void allocateSpace(S64 size)
virtual void write(const void *ptr, int size)
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
void profilePush(const char *id)