问题描述
是否可以从HeapCreate()获取堆句柄,并将所有可用内存设置为某个值?
Is it possible to take a heap handle from HeapCreate() and set all available memory to a certain value?
我已经尝试按区域枚举堆设置它的方式,但我得到访问违反。
I've tried enumerating the heap by region and setting it that way, but I get access violations.
我基本上想设置我的堆中的内存为一个自定义值,我可以用于调试,通过HeapDestroy销毁。
I basically want to set the memory in my heap to a custom value that I can use for debugging, before it's ultimately destroyed via HeapDestroy.
推荐答案
简短的回答是不,你不能填充堆一个特定的值。
The short answer is "no, you can't fill the heap with a specific value".
您可以使用调试版本封装堆访问函数,例如
You could wrap your heap access functions with debug versions, for example
// In some header file
#define HeapAlloc(a, b, c) MyHeapAlloc(a, b, c, __FILE__, __LINE__)
#define HeapFree(a, b, c) MyHeapFree(a, b, c, __FILE__, __LINE__)
...
// In a separate source file:
#ifdef HeapAlloc
#undef HeapAlloc
#undef HeapFree
#endif
struct extra
{
extra *next;
size_t size;
const char *file;
int line;
};
extra* head;
LPVOID MyHeapAlloc(HANDLE heap, DWORD flags, SIZE_T size, const char *file, int line)
{
LPVOID res = HeapAlloc(heap, flags, size + sizeof(extra));
if (!res)
{
cout << "Allocation failed, called from " << file << ":" << line << endl;
return 0;
}
extra *p = reinterpret_cast<extra*>(res);
res = reinterpret_cast<void*>(&p[1]);
p->next = head;
p->size = size;
p->file = file;
p->line = line;
memset(res, 0xAA, size);
return res;
}
BOOL MyHeapFree(HANDLE heap, DWORD flags, LPVOID mem, const char *file, int line)
{
extra *p = reinterpret_cast<extra*>(mem);
p = reinterpret_cast<void*>(&p[-1]);
extra *q = head;
extra *prev = 0;
while(q)
{
if (reinterpret_cast<void*>(&q[1]) == mem)
{
break;
}
prev = q;
q = next;
}
if (!q)
{
cout << "Attempt to free memory that wasn't allocated from " << file << ":" << line << endl;
return false;
}
/* Unlink q - if prev = NULL then it's the first one, so just move head */
if (prev)
{
prev->next = q->next;
}
else
{
head = q->next;
}
memset(mem, 0xBB, q->size);
return HeapFree(heap, flags, reinterpret_cast<void *>(q);
}
我已经输入了所有的内容,所以可能有小的打字错误,但希望它显示一个处理内存分配的方法。在某些情况下,你可能需要填充
struct为16或32个字节的倍数,以确保以下数据的对齐。当然,可以随时转储 head
,查看分配的内容。
I have just typed all that in, so there may be minor typos, but hopefully it shows you a method of dealing with memory allocations. For some case, you may have to pad the extra
struct to be a multiple of 16 or 32 bytes to ensure alignment of the following data. You can of course, at any time, dump the linked list pointed to by head
, to see what is allocated.
这篇关于在自定义堆上设置内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!