Uint32 ext2_write_file_by_inode (struct ext2_filesystem_context *context, struct ext2_directory_entry *file, void *buffer, Uint32 start, Uint32 nbytes) { Uint32 inode_num = file->inode_number; struct ext2_inode *fp = get_inode( context, inode_num ); Uint32 block_size = get_block_size( context->sb ); Uint32 indirect_block_size = block_size * block_size / sizeof(Uint32); // If we're going to write off of the end of a file, we need to // allocate a new block for the inode. if( fp->size < (nbytes+start) ) { // We can't have a gap in our file; don't start writing past // the end of the file. if( start > fp->size ) { return EXT2_WRITE_NON_CONTIGUOUS; } else { increase_inode_size ( context, fp, nbytes+start-(fp->size) ); } } // At this point, we know that we have enough blocks to store the data. Uint32 remaining_bytes = nbytes; // Skip ahead to the block that we need to write Uint32 first_inode_block = start / block_size; // Literal inode block indexes Uint32 indirect_inode_block_idx; Uint32 direct_inode_block_idx; if( first_inode_block >= EXT2_INODE_INDIRECT_BLOCK_IDX ) { direct_inode_block_idx = EXT2_INODE_INDIRECT_BLOCK_IDX; indirect_inode_block_idx = first_inode_block - EXT2_INODE_INDIRECT_BLOCK_IDX; } else { direct_inode_block_idx = first_inode_block; indirect_inode_block_idx = 0; } // The number of bytes into the first block that we need to skip Uint32 block_offset = start % block_size; for (Uint32 inode_block_idx = direct_inode_block_idx; inode_block_idx < EXT2_INODE_TOTAL_BLOCKS; inode_block_idx++) { Uint32 block_number = fp->blocks[inode_block_idx]; PRINT_VARIABLE( block_number ); Uint32 direct_inode_block_idx = first_inode_block % EXT2_INODE_TOTAL_BLOCKS; const char *data = (const char*) (block_offset + ((void*) block_number_to_address( context, block_number ))); // Pointer to an indirect block data area (can't be declared // inside of the `switch' statement. const void *indirect_block; #if DEBUG_FILESYSTEM PRINT_VARIABLE( block_offset ); serial_printf("inode_block_idx: %d\n\r", inode_block_idx ); PRINT_VARIABLE( indirect_inode_block_idx ); PRINT_VARIABLE( dub_indirect_inode_block_idx ); PRINT_VARIABLE( trip_indirect_inode_block_idx ); #endif // TODO: Support dub. indirect, trip. indirect blocks switch( inode_block_idx ) { case EXT2_INODE_TRIP_INDIRECT_BLOCK_IDX: case EXT2_INODE_DUB_INDIRECT_BLOCK_IDX: _kpanic( "ext2", __FILE__ ":" CPP_STRINGIFY_RESULT(__LINE__) " No support for double or tripple indirect blocks", FEATURE_UNIMPLEMENTED ); case EXT2_INODE_INDIRECT_BLOCK_IDX: // Dont' factor in the block offset when getting the // indirect data block. indirect_block = (const void*) (block_number_to_address( context, block_number )); // Don't let the inode block index advance unless // we've used all of the blocks in the indirect data // block. if( indirect_inode_block_idx < indirect_data_block_size(context) ) { --inode_block_idx; } data = (const void*) block_number_to_address( context, ((Uint32*) indirect_block)[indirect_inode_block_idx] ); data += block_offset; serial_printf("data: %x\n\r", data ); ++indirect_inode_block_idx; } if( remaining_bytes < block_size ) { _kmemcpy( data, buffer, remaining_bytes ); // We've copied everything we need to. Time to leave. remaining_bytes = 0; break; } else { _kmemcpy( data, buffer, block_size-block_offset ); buffer += (block_size-block_offset); // There's more to read remaining_bytes -= (block_size-block_offset); } // Reset the block offset -- we only use it once! block_offset = 0; } return nbytes-remaining_bytes; }
// Can't really test these other than that they compile and visually looking at output void TestDebugMacros() { TRACE("Some trace"); // Note that these macros do nothing in NDEBUG -- we should ensure that the variables get used anyway double use_vars = 0.0; unsigned my_var = 3141; PRINT_VARIABLE(my_var); use_vars += (double) my_var; double another_var = 2.81; PRINT_2_VARIABLES(my_var, another_var); use_vars += another_var; double cancer_curing_constant = 0.053450242435; PRINT_3_VARIABLES(my_var, another_var, cancer_curing_constant); use_vars += cancer_curing_constant; double heart_disease_ending_constant = -3e-141; PRINT_4_VARIABLES(my_var, another_var, cancer_curing_constant, heart_disease_ending_constant); use_vars += heart_disease_ending_constant; std::cout << "\n\n"; for (unsigned i=0; i<10; i++) { HOW_MANY_TIMES_HERE("inside for loop"); for (unsigned j=0; j<2; j++) { HOW_MANY_TIMES_HERE("nested loop"); } } for (unsigned j=0; j<10 /*change to 11 and it should quit*/; j++) { QUIT_AFTER_N_VISITS(11); } std::cout << "\n\n\n"; for (unsigned j=0; j<3; j++) { TRACE_FROM_NTH_VISIT("hello",2); } std::vector<double> vec(4); vec[0] = 0.0; vec[1] = 1.0; vec[2] = 2.7; vec[3] = 3.1; PRINT_VECTOR(vec); MARK; // Something like: "DEBUG: ./global/test/TestDebug.hpp at line 110" MARK_IN_ORDER; // Something like: "DEBUG: proc 0 ./global/test/TestDebug.hpp at line 111" etc MARK_ON_PROCESS(1); // In serial there is no output. In parallel, something like: "DEBUG: proc 1: ./global/test/TestDebug.hpp at line 112" // Check interaction with process isolation PetscTools::IsolateProcesses(); TRACE("Processes are isolated."); PetscTools::IsolateProcesses(false); AnotherFunctionOnTheStack(); MARK_MEMORY; // Should print that PRINT_MEMORY will now be relative to here PRINT_MEMORY; // Should be 0 Mb UNMARK_MEMORY; // Should print that PRINT_MEMORY will now be the absolute footprint PRINT_MEMORY; // Should be > 0 Mb MARK_MEMORY; std::vector<double> poinless_vector; poinless_vector.resize(100000); PRINT_MEMORY; // Should be x Mb poinless_vector.resize(200000); PRINT_MEMORY; // Should be at least 2x Mb }
// Can't really test these other than that they compile and visually looking at output void TestDebugMacros() { TRACE("Some trace"); // Note that these macros do nothing in NDEBUG -- we should ensure that the variables get used anyway double use_vars = 0.0; unsigned my_var = 3141; PRINT_VARIABLE(my_var); use_vars += (double) my_var; double another_var = 2.81; PRINT_2_VARIABLES(my_var, another_var); use_vars += another_var; double cancer_curing_constant = 0.053450242435; PRINT_3_VARIABLES(my_var, another_var, cancer_curing_constant); use_vars += cancer_curing_constant; double heart_disease_ending_constant = -3e-141; PRINT_4_VARIABLES(my_var, another_var, cancer_curing_constant, heart_disease_ending_constant); use_vars += heart_disease_ending_constant; std::cout << "\n\n"; for (unsigned i=0; i<10; i++) { HOW_MANY_TIMES_HERE("inside for loop"); for (unsigned j=0; j<2; j++) { HOW_MANY_TIMES_HERE("nested loop"); } } for (unsigned j=0; j<10 /*change to 11 and it should quit*/; j++) { QUIT_AFTER_N_VISITS(11); } std::cout << "\n\n\n"; for (unsigned j=0; j<3; j++) { TRACE_FROM_NTH_VISIT("hello",2); } std::vector<double> vec(4); vec[0] = 0.0; vec[1] = 1.0; vec[2] = 2.7; vec[3] = 3.1; PRINT_VECTOR(vec); MARK; // Something like: "DEBUG: ./global/test/TestDebug.hpp at line 104" MARK_IN_ORDER; // Something like: "DEBUG: proc 0 ./global/test/TestDebug.hpp at line 106" etc // Check interaction with process isolation PetscTools::IsolateProcesses(); TRACE("Processes are isolated."); PetscTools::IsolateProcesses(false); AnotherFunctionOnTheStack(); }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void MRCReader::printHeader(MRCHeader* h, std::ostream& out) { out << "MRC Header ----------------------------------" << std::endl; PRINT_VARIABLE (out, "nx:" , h->nx ) PRINT_VARIABLE (out, "ny:" , h->ny ) PRINT_VARIABLE (out, "nz:" , h->nz ) PRINT_VARIABLE (out, "mode:" , h->mode ) PRINT_VARIABLE (out, "nxstart:" , h->nxstart ) PRINT_VARIABLE (out, "nystart:" , h->nystart ) PRINT_VARIABLE (out, "nzstart:" , h->nzstart ) PRINT_VARIABLE (out, "mx:" , h->mx ) PRINT_VARIABLE (out, "my:" , h->my ) PRINT_VARIABLE (out, "mz:" , h->mz ) PRINT_VARIABLE (out, "xlen:" , h->xlen ) PRINT_VARIABLE (out, "ylen:" , h->ylen ) PRINT_VARIABLE (out, "zlen:" , h->zlen ) PRINT_VARIABLE (out, "alpha:" , h->alpha ) PRINT_VARIABLE (out, "beta:" , h->beta ) PRINT_VARIABLE (out, "gamma:" , h->gamma ) PRINT_VARIABLE (out, "mapc:" , h->mapc ) PRINT_VARIABLE (out, "mapr:" , h->mapr ) PRINT_VARIABLE (out, "maps" , h->maps ) PRINT_VARIABLE (out, "amin:" , h->amin ) PRINT_VARIABLE (out, "amax:" , h->amax ) PRINT_VARIABLE (out, "amean:" , h->amean ) PRINT_VARIABLE (out, "ispg:" , h->ispg ) PRINT_VARIABLE (out, "nsymbt:" , h->nsymbt ) PRINT_VARIABLE (out, "next:" , h->next ) PRINT_VARIABLE (out, "creatid:" , h->creatid ) PRINT_VARIABLE (out, "extra_data:" , h->extra_data ) // print hex PRINT_VARIABLE (out, "nreal:" , h->nreal ) PRINT_VARIABLE (out, "extra_data_2:" , h->extra_data_2 ) PRINT_VARIABLE (out, "imodStamp:" , h->imodStamp ) PRINT_VARIABLE (out, "imodFlags:" , h->imodFlags ) PRINT_VARIABLE (out, "idtype:" , h->idtype ) PRINT_VARIABLE (out, "lens:" , h->lens ) PRINT_VARIABLE (out, "nd1:" , h->nd1 ) PRINT_VARIABLE (out, "nd2:" , h->nd2 ) PRINT_VARIABLE (out, "vd1:" , h->vd1 ) PRINT_VARIABLE (out, "vd2:" , h->vd2 ) PRINT_VARIABLE (out, "tiltangles:" , h->tiltangles[0] << h->tiltangles[1] << h->tiltangles[2] << h->tiltangles[3] << h->tiltangles[4] << h->tiltangles[5] ) PRINT_VARIABLE (out, "xorg:" , h->xorg ) PRINT_VARIABLE (out, "yorg:" , h->yorg ) PRINT_VARIABLE (out, "zorg:" , h->zorg ) PRINT_VARIABLE (out, "cmap:" , h->cmap[0] << h->cmap[1] << h->cmap[2] << h->cmap[3] ) PRINT_VARIABLE (out, "stamp:" , h->stamp[0] << h->stamp[1] << h->stamp[2] << h->stamp[3] ) PRINT_VARIABLE (out, "rms:" , h->rms ) PRINT_VARIABLE (out, "nLabels" , h->nLabels ) for(int i = 0; i < h->nLabels; ++i) { PRINT_VARIABLE (out, " Label: " << i , h->labels[i] ) } if (h->feiHeaders != NULL) { char buf[FW + 1]; buf[FW] = 0; std::vector<std::string> strings; strings.push_back("a_tilt"); strings.push_back("b_tilt"); strings.push_back("x_stage"); strings.push_back("y_stage"); strings.push_back("z_stage"); strings.push_back("x_shift"); strings.push_back("y_shift"); strings.push_back("defocus"); strings.push_back("exp_time"); strings.push_back("mean_int"); strings.push_back("tiltaxis"); strings.push_back("pixelsize"); strings.push_back("magnification"); strings.push_back("voltage"); for(size_t i = 0; i < strings.size(); ++i) { ::memset(buf, 32, FW); snprintf(buf, FW, "%s", strings[i].c_str()); buf[strings[i].length()] = 32; out << buf << "\t"; } out << std::endl; // std::cout << "a_tilt \t b_tilt \t x_stage \t y_stage \t z_stage \t x_shift \t y_shift \t defocus \t exp_time \t mean_int \t tiltaxis \t pixelsize \t magnification \t voltage" << std::endl; FEIHeader* fei = NULL; for (int i = 0; i < h->nz; ++i) { fei = &(h->feiHeaders[i]); out.setf(std::ios::left); out << std::setw(FW) << fei->a_tilt << "\t" << std::setw(FW) << fei->b_tilt << "\t" << std::setw(FW) << fei->x_stage << "\t" << std::setw(FW) << fei->y_stage << "\t" << std::setw(FW) << fei->z_stage << "\t" << std::setw(FW) << fei->x_shift << "\t" << std::setw(FW) << fei->y_shift << "\t" << std::setw(FW) << fei->defocus << "\t" << std::setw(FW) << fei->exp_time << "\t" << std::setw(FW) << fei->mean_int << "\t" << std::setw(FW) << fei->tiltaxis << "\t" << std::setw(FW) << fei->pixelsize << "\t" << std::setw(FW) << fei->magnification << "\t" << std::setw(FW) << fei->voltage << "\t" << std::endl; } } out << "---------------------------------------" << std::endl; }