Example/* HEAPWALK.C: This program "walks" the heap, starting * at the beginning (_pentry = NULL). It prints out each * heap entry's use, location, and size. It also prints * out information about the overall state of the heap as * soon as _heapwalk returns a value other than _HEAPOK. */#include <stdio.h> #include <malloc.h>void heapdump( void );void main( void ) { char *buffer; heapdump(); if( (buffer = malloc( 59 )) != NULL ) { heapdump(); free( buffer ); } heapdump(); }void heapdump( void ) { _HEAPINFO hinfo; int heapstatus; hinfo._pentry = NULL; while( ( heapstatus = _heapwalk( &hinfo ) ) == _HEAPOK ) { printf( "%6s block at %Fp of size %4.4X\n", ( hinfo._useflag == _USEDENTRY ? "USED" : "FREE" ), hinfo._pentry, hinfo._size ); } switch( heapstatus ) { case _HEAPEMPTY: printf( "OK - empty heap\n" ); break; case _HEAPEND: printf( "OK - end of heap\n" ); break; case _HEAPBADPTR: printf( "ERROR - bad pointer to heap\n" ); break; case _HEAPBADBEGIN: printf( "ERROR - bad start of heap\n" ); break; case _HEAPBADNODE: printf( "ERROR - bad node in heap\n" ); break; } } Output USED block at 002C0004 of size 0014 USED block at 002C001C of size 0054 USED block at 002C0074 of size 0024 USED block at 002C009C of size 0010 USED block at 002C00B0 of size 0018 USED block at 002C00CC of size 000C USED block at 002C00DC of size 001C USED block at 002C00FC of size 0010 USED block at 002C0110 of size 0014 USED block at 002C0128 of size 0010 USED block at 002C013C of size 0028 USED block at 002C0168 of size 0088 USED block at 002C01F4 of size 001C USED block at 002C0214 of size 0014 USED block at 002C022C of size 0010 USED block at 002C0240 of size 0014 USED block at 002C0258 of size 0010 USED block at 002C026C of size 000C USED block at 002C027C of size 0010 USED block at 002C0290 of size 0014 USED block at 002C02A8 of size 0010 USED block at 002C02BC of size 0010 USED block at 002C02D0 of size 1000 FREE block at 002C12D4 of size ED2C OK - end of heap
VirtualQuery()
一句话
看一下<<Windows核心编程>>
呵呵答和没答一样
不过它的第三或二版<<Windows高级编程>>翻译的好些
内存管理部分
假如要取数组VDATA的段地址和偏移的址,怎么取?
* at the beginning (_pentry = NULL). It prints out each
* heap entry's use, location, and size. It also prints
* out information about the overall state of the heap as
* soon as _heapwalk returns a value other than _HEAPOK.
*/#include <stdio.h>
#include <malloc.h>void heapdump( void );void main( void )
{
char *buffer; heapdump();
if( (buffer = malloc( 59 )) != NULL )
{
heapdump();
free( buffer );
}
heapdump();
}void heapdump( void )
{
_HEAPINFO hinfo;
int heapstatus;
hinfo._pentry = NULL;
while( ( heapstatus = _heapwalk( &hinfo ) ) == _HEAPOK )
{ printf( "%6s block at %Fp of size %4.4X\n",
( hinfo._useflag == _USEDENTRY ? "USED" : "FREE" ),
hinfo._pentry, hinfo._size );
} switch( heapstatus )
{
case _HEAPEMPTY:
printf( "OK - empty heap\n" );
break;
case _HEAPEND:
printf( "OK - end of heap\n" );
break;
case _HEAPBADPTR:
printf( "ERROR - bad pointer to heap\n" );
break;
case _HEAPBADBEGIN:
printf( "ERROR - bad start of heap\n" );
break;
case _HEAPBADNODE:
printf( "ERROR - bad node in heap\n" );
break;
}
}
Output USED block at 002C0004 of size 0014
USED block at 002C001C of size 0054
USED block at 002C0074 of size 0024
USED block at 002C009C of size 0010
USED block at 002C00B0 of size 0018
USED block at 002C00CC of size 000C
USED block at 002C00DC of size 001C
USED block at 002C00FC of size 0010
USED block at 002C0110 of size 0014
USED block at 002C0128 of size 0010
USED block at 002C013C of size 0028
USED block at 002C0168 of size 0088
USED block at 002C01F4 of size 001C
USED block at 002C0214 of size 0014
USED block at 002C022C of size 0010
USED block at 002C0240 of size 0014
USED block at 002C0258 of size 0010
USED block at 002C026C of size 000C
USED block at 002C027C of size 0010
USED block at 002C0290 of size 0014
USED block at 002C02A8 of size 0010
USED block at 002C02BC of size 0010
USED block at 002C02D0 of size 1000
FREE block at 002C12D4 of size ED2C
OK - end of heap
平面地址空间,地址是线性的。
你可以得到一个变量的地址,但是它是线性的虚拟地址
也可以把它转化为真实的物理地址,不过也不是段和偏移的概念
在dos下地址是用段和偏移的,ds存放段地址,ip是偏移地质。
到了windows年代,ds虽然还使用,但已经不是存放段地址的功能了
他代表一个段选择子,用来在页表目录中定位虚拟地址对应的物理地址。
FP_OFF():get a far address offset
FP_SEG():get a far address segment
源程序是:
.......
addr24=FP_SEG(wave);
addr24=addr24*16;
addr24 +=FP_OFF(wave); addr24=addr24>>1; /* even addr *///偶地址
.......
就像资本主义的某些概念,不能在社会主义制度里找到对应概念一样。
例如社会主义的“国家主席”,如果直译,就是“最终公仆,全体国民之仆人”。这样的概念就会让资本主义的人得到错误的信息,从而导致错误的行为。所以只能变通,把“国家主席”说成“总统”,尽管这两个概念是天壤之别,分别代表了社会的两个对立的极端。
所以你的问题也类似,不要想着翻译。
而应该把程序看懂,搞清楚它实现什么功能,然后写出实现同样功能的Windows程序就行。
当然,DOS程序里可能有一些操作,对Windows来说是没有意义的。
例如:DOS程序可以通过一种特殊手段进入到保护模式,从而获得更高的性能;而Windows本身就是运行在保护模式下的,这样的程序就无意义了,而且不能用了。
你贴出来的那一小段代码,我看着就像在为进保护模式作准备似的。
要不你把代码全贴出来,大家帮你参考。
源程序是:
.......
addr24=FP_SEG(wave);
addr24=addr24*16;
addr24 +=FP_OFF(wave); addr24=addr24>>1; /* even addr *///偶地址
.......等价的方法:
#define FP_SEG(x) (0)
#define FP_OFF(x) (x)
这样不知道行不行?
http://www.5xsoft.com/data/200109/1108240901.htm