EDIT: I should clarify what I wrote earlier.
I ran some tests with a mockup of you code, and it worked exactly as intended, more or less. Whatever the problem is, it probably isn't in the InitMemory() function itself. This supports my conjecture that it is some kind of problem with the system environment which is causing you data segment to overlap with you stack.
In testing, I found another possible problem, one which is probably unrelated but nonntheless probably should be addressed.
In you current setup, is the MAXMEMORY constant set to 4294967296 (2[sup]32[/sup]) or to 4294967295 (2[sup]32[/sup] - 1)? Either one will cause a problem, but the former is more serious - a 32-bit long will overflow with that value. The result will be that the assignment
[tt]SystemTotalMemory = MAXMEMORY;[/tt]
SystemTotalMemory set to zero, not 2[sup]32[/sup].
The easiest solution is probably to use unsigned long long, a 64-bit type which gcc supports now (on a side note, there's no reason that Cnt has to be a long, in fact even an unsigned short will do nicely here). It may seem a waste of memory, but it's probably less so than the other possibilities.
The test code I used is
Code: Select all
#include <stdio.h>
/*********************************************************
/ VARIABLE DECLERATION
*********************************************************/
#define MAXMEMORY 4294967296 // 4 gigabytes
#define PAGESIZE 32768 // 32 kilobytes
#define MAXPAGENUMBER (MAXMEMORY / PAGESIZE)
#define MAXPAGEBLOCKS (MAXPAGENUMBER / 32)
static unsigned long MemoryMap[MAXPAGEBLOCKS]; //1bit/page
static unsigned long long SystemTotalMemory;
static unsigned long SystemTotalPageNumber;
static unsigned long CurrentFreePageNo;
// this function added for testing purposes
unsigned long long GetMemorySize()
{
return (unsigned long long) MAXMEMORY;
}
/*********************************************************
/ FUNCTION IMPLEMENTATIONS
*********************************************************/
/*********************************************************
/ Initialising memory
/ in : None
/ out : None
*********************************************************/
void InitMemory(void)
{
unsigned short Cnt;
SystemTotalMemory = GetMemorySize();
printf("%I64d\n", SystemTotalMemory);
getch();
if(SystemTotalMemory > (unsigned long long) MAXMEMORY) {
SystemTotalMemory = (unsigned long long) MAXMEMORY;
}
SystemTotalPageNumber = (unsigned long) (SystemTotalMemory / PAGESIZE);
printf("%lu\n", SystemTotalPageNumber);
getch();
for(Cnt = 0; Cnt < MAXPAGEBLOCKS; Cnt++) { //this loop never ends
MemoryMap[Cnt] = 0;
printf("%hu\n", Cnt);
}
CurrentFreePageNo = 0;
}
main()
{
InitMemory();
getch();
}