メインページ | アルファベット順一覧 | 構成 | ファイル一覧 | 構成メンバ | ファイルメンバ | 関連ページ

# dkcOSIndependent.c

説明を見る。

    00001
    00007 #define DKUTIL_C_OS_INDEPENDENT_C
    00008
    00009 #include "dkcOSIndependent.h"
    00010 #include "dkcDefined.h"
    00011
    00012 #include "dkcQueue.h"
    00013
    00014
    00015 #include <assert.h>
    00016 #include <math.h>
    00017
    00018
    00019 #ifdef WIN32
    00020
    00021 #include <shellapi.h>
    00022
    00023 #pragma warning(disable:4127)
    00024
    00025 int dkcDynamicCheckEnvironment(BOOL isForceExit){
    00026     int r = edk_FAILED;
    00027     if((sizeof(int) != sizeof(long))) goto Error;
    00028     if(sizeof(int) != 4) goto Error;
    00029
    00030     if((sizeof(UINT) != sizeof(ULONG))) goto Error;
    00031     if(sizeof(UINT) != 4) goto Error;
    00032
    00033     if((sizeof(BYTE) != sizeof(char))) goto Error;
    00034     if((sizeof(BYTE) != 1)) goto Error;
    00035     if(sizeof(double) != 8) goto Error;
    00036     if(sizeof(void *) != sizeof(unsigned)) goto Error;
    00037     if(sizeof(void *) != sizeof(int)) goto Error;
    00038     if(sizeof(short) != 2) goto Error;
    00039     r = edk_SUCCEEDED;
    00040 Error:
    00041     if(DKUTIL_FAILED(r) && isForceExit){
    00042         exit(-1);
    00043     }
    00044     return r;
    00045 }
    00046
    00047 #pragma warning(default:4127)
    00048 #else
    00049 int dkcDynamicCheckEnvironment(BOOL isForceExit){
    00050
    00051 }
    00052
    00053 #endif
    00054
    00055
    00056 int WINAPI dkcIsNativePathString(const char *s,size_t size){
    00057
    00058     size_t i=0;
    00059     if(s==NULL) return edk_FAILED;
    00060     for(;i<size;i++){
    00061         if(dkcmIS_INVALID_PATH_CHAR(s[i]))
    00062         {
    00063             return FALSE;
    00064         }
    00065     }
    00066     return TRUE;
    00067 }
    00068
    00069 DKC_INLINE int WINAPI dkcOutputDebugString(const char *str,...){
    00070     char s[1024 * 2];
    00071
    00072
    00073     int va_list_result;
    00074     va_list Valist;
    00075
    00076
    00077
    00078     SET_VA_LIST_C(Valist,va_list_result,s,sizeof(s),str);
    00079     if(0 > va_list_result){
    00080         return edk_FAILED;
    00081     }
    00082     OutputDebugString(s);
    00083     return edk_SUCCEEDED;
    00084     //fprintf(stderr,s);
    00085 }
    00086
    00087 int WINAPI dkcErrorMessage(const char *expression,
    00088                                          const char *filename,size_t line,const char *message,...)
    00089 {
    00090     char s[1024];
    00091     char ss[1024 * 2];
    00092
    00093     int va_list_result;
    00094     va_list Valist;
    00095
    00096     NULL_CHAR_ARRAY(s);
    00097     NULL_CHAR_ARRAY(ss);
    00098
    00099     if(message){
    00100         SET_VA_LIST_C(Valist,va_list_result,s,sizeof(s),message);
    00101     }else{
    00102         //SET_VA_LIST(s,sizeof(s),"No message");
    00103         _snprintf(s,sizeof(s),"No message\n");
    00104     }
    00105
    00106     _snprintf(ss,sizeof(ss),
    00107         "DKUTIL_ASSERT(%s):\nfile:%s / \n line:%d / \n message: %s",
    00108         expression,filename,line,s);
    00109
    00110
    00111
    00112 #ifdef NDEBUG//release
    00113 #   ifdef WIN32
    00114         ODS(ss);
    00115     //::MessageBox(NULL,ss,"ERROR ASSERTION !!",MB_OK);
    00116     {
    00117         LPCTSTR name="ERROR_TEMP.txt";
    00118         {
    00119             //ファイルをオープン
    00120             FILE *fp;
    00121             if(NULL != (fp = fopen( name , "at" ))){
    00122
    00123                 // エラーログファイルに書き出す
    00124                 fputs( ss , fp ) ;
    00125                 fclose( fp ) ;
    00126                 Sleep(800);
    00127             }
    00128         }
    00129         ShellExecute(NULL, "open",name, NULL, NULL, SW_SHOWNORMAL);
    00130         //Sleep(1000);
    00131         //DeleteFile(name);
    00132     }
    00133     MB("このテキストをこのソフトの開発者に渡せば、バグ発見の近道なります。\n \
    00134         開発者に知らせるときはこのテキストを転記してあげましょう。");
    00135
    00136     /*if(IDYES==MessageBox(NULL,
    00137         "このままこのソフトを起動しつづけると、更なるエラーが発生する可能性があります。\n終了しますか？",
    00138         "あってはいけない エラー!!!",
    00139         MB_YESNO))
    00140     {
    00141         exit(edk_FAILED);
    00142         //terminate();
    00143     }else{
    00144     }*/
    00145     exit(edk_FAILED);
    00146 #   else    //else of win32
    00147
    00148     fprintf(stderr,ss);
    00149     exit(edk_FAILED);
    00150
    00151 #   endif //end of win32
    00152
    00153 #else //debug
    00154
    00155 #   ifdef WIN32
    00156         ODS(ss);
    00157     fprintf(stderr,ss);
    00158     //_CrtDebugBreak();
    00159     DebugBreak();
    00160 #   else //else of win32
    00161
    00162     fprintf(stderr,ss);
    00163     Debugger();
    00164
    00165 #   endif //end of win32
    00166
    00167     return TRUE;
    00168 #endif//end of NDEBUG
    00169     //assert(ss);
    00170
    00171 }
    00172
    00173 void *WINAPIV dkcMallocAdapter(size_t s){
    00174     return malloc(s);
    00175 }
    00176
    00177 void WINAPIV dkcFreeAdapter(void *p){
    00178     if(p){
    00179         free(p);
    00180     }
    00181 }
    00182
    00183 DKC_INLINE void *dkcAllocateFill(size_t size,BYTE fill){
    00184     return dkcAllocateFill_INL(size,fill);
    00185 }
    00186 DKC_INLINE void *dkcAllocateFast(size_t size){
    00187     return dkcAllocateFast_INL(size);
    00188 }
    00189 DKC_INLINE void* dkcAllocate(size_t size){
    00190     return dkcAllocate_INL(size);
    00191 }
    00192
    00193 DKC_INLINE int  dkcFree(void **p){
    00194     return dkcFree_INL(p);
    00195 }
    00196 BOOL dkcIs_foepn_mode(const char *s){
    00197     /* wrtb+ */
    00198     const char *ms= "wrtb+";
    00199     size_t slen = strlen(s);
    00200     size_t mslen = strlen(ms);
    00201     size_t i,j;
    00202     int flag = FALSE;
    00203     for(i=0;i<slen;i++){
    00204         for(j=0;j<mslen;j++){
    00205             if(ms[j]==s[i]){
    00206                 flag = TRUE;
    00207                 break;
    00208             }
    00209         }
    00210         if(TRUE!=flag){
    00211             return FALSE;
    00212         }else{
    00213             flag = FALSE;
    00214         }
    00215     }
    00216     return TRUE;
    00217 }
    00218 /*
    00219 struct dkcDoublePoint{
    00220     double x,y;
    00221 }DKC_DOUBLE_POINT;
    00222
    00223 int calc(DKC_DOUBLE_POINT *pa,size_t nums){
    00224     pa->x
    00225
    00226 }*/
    00227 /*
    00228 double calc(double StartX,double StartY,double EndX,double EndY)
    00229 {
    00230     return  StartY - ((EndY - StartY) / (EndX - StartX)) * StartX;
    00231 }
    00232 */
    00233 size_t dkcReallocateSizeFunction(size_t OldSize,size_t ExpandSize){
    00234     //y=-25.43035 * pow(x,2) + 116.7214
    00235     //y = -0.00000018 * pow(x,2) + 0.00019
    00236     //y = -99.9999 * pow(OldSize,2) + 104857600;
    00237     //y = 0.105263 * x * x + 10.52631
    00238     //double x;
    00239 //  size_t size;
    00240     if(OldSize <= 1024 * 5){//5KB以内なら2倍
    00241         return OldSize * 2;
    00242     }else if(OldSize >= 1024 * 1024 * 10 || 0==OldSize)
    00243     {//10MB以上なら + ExpandSize
    00244         return OldSize + ExpandSize;
    00245     }
    00246     //それ以外ならExpandSize * 3
    00247     return OldSize + ExpandSize * 3;
    00248     //return OldSize * 2;//ひでぇ関数だ。
    00249 }
    00250
    00251 int WINAPI dkcReallocate(void **NewPtr,size_t NewSize,
    00252     void **OldPtr)
    00253 {
    00254     return dkcReallocateEx(realloc,NewPtr,NewSize,OldPtr);
    00255 }
    00256
    00257 int WINAPI dkcReallocateEx(
    00258     DKC_REALLOC_F_TYPE your_realloc,void **NewPtr,size_t NewSize,
    00259     void **OldPtr
    00260 )
    00261 {
    00262     void *ptr = NULL;
    00263
    00264     if(NULL== your_realloc  /*|| NULL == *OldPtr ||
    00265         0==OldSize*/
    00266     ){
    00267         return edk_ArgumentException;//アホ引数入れるな！！
    00268     }
    00269
    00270     ptr = your_realloc(*OldPtr,NewSize);
    00271     if(NULL==ptr) return edk_FAILED;
    00272
    00273     //更新
    00274     *OldPtr = NULL;
    00275     *NewPtr = ptr;
    00276
    00277     return edk_SUCCEEDED;
    00278 }
    00279
    00280
    00281
    00282 int WINAPI dkcReallocateAutoExpand(
    00283     DKC_REALLOC_F_TYPE your_realloc,void **NewPtr,
    00284     void **OldPtr,size_t OldSize,size_t *reallocated_size
    00285     )
    00286 {
    00287     void *ptr = NULL;
    00288     size_t want_size = OldSize / 8;
    00289     size_t ra_size = dkcReallocateSizeFunction(OldSize,
    00290          (want_size <= 20) ? 20 : want_size
    00291     );
    00292     size_t div = 8;
    00293     size_t tmp_ra = ra_size / div;
    00294     size_t i;
    00295     if(NULL== your_realloc /*|| NULL==*NewPtr*/ || NULL == *OldPtr ||
    00296         0==OldSize  || NULL==reallocated_size
    00297     ){
    00298         return edk_ArgumentException;//アホ引数入れるな！！
    00299     }
    00300
    00301     for(i=0;i<div;i++){
    00302         ptr = your_realloc(*OldPtr,ra_size);
    00303         if(NULL==ptr){
    00304             if(OldSize > (ra_size -= tmp_ra)){//とりあえずtmp_ra分を引く。
    00305                 break;//こら望みなしだわ
    00306             }
    00307             continue;
    00308         }else{
    00309             break;
    00310         }
    00311     }
    00312     if(NULL==ptr) return edk_FAILED;
    00313
    00314     //更新
    00315     *NewPtr = ptr;
    00316     *OldPtr = NULL;
    00317
    00318     *reallocated_size = ra_size;
    00319
    00320     return edk_SUCCEEDED;
    00321 }
    00322
    00323 int WINAPI dkcGetMemorySize(uint64 *pTotalMemory,uint64 *pFreeMemory)
    00324 {
    00325 #ifdef WIN32
    00326     MEMORYSTATUS ms;
    00327     memset(&ms,0,sizeof(ms));
    00328     ms.dwLength =  sizeof( ms );
    00329     GlobalMemoryStatus( &ms );
    00330
    00331     if ( pTotalMemory == NULL || pFreeMemory  == NULL)
    00332         return edk_FAILED;
    00333
    00334     *pTotalMemory = (uint64)ms.dwTotalPhys;
    00335     *pFreeMemory  = (uint64)ms.dwAvailPhys;
    00336     return edk_SUCCEEDED;
    00337 #else
    00338
    00339
    00340 #endif
    00341 }
    00342
    00343
    00344 static int WINAPI dkcLoadFile(void *data,size_t size,const char *fname,const char *mode,size_t *readsize){//="rb"
    00345     FILE *fp ;
    00346     if(NULL==readsize || NULL==data || NULL==fname || NULL==mode){
    00347         return edk_FAILED;
    00348     }
    00349     fp = fopen( fname , mode ) ;//rb
    00350     if(fp==NULL)return edk_FAILED;
    00351     *readsize = dkcFReadAll(data,size,fp);
    00352     //*readsize = fread( data , 1 , size , fp ) ;
    00353     //fread( data , sizeof(size)  , 1 , fp ) ;
    00354     fclose( fp ) ;
    00355
    00356     return edk_SUCCEEDED;
    00357
    00358 }
    00359 static int WINAPI dkcSaveFile(const void *data,size_t size,const char *fname,const char *mode)
    00360 {
    00361     FILE *fp;
    00362     if(NULL==fname || NULL==mode){
    00363         return edk_FAILED;
    00364     }
    00365     fp = fopen( fname , mode ) ;//wb
    00366     if(fp==NULL) return edk_FAILED;
    00367     dkcFWriteAll(data,size,fp);
    00368     //fwrite( data , size  , 1 , fp ) ;
    00369     //fwrite( data , sizeof(size)  , 1 , fp ) ;
    00370     fclose( fp ) ;
    00371     return edk_SUCCEEDED;
    00372 }
    00373
    00374
    00375 DKC_EXTERN FILE * WINAPI dkcFOpen(const char *filename,const char *mode){
    00376     return fopen(filename,mode);
    00377 }
    00378
    00379 DKC_EXTERN int WINAPI dkcFClose(FILE **ptr){
    00380     if(NULL==ptr || NULL==*ptr) return edk_ArgumentException;
    00381     fclose(*ptr);
    00382     *ptr = NULL;
    00383     return edk_SUCCEEDED;
    00384 }
    00385 DKC_EXTERN size_t WINAPI dkcFReadAll(void *buffer,size_t size,FILE *fp)
    00386 {
    00387     size_t count;
    00388     size_t tc,tsize;
    00389     BYTE *tbuffer;
    00390
    00391     //read size
    00392     tsize = size;
    00393     //temp variable
    00394     tc = 0;
    00395     //offset counter
    00396     count = 0;
    00397     //byte type pointer
    00398     tbuffer = (BYTE *)buffer;
    00399     for(;;)
    00400     {
    00401         //error check
    00402         if(ferror(fp) ){
    00403             break;
    00404         }
    00405         if(feof(fp)){
    00406             break;
    00407         }
    00408         //read
    00409         tc = fread(&tbuffer[count],1,tsize,fp);
    00410         //update
    00411         tsize -= tc;
    00412         count += tc;
    00413
    00414         if(count == size){
    00415             break;
    00416         }
    00417 #   ifdef DEBUG
    00418         //ありえないエラーチェック
    00419         dkcmNOT_ASSERT(count > size);
    00420 #   else
    00421         if(count > size){
    00422             break;
    00423         }
    00424 #   endif
    00425     }
    00426     return count;
    00427 }
    00428
    00429 DKC_EXTERN size_t WINAPI dkcFWriteAll(const void *buffer,size_t size,FILE *fp){
    00430     size_t count;
    00431     size_t tc,tsize;
    00432     BYTE *tbuffer;
    00433
    00434     //read size
    00435     tsize = size;
    00436     //temp variable
    00437     tc = 0;
    00438     //offset counter
    00439     count = 0;
    00440     //byte type pointer
    00441     tbuffer = (BYTE *)buffer;
    00442     for(;;)
    00443     {
    00444         //error check
    00445         if(ferror(fp) ){
    00446             break;
    00447         }
    00448         if(feof(fp)){
    00449             break;
    00450         }
    00451         //read
    00452         tc = fwrite(&tbuffer[count],1,tsize,fp);
    00453         //update
    00454         tsize -= tc;
    00455         count += tc;
    00456
    00457         if(count == size){
    00458             break;
    00459         }
    00460 #   ifdef DEBUG
    00461         //ありえないエラーチェック
    00462         dkcmNOT_ASSERT(count > size);
    00463 #   else
    00464         if(count > size){
    00465             break;
    00466         }
    00467 #   endif
    00468     }
    00469     return count;
    00470
    00471 }
    00472
    00473
    00474 DKC_INLINE size_t WINAPI dkcFSize(FILE *fp)
    00475 {
    00476     long t;
    00477     size_t len;
    00478     //temporary
    00479     t = ftell(fp);
    00480     fseek(fp,0,SEEK_END);
    00481     len = ftell(fp);
    00482     fseek(fp,t,SEEK_SET);
    00483     return len;
    00484 }
    00485
    00486 //fopenのファイル関数を使ってバイナリセーブを行う。
    00487 int WINAPI dkcSaveBinary(const void *data,size_t size,const char *fname){//="wb"
    00488     return dkcSaveFile(data,size,fname,"wb");
    00489 }
    00490
    00491 DKC_INLINE BOOL WINAPI dkcCreateEmptyFile(const char *filename){
    00492     FILE *fp;
    00493     fp = fopen( filename , "wb" ) ;//wb
    00494     if(fp==NULL) return FALSE;
    00495     fclose( fp ) ;
    00496     return TRUE;
    00497 }
    00498
    00499
    00500 DKC_INLINE int WINAPI dkcLoadBinary(void *data,size_t size,const char *fname,size_t *readsize){//="rb"
    00501     return dkcLoadFile(data,size,fname,"rb",readsize);
    00502 }
    00503
    00504 DKC_INLINE int WINAPI dkcSaveText(const char *text,size_t length,const char *fname){
    00505     return dkcSaveFile(text,length,fname,"wt");
    00506 }
    00507 DKC_INLINE int WINAPI dkcLoadText(char *text,size_t length,const char *fname,size_t *readsize){
    00508     return dkcLoadFile(text,length,fname,"rt",readsize);
    00509 }
    00510
    00511
    00512 DKC_INLINE int WINAPI dkcInitFileFast( const char *filename )
    00513 {
    00514     FILE *fp;
    00515     // エラーログファイルを開く(初期化)
    00516     if(NULL==(fp = fopen( filename , "wt" ))){
    00517         return edk_FAILED;
    00518     }
    00519     fclose( fp ) ;
    00520
    00521     return edk_SUCCEEDED;
    00522 }
    00523
    00524 DKC_INLINE  int WINAPI dkcAddLogFile( const char *filename,const char *str , ... ){
    00525     FILE *fp=NULL;
    00526     char s[2048]="";
    00527     va_list VaList;
    00528     int r;
    00529     //SET_VA_LIST_INSERT_YEN_N(s,2048,str);
    00530     SET_VA_LIST_C(VaList,r,s,sizeof(s),str);
    00531
    00532     //ファイルをオープン
    00533     if(NULL == (fp = fopen( filename , "at" )))
    00534         return edk_FAILED;
    00535     // エラーログファイルに書き出す
    00536     fputs( s , fp ) ;
    00537     fclose( fp ) ;
    00538     return edk_SUCCEEDED;
    00539 }
    00540
    00541
    00542 BOOL WINAPI dkcSwapFast(void *p1,void *p2,size_t size)
    00543 {
    00544     void *p;
    00545 #if 0//stack overflow 例外が発生するのも嫌だから挙動は共通で。
    00546     int state = 0;
    00547     p = _alloca( size );
    00548     if(NULL==p){
    00549         state = 1;
    00550         p = malloc(size);
    00551         if(NULL==p)
    00552             return FALSE;
    00553     }
    00554 #else
    00555     p = malloc(size);
    00556     if(NULL==p)
    00557         return FALSE;
    00558 #endif
    00559     memcpy(p,p1,size);
    00560     memcpy(p1,p2,size);
    00561     memcpy(p2,p,size);
    00562 #if 0
    00563     if(1==state){
    00564         free(p);
    00565     }
    00566
    00567 #else
    00568     free(p);
    00569 #endif
    00570     return TRUE;
    00571 }
    00572 /*
    00573 template<typename TYPE_T>
    00574 inline void dkcSwapT(TYPE_T *p1,TYPE_T *p2,size_t size){
    00575     size_t i;
    00576     char buf;
    00577     size_t mod_ = size % sizeof(TYPE_T);
    00578     size_t cnt = size / sizeof(TYPE_T);
    00579     for(i=0;i<cnt;i++){
    00580         SWAP_NUM(p1[i],p2[i]);
    00581     }
    00582     for(i=0;i<mod_;i++){
    00583         buf = (((BYTE *)p1)[size - mod_ + i]);
    00584         (((BYTE *)p1)[size - mod_ + i]) =
    00585             (((BYTE *)p2)[size - mod_ + i]) ;
    00586         (((BYTE *)p2)[size - mod_ + i]) = buf;
    00587     }
    00588
    00589 }
    00590
    00591
    00592 void WINAPI dkcSwapFast(int *p1,int *p2,size_t size)
    00593 {
    00594     size_t i;
    00595     char buf;
    00596     size_t mod_ = size % sizeof(int);
    00597     size_t cnt = size / sizeof(int);
    00598     for(i=0;i<cnt;i++){
    00599         SWAP_NUM(p1[i],p2[i]);
    00600     }
    00601     for(i=0;i<mod_;i++){
    00602         buf = (((BYTE *)p1)[size - mod_ + i]);
    00603         (((BYTE *)p1)[size - mod_ + i]) =
    00604             (((BYTE *)p2)[size - mod_ + i]) ;
    00605         (((BYTE *)p2)[size - mod_ + i]) = buf;
    00606     }
    00607 }*/
    00608
    00609 #ifdef _MSC_VER
    00610 #   pragma warning(disable:4244)
    00611 #endif
    00612
    00613 void WINAPI dkcSwap64(ULONGLONG *p1,ULONGLONG *p2,size_t size){
    00614     size_t i;
    00615     BYTE buf;
    00616     size_t mod_ = size % sizeof(ULONGLONG);
    00617     size_t cnt = size / sizeof(ULONGLONG);
    00618     //char *tp1 = (char *)p1,*tp2 = (char *)p2;
    00619     for(i=0;i<cnt;i++){
    00620         SWAP_TEMP(register ULONGLONG,p1[i],p2[i]);
    00621         //SWAP_NUM(p1[i],p2[i]);
    00622     }
    00623     for(i=0;i<mod_;i++){
    00624
    00625         //SWAP_NUM((char)tp1[size - mod_ + 1],(char)tp2[size - mod_ + i]);
    00626         /*a = b - a ;\
    00627     b -= a ;\
    00628     a += b
    00629         */
    00630         /*( ( ((char *) p2)[size - mod_ + i]))
    00631             = (char)( ( ((char *) p2)[size - mod_ + i])) -
    00632             (char)( ( ((char *) p1)[size - mod_ + i]));
    00633
    00634         ( ( ((char *) p2)[size - mod_ + i])) -= ( ( ((char *) p1)[size - mod_ + i]));
    00635
    00636         ( ( ((char *) p1)[size - mod_ + i])) += ( ( ((char *) p2)[size - mod_ + i]));
    00637             */
    00638
    00639
    00640         buf = (((BYTE *)p1)[size - mod_ + i]);
    00641         (((BYTE *)p1)[size - mod_ + i]) =
    00642             (((BYTE *)p2)[size - mod_ + i]) ;
    00643         (((BYTE *)p2)[size - mod_ + i]) = buf;
    00644
    00645     }
    00646
    00647 }
    00648
    00649 void WINAPI dkcSwap(void *p1,void *p2,size_t size)
    00650 {
    00651     dkcSwap64((ULONGLONG *)p1,(ULONGLONG *)p2,size);
    00652 }
    00653
    00654
    00655
    00656 DKC_INLINE void WINAPI dkcTwoDWORDToULONGLONG(ULONGLONG *dest,DWORD high,DWORD low){
    00657     //*dest = (ULONGLONG)((high << 32) + low);
    00658     *dest = (ULONGLONG)high << 32;
    00659     *dest += low;
    00660         //( high * MAXDWORD ) + low;
    00661 }
    00663 DKC_INLINE void WINAPI dkcULONGLONGToTwoDWORD(DWORD *dhigh,DWORD *dlow,ULONGLONG src){
    00664     DWORD *o = (DWORD *)&src;
    00665     dkcmNOT_ASSERT(sizeof(DWORD) * 2 != sizeof(ULONGLONG));
    00666
    00667
    00668     //*dhigh = o[0];*dlow = o[1];
    00669     *dhigh = o[1];*dlow = o[0];
    00670 }
    00671
    00672 DKC_INLINE void WINAPI dkcLONGLONGToTwoLONG(LONG *high,LONG *low,LONGLONG src){
    00673     /*LONG *o = (LONG *)&src;
    00674     dkcmNOT_ASSERT(sizeof(LONG) * 2 != sizeof(LONGLONG));
    00675     *high = o[0];
    00676     *low = o[1];*/
    00677 #ifdef WIN32
    00678     DKC_TWOINT32_TO_INT64 a;
    00679     a.QuadPart = src;
    00680     *low = a.u.LowPart;
    00681     *high = a.u.HighPart;
    00682 #else
    00683
    00684 #endif
    00685 }
    00686
    00687 DKC_INLINE void WINAPI dkcTwoLONGToLONGLONG(LONGLONG *dest,LONG high,LONG low){
    00688     /*//*dest = (ULONGLONG)((high << 32) + low);
    00689     *dest = (LONGLONG)high << 32;
    00690     //*dest += low;
    00691     (*dest) &= low;
    00692     */
    00693 #ifdef WIN32
    00694     DKC_TWOINT32_TO_INT64 a;
    00695
    00696     a.u.HighPart = high;
    00697     a.u.LowPart = low;
    00698     *dest = a.QuadPart;
    00699
    00700 #else
    00701
    00702 #endif
    00703 }
    00704
    00705 /*
    00706 //まずい、これは難しい。
    00707 DKC_INLINE int WINAPI dkcDataReplace(BYTE *dest,size_t destsize,
    00708                                                                          const BYTE *src,size_t srcsize,
    00709                                                                          const BYTE *target_data,size_t target_data_size,
    00710                                                                          const BYTE *replace_data,size_t replace_data_size)
    00711 {
    00712
    00713     size_t i;
    00714
    00715     //とりあえず、20個　で　offsetはsize_t分
    00716     DKC_QUEUE *pq = dkcAllocQueue(20,sizeof(size_t));
    00717     if(NULL==pq){
    00718         return edk_OutOfMemory;
    00719     }
    00720
    00721     //まずは置換場所を探す
    00722     for(i=0;i<srcsize;i++){
    00723         memcmp(&src[i],
    00724
    00725
    00726
    00727
    00728
    00729 }*/
    00730
    00731
    00732
    00733 #ifdef _MSC_VER
    00734 #   pragma warning(default:4244)
    00735
    00736
    00737
    00738 void WINAPI dkcCheckMemoryLeak(BOOL flag){
    00739     if(flag){
    00740         _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    00741     }else{
    00742         _CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF);
    00743     }
    00744 }
    00745
    00746 #else
    00747
    00748 void WINAPI dkcCheckMemoryLeak(BOOL flag){
    00749
    00750 }
    00751 #endif
    00752
    00753 const char *WINAPI dkcGetPathSep(){
    00754 #ifdef WIN32
    00755     static char target[3]={dkcdPATH_SEP,'/','\0'};
    00756 #else
    00757     static char target[3]={dkcdPATH_SEP,'\0'};
    00758 #endif
    00759     return target;
    00760 }
    00761
    00762
    00763 #ifdef _MSC_VER
    00764 BOOL WINAPI dkcIsMMX(void)
    00765 {
    00766     BOOL flag = FALSE;
    00767
    00768     __asm{
    00769         push    edx
    00770         push    ecx
    00771         pushfd
    00772         pop     eax
    00773         xor     eax, 00200000h
    00774         push    eax
    00775         popfd
    00776         pushfd
    00777         pop     ebx
    00778         cmp     eax, ebx
    00779         jnz     non_mmx
    00780         mov     eax, 0
    00781         cpuid
    00782         cmp     eax, 0
    00783         jz      non_mmx
    00784         mov     eax, 1
    00785         cpuid
    00786         and     edx, 00800000h
    00787         jz      non_mmx
    00788         mov     flag, TRUE
    00789     non_mmx:
    00790         pop     ecx
    00791         pop     edx
    00792     }
    00793     return flag;
    00794 }
    00795 #else
    00796 BOOL WINAPI dkcIsMMX(void){
    00797   return FALSE;
    00798 }
    00799 #endif
    00800
    00801
    00802 DKC_INLINE USHORT dkcReverseEndian16(USHORT x){
    00803     return (USHORT)((x >> 8) | (x << 8));
    00804 }
    00805
    00806 DKC_INLINE DWORD dkcReverseEndian32(DWORD x){
    00807     return (x << 24) | ((x & 0x0000ff00) << 8) | ((x & 0x00ff0000) >> 8) | (x >> 24);
    00808 }
    00809
    00810 DKC_INLINE ULONGLONG dkcReverseEndian64(ULONGLONG x) {
    00811
    00812     return (
    00813         (ULONGLONG)dkcReverseEndian32((DWORD)(x & 0x00000000ffffffff)) << 32)
    00814         |
    00815         dkcReverseEndian32((DWORD)(x >> 32)
    00816     );
    00817
    00818 }
    00819 DKC_INLINE  BOOL dkcIsLittleEndian(){
    00820     int x=1;
    00821     return (BOOL)((*(char*)&x));
    00822 }
    00823
    00824
    00825
    00826 int WINAPI dkcGetByteOrder()
    00827 {
    00828     DKC_BYTE_ORDER_CHECK_UNION a;
    00829     a.x = 0x04030201;
    00830     switch(a.abcd[0]){
    00831     case 0x01:
    00832         return edkcByteOrder_DCBA;
    00833     case 0x02:
    00834         return edkcByteOrder_Unknown;
    00835     case 0x03:
    00836         return edkcByteOrder_BADC;
    00837     case 0x04:
    00838         return edkcByteOrder_ABCD;
    00839     }
    00840     return edkcByteOrder_Unknown;
    00841 }
    00842
    00843 DKC_INLINE BOOL dkcCheckOverflowULONG(ULONG a1,ULONG a2){
    00844     ULONG maxv = ULONG_MAX;
    00845   ULONG halfv = maxv / 2 ;
    00846   ULONG x = a1,y = a2;
    00847   if(x < y){
    00848     SWAP_NUM(x,y);
    00849   }
    00850   if(x > halfv + 1){
    00851     if(y >= halfv){
    00852       return TRUE;
    00853     }
    00854   }
    00855   return FALSE;
    00856 }
    00857
    00858 DKC_INLINE BOOL dkcCheckOverflowULONGLONG(ULONGLONG a1,ULONGLONG a2)
    00859 {
    00860     ULONGLONG maxv = ULONGLONG_MAX;
    00861   ULONGLONG halfv = maxv / 2 ;
    00862   ULONGLONG x = a1,y = a2;
    00863   if(x < y){
    00864     SWAP_NUM(x,y);
    00865   }
    00866   if(x > halfv + 1){
    00867     if(y >= halfv){
    00868       return TRUE;
    00869     }
    00870   }
    00871   return FALSE;
    00872 }
    00873
    00874
    00875 static void *get_offset(void *p,size_t offset){
    00876     BYTE *oo = (BYTE *)p;
    00877
    00878     return (void *)&(oo[offset]);
    00879 }
    00880
    00886 int WINAPI dkcSelect(void *dest,const void *a_src,size_t n,int k,size_t width ,DKC_COMPARE_TYPE less)
    00887 {
    00888
    00889     int i,j,left, right;
    00890
    00891     void *x = malloc(width);
    00892     void *a = malloc(n * width);
    00893     if(NULL==x || NULL==a){
    00894         return edk_ArgumentException;
    00895     }
    00896     if(n > INT_MAX){
    00897         return edk_FAILED;
    00898     }
    00899
    00900
    00901     memcpy(a,a_src,width * n);
    00902
    00903
    00904     left = 0;  right = n - 1;
    00905
    00906     while (left < right)
    00907     {
    00908
    00909         memcpy(x,   (const void *)get_offset(a,k * width),width);
    00910         i = left;
    00911         j = right;
    00912
    00913         for ( ; ; ) {
    00914
    00915             while( less(get_offset(a,i * width),x) > 0){
    00916             //while( *(int *)get_offset(a,i * width) < *(int *)x){
    00917                 i++;
    00918             }
    00919
    00920             while( less(x,get_offset(a,j * width)) > 0){
    00921             //while( *(int *)x < *(int *)get_offset(a,j * width) ){
    00922                 j--;
    00923             }
    00924
    00925             if (i > j){
    00926                 break;
    00927             }
    00928
    00929
    00930             dkcSwapFast(
    00931                 get_offset(a,i * width),
    00932                 get_offset(a,j * width),
    00933                 width
    00934                 );
    00935
    00936             i++;
    00937             j--;
    00938         }
    00939         if (j < k){
    00940             left  = i;
    00941         }
    00942         if (k < i){
    00943             right = j;
    00944         }
    00945     }
    00946
    00947     //ゲットする
    00948     {
    00949         //printf("%d",*(int *)get_offset(a,k * width));
    00950
    00951         memcpy(dest,(const void *)get_offset(a,k * width),width);
    00952
    00953     }
    00954     free(x);
    00955     free(a);
    00956
    00957     return edk_SUCCEEDED;
    00958
    00959 }
    00960
    00961
    00962 DKC_INLINE int WINAPI dkcSelectMax(void *a,const void *a_src, size_t n,size_t width ,DKC_COMPARE_TYPE less){
    00963     if(n-1 > INT_MAX){
    00964         return edk_FAILED;
    00965     }
    00966     return dkcSelect(a,a_src,n,(int)n-1,width,less);
    00967 }
    00968 DKC_INLINE int WINAPI dkcSelectMin(void *a,const void *a_src, size_t n,size_t width ,DKC_COMPARE_TYPE less){
    00969     return dkcSelect(a,a_src,n,(int)0,width,less);
    00970 }
    00971 #if 0
    00972 int WINAPI dkcSelect(void *dest,const void *a_src,size_t n,size_t k,size_t width ,DKC_COMPARE_TYPE less)
    00973 {
    00974     //int i, j, left, right;
    00975     size_t i,j,left, right;
    00976     //keytype x, t;
    00977     //size_t x,t;
    00978     void *x = malloc(width);
    00979     void *a = malloc(n * width);
    00980     if(NULL==x || NULL==a){
    00981         return edk_ArgumentException;
    00982     }
    00983
    00984     //void *t = malloc(width);
    00985     memcpy(a,a_src,width * n);
    00986
    00987
    00988     left = 0;  right = n - 1;
    00989     /*
    00990     x = a[k];  i = left;  j = right;
    00991         for ( ; ; ) {
    00992             while (a[i] < x) i++;
    00993             while (x < a[j]) j--;
    00994             if (i > j) break;
    00995             t = a[i];  a[i] = a[j];  a[j] = t;
    00996             i++;  j--;
    00997         }
    00998         if (j < k) left  = i;
    00999         if (k < i) right = j;
    01000     */
    01001     while (left < right)
    01002     {
    01003         //x = a[k];  i = left;  j = right;
    01004         //offset_temp = (BYTE *)a;
    01005
    01006         //memcpy(x, (const void *)&offset_temp[k * width],width);
    01007         memcpy(x,   (const void *)get_offset(a,k * width),width);
    01008         i = left;
    01009         j = right;
    01010
    01011         for ( ; ; ) {
    01012             //while (a[i] < x) i++;
    01013             //while(less(a[i],x)) i++;
    01014 //#error less じゃなくて greater でも、qsortと同じ仕様にするため･･･。スマン
    01015             //while( less(get_offset(a,i * width),x) < 0){
    01016             while( less(get_offset(a,i * width),x) ){
    01017                 i++;
    01018             }
    01019
    01020             /*void *p = get_offset(a,i * width);
    01021             BYTE *ma = (BYTE *)get_offset(a,n * width);
    01022             dkcmNOT_ASSERT(p < a);
    01023             dkcmNOT_ASSERT(p > ((BYTE *)a) + n * width);
    01024             dkcmNOT_ASSERT((ma - (BYTE *)p) % width != 0);
    01025             while(less(p,x) > 0){
    01026                 i++;
    01027                 p = get_offset(a,i * width);
    01028                 ma = (BYTE *)get_offset(a,n * width);
    01029                 dkcmNOT_ASSERT(p < a);
    01030                 dkcmNOT_ASSERT(p > ma - width);
    01031                 dkcmNOT_ASSERT((ma - p) % width != 0);
    01032             }*/
    01033
    01034             //while (x < a[j]) j--;
    01035             //while(less(x,a[j]) j--;
    01036             //while( less(x,get_offset(a,j * width)) < 0){
    01037             while( less(x,get_offset(a,j * width)) ){
    01038                 j--;
    01039             }
    01040
    01041             if (i > j){
    01042                 break;
    01043             }
    01044
    01045             //t = a[i];  a[i] = a[j];  a[j] = t;
    01046             //dkcSwap(&a[i],&a[j],width);
    01047             dkcSwap(
    01048                 get_offset(a,i * width),
    01049                 get_offset(a,j * width),
    01050                 width
    01051                 );
    01052
    01053             i++;
    01054             j--;
    01055         }
    01056         if (j < k){
    01057             left  = i;
    01058         }
    01059         if (k < i){
    01060             right = j;
    01061         }
    01062     }
    01063
    01064     //ゲットする
    01065     {
    01066         printf("%d",*(int *)get_offset(a,k * width));
    01067         //memcpy(,  (const void *)get_offset(a,k * width),width);
    01068         //memcpy(dest,(const void *)get_offset(a,k * width),width);
    01069         memcpy(dest,x,width);
    01070     }
    01071     free(x);
    01072     free(a);
    01073     //free(t);
    01074     return edk_SUCCEEDED;
    01075     //return a[k];
    01076 }
    01077 #endif
    01078
    01079 DKC_INLINE int dkcRotateShiftRightMemoryLogic(void *d,size_t size,size_t n,void *workbuff,size_t worksize)
    01080 {
    01081     BYTE *dest = (BYTE *)d;
    01082     size_t temp;
    01083
    01084     if(n > worksize){
    01085         return edk_FAILED;
    01086     }
    01087     for(;;){
    01088         if(n < size){
    01089             break;
    01090         }
    01091         n -= size;
    01092     }
    01093     temp = size - n;
    01094     memcpy(workbuff,dest + temp,n);
    01095     memmove(dest + n,dest,temp);
    01096     memcpy(dest,workbuff,n);
    01097     return edk_SUCCEEDED;
    01098
    01099 }
    01100
    01101 int WINAPI dkcRotateShiftRightMemory(void *d,size_t size,size_t num){
    01102     //size_t i = 0;
    01103     void *pd = NULL;
    01104     int r = edk_FAILED;
    01105
    01106     pd = malloc(num);
    01107     if(NULL==pd){
    01108         return edk_OutOfMemory;
    01109     }
    01110     if(DKUTIL_SUCCEEDED(dkcRotateShiftRightMemoryLogic(d,size,num,pd,num))){
    01111         r = edk_SUCCEEDED;
    01112     }
    01113
    01114     free(pd);
    01115
    01116     return r;
    01117
    01118 }