1 /**************************************************************************
3 * Copyright 2008-2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Stream implementation for the Windows Display driver.
33 #include "pipe/p_config.h"
35 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
40 #include "os_memory.h"
41 #include "os_stream.h"
44 #define MAP_FILE_SIZE (4*1024*1024)
49 char filename
[MAX_PATH
+ 1];
50 WCHAR wFileName
[MAX_PATH
+ 1];
61 os_stream_map(struct os_stream
*stream
)
63 ULONG BytesInUnicodeString
;
64 static char filename
[MAX_PATH
+ 1];
65 unsigned filename_len
;
68 filename_len
= snprintf(filename
,
74 filename_len
= snprintf(filename
,
79 EngMultiByteToUnicodeN(
81 sizeof(stream
->wFileName
),
82 &BytesInUnicodeString
,
86 stream
->pMap
= EngMapFile(stream
->wFileName
, stream
->map_size
, &stream
->iFile
);
90 memset(stream
->pMap
, 0, stream
->map_size
);
98 os_stream_unmap(struct os_stream
*stream
)
100 EngUnmapFile(stream
->iFile
);
101 if(stream
->written
< stream
->map_size
) {
102 /* Truncate file size */
103 stream
->pMap
= EngMapFile(stream
->wFileName
, stream
->written
, &stream
->iFile
);
105 EngUnmapFile(stream
->iFile
);
113 os_stream_full_qualified_filename(char *dst
, size_t size
, const char *src
)
115 boolean need_drive
, need_root
;
117 if((('A' <= src
[0] && src
[0] <= 'Z') || ('a' <= src
[0] && src
[0] <= 'z')) && src
[1] == ':') {
119 need_root
= src
[2] == '\\' ? FALSE
: TRUE
;
123 need_root
= src
[0] == '\\' ? FALSE
: TRUE
;
128 need_drive
? "C:" : "",
129 need_root
? "\\" : "",
135 os_stream_create(const char *filename
, size_t max_size
)
137 struct os_stream
*stream
;
139 stream
= CALLOC_STRUCT(os_stream
);
143 os_stream_full_qualified_filename(stream
->filename
,
144 sizeof(stream
->filename
),
148 stream
->growable
= FALSE
;
149 stream
->map_size
= max_size
;
152 stream
->growable
= TRUE
;
153 stream
->map_size
= MAP_FILE_SIZE
;
156 if(!os_stream_map(stream
))
169 os_stream_copy(struct os_stream
*stream
, const char *data
, size_t size
)
171 assert(stream
->written
+ size
<= stream
->map_size
);
172 memcpy(stream
->pMap
+ stream
->written
, data
, size
);
173 stream
->written
+= size
;
178 os_stream_write(struct os_stream
*stream
, const void *data
, size_t size
)
186 while(stream
->written
+ size
> stream
->map_size
) {
187 size_t step
= stream
->map_size
- stream
->written
;
188 os_stream_copy(stream
, data
, step
);
189 data
= (const char *)data
+ step
;
192 os_stream_unmap(stream
);
193 if(!stream
->growable
|| !os_stream_map(stream
))
197 os_stream_copy(stream
, data
, size
);
204 os_stream_flush(struct os_stream
*stream
)
211 os_stream_close(struct os_stream
*stream
)
216 os_stream_unmap(stream
);