/************************************************* * Allocation * *************************************************/ void* Pooling_Allocator::allocate(u32bit n) { const u32bit BITMAP_SIZE = Memory_Block::bitmap_size(); const u32bit BLOCK_SIZE = Memory_Block::block_size(); Mutex_Holder lock(mutex); if(n <= BITMAP_SIZE * BLOCK_SIZE) { const u32bit block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE; byte* mem = allocate_blocks(block_no); if(mem) return mem; get_more_core(PREF_SIZE); mem = allocate_blocks(block_no); if(mem) return mem; throw Memory_Exhaustion(); } void* new_buf = alloc_block(n); if(new_buf) return new_buf; throw Memory_Exhaustion(); }
/* * Decompress Input with Gzip */ void Gzip_Decompression::write(const byte input_arr[], size_t length) { if(length) no_writes = false; // non-const needed by gzip api :( Bytef* input = reinterpret_cast<Bytef*>(const_cast<byte*>(input_arr)); gzip->stream.next_in = input; gzip->stream.avail_in = length; while(gzip->stream.avail_in != 0) { gzip->stream.next_out = reinterpret_cast<Bytef*>(&buffer[0]); gzip->stream.avail_out = buffer.size(); int rc = inflate(&(gzip->stream), Z_SYNC_FLUSH); if(rc != Z_OK && rc != Z_STREAM_END) { clear(); if(rc == Z_DATA_ERROR) throw Decoding_Error("Gzip_Decompression: Data integrity error"); else if(rc == Z_NEED_DICT) throw Decoding_Error("Gzip_Decompression: Need preset dictionary"); else if(rc == Z_MEM_ERROR) throw Memory_Exhaustion(); else throw std::runtime_error("Gzip decompression: Unknown error"); } send(&buffer[0], buffer.size() - gzip->stream.avail_out); if(rc == Z_STREAM_END) { size_t read_from_block = length - gzip->stream.avail_in; clear(); gzip = new Gzip_Stream; if(inflateInit2(&(gzip->stream), (raw_deflate ? -15 : (MAX_WBITS + 32))) != Z_OK) { throw Memory_Exhaustion(); } gzip->stream.next_in = input + read_from_block; gzip->stream.avail_in = length - read_from_block; input += read_from_block; length -= read_from_block; } } }
/************************************************* * Allocate more memory for the pool * *************************************************/ void Pooling_Allocator::get_more_core(u32bit in_bytes) { const u32bit BITMAP_SIZE = Memory_Block::bitmap_size(); const u32bit BLOCK_SIZE = Memory_Block::block_size(); const u32bit TOTAL_BLOCK_SIZE = BLOCK_SIZE * BITMAP_SIZE; const u32bit in_blocks = round_up(in_bytes, BLOCK_SIZE) / TOTAL_BLOCK_SIZE; const u32bit to_allocate = in_blocks * TOTAL_BLOCK_SIZE; void* ptr = alloc_block(to_allocate); if(ptr == 0) throw Memory_Exhaustion(); allocated.push_back(std::make_pair(ptr, to_allocate)); for(u32bit j = 0; j != in_blocks; ++j) { byte* byte_ptr = static_cast<byte*>(ptr); blocks.push_back(Memory_Block(byte_ptr + j * TOTAL_BLOCK_SIZE)); } std::sort(blocks.begin(), blocks.end()); last_used = std::lower_bound(blocks.begin(), blocks.end(), Memory_Block(ptr)); }
/* * Start Decompressing with Gzip */ void Gzip_Decompression::start_msg() { clear(); gzip = new Gzip_Stream; if(inflateInit2(&(gzip->stream), (raw_deflate ? -15 : (MAX_WBITS + 32))) != Z_OK) throw Memory_Exhaustion(); }
/* * Start Compressing with Gzip */ void Gzip_Compression::start_msg() { clear(); gzip = new Gzip_Stream; int res = deflateInit2(&(gzip->stream), level, Z_DEFLATED, (raw_deflate ? -15 : (MAX_WBITS + 16)), 8, Z_DEFAULT_STRATEGY); if(res == Z_STREAM_ERROR) throw Invalid_Argument("Bad setting in deflateInit2"); else if(res != Z_OK) throw Memory_Exhaustion(); }