diff --git a/src/jrd/sort_mem.cpp b/src/jrd/sort_mem.cpp index 61b9a82525..03af59ebca 100644 --- a/src/jrd/sort_mem.cpp +++ b/src/jrd/sort_mem.cpp @@ -30,20 +30,17 @@ #include "../common/memory/allocators.h" #include "../jrd/sort_proto.h" #include "../jrd/gdsassert.h" - #include "../jrd/sort_mem.h" - #define BLOCK_SIZE_KEY "SORT_MEM_BLOCK_SIZE" #define UPPER_LIMIT_KEY "SORT_MEM_UPPER_LIMIT" - bool SortMem::is_initialized = false; -unsigned long SortMem::mem_block_size = 1048576; // 1MB -unsigned long SortMem::mem_upper_limit = (unsigned long) -1; // ~4GB +size_t SortMem::mem_block_size = 1048576; // 1MB +size_t SortMem::mem_upper_limit = (size_t) -1; // ~4GB -unsigned long SortMem::mem_total_size = 0; +size_t SortMem::mem_total_size = 0; /****************************************************************************** @@ -51,7 +48,7 @@ unsigned long SortMem::mem_total_size = 0; * Generic storage block implementation */ -SortMem::Block::Block(Block *tail, long length) +SortMem::Block::Block(Block *tail, size_t length) : next(0), size(length) { // Link block with the chain @@ -67,7 +64,7 @@ SortMem::Block::Block(Block *tail, long length) * Virtual memory block implementation */ -SortMem::MemoryBlock::MemoryBlock(Block* tail, long length) +SortMem::MemoryBlock::MemoryBlock(Block* tail, size_t length) : Block(tail, length) { // Allocate virtual memory block @@ -82,7 +79,7 @@ SortMem::MemoryBlock::~MemoryBlock() // MemoryPool::free_from_system(address); } -long SortMem::MemoryBlock::read(STATUS *status, long position, char *buffer, long length) +size_t SortMem::MemoryBlock::read(STATUS *status, size_t position, char *buffer, size_t length) { // Read block from memory if (position + length > size) @@ -93,7 +90,7 @@ long SortMem::MemoryBlock::read(STATUS *status, long position, char *buffer, lon return length; } -long SortMem::MemoryBlock::write(STATUS *status, long position, char *buffer, long length) +size_t SortMem::MemoryBlock::write(STATUS *status, size_t position, char *buffer, size_t length) { // Write block to memory if (position + length > size) @@ -109,7 +106,7 @@ long SortMem::MemoryBlock::write(STATUS *status, long position, char *buffer, lo * File block implementation */ -SortMem::FileBlock::FileBlock(Block *tail, long length, struct sfb *blk, long position) +SortMem::FileBlock::FileBlock(Block *tail, size_t length, struct sfb *blk, size_t position) : Block(tail, length), file(blk), offset(position) { } @@ -118,7 +115,7 @@ SortMem::FileBlock::~FileBlock() { } -long SortMem::FileBlock::read(STATUS *status, long position, char *buffer, long length) +size_t SortMem::FileBlock::read(STATUS *status, size_t position, char *buffer, size_t length) { // Read block from file if (position + length > size) @@ -131,7 +128,7 @@ long SortMem::FileBlock::read(STATUS *status, long position, char *buffer, long // return _read(file->sfb_file, buffer, length); } -long SortMem::FileBlock::write(STATUS *status, long position, char *buffer, long length) +size_t SortMem::FileBlock::write(STATUS *status, size_t position, char *buffer, size_t length) { // Write block to file if (position + length > size) @@ -149,7 +146,7 @@ long SortMem::FileBlock::write(STATUS *status, long position, char *buffer, long * Virtual scratch file implementation */ -SortMem::SortMem(struct sfb *blk, long size) +SortMem::SortMem(struct sfb *blk, size_t size) : internal(blk), logical_size(0), physical_size(0), file_size(0), head(0), tail(0) { // Override defaults with the configuration values, if they exist @@ -183,7 +180,7 @@ SortMem::~SortMem() mem_total_size -= physical_size - file_size; } -void SortMem::allocate(long size) +void SortMem::allocate(size_t size) { if (size > 0) { @@ -196,7 +193,7 @@ void SortMem::allocate(long size) Block *block; // Calculate how much virtual memory we should allocate - long smart_size = (mem_block_size > size) ? mem_block_size : size; + size_t smart_size = (mem_block_size > size) ? mem_block_size : size; // Check whether virtual memory should be allocated or file should be used instead if (mem_total_size + smart_size <= mem_upper_limit) @@ -253,7 +250,7 @@ void SortMem::allocate(long size) } } -SortMem::Block* SortMem::seek(long &position) +SortMem::Block* SortMem::seek(size_t &position) { Block *block = 0; @@ -286,21 +283,21 @@ SortMem::Block* SortMem::seek(long &position) return block; } -long SortMem::read(STATUS *status, long position, char *address, long length) +size_t SortMem::read(STATUS *status, size_t position, char *address, size_t length) { - long copied = 0; + size_t copied = 0; if (length > 0) { // Search for the first needed block - long pos = position; + size_t pos = position; Block *block = seek(pos); assert(block); // Read data from as many blocks as necessary for (Block *itr = block; itr, length > 0; itr = itr->next, pos = 0) { - long n = itr->read(status, pos, address, length); + size_t n = itr->read(status, pos, address, length); address += n; copied += n; length -= n; @@ -312,7 +309,7 @@ long SortMem::read(STATUS *status, long position, char *address, long length) return position + copied; } -long SortMem::write(STATUS *status, long position, char *address, long length) +size_t SortMem::write(STATUS *status, size_t position, char *address, size_t length) { // There's probably not enough space, try to allocate one more block if (position + length >= logical_size) @@ -320,19 +317,19 @@ long SortMem::write(STATUS *status, long position, char *address, long length) allocate(position + length - logical_size); } - long copied = 0; + size_t copied = 0; if (length > 0) { // Search for the first needed block - long pos = position; + size_t pos = position; Block *block = seek(pos); assert(block); // Write data to as many blocks as necessary for (Block *itr = block; itr, length > 0; itr = itr->next, pos = 0) { - long n = itr->write(status, pos, address, length); + size_t n = itr->write(status, pos, address, length); address += n; copied += n; length -= n; diff --git a/src/jrd/sort_mem.h b/src/jrd/sort_mem.h index c394622c5e..1be9d8df22 100644 --- a/src/jrd/sort_mem.h +++ b/src/jrd/sort_mem.h @@ -46,15 +46,15 @@ private: protected: Block *prev; Block *next; - long size; + size_t size; public: // Read bytes from the block into the given buffer - virtual long read(STATUS*, long, char*, long) = 0; + virtual size_t read(STATUS*, size_t, char*, size_t) = 0; // Write bytes from the given buffer into the block - virtual long write(STATUS*, long, char*, long) = 0; + virtual size_t write(STATUS*, size_t, char*, size_t) = 0; - Block(Block*, long); + Block(Block*, size_t); virtual ~Block() {} }; @@ -65,10 +65,10 @@ private: char* address; public: - long read(STATUS*, long, char*, long); - long write(STATUS*, long, char*, long); + size_t read(STATUS*, size_t, char*, size_t); + size_t write(STATUS*, size_t, char*, size_t); - MemoryBlock(Block*, long); + MemoryBlock(Block*, size_t); ~MemoryBlock(); }; @@ -78,13 +78,13 @@ private: // Sort file block struct sfb *file; // File offset - long offset; + size_t offset; public: - long read(STATUS*, long, char*, long); - long write(STATUS*, long, char*, long); + size_t read(STATUS*, size_t, char*, size_t); + size_t write(STATUS*, size_t, char*, size_t); - FileBlock(Block*, long, struct sfb*, long); + FileBlock(Block*, size_t, struct sfb*, size_t); ~FileBlock(); }; @@ -92,20 +92,20 @@ private: static bool is_initialized; // Virtual memory allocation values - static unsigned long mem_block_size; - static unsigned long mem_upper_limit; + static size_t mem_block_size; + static size_t mem_upper_limit; // Total amount of allocated virtual memory - static unsigned long mem_total_size; + static size_t mem_total_size; struct sfb *internal; // Virtual scratch file size - long logical_size; + size_t logical_size; // Amount of storage space allocated for this scratch file - long physical_size; + size_t physical_size; // File size on disk - long file_size; + size_t file_size; // First block in chain Block *head; @@ -113,17 +113,17 @@ private: Block *tail; // Allocate one more block, if necessary - void allocate(long); + void allocate(size_t); // Convert logical position to the physical one - pair [block, offset] - Block* seek(long&); + Block* seek(size_t&); public: // Read bytes from the scratch file - long read(STATUS*, long, char*, long); + size_t read(STATUS*, size_t, char*, size_t); // Write bytes into the scratch file - long write(STATUS*, long, char*, long); + size_t write(STATUS*, size_t, char*, size_t); - SortMem(struct sfb*, long); + SortMem(struct sfb*, size_t); ~SortMem(); };