1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 "util/u_memory.h"
41 #include "util/u_string.h"
46 #define MAP_FILE_SIZE (4*1024*1024)
51 char filename
[MAX_PATH
+ 1];
52 WCHAR wFileName
[MAX_PATH
+ 1];
63 util_stream_map(struct util_stream
*stream
)
65 ULONG BytesInUnicodeString
;
66 static char filename
[MAX_PATH
+ 1];
67 unsigned filename_len
;
70 filename_len
= util_snprintf(filename
,
76 filename_len
= util_snprintf(filename
,
81 EngMultiByteToUnicodeN(
83 sizeof(stream
->wFileName
),
84 &BytesInUnicodeString
,
88 stream
->pMap
= EngMapFile(stream
->wFileName
, stream
->map_size
, &stream
->iFile
);
92 memset(stream
->pMap
, 0, stream
->map_size
);
100 util_stream_unmap(struct util_stream
*stream
)
102 EngUnmapFile(stream
->iFile
);
103 if(stream
->written
< stream
->map_size
) {
104 /* Truncate file size */
105 stream
->pMap
= EngMapFile(stream
->wFileName
, stream
->written
, &stream
->iFile
);
107 EngUnmapFile(stream
->iFile
);
115 util_stream_create(const char *filename
, size_t max_size
)
117 struct util_stream
*stream
;
119 stream
= CALLOC_STRUCT(util_stream
);
123 strncpy(stream
->filename
, filename
, sizeof(stream
->filename
));
126 stream
->growable
= FALSE
;
127 stream
->map_size
= max_size
;
130 stream
->growable
= TRUE
;
131 stream
->map_size
= MAP_FILE_SIZE
;
134 if(!util_stream_map(stream
))
147 util_stream_copy(struct util_stream
*stream
, const char *data
, size_t size
)
149 assert(stream
->written
+ size
<= stream
->map_size
);
150 memcpy(stream
->pMap
+ stream
->written
, data
, size
);
151 stream
->written
+= size
;
156 util_stream_write(struct util_stream
*stream
, const void *data
, size_t size
)
164 while(stream
->written
+ size
> stream
->map_size
) {
165 size_t step
= stream
->map_size
- stream
->written
;
166 util_stream_copy(stream
, data
, step
);
167 data
= (const char *)data
+ step
;
170 util_stream_unmap(stream
);
171 if(!stream
->growable
|| !util_stream_map(stream
))
175 util_stream_copy(stream
, data
, size
);
182 util_stream_flush(struct util_stream
*stream
)
189 util_stream_close(struct util_stream
*stream
)
194 util_stream_unmap(stream
);