@ -14,7 +14,7 @@
// Always want minimum headers
// Always want minimum headers
# ifndef WIN32_LEAN_AND_MEAN
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# endif
// Assume that for everywhere
// Assume that for everywhere
@ -42,7 +42,8 @@
typedef SSIZE_T ssize_t ;
typedef SSIZE_T ssize_t ;
// size_t printf formatting named in the manner of C99 standard formatting strings such as PRIu64
// size_t printf formatting named in the manner of C99 standard formatting
// strings such as PRIu64
// in fact, we could use that one
// in fact, we could use that one
# define ROCKSDB_PRIszt "Iu"
# define ROCKSDB_PRIszt "Iu"
@ -77,8 +78,7 @@ namespace rocksdb {
# define PREFETCH(addr, rw, locality)
# define PREFETCH(addr, rw, locality)
namespace port
namespace port {
{
// For use at db/file_indexer.h kLevelMaxIndex
// For use at db/file_indexer.h kLevelMaxIndex
const int kMaxInt32 = INT32_MAX ;
const int kMaxInt32 = INT32_MAX ;
@ -88,82 +88,67 @@ const bool kLittleEndian = true;
class CondVar ;
class CondVar ;
class Mutex
class Mutex {
{
public :
public :
/* implicit */ Mutex ( bool adaptive = false ) ;
/* implicit */ Mutex ( bool adaptive = false ) ;
~ Mutex ( ) ;
~ Mutex ( ) ;
void Lock ( ) ;
void Lock ( ) ;
void Unlock ( ) ;
void Unlock ( ) ;
// this will assert if the mutex is not locked
// this will assert if the mutex is not locked
// it does NOT verify that mutex is held by a calling thread
// it does NOT verify that mutex is held by a calling thread
void AssertHeld ( ) ;
void AssertHeld ( ) ;
std : : unique_lock < std : : mutex > & getLock ( ) {
std : : unique_lock < std : : mutex > & getLock ( ) { return lock ; }
return lock ;
}
private :
private :
friend class CondVar ;
friend class CondVar ;
std : : mutex m_mutex ;
std : : mutex m_mutex ;
std : : unique_lock < std : : mutex > lock ;
std : : unique_lock < std : : mutex > lock ;
# ifndef NDEBUG
# ifndef NDEBUG
bool locked_ ;
bool locked_ ;
# endif
# endif
// No copying
// No copying
Mutex ( const Mutex & ) ;
Mutex ( const Mutex & ) ;
void operator = ( const Mutex & ) ;
void operator = ( const Mutex & ) ;
} ;
} ;
class RWMutex
class RWMutex {
{
public :
public :
RWMutex ( ) { InitializeSRWLock ( & srwLock_ ) ; }
RWMutex ( ) {
InitializeSRWLock ( & srwLock_ ) ;
}
void ReadLock ( ) {
void ReadLock ( ) { AcquireSRWLockShared ( & srwLock_ ) ; }
AcquireSRWLockShared ( & srwLock_ ) ;
}
void WriteLock ( ) {
AcquireSRWLockExclusive ( & srwLock_ ) ;
}
void ReadUnlock ( ) {
void WriteLock ( ) { AcquireSRWLockExclusive ( & srwLock_ ) ; }
ReleaseSRWLockShared ( & srwLock_ ) ;
}
void WriteUnlock ( ) {
void ReadUnlock ( ) { ReleaseSRWLockShared ( & srwLock_ ) ; }
ReleaseSRWLockExclusive ( & srwLock_ ) ;
}
// Empty as in POSIX
void WriteUnlock ( ) { ReleaseSRWLockExclusive ( & srwLock_ ) ; }
void AssertHeld ( ) { }
private :
// Empty as in POSIX
void AssertHeld ( ) { }
private :
SRWLOCK srwLock_ ;
SRWLOCK srwLock_ ;
// No copying allowed
// No copying allowed
RWMutex ( const RWMutex & ) ;
RWMutex ( const RWMutex & ) ;
void operator = ( const RWMutex & ) ;
void operator = ( const RWMutex & ) ;
} ;
} ;
class CondVar
class CondVar {
{
public :
public :
explicit CondVar ( Mutex * mu ) ;
explicit CondVar ( Mutex * mu ) ;
~ CondVar ( ) ;
~ CondVar ( ) ;
void Wait ( ) ;
void Wait ( ) ;
bool TimedWait ( uint64_t expiration_time ) ;
bool TimedWait ( uint64_t expiration_time ) ;
void Signal ( ) ;
void Signal ( ) ;
void SignalAl l ( ) ;
void SignalAll ( ) ;
private :
private :
std : : condition_variable cv_ ;
std : : condition_variable cv_ ;
Mutex * mu_ ;
Mutex * mu_ ;
} ;
} ;
typedef std : : once_flag OnceType ;
typedef std : : once_flag OnceType ;
@ -171,16 +156,15 @@ typedef std::once_flag OnceType;
extern void InitOnce ( OnceType * once , void ( * initializer ) ( ) ) ;
extern void InitOnce ( OnceType * once , void ( * initializer ) ( ) ) ;
inline bool Snappy_Compress ( const CompressionOptions & opts , const char * input ,
inline bool Snappy_Compress ( const CompressionOptions & opts , const char * input ,
size_t length , : : std : : string * output )
size_t length , : : std : : string * output ) {
{
# ifdef SNAPPY
# ifdef SNAPPY
output - > resize ( snappy : : MaxCompressedLength ( length ) ) ;
output - > resize ( snappy : : MaxCompressedLength ( length ) ) ;
size_t outlen ;
size_t outlen ;
snappy : : RawCompress ( input , length , & ( * output ) [ 0 ] , & outlen ) ;
snappy : : RawCompress ( input , length , & ( * output ) [ 0 ] , & outlen ) ;
output - > resize ( outlen ) ;
output - > resize ( outlen ) ;
return true ;
return true ;
# endif
# endif
return false ;
return false ;
}
}
inline bool Snappy_GetUncompressedLength ( const char * input , size_t length ,
inline bool Snappy_GetUncompressedLength ( const char * input , size_t length ,
@ -192,8 +176,7 @@ inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
# endif
# endif
}
}
inline bool Snappy_Uncompress ( const char * input , size_t length ,
inline bool Snappy_Uncompress ( const char * input , size_t length , char * output ) {
char * output ) {
# ifdef SNAPPY
# ifdef SNAPPY
return snappy : : RawUncompress ( input , length , output ) ;
return snappy : : RawUncompress ( input , length , output ) ;
# else
# else
@ -223,14 +206,14 @@ inline bool Zlib_Compress(const CompressionOptions& opts, const char* input,
output - > resize ( length ) ;
output - > resize ( length ) ;
// Compress the input, and put compressed data in output.
// Compress the input, and put compressed data in output.
_stream . next_in = ( Bytef * ) input ;
_stream . next_in = ( Bytef * ) input ;
_stream . avail_in = length ;
_stream . avail_in = length ;
// Initialize the output size.
// Initialize the output size.
_stream . avail_out = length ;
_stream . avail_out = length ;
_stream . next_out = ( Bytef * ) & ( * output ) [ 0 ] ;
_stream . next_out = ( Bytef * ) & ( * output ) [ 0 ] ;
int old_sz = 0 , new_sz = 0 , new_sz_delta = 0 ;
int old_sz = 0 , new_sz = 0 , new_sz_delta = 0 ;
bool done = false ;
bool done = false ;
while ( ! done ) {
while ( ! done ) {
int st = deflate ( & _stream , Z_FINISH ) ;
int st = deflate ( & _stream , Z_FINISH ) ;
@ -246,7 +229,7 @@ inline bool Zlib_Compress(const CompressionOptions& opts, const char* input,
new_sz = output - > size ( ) + ( new_sz_delta < 10 ? 10 : new_sz_delta ) ;
new_sz = output - > size ( ) + ( new_sz_delta < 10 ? 10 : new_sz_delta ) ;
output - > resize ( new_sz ) ;
output - > resize ( new_sz ) ;
// Set more output.
// Set more output.
_stream . next_out = ( Bytef * ) & ( * output ) [ old_sz ] ;
_stream . next_out = ( Bytef * ) & ( * output ) [ old_sz ] ;
_stream . avail_out = new_sz - old_sz ;
_stream . avail_out = new_sz - old_sz ;
break ;
break ;
case Z_BUF_ERROR :
case Z_BUF_ERROR :
@ -264,7 +247,7 @@ inline bool Zlib_Compress(const CompressionOptions& opts, const char* input,
}
}
inline char * Zlib_Uncompress ( const char * input_data , size_t input_length ,
inline char * Zlib_Uncompress ( const char * input_data , size_t input_length ,
int * decompress_size , int windowBits = - 14 ) {
int * decompress_size , int windowBits = - 14 ) {
# ifdef ZLIB
# ifdef ZLIB
z_stream _stream ;
z_stream _stream ;
memset ( & _stream , 0 , sizeof ( z_stream ) ) ;
memset ( & _stream , 0 , sizeof ( z_stream ) ) ;
@ -272,13 +255,13 @@ inline char* Zlib_Uncompress(const char* input_data, size_t input_length,
// For raw inflate, the windowBits should be -8..-15.
// For raw inflate, the windowBits should be -8..-15.
// If windowBits is bigger than zero, it will use either zlib
// If windowBits is bigger than zero, it will use either zlib
// header or gzip header. Adding 32 to it will do automatic detection.
// header or gzip header. Adding 32 to it will do automatic detection.
int st = inflateInit2 ( & _stream ,
int st =
windowBits > 0 ? windowBits + 32 : windowBits ) ;
inflateInit2 ( & _stream , windowBits > 0 ? windowBits + 32 : windowBits ) ;
if ( st ! = Z_OK ) {
if ( st ! = Z_OK ) {
return nullptr ;
return nullptr ;
}
}
_stream . next_in = ( Bytef * ) input_data ;
_stream . next_in = ( Bytef * ) input_data ;
_stream . avail_in = input_length ;
_stream . avail_in = input_length ;
// Assume the decompressed data size will 5x of compressed size.
// Assume the decompressed data size will 5x of compressed size.
@ -286,14 +269,14 @@ inline char* Zlib_Uncompress(const char* input_data, size_t input_length,
char * output = new char [ output_len ] ;
char * output = new char [ output_len ] ;
int old_sz = output_len ;
int old_sz = output_len ;
_stream . next_out = ( Bytef * ) output ;
_stream . next_out = ( Bytef * ) output ;
_stream . avail_out = output_len ;
_stream . avail_out = output_len ;
char * tmp = nullptr ;
char * tmp = nullptr ;
int output_len_delta ;
int output_len_delta ;
bool done = false ;
bool done = false ;
//while(_stream.next_in != nullptr && _stream.avail_in != 0) {
// while(_stream.next_in != nullptr && _stream.avail_in != 0) {
while ( ! done ) {
while ( ! done ) {
int st = inflate ( & _stream , Z_SYNC_FLUSH ) ;
int st = inflate ( & _stream , Z_SYNC_FLUSH ) ;
switch ( st ) {
switch ( st ) {
@ -311,7 +294,7 @@ inline char* Zlib_Uncompress(const char* input_data, size_t input_length,
output = tmp ;
output = tmp ;
// Set more output.
// Set more output.
_stream . next_out = ( Bytef * ) ( output + old_sz ) ;
_stream . next_out = ( Bytef * ) ( output + old_sz ) ;
_stream . avail_out = output_len - old_sz ;
_stream . avail_out = output_len - old_sz ;
break ;
break ;
case Z_BUF_ERROR :
case Z_BUF_ERROR :
@ -349,15 +332,15 @@ inline bool BZip2_Compress(const CompressionOptions& opts, const char* input,
output - > resize ( length ) ;
output - > resize ( length ) ;
// Compress the input, and put compressed data in output.
// Compress the input, and put compressed data in output.
_stream . next_in = ( char * ) input ;
_stream . next_in = ( char * ) input ;
_stream . avail_in = length ;
_stream . avail_in = length ;
// Initialize the output size.
// Initialize the output size.
_stream . next_out = ( char * ) & ( * output ) [ 0 ] ;
_stream . next_out = ( char * ) & ( * output ) [ 0 ] ;
_stream . avail_out = length ;
_stream . avail_out = length ;
int old_sz = 0 , new_sz = 0 ;
int old_sz = 0 , new_sz = 0 ;
while ( _stream . next_in ! = nullptr & & _stream . avail_in ! = 0 ) {
while ( _stream . next_in ! = nullptr & & _stream . avail_in ! = 0 ) {
int st = BZ2_bzCompress ( & _stream , BZ_FINISH ) ;
int st = BZ2_bzCompress ( & _stream , BZ_FINISH ) ;
switch ( st ) {
switch ( st ) {
case BZ_STREAM_END :
case BZ_STREAM_END :
@ -369,7 +352,7 @@ inline bool BZip2_Compress(const CompressionOptions& opts, const char* input,
new_sz = ( int ) ( output - > size ( ) * 1.2 ) ;
new_sz = ( int ) ( output - > size ( ) * 1.2 ) ;
output - > resize ( new_sz ) ;
output - > resize ( new_sz ) ;
// Set more output.
// Set more output.
_stream . next_out = ( char * ) & ( * output ) [ old_sz ] ;
_stream . next_out = ( char * ) & ( * output ) [ old_sz ] ;
_stream . avail_out = new_sz - old_sz ;
_stream . avail_out = new_sz - old_sz ;
break ;
break ;
case BZ_SEQUENCE_ERROR :
case BZ_SEQUENCE_ERROR :
@ -397,7 +380,7 @@ inline char* BZip2_Uncompress(const char* input_data, size_t input_length,
return nullptr ;
return nullptr ;
}
}
_stream . next_in = ( char * ) input_data ;
_stream . next_in = ( char * ) input_data ;
_stream . avail_in = input_length ;
_stream . avail_in = input_length ;
// Assume the decompressed data size will be 5x of compressed size.
// Assume the decompressed data size will be 5x of compressed size.
@ -405,12 +388,12 @@ inline char* BZip2_Uncompress(const char* input_data, size_t input_length,
char * output = new char [ output_len ] ;
char * output = new char [ output_len ] ;
int old_sz = output_len ;
int old_sz = output_len ;
_stream . next_out = ( char * ) output ;
_stream . next_out = ( char * ) output ;
_stream . avail_out = output_len ;
_stream . avail_out = output_len ;
char * tmp = nullptr ;
char * tmp = nullptr ;
while ( _stream . next_in ! = nullptr & & _stream . avail_in ! = 0 ) {
while ( _stream . next_in ! = nullptr & & _stream . avail_in ! = 0 ) {
int st = BZ2_bzDecompress ( & _stream ) ;
int st = BZ2_bzDecompress ( & _stream ) ;
switch ( st ) {
switch ( st ) {
case BZ_STREAM_END :
case BZ_STREAM_END :
@ -425,7 +408,7 @@ inline char* BZip2_Uncompress(const char* input_data, size_t input_length,
output = tmp ;
output = tmp ;
// Set more output.
// Set more output.
_stream . next_out = ( char * ) ( output + old_sz ) ;
_stream . next_out = ( char * ) ( output + old_sz ) ;
_stream . avail_out = output_len - old_sz ;
_stream . avail_out = output_len - old_sz ;
break ;
break ;
default :
default :
@ -442,12 +425,12 @@ inline char* BZip2_Uncompress(const char* input_data, size_t input_length,
return nullptr ;
return nullptr ;
}
}
inline bool LZ4_Compress ( const CompressionOptions & opts , const char * input ,
inline bool LZ4_Compress ( const CompressionOptions & opts , const char * input ,
size_t length , : : std : : string * output ) {
size_t length , : : std : : string * output ) {
# ifdef LZ4
# ifdef LZ4
int compressBound = LZ4_compressBound ( length ) ;
int compressBound = LZ4_compressBound ( length ) ;
output - > resize ( 8 + compressBound ) ;
output - > resize ( 8 + compressBound ) ;
char * p = const_cast < char * > ( output - > c_str ( ) ) ;
char * p = const_cast < char * > ( output - > c_str ( ) ) ;
memcpy ( p , & length , sizeof ( length ) ) ;
memcpy ( p , & length , sizeof ( length ) ) ;
size_t outlen ;
size_t outlen ;
outlen = LZ4_compress_limitedOutput ( input , p + 8 , length , compressBound ) ;
outlen = LZ4_compress_limitedOutput ( input , p + 8 , length , compressBound ) ;
@ -468,7 +451,7 @@ inline char* LZ4_Uncompress(const char* input_data, size_t input_length,
}
}
int output_len ;
int output_len ;
memcpy ( & output_len , input_data , sizeof ( output_len ) ) ;
memcpy ( & output_len , input_data , sizeof ( output_len ) ) ;
char * output = new char [ output_len ] ;
char * output = new char [ output_len ] ;
* decompress_size = LZ4_decompress_safe_partial (
* decompress_size = LZ4_decompress_safe_partial (
input_data + 8 , output , input_length - 8 , output_len , output_len ) ;
input_data + 8 , output , input_length - 8 , output_len , output_len ) ;
if ( * decompress_size < 0 ) {
if ( * decompress_size < 0 ) {
@ -480,12 +463,12 @@ inline char* LZ4_Uncompress(const char* input_data, size_t input_length,
return nullptr ;
return nullptr ;
}
}
inline bool LZ4HC_Compress ( const CompressionOptions & opts , const char * input ,
inline bool LZ4HC_Compress ( const CompressionOptions & opts , const char * input ,
size_t length , : : std : : string * output ) {
size_t length , : : std : : string * output ) {
# ifdef LZ4
# ifdef LZ4
int compressBound = LZ4_compressBound ( length ) ;
int compressBound = LZ4_compressBound ( length ) ;
output - > resize ( 8 + compressBound ) ;
output - > resize ( 8 + compressBound ) ;
char * p = const_cast < char * > ( output - > c_str ( ) ) ;
char * p = const_cast < char * > ( output - > c_str ( ) ) ;
memcpy ( p , & length , sizeof ( length ) ) ;
memcpy ( p , & length , sizeof ( length ) ) ;
size_t outlen ;
size_t outlen ;
# ifdef LZ4_VERSION_MAJOR // they only started defining this since r113
# ifdef LZ4_VERSION_MAJOR // they only started defining this since r113
@ -515,47 +498,43 @@ inline bool LZ4HC_Compress(const CompressionOptions &opts, const char* input,
// For Thread Local Storage abstraction
// For Thread Local Storage abstraction
typedef DWORD pthread_key_t ;
typedef DWORD pthread_key_t ;
inline
inline int pthread_key_create ( pthread_key_t * key , void ( * destructor ) ( void * ) ) {
int pthread_key_create ( pthread_key_t * key , void ( * destructor ) ( void * ) ) {
// Not used
// Not used
( void ) destructor ;
( void ) destructor ;
pthread_key_t k = TlsAlloc ( ) ;
pthread_key_t k = TlsAlloc ( ) ;
if ( TLS_OUT_OF_INDEXES = = k ) {
if ( TLS_OUT_OF_INDEXES = = k ) {
return ENOMEM ;
return ENOMEM ;
}
}
* key = k ;
* key = k ;
return 0 ;
return 0 ;
}
}
inline
inline int pthread_key_delete ( pthread_key_t key ) {
int pthread_key_delete ( pthread_key_t key ) {
if ( ! TlsFree ( key ) ) {
if ( ! TlsFree ( key ) ) {
return EINVAL ;
return EINVAL ;
}
}
return 0 ;
return 0 ;
}
}
inline
inline int pthread_setspecific ( pthread_key_t key , const void * value ) {
int pthread_setspecific ( pthread_key_t key , const void * value ) {
if ( ! TlsSetValue ( key , const_cast < void * > ( value ) ) ) {
if ( ! TlsSetValue ( key , const_cast < void * > ( value ) ) ) {
return ENOMEM ;
return ENOMEM ;
}
}
return 0 ;
return 0 ;
}
}
inline
inline void * pthread_getspecific ( pthread_key_t key ) {
void * pthread_getspecific ( pthread_key_t key ) {
void * result = TlsGetValue ( key ) ;
void * result = TlsGetValue ( key ) ;
if ( ! result ) {
if ( ! result ) {
if ( GetLastError ( ) ! = ERROR_SUCCESS ) {
if ( GetLastError ( ) ! = ERROR_SUCCESS ) {
errno = EINVAL ;
errno = EINVAL ;
} else {
} else {
errno = NOERROR ;
errno = NOERROR ;
}
}
}
return result ;
}
return result ;
}
}
// UNIX equiv although errno numbers will be off
// UNIX equiv although errno numbers will be off
@ -563,7 +542,7 @@ void* pthread_getspecific(pthread_key_t key) {
// feel space with zeros in case the file is extended.
// feel space with zeros in case the file is extended.
int truncate ( const char * path , int64_t length ) ;
int truncate ( const char * path , int64_t length ) ;
} // namespace port
} // namespace port
using port : : pthread_key_t ;
using port : : pthread_key_t ;
using port : : pthread_key_create ;
using port : : pthread_key_create ;
@ -572,6 +551,6 @@ using port::pthread_setspecific;
using port : : pthread_getspecific ;
using port : : pthread_getspecific ;
using port : : truncate ;
using port : : truncate ;
} // namespace rocksdb
} // namespace rocksdb
# endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_
# endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_