 |
|
 |
how i to new a object array on memory pool and then delete it? thank u.
|
|
|
|
 |
|
 |
#include <assert.h>
#include <malloc.h>
template<typename T>
class CVIemPool
{
public:
static int m_dwObjSize;
void* operator new ( size_t z )
{
m_dwObjSize = z;
return malloc( z );
}
};
class I1 : public CVIemPool<I1> {
public:
char m_aGrowing[ 10 ];
};
class I2 : virtual public I1
{
public:
char m_aBulge[ 10 * 10 ];
};
template<typename T> int CVIemPool<T>::m_dwObjSize;
void main()
{
assert ( &(I2::m_dwObjSize) == &I1::m_dwObjSize );
I1* m1 = new I1;
assert( CVIemPool<I1>::m_dwObjSize == sizeof( I1 ) );
I2* m2 = new I2;
assert( CVIemPool<I1>::m_dwObjSize == sizeof( I2 ) );
assert ( sizeof( I2 ) == sizeof( I1 ) );
}
no any way, go any way.
modified on Tuesday, November 9, 2010 2:47 AM
|
|
|
|
 |
|
 |
님이 짠 코드를 사용할 경우가 발생할 지는 잘모르겠지만....현재는 이미지센서 관련 isp와 pattern recognition관련 작업 중입니다.
가까운 곳의 고수님을 알게되서 반갑습니다.
앞으로도 중생들을 위한 비법을 많이 알려주시기 바랍니다.
Nature tells us how to ask
|
|
|
|
 |
|
 |
Consider this case:
class CObj: public CVMemPool
{
...
};
class CGlobObj
{
CObj * obj;
CGlobObj()
{
obj = new CObj();
}
};
/// this will cause surprise around (CSLock) m_csLock inside the CVMemPool class
/// during "new CObj()" call.
CGlobObj bug;
int main()
{
...
}
When you run your app, initialization begins with 'CGlobObj bug', thus also calling new() on its 'obj' attribute. But call co CSLock() constructor where you are initializing the critical section comes later actually.
So we will probably need to add some static bool to the CVMemPool class, having its default value to false - so it will be initialized before
CGlobObj is initialized. Then, a call to CVMemPool::new() appears; that's the second place for change - you will need to put here a check
if CSLock constructor was called already. If not, assign m_csLock = CSLock(); and set the flag to true. Finally, in the CSLock constructor,
make another check of the flag, not to initialize critical section 2-times.
But overall - I am afraid of usage of this CVMemPool with such kind of static objects. With example above, you have to treat with late initialization of CBitSet as well - in the time, when the bits are already allocated, constructor makes "= NULL" -> memory leak.
modified on Saturday, September 27, 2008 5:35 PM
|
|
|
|
 |
|
 |
I have a MFC application with CFormView as the base view class. In this I am creating controls like textbox, button and labels dynamically. Controls are drawn using ->Create function.
When the screen closes I have also destroyed these controls using delete and I have also gone to the level of DestroyWindow of the base class and found that it is returning 1 (successful destroying of windows window).
What I observe is the memory consumed by my application is not coming down when I observe in the Windows task manager whereas the memory goes up to few MBs (2 to 3MBs) while drawing the screen and the same does not come down when I close the screen
Can some one tell me why is the memory not released to my OS from the memory pool of MFC.
Thanks in advance
|
|
|
|
 |
|
 |
I found a problem in the case of using your code in creating a ActiveX.You use lots of static vars,so if I do a Activex project,and this ActiveX control is loaded by IE(we know that there maybe one IE process but lots windows load my ActiveX),your code maybe have some abnormality behavior.
|
|
|
|
 |
|
 |
First of all, Thank you for good lesson.
I am beginner for programming.(for English too. ㅡㅡ;;)
I have two questions now.
1.
Why do you align with 4 bytes In 'CBitSet::sCalcUsedBytes'?
Maybe I think that it is Optimization for Speed.
then Do you have the other reasons?
2.
I looked at the VMemPool Header. But I didn't understand your naming concept For '~size'.
For example, below
static DWORD ms_dwFreeQueueSize;
static DWORD ms_dwTotalSize;
static DWORD ms_dwPoolSize; // Total Pool object counter
static DWORD ms_dwObjSize; // one object size (bytes)
static DWORD ms_dwAllocObjCount; // current allocated counts
size,counter,counts...
What is different with them?
Thanks a lot. Have you nice day!
Let's conquer the world with my power.
-- modified at 23:11 Thursday 12th January, 2006
|
|
|
|
 |
|
 |
Aligning memory along natural machine word boundaries (for most CPUs, this is still 32 bits or 4 bytes) provides optimal performance. Many (if not all) CPUs generate fault interrupts for non-word-aligned access, and although CPUs in the x86 architecture trap these automatically to provide the reqeuested access, this takes more time than access on word-aligned addresses.
I couldn't answer the question regarding the specifics of the VMemPool header, but I have this one covered.
jbm!
|
|
|
|
 |
|
 |
When compiling the demo project with VS.NET 2003 the following errors occur!
Any ideas?
VMemPool_demo\VMemPool.h(328) : warning C4346: 'CVMemPool<objT,_dwPoolSizeT>::CSLock': dependent name is not a type
prefix with 'typename' to indicate a type
VMemPool_demo\VMemPool.h(328) : error C2143: syntax error : missing ';' before 'CVMemPool<objT,_dwPoolSizeT>::ms_csLock'
VMemPool_demo\VMemPool.h(328) : error C2501: 'CVMemPool<objT,_dwPoolSizeT>::CSLock': missing storage-class or type specifiers
I was using this class for several years in VS6 and it worked great!
Thank you Thilo Wawrzik
|
|
|
|
 |
|
|
 |
|
 |
I tested your code with VMemPool_demo project.
You used timeGetTime function in multimedia library.
But, it seemed that the function does not provide exact time.
How about using QueryPerformanceCounter function?
|
|
|
|
 |
|
 |
So good idea!
Can you show me the way for other guys?
Thanks
|
|
|
|
 |
|
 |
Hi Bro,
Great code indeed, but I have a question regarding it...
I was looking at the code and I was wandering if the bit set is absolutely necessary.
If I understand the code correctly the use of the bit set is to validate the memory blocks and to count the used blocks.
Are there any more critical used for the bit set or is it possible to remove it and use the data and the mem pool only?
Ron
|
|
|
|
 |
|
 |
Hi Ronma,
Thanks for regards.
in the first, sorry for late response. ( for busy .. )
Yes, bitset is used to manage allocation status for memory layout.
you can use it for graphical allocation table on server side helper things.
and in source code, bitset is used to check validate memory block.
for my job, it was important.
Thanks. and happy new year!!
Thank you all. No one know oneself all. My idea from you, book, all others. already we are shared brains.
|
|
|
|
 |
|
 |
First, thanks so much. This template was what I was looking for. I had a (poorly written) memory pool template that I wanted to revise but looking over CVMemPool, I like yours better. I did find a small bug: In the new operator, if you run out of memory (id from PopFree returns 0xFFFFFFFF), you return a NULL, which is fine. I think you forgot to unlock ms_csLock before the return, though. Also, I was going to make an enhancement for a special case I have. I was going to change: template <class objT,DWORD _dwPoolSizeT = 1000> to template <class objT,DWORD _dwPoolSizeT = 1000,DWORD _dwGrowBySizeT = 0> and set the GrowBy parameter to 500 for a class I have. This would allow the pool to grow if necessary, which in my case would happen very infrequently. I was curious if you had already given this some thought, and if so, what ideas you might have towards implementing this. Thanks, Paul
|
|
|
|
 |
|
 |
that's good idea, good job, paul
if you can share our enhanced code to me or others,
then you can send to me your vmclass for updating this article,
or redistribute your own vmclass with small my info
right now, i have no plan to enhance vmpool.
but i haved some ideas
- supports memory allocation table with GUI.
( for my project, server administrator may wanna see that )
etc.
i will enhance someday.
thanks you!!
Thank you all. No one know oneself all. My idea from you, book, all others. already we are shared brains.
|
|
|
|
 |
|
 |
First, really good article. I learned a lot.
I thought about adding a grow feature as well, but then I was scared away by the amount of work.
Maybe I'm misreading the code, but since the entire pool is allocated at once with VirtualAlloc and a NULL address parameter, doesn't that mean growing the pool would require an additional call to VirtualAlloc, and wouldn't that invalidate all current pointers to objects in the pool? Please correct me if I am mistaken. I haven't used VirtualAlloc before but I think I am understanding the docs correctly.
If so, a grow feature would require either the ability to store/index more than one pool block, or a different approach to the allocation like per object (meaning a bunch at one time, not literally one per request obviously). I haven't completely thought through how to make that work yet.
Is there any way with VirtualAlloc to have the existing block stay valid and simply add to it for the grow feature?
Thank you.
|
|
|
|
 |
|
 |
I started the "grow" feature but quickly abandoned it for the exact reason you highlighted.
I did do some coding with VirtualAlloc though. I used the CreateFileMapping and MapViewOfFile to get a map handle and a pointer to a very large chunk of memory respectively. The pointer from MapViewOfFile was passed into the VirtualAlloc funtions. This may not be necessary for your app as the primary reason for this was the memory was a shared access area for different apps. However, I used the PAGE_READWRITE | SEC_RESERVE flags on the CreateFileMapping. The system, as I understood it, reserved the large space in memory but it was not available (yet) for me to use and also the system did not consider it used (yet). Subsequent calls to VirtualAlloc used the MEM_COMMIT flag as I needed chunks of this space for application use. I believe you can use the MEM_RESERVE flag with VirtualAlloc instead of the map approach.
Perhaps this method could be used as an alternative to the "grow" feature....
Good luck,
Paul
|
|
|
|
 |
|
 |
to fix this small bug:
replace
static CSLock ms_csLock;
with
static boost::mutex m_csLock;
and the lock method is automatic as
boost::mutex::scoped_lock lock ( m_csLock );
no any way, go any way.
|
|
|
|
 |
|
 |
I like the idea of the class, specially the transparency to the user of the CObj class. However, I have some issues with the design of this class (or the way the article explains how to use the class):
1. The is-a relation for CObj and VMemPool fails, i.e. CObj is not a memory pool. For example, CEmployee is not a VMemPool.
2. The need for inheritance will most probably cause Multiple Inheritance issues, since the CEmployee will have to inherit from CPerson and VMemPool, where CPerson might also have inherited from VMemPool. Assuming that CPerson is not an abstract class.
3. To make use of this memory pool, you will need to change the code of the class being allocated. In some cases this is not desirable. For example, if your class is a container which is allocating 100000 instances of a class that is implemented in a 3rd party library. The design should be flexible enough to allow using this techique for the class under development or for a class that is used by the class under development.
My suggestion is to be able to use the class in this way:
// A sample where the memory pool is implemented as part of the class.
class CEmployee
{
// define new / delete to map to m_MemPool new / delete
// a macro can be defined to automatically do that
// #define USE_MEM_POOL(class,mempool)
// in this case USE_MEM_POOL(CEmployee, m_MemPool);
private:
static VMemPool m_MemPool;
}
// A sample where the memory pool is implemented as part of a collection
// class.
class CMyList
{
// in this case the list should use m_3rdPartyMemPoolA to new or
// delete instances of C3rdPartClassA. In this case it is not
// as transparent, but it is possible.
private:
VMemPool m_3rdPartyMemPoolA;
VMemPool m_3rdPartyMemPoolB;
}
These are just some thoughts that I wanted to share after reading the article and having a quick look at the code. I have to admit that I didn't get a chance yet to actually use the code.
http://webnotes.sf.net[^]
|
|
|
|
 |
|
 |
Look at the two posts I made about the Lea allocator and some research done at Texas U of something or another.
Basically, excluding very very very special instances, there is no point to using custom allocators. I was a bit shocked by the results of their tests too. Even with the bugs in this custom allocator, the Lea is faster and it is a malloc replacement.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
 |
|
 |
Hi Victor Boctor.
I can understand what you wanna say to me
and it's important thingy, and thanks !
In my side, I thought my library's client developers
want to use very easily. and they might doesn't know class design
pattern and more.
and I wrote in my article as 'I assumed the client would want a similar usage as new/delete'. so I deal with easiness. that's all.
Thank you all. No one know oneself all. My idea from you, book, all others. already we are shared brains.
|
|
|
|
 |
|
 |
I just tried this software. If I modify it so that after a VMemPoll object is allocated, the first byte of data is set, then the program crashes.
for( int i = 0 ; i < n; i++)
{
p[i] = new CVMObj;
p[i] ->data [0] = 0;
}
The reason this allocator seems to be fast is that no pages of memory are actually allocated.
I tried the Lea allocator and it was actually faster than this allocator and it actually allocated the memory.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
 |
|
|
 |
|
 |
Hi Tim Smith.
Thanks for your interesting.
but, I don't understand your practices.
I did test like you.
class CObj : public CVMemPool
{
public:
int data[4];
};
void CTestVMDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CObj* obj = new CObj;
obj->data[0] = 1;
obj->data[1] = 2;
}
but, I couldn't see any crash.. ( surely, I thought this simple test will be
ok ) and, actually upper code occur memory allocation in Virtual Memory page, not in Win32 process default heap. I'm puzzled when you said, it doesn't make any memory.. and I think that sounds fault.
anyway, thank your test and regards!
have a good day!!
Thank you all. No one know oneself all. My idea from you, book, all others. already we are shared brains.
|
|
|
|
 |