VirtualAlloc question ?
-
Assuming that I know my program will not need to dynamically allocate more than 5 pages of memory(5*4096),Can I use this function for my memory allocations ?This function gets a pointer,number of bytes requested and returns the address of memory(in p).If failed returns 0. The first time this function is called it reserves 5 pages of memory and saves its start address in "bm" variable.and commites one page of those pages."l" variable acts as a counter(accumulator) that ranges from 0 to 4096(one page size);I used this to to decide when to commite a new page.(when the size requested by the "size" parameter plus "l" exceeds 4096)and to return the next address to the caller(p=(char*)m+l)."m" variable is start address of the commited page.Memory is returned in the "p" parameter. In short this function commites 1 page ,returns requested addresses from that page until reaches the end of the page where it commites another page. According to my assumption can using this function cause trouble in my application or not. I tested this function for allocating int ,double,string data types and struct and this worked.I mean I could write to the returned address and read the written values.I would have used "npr" and "npc" variables to keep the count of reserved and comitted pages respectively but accoring to my assumption I found them useless. Please evaluate the function and tell me the probable problems that you think can occure when used.Or any suggestion ,experience you may have .Guide me (consider my assumption)
int mem(void* &p,int size) { static void * m=0; static void * bm=0; static int l=0; static int npr=5; static int npc=0; if(bm==0) { bm=m=VirtualAlloc(0,5*4096,MEM_RESERVE,PAGE_READWRITE); if(m==0) return 0; m=VirtualAlloc(m,4096,MEM_COMMIT,PAGE_READWRITE); } if(size+l>=4096) { if((m=VirtualAlloc((char*)m+4096,4096,MEM_COMMIT,PAGE_READWRITE))!=0) { l=0; l+=size; p=m; } else return 0; } else { p=(char*)m+l; l+=size; } }
-- modified at 17:26 Monday 30th January, 2006
-
Assuming that I know my program will not need to dynamically allocate more than 5 pages of memory(5*4096),Can I use this function for my memory allocations ?This function gets a pointer,number of bytes requested and returns the address of memory(in p).If failed returns 0. The first time this function is called it reserves 5 pages of memory and saves its start address in "bm" variable.and commites one page of those pages."l" variable acts as a counter(accumulator) that ranges from 0 to 4096(one page size);I used this to to decide when to commite a new page.(when the size requested by the "size" parameter plus "l" exceeds 4096)and to return the next address to the caller(p=(char*)m+l)."m" variable is start address of the commited page.Memory is returned in the "p" parameter. In short this function commites 1 page ,returns requested addresses from that page until reaches the end of the page where it commites another page. According to my assumption can using this function cause trouble in my application or not. I tested this function for allocating int ,double,string data types and struct and this worked.I mean I could write to the returned address and read the written values.I would have used "npr" and "npc" variables to keep the count of reserved and comitted pages respectively but accoring to my assumption I found them useless. Please evaluate the function and tell me the probable problems that you think can occure when used.Or any suggestion ,experience you may have .Guide me (consider my assumption)
int mem(void* &p,int size) { static void * m=0; static void * bm=0; static int l=0; static int npr=5; static int npc=0; if(bm==0) { bm=m=VirtualAlloc(0,5*4096,MEM_RESERVE,PAGE_READWRITE); if(m==0) return 0; m=VirtualAlloc(m,4096,MEM_COMMIT,PAGE_READWRITE); } if(size+l>=4096) { if((m=VirtualAlloc((char*)m+4096,4096,MEM_COMMIT,PAGE_READWRITE))!=0) { l=0; l+=size; p=m; } else return 0; } else { p=(char*)m+l; l+=size; } }
-- modified at 17:26 Monday 30th January, 2006
Why are you doing manual virtual memory management for such a small amount of memory? If having a statically-allocated buffer is the goal, just do
new BYTE[5*4096]
and use that. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ -
Assuming that I know my program will not need to dynamically allocate more than 5 pages of memory(5*4096),Can I use this function for my memory allocations ?This function gets a pointer,number of bytes requested and returns the address of memory(in p).If failed returns 0. The first time this function is called it reserves 5 pages of memory and saves its start address in "bm" variable.and commites one page of those pages."l" variable acts as a counter(accumulator) that ranges from 0 to 4096(one page size);I used this to to decide when to commite a new page.(when the size requested by the "size" parameter plus "l" exceeds 4096)and to return the next address to the caller(p=(char*)m+l)."m" variable is start address of the commited page.Memory is returned in the "p" parameter. In short this function commites 1 page ,returns requested addresses from that page until reaches the end of the page where it commites another page. According to my assumption can using this function cause trouble in my application or not. I tested this function for allocating int ,double,string data types and struct and this worked.I mean I could write to the returned address and read the written values.I would have used "npr" and "npc" variables to keep the count of reserved and comitted pages respectively but accoring to my assumption I found them useless. Please evaluate the function and tell me the probable problems that you think can occure when used.Or any suggestion ,experience you may have .Guide me (consider my assumption)
int mem(void* &p,int size) { static void * m=0; static void * bm=0; static int l=0; static int npr=5; static int npc=0; if(bm==0) { bm=m=VirtualAlloc(0,5*4096,MEM_RESERVE,PAGE_READWRITE); if(m==0) return 0; m=VirtualAlloc(m,4096,MEM_COMMIT,PAGE_READWRITE); } if(size+l>=4096) { if((m=VirtualAlloc((char*)m+4096,4096,MEM_COMMIT,PAGE_READWRITE))!=0) { l=0; l+=size; p=m; } else return 0; } else { p=(char*)m+l; l+=size; } }
-- modified at 17:26 Monday 30th January, 2006
I think this can be possible to use your allocated space, As Mike was saying in another message, I am also not finding this much useful. As If you have some use of this in your mine, please let us know. some points I found are : ** it'll never release memory, whenever you need memory you are just allocating(if not available then new page). If it started using same memory then it would become long task. ** If one have to use any dynamic variable like CArray, CMap etc. as used in MFC. Regards, Sumit K.:) Never consider anything impossible before trying to solve that..---Sumit Kapoor--- sumit_kapoor1980@hotmail.com
-
Why are you doing manual virtual memory management for such a small amount of memory? If having a statically-allocated buffer is the goal, just do
new BYTE[5*4096]
and use that. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQHi Mich You mentioned a nice point,I wouldnt have thought of that. Yes your right for small amount of memory its not a good way.But how about when I was to use larger blocks(for example: 100*4096).Does it work fine for that case ? Though your right but allocating such small memory dynamically can have an advantage; Pages are not allcoated in physical memory until they are really needed.(Reserved memory is not allocated in physical memory till commited). Thanks for your reply.:) -- modified at 18:01 Tuesday 31st January, 2006
-
I think this can be possible to use your allocated space, As Mike was saying in another message, I am also not finding this much useful. As If you have some use of this in your mine, please let us know. some points I found are : ** it'll never release memory, whenever you need memory you are just allocating(if not available then new page). If it started using same memory then it would become long task. ** If one have to use any dynamic variable like CArray, CMap etc. as used in MFC. Regards, Sumit K.:) Never consider anything impossible before trying to solve that..---Sumit Kapoor--- sumit_kapoor1980@hotmail.com
Hi, Releasing memory is easy since I have stored the start address of pages in "bm" variable.Calling "VirtualFree" will do it. About use of this : Why I started to use it has a story.When I was very beginner.I decided to write a parser which can evaluate mathematical expressions.I implemented that using linklist and stack data structures.I stored my postfix form of my expressions in nodes of a link list.I allocated memory using "new" operator in that.I made it.After that I decided to make an application that draw functions graph using that parser I made.I coded the program.It worked in "Debug" version of my program.But when I changed the build mode to release.The program didnt work and It caused "Access violation" error.That was when I thought that "new" operator didnt work well.Because my program worked in debug mode but... Thats why I became intrested to use my own memory management.This way I feel better.I want to know what I do .And to know Im doing it right...Story became long:-D Thanks -- modified at 18:22 Tuesday 31st January, 2006