Browse Subversion Repository
Contents of /branches/mty-makai/scoreboard.c
Parent Directory
| Revision Log
Revision 253 -
( show annotations)
( download)
( as text)
Mon Dec 27 09:22:27 2010 UTC
(13 years, 3 months ago)
by notanpe
File MIME type: text/x-csrc
File size: 2519 byte(s)
とりあえず今の開発環境で動くように変更。
| 1 |
/*********************************************************************** |
| 2 |
* |
| 3 |
* file: scoreboard.c |
| 4 |
* |
| 5 |
* 作業ファイルを扱う |
| 6 |
* |
| 7 |
* $Id$ |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <assert.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <sys/types.h> |
| 15 |
|
| 16 |
#if defined(WIN32) |
| 17 |
|
| 18 |
#include <windows.h> |
| 19 |
#include <fcntl.h> |
| 20 |
#include <io.h> |
| 21 |
|
| 22 |
#elif defined(__GNUC__) |
| 23 |
|
| 24 |
#include <sys/mman.h> |
| 25 |
#include <unistd.h> |
| 26 |
|
| 27 |
#endif |
| 28 |
|
| 29 |
#include "config.h" |
| 30 |
#include "scoreboard.h" |
| 31 |
|
| 32 |
/*************************************************************** |
| 33 |
* |
| 34 |
* 作業ファイルを開く |
| 35 |
* 現版では1本のファイルを新規作成 |
| 36 |
* |
| 37 |
*/ |
| 38 |
|
| 39 |
FILE *scoreboard_open(void) |
| 40 |
{ |
| 41 |
char *fn = tempnam(".", "mtywk"); |
| 42 |
|
| 43 |
#ifdef WIN32 |
| 44 |
FILE *fp; |
| 45 |
HANDLE h; |
| 46 |
h = CreateFile(fn, |
| 47 |
GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE, |
| 48 |
FILE_SHARE_DELETE |
| 49 |
#if DEBUG>=1 |
| 50 |
| FILE_SHARE_READ | FILE_SHARE_WRITE |
| 51 |
#endif |
| 52 |
| 0, |
| 53 |
NULL, |
| 54 |
CREATE_ALWAYS, |
| 55 |
FILE_ATTRIBUTE_NORMAL |
| 56 |
#if DEBUG<1 |
| 57 |
| FILE_FLAG_DELETE_ON_CLOSE |
| 58 |
#endif |
| 59 |
| 0, |
| 60 |
NULL); |
| 61 |
fp = _fdopen(_open_osfhandle((intptr_t)h, _O_RDWR), "w+b"); |
| 62 |
#else |
| 63 |
FILE *fp = fopen(fn, "w+b"); |
| 64 |
#endif |
| 65 |
|
| 66 |
assert(fp != NULL); |
| 67 |
#if DEBUG>=1 |
| 68 |
fprintf(stderr, "scoreboard: <%s>\n", fn); |
| 69 |
#else |
| 70 |
unlink(fn); |
| 71 |
#endif |
| 72 |
free(fn); /* malloc(3)にて確保されていた */ |
| 73 |
return fp; |
| 74 |
} |
| 75 |
|
| 76 |
/*************************************************************** |
| 77 |
* |
| 78 |
* 作業ファイルをメモリ上に実現する(W32) |
| 79 |
* |
| 80 |
*/ |
| 81 |
|
| 82 |
void *scoreboard_map(FILE *fp) |
| 83 |
{ |
| 84 |
void *adr = NULL; |
| 85 |
off_t len; |
| 86 |
|
| 87 |
#ifdef WIN32 |
| 88 |
HANDLE h; |
| 89 |
#endif |
| 90 |
|
| 91 |
/* イメージ長を得る */ |
| 92 |
fseek(fp, 0, SEEK_END); |
| 93 |
len = ftell(fp); |
| 94 |
assert(len > 0); |
| 95 |
|
| 96 |
fflush(fp); |
| 97 |
|
| 98 |
#ifdef WIN32 |
| 99 |
h = CreateFileMapping((HANDLE)_get_osfhandle(_fileno(fp)), |
| 100 |
NULL, |
| 101 |
PAGE_EXECUTE_READWRITE, |
| 102 |
0, 0, |
| 103 |
NULL); |
| 104 |
#ifndef FILE_MAP_EXECUTE |
| 105 |
#define FILE_MAP_EXECUTE SECTION_MAP_EXECUTE /* XXX cygwin */ |
| 106 |
#endif |
| 107 |
if (h) |
| 108 |
adr = MapViewOfFile(h, |
| 109 |
FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, |
| 110 |
0, 0, |
| 111 |
0); |
| 112 |
#else |
| 113 |
adr = mmap(NULL, |
| 114 |
len, |
| 115 |
PROT_EXEC | PROT_READ | PROT_WRITE, |
| 116 |
MAP_SHARED, |
| 117 |
fileno(fp), |
| 118 |
0); |
| 119 |
if (adr == MAP_FAILED) |
| 120 |
adr = NULL; |
| 121 |
#endif |
| 122 |
|
| 123 |
/* メモリが確保できていない場合のいんちき対策 */ |
| 124 |
if (!adr) |
| 125 |
{ |
| 126 |
adr = malloc(len); |
| 127 |
assert(adr != NULL); |
| 128 |
rewind(fp); |
| 129 |
fread(adr, 1, len, fp); |
| 130 |
} |
| 131 |
|
| 132 |
#if DEBUG>=1 |
| 133 |
fprintf(stderr, |
| 134 |
"scoreboard: mapped to 0x%p, siz=0x%08X(%d)\n", |
| 135 |
adr, |
| 136 |
(unsigned)len, (unsigned)len); |
| 137 |
#endif |
| 138 |
fprintf(stderr, |
| 139 |
"作業領域を %d バイト確保しました。\n", |
| 140 |
(unsigned)len); |
| 141 |
|
| 142 |
return adr; |
| 143 |
} |
| 144 |
|
| 145 |
/* |
| 146 |
* Local Variables: |
| 147 |
* tab-width: 4 |
| 148 |
* End: |
| 149 |
* |
| 150 |
* EOF */ |
Properties
| svn:eol-style |
native
|
| svn:keywords |
Author Date Id Rev URL
|
| |