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

# dkcOSIndependent.h

説明を見る。

    00001
    00008 #ifndef DKUTIL_C_OS_INDEPENDENT_H
    00009 #define DKUTIL_C_OS_INDEPENDENT_H
    00010
    00011
    00012
    00013 #include "dkc_misc.h"
    00014
    00015
    00016
    00018 DKC_EXTERN BOOL WINAPI dkcIsNativePathString(const char *s,size_t size);
    00019
    00020
    00021
    00022 /*
    00023 @param OldSize[in] 元のメモリ領域のサイズ
    00024 @param ExpandSize[in] 足りないメモリ領域のサイズ
    00025 @return 多分、理想系のサイズ？カナ？
    00026 @note reallocする時の最適なメモリサイズを計算する。
    00027 (内部実装はちょくちょく変わります。
    00028 */
    00029
    00030 DKC_EXTERN  size_t dkcReallocateSizeFunction(size_t OldSize,size_t ExpandSize);
    00031
    00033 typedef void *( WINAPIV *DKC_ALLOC_FUNC_TYPE)(size_t);
    00035 typedef void (WINAPIV *DKC_FREE_FUNC_TYPE)(void *);
    00036
    00037 DKC_EXTERN void *WINAPIV dkcMallocAdapter(size_t);
    00038
    00039 DKC_EXTERN void WINAPIV dkcFreeAdapter(void *);
    00040
    00041 DKC_EXTERN  void* WINAPIV dkcAllocate(size_t size);
    00042
    00043 DKC_EXTERN  int WINAPIV dkcFree(void **p);
    00044
    00045 DKC_EXTERN void *WINAPIV dkcAllocateFast(size_t size);
    00046
    00047 DKC_EXTERN void *WINAPIV dkcAllocateFill(size_t size,BYTE fill);
    00048
    00049 DKC_EXTERN  int WINAPI dkcReallocate(
    00050     void **NewPtr,size_t NewSize,void **OldPtr
    00051 );
    00052
    00060
    00061 DKC_EXTERN int WINAPI dkcReallocateEx(
    00062     DKC_REALLOC_F_TYPE your_realloc,void **NewPtr,size_t NewSize,
    00063     void **OldPtr
    00064 );
    00065
    00075 DKC_EXTERN int WINAPI dkcReallocateAutoExpand(
    00076     DKC_REALLOC_F_TYPE your_realloc,void **NewPtr,
    00077     void **OldPtr,size_t OldSize,size_t *reallocated_size
    00078 );
    00079
    00080 DKC_EXTERN int WINAPI dkcGetMemorySize(uint64 *pTotalMemory,uint64 *pFreeMemory);
    00081
    00085 DKC_EXTERN void WINAPI
    00086     dkcSwap(void *p1,void *p2,size_t size);
    00093 DKC_EXTERN BOOL WINAPI dkcSwapFast(void *p1,void *p2,size_t size);
    00094
    00102 DKC_EXTERN void WINAPI dkcSwap64(ULONGLONG *p1,ULONGLONG *p2,size_t size);
    00103
    00104
    00105 //#define dkcSwap(p1_,p2_,size) dkcSwap64(p1_,p2_,size)
    00106 /*
    00107 #define dkcSwapFast(p1,p2,size) dkcSwap64(p1,p2,size)
    00108 */
    00109
    00110
    00112 DKC_EXTERN BOOL dkcIs_foepn_mode(const char *s);
    00114 DKC_EXTERN FILE * WINAPI dkcFOpen(const char *filename,const char *mode);
    00116 DKC_EXTERN int WINAPI dkcFClose(FILE **ptr);
    00117
    00119 DKC_EXTERN size_t WINAPI dkcFReadAll(void *,size_t size,FILE *fp);
    00120
    00121
    00123 DKC_EXTERN size_t WINAPI dkcFWriteAll(const void *,size_t size,FILE *fp);
    00124
    00126 DKC_EXTERN size_t WINAPI dkcFSize(FILE *fp);
    00127
    00128
    00130 DKC_EXTERN int WINAPI dkcSaveBinary(const void *data,size_t size,const char *fname);
    00132 DKC_EXTERN int WINAPI dkcLoadBinary(void *data,size_t size,const char *fname,size_t *readsize);
    00134 DKC_EXTERN int WINAPI dkcSaveText(const char *text,size_t length,const char *fname);
    00136 DKC_EXTERN int WINAPI dkcLoadText(char *text,size_t length,const char *fname,size_t *readsize);
    00138 DKC_EXTERN BOOL WINAPI dkcCreateEmptyFile(const char *filename);
    00140 DKC_EXTERN int WINAPI dkcInitFileFast( const char *filename );
    00142 DKC_EXTERN  int WINAPI dkcAddLogFile( const char *filename,const char *str , ... );
    00143
    00144 DKC_EXTERN void WINAPI dkcTwoDWORDToULONGLONG(ULONGLONG *dest,DWORD high,DWORD low);
    00145
    00146 DKC_EXTERN void WINAPI dkcULONGLONGToTwoDWORD(DWORD *dhigh,DWORD *dlow,ULONGLONG src);
    00147
    00148 DKC_EXTERN void WINAPI dkcLONGLONGToTwoLONG(LONG *high,LONG *low,LONGLONG src);
    00149
    00150 DKC_EXTERN void WINAPI dkcTwoLONGToLONGLONG(LONGLONG *dest,LONG high,LONG low);
    00151
    00152
    00154 DKC_INLINE void dkcTwoLONGToULONGLONG(ULONGLONG *dest,LONG high,LONG low){
    00155     ULONG *p = (ULONG *)&low,*p2;
    00156     *dest = *p;
    00157     p = (ULONG *)(dest + 1);
    00158     p2 = (ULONG *)&high;
    00159     *p = *p2;
    00160 }
    00161
    00162 DKC_EXTERN int WINAPI dkcOutputDebugString(const char *str,...);
    00166 DKC_EXTERN int WINAPI dkcErrorMessage(const char *expression,
    00167                                          const char *filename,size_t line,const char *message,...);
    00168
    00169
    00170
    00171 #if defined(DEBUG) || defined(DKUTIL_DEBUG)
    00172 #   define dkcmNOT_ASSERT(ex) ( (ex) && dkcErrorMessage(#ex,__FILE__,__LINE__,NULL) )
    00173 #   define dkcmASSERT(ex) ( (ex) || dkcErrorMessage(#ex,__FILE__,__LINE__,NULL) )
    00174 #   define dkcmNOT_ASSERT_MESSAGE(ex,mes) ( (ex) && dkcErrorMessage(#ex,__FILE__,__LINE__,mes) )
    00175 #   define dkcmASSERT_MESSAGE(ex,mes) ( (ex) || dkcErrorMessage(#ex,__FILE__,__LINE__,mes) )
    00176 #else
    00177 #   define dkcmNOT_ASSERT_MESSAGE(ex,mes) ((void)0)
    00178 #   define dkcmASSERT_MESSAGE(ex,mes) ((void)0)
    00179 #   define dkcmNOT_ASSERT(ex) ((void)0)
    00180 #   define dkcmASSERT(ex) ((void)0)
    00181 #endif
    00182
    00183 #   define dkcmFORCE_NOT_ASSERT(ex) ( (ex) && dkcErrorMessage(#ex,__FILE__,__LINE__,NULL) )
    00184 #   define dkcmFORCE_ASSERT(ex) ( (ex) || dkcErrorMessage(#ex,__FILE__,__LINE__,NULL) )
    00185 #   define dkcmFORCE_NOT_ASSERT_MESSAGE(ex,mes) ( (ex) && dkcErrorMessage(#ex,__FILE__,__LINE__,mes) )
    00186 #   define dkcmFORCE_ASSERT_MESSAGE(ex,mes) ( (ex) || dkcErrorMessage(#ex,__FILE__,__LINE__,mes) )
    00187
    00189 DKC_EXTERN const char *WINAPI dkcGetPathSep();
    00190
    00195 DKC_EXTERN int dkcDynamicCheckEnvironment(BOOL isForceExit);
    00197 DKC_EXTERN void WINAPI dkcCheckMemoryLeak(BOOL flag);
    00199 DKC_EXTERN BOOL WINAPI dkcIsMMX();
    00200
    00201
    00202
    00203 #define dkcmREVERSE_ENDIAN16(x) ( (USHORT)( (x >> 8) | (x << 8) ) )
    00204
    00205 #define dkcmREVERSE_ENDIAN32(x) \
    00206     ( (x << 24) | ( (x & 0x0000ff00) << 8) | ( (x & 0x00ff0000) >> 8 ) | (x >> 24) )
    00207
    00208 #ifdef WIN32
    00209 #   define dkcmREVERSE_ENDIAN64(x) (\
    00210         (ULONGLONG)dkcReverseEndian32( (DWORD) (x & 0x00000000ffffffff) ) << 32 )\
    00211         | \
    00212         dkcReverseEndian32((DWORD)(x >> 32)\
    00213     )
    00214 #else
    00215 #   define dkcmREVERSE_ENDIAN64(x) dkcReverseEndian64(x)
    00216
    00217 #endif
    00218
    00219 DKC_EXTERN ULONG dkcReverseEndian32(ULONG x);
    00221 DKC_EXTERN ULONGLONG dkcReverseEndian64(ULONGLONG x);
    00223 DKC_EXTERN USHORT dkcReverseEndian16(USHORT x);
    00224
    00226 DKC_EXTERN BOOL dkcIsLittleEndian();
    00228 #define dkcIsBigEndian() (!dkcIsLittleEndian())
    00229
    00230 enum edkcByteOrder{
    00232     edkcByteOrder_ABCD = 0,
    00234     edkcByteOrder_DCBA,
    00236     edkcByteOrder_BADC,
    00237     edkcByteOrder_Unknown = 255,
    00238 };
    00239 typedef union dkc_byte_order_check_union{
    00240     uint32 x;
    00241     uint8 abcd[4];
    00242 }DKC_BYTE_ORDER_CHECK_UNION;
    00244 DKC_EXTERN int WINAPI dkcGetByteOrder();
    00245
    00247 DKC_EXTERN BOOL dkcCheckOverflowULONG(ULONG a1,ULONG a2);
    00248 DKC_EXTERN BOOL dkcCheckOverflowULONGLONG(ULONGLONG a1,ULONGLONG a2);
    00249
    00250 #define dkcCheckOverflow32(a,b) dkcCheckOverflowULONG(a,b)
    00251 #define dkcCheckOverflow64(a,b) dkcCheckOverflowULONGLONG(a,b)
    00252
    00253
    00254 typedef int (WINAPIV *DKC_COMPARE_TYPE)(const void *,const void *);
    00255
    00279
    00280 DKC_EXTERN int WINAPI dkcSelect(void *dest,const void *a_src,size_t n,int k,size_t width ,DKC_COMPARE_TYPE comp);
    00281
    00286 DKC_EXTERN int WINAPI dkcSelectMax(void *dest,const void *a, size_t n,size_t width ,DKC_COMPARE_TYPE comp);
    00291 DKC_EXTERN int WINAPI dkcSelectMin(void *dest,const void *a, size_t n,size_t width ,DKC_COMPARE_TYPE comp);
    00292
    00293
    00294 DKC_INLINE int dkcRotateShiftRightMemoryLogic(void *dest,size_t size,size_t n,void *workbuff,size_t worksize);
    00295
    00296
    00297 DKC_EXTERN int WINAPI dkcRotateShiftRightMemory(void *dest,size_t size,size_t n);
    00298
    00299 /*
    00300 DKC_EXTERN void dkcSetAllocateMode(int flag);
    00301
    00302 DKC_EXTERN void *dkcDebugAlloc
    00303 */
    00304
    00305
    00306
    00307 #if DKC_HAVE_INLINE==1
    00308
    00309 DKC_FORCE_INLINE void *WINAPI dkcSecureFillMemory (void * Destination,size_t Length,uint8 Fill){
    00310     volatile uint8 *vptr = (volatile uint8 *)Destination;
    00311     while(Length){
    00312         *vptr = Fill;
    00313         vptr++;
    00314         Length--;
    00315     }
    00316     return Destination;
    00317 }
    00318
    00319 DKC_FORCE_INLINE void *dkcSecureZeroMemory(void  *ptr, size_t cnt)
    00320 {
    00321     /*volatile char *vptr = (volatile char *)ptr;
    00322     while (cnt) {
    00323         *vptr = 0;
    00324         vptr++;
    00325         cnt--;
    00326     }
    00327     return ptr;*/
    00328     return dkcSecureFillMemory(ptr,cnt,0);
    00329 }
    00330
    00331
    00333 DKC_INLINE BOOL dkcFReadAllCheck(void *buff,size_t size,FILE *fp)
    00334 {
    00335     if(size != dkcFReadAll(buff,size,fp))
    00336         return FALSE;
    00337     return TRUE;
    00338 }
    00339 DKC_INLINE BOOL dkcFWriteAllCheck(const void *buff,size_t size,FILE *fp)
    00340 {
    00341     if(size != dkcFWriteAll(buff,size,fp))
    00342         return FALSE;
    00343     return TRUE;
    00344 }
    00345
    00346
    00347
    00349 DKC_INLINE size_t dkcGetProperTemporaryBufferSize(){
    00350     uint64 total_size,free_size;
    00351     if(DKUTIL_FAILED(dkcGetMemorySize(&total_size,&free_size)))
    00352     {
    00353         return 1024 * 64;
    00354     }
    00355
    00356     /*if(free_size < 1024 * 1024)
    00357     {
    00358         return 1024;
    00359     }
    00360     //freespaceが半分よりあったら
    00361     if(free_size > total_size / 2)
    00362     {
    00363         return total_size / 0x4000;//32768
    00364     }*/
    00365     total_size = free_size / 0x1000;//4096
    00366     if(total_size > 1024 * 1024){
    00367         total_size = 1024 * 1024;
    00368     }
    00369     else if(0==total_size)
    00370     {
    00371         total_size = free_size % 0x1000;
    00372         if(0==total_size){
    00373             return 1;
    00374         }
    00375     }
    00376     return (size_t)total_size;
    00377 }
    00378 #else
    00379
    00380 // WINAPI ...
    00381
    00382 #endif
    00383
    00384
    00385
    00386
    00387
    00388 #ifdef __cplusplus
    00389
    00390 namespace dkutil{
    00391
    00392
    00402
    00403 inline bool SaveBinary(const void *data,size_t size,const char *fname,const char *mode="wb"){//="wb"
    00404     FILE *fp;
    00405     fp = fopen( fname , mode ) ;//wb
    00406     if(fp==NULL) return false;
    00407     //fwrite( data , size  , 1 , fp ) ;
    00408     dkcFWriteAll(data,size,fp);
    00409     //fwrite( data , sizeof(size)  , 1 , fp ) ;
    00410     fclose( fp ) ;
    00411     return true;
    00412     //return dkcSaveBinary(data,size,fname);
    00413 }
    00414
    00422
    00423 inline bool LoadBinary(void *data,size_t size,const char *fname,const char *mode="rb" ){//="rb"
    00424     FILE *fp ;
    00425     fp = fopen( fname , mode ) ;//rb
    00426     if(fp==NULL)return false;
    00427     dkcFReadAll(data,size,fp);
    00428     //fread( data ,  size , 1 , fp ) ;
    00429     //fread( data , sizeof(size)  , 1 , fp ) ;
    00430     fclose( fp ) ;
    00431     return true;
    00432 }
    00433
    00434 inline bool SaveText(const char *text,size_t length,const char *fname,const char *mode="wt"){
    00435     return SaveBinary(text,length,fname,mode);
    00436 }
    00437 inline bool LoadText(char *text,size_t length,const char *fname,const char *mode="rt"){
    00438     return LoadBinary(text,length,fname,mode);
    00439 }
    00440
    00441
    00442
    00443 }//end of dkutil namespace
    00444
    00445
    00446 #else //for c lang
    00447
    00448
    00449
    00450
    00451 #endif //end of __cplusplus
    00452
    00453 #endif //end of include once