mallocr.c implementation
Craig Edwards
craig@haenterprises.com.au
Wed Nov 17 09:28:00 GMT 2004
A while ago, I ported newlib to run on the XBOX, and to date it has been
awesome. However, when performing a few malloc boundary tests, I see
something that seems odd. Consider the following code:
int size = 1024*1024;
char *c = malloc(62 * size);
This allocates 62MB of memory just fine (the XBOX has 64MB physical RAM).
If I use 63, then it hangs (which I am investigating separately). So, I
know that I can successfully allocate up to at least 62MB. Now, if I
change the program to look like:
int size = 1024*1024;
for (int i = 0; i < 62; i++)
char *c = malloc(size);
I would have suspected that this should work too, but it only manages to
allocate eight buffers. Why such a disparity? I might have understood if
it could only get to 61 or 60, but only 8?
To compile newlib, I used:
./configure --target=i386-pc-xbox --prefix=$installDir --with-newlib
--without-headers
Where the i386-pc-xbox target is just a symbolic link to the normal Cygwin
win32 apps. The malloc routines that get linked in are
newlib-1.12.0/newlib/libc/stdlib/mallocr.c. Is there something I am
missing? Any theories or guidance would be much appreciated.
If it helps, I have appended my implementation of the VirtualXXX calls
that mallocr.c uses internally.
unsigned int NTAPI VirtualAlloc(void *lpAddress, unsigned long dwSize,
unsigned int flAllocationType, unsigned int flProtect)
{
NtAllocateVirtualMemory(&lpAddress, 0, &dwSize, flAllocationType,
flProtect);
return (unsigned int)lpAddress;
}
int NTAPI VirtualFree(void *lpAddress, unsigned long dwSize, unsigned int
dwFreeType)
{
return NtFreeVirtualMemory(lpAddress, &dwSize, dwFreeType);
}
unsigned int NTAPI VirtualQuery(const void *lpAddress,
PMEMORY_BASIC_INFORMATION lpBuffer, unsigned int dwLength)
{
return NtQueryVirtualMemory((HANDLE)lpAddress, lpBuffer);
}
int NTAPI LocalAlloc(unsigned int flags, unsigned int size)
{
return VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
}
int NTAPI LocalFree(int address)
{
return VirtualFree((void*)address, 0, 0);
}
--
Craig Edwards
More information about the Newlib
mailing list