我想确定给定内存块中32 KB块的数量(始终为32KB的倍数),每个偏移量为4KB,8KB,16KB(始终为4KB的倍数)

For ex:
1.) Input: memory block size: 128KB
2.) Output: Overall 32KB chunks for every of 8KB :
             Total number of chunks : 12
               Start            End
    Chunk 1 :    0               32KB
    Chunk 2 :    8KB             40KB
    Chunk 3 :    16KB            48KB
    Chunk 4 :    32KB            64KB
    Chunk 5 :    38KB            70KB
    ..................................
    Chunk 12:    64Kb            128KB**


我的程序

void determine_32Kb_chunks ( UINT64 size, UINT32 offset )

{
   UINT32 num_32KB_blocks = size / 32KB;
   UINT32 num_offset_size_blocks = size /offset;

   // Is this a valid formula ?
   UINT32 total_numberof_32KB_chunks = num_32KB_blocks- num_offset_size_blocks;

}


手动计算和程序公式对于4KB,16KB等显示了不同的结果。有人可以帮忙吗?

最佳答案

您的示例很幸运,​​因为:

size / 32KB = 128/32 = 4
size /offset = 128/8 = 16
num_32KB_blocks- num_offset_size_blocks = 4-16 = -12


足够接近要求的结果12

但公式应为:

number_chunks = (block_size - chunk_size) / offset


在您的示例中:

(block_size - chunk_size) / offset = (128 - 32) / 8 = 12


并在代码中(假设32KB出于某种原因起作用):

UINT32 determine_32Kb_chunks ( UINT64 size, UINT32 offset )
{
   UINT32 total_numberof_32KB_chunks = (size - 32KB) / offset;
   return total_numberof_32KB_chunks;
}

10-05 21:09