What's new

Memory Object (Signs of too much VB)

BGNG

New member
My current project has led me to implement a memory object-esque style of manipulation exactly the way things work in Windows GDI: where an object is created in the next available memory and a pointer to it is returned. The question is: how do I do this in C++?

Let's say that I have a data structure, "typedef struct Blah." I want to stash that in the next available piece of system memory, then return a handle to it. I also want to be able to clear it out of memory. The project I'm working on is a DLL that creates any number of such objects into memory for use by the parent application; which may need to use one or more Blah objects. Again, it's like Windows GDI: every time you call "CreateDC," you get a pointer to a new DC object.

Could someone give me some sample code of how to go about doing this?
 

smcd

Active member
Don't know if this is what you meant, but :
Code:
struct _foo{
int bar;
int bas;
};

// in C you'd need "struct _foo*" instead, C++ allows the below just fine
_foo* Getfoo()
{
    _foo *allocated;
    allocated = new _foo;
    return allocated;
}

Pretty sure that'd work, the caller would have to know about "struct _foo" and it's format though, like in the header file for the DLL you're making. Then you could say _foo* myfoo = Getfoo(); and don't forget to deallocate it when you're done.
 
OP
BGNG

BGNG

New member
Ah, so the "new" statemet does stick stuff in global system memory and not just program memory? If so, then I should be able to access the same "_foo" from different applications, right? For example, if a second program were to run, "2nd.exe," it would be able to access the "_foo" via the same pointer as the first program, say "1st.exe." Is this possible with the code you have provided?

And while I'm at it, I might as well ask about deallocation. That just uses the "dealloc" statement and structure pointer/size, right?

Other than that, thank you for your help.
 

smcd

Active member
hmm, i think new won't work properly didnt know you wanted to share it across program instances, you might want to try a windows function, GlobalAlloc, which has an accompanying GlobalFree. If those don't work, DLLs can have shared data segments across all programs that load it.
 

blight

New member
BGNG said:
Ah, so the "new" statemet does stick stuff in global system memory and not just program memory? If so, then I should be able to access the same "_foo" from different applications, right? For example, if a second program were to run, "2nd.exe," it would be able to access the "_foo" via the same pointer as the first program, say "1st.exe." Is this possible with the code you have provided?

No, that won't work. Each (instance of an) application runs inside it's own virtual address space. If you want to share memory between multiple instances of an application you need support from the OS. For the win32 API you would call CreateSection() (to create a section of shared memory in the first instance) and for future instances of the app you OpenSection() the created section. Once you have a handle to the section you can map it into the address spaces of the different instances, which will effectively share the memory between the instances of the application. Just search for "shared memory"

But you are trying to write a DLL, aren't you? It will always run in the address space of the calling application. So you shouldnt need shared memory i think...
 
OP
BGNG

BGNG

New member
Well, the thing is that there might be two or more wholly different applications (not just instances of one application) running, and I want any and all possible instances of any number of programs to be able to access the same memory.
 

Doomulation

?????????????????????????
Is there not something about dlls only become loaded once? Like the whole "don't unload dlls" options I've seen at places...
I just wonder though... why do the apps need to share it? Isn't it simpler to have a local pointer in the apps and call the dll to aquire the object?
 
OP
BGNG

BGNG

New member
Like GDI, any given DC can be altered by different applications (ie. your application and OpenGL)... I just wanted to know how to do that because a project I'm working on MIGHT be able to do stuff that way.
 

gamefreaks

New member
Here is a memory object handler I have written for my personal libary. Feel free to use it in your own project.


Here's the header: (Create a .h file and include in project.)


Code:
#ifndef MEMBLOCK_H
#define MEMBLOCK_H

class CMemBlock
{
	public:
	CMemBlock::CMemBlock(void);
	CMemBlock::CMemBlock(DWORD size);
	CMemBlock::~CMemBlock(void);
	bool	Alloc(DWORD size);
	bool	ReSize(DWORD size);
	bool	Clear(void);
	bool	Free(void);
	DWORD	Len(void);
	void*	Ptr(void);
	
	private:
	DWORD	length;
	void*	ptr;
};

#endif //!MEMBLOCK_H


The code for implementation. (Create a .cpp file and add to project.)

Code:
//********************************************************************
//Description: Memory handling encapsulation
//********************************************************************

#include <stdlib.h>
#include <windows.h>
#include "MemBlock.h"


CMemBlock::CMemBlock()
{
	length	= 0;
	ptr		= NULL;
}

CMemBlock::CMemBlock(DWORD size)
{
	Alloc(size);
}

CMemBlock::~CMemBlock()
{
	if(ptr)
		free(ptr);

	length = 0;
}

bool CMemBlock::Alloc(DWORD size)
{
	ptr = malloc(size);

	if(!ptr)
		length = 0;
	else
		length = size;

	return (ptr != 0);
}

bool CMemBlock::Clear()
{
	if(!ptr)
		return false;

	::ZeroMemory(ptr,length);

	return true;
}

DWORD CMemBlock::Len()
{
	return length;
}

void* CMemBlock::Ptr()
{
	return ptr;
}

bool CMemBlock::ReSize(DWORD size)
{
	if(!ptr)
		return false;

	ptr = realloc(ptr,size);

	if(!ptr)
		length = 0;
	else
		length = size;

	return (ptr != 0);
}

bool CMemBlock::Free()
{
	if(ptr)
		free(ptr);

	ptr		= NULL;
	length	= 0;

	return true;
}


One of the best things about using a C++ class for memory is that if you use a destructor, you can free the memory if the variable goes out of focus. (Resolves memory leaks)

Hope this helps!
 

zilmar

Emulator Developer
Moderator
except for the reallocate

I would just do "std::auto_ptr<variable> blah(new variable);" same thing it will auto deconstruct when it goes out of scope, and the better part it is type safe.

gamefreaks said:
Here is a memory object handler I have written for my personal libary. Feel free to use it in your own project.


Here's the header: (Create a .h file and include in project.)


Code:
#ifndef MEMBLOCK_H
#define MEMBLOCK_H

class CMemBlock
{
	public:
	CMemBlock::CMemBlock(void);
	CMemBlock::CMemBlock(DWORD size);
	CMemBlock::~CMemBlock(void);
	bool	Alloc(DWORD size);
	bool	ReSize(DWORD size);
	bool	Clear(void);
	bool	Free(void);
	DWORD	Len(void);
	void*	Ptr(void);
	
	private:
	DWORD	length;
	void*	ptr;
};

#endif //!MEMBLOCK_H


The code for implementation. (Create a .cpp file and add to project.)

Code:
//********************************************************************
//Description: Memory handling encapsulation
//********************************************************************

#include <stdlib.h>
#include <windows.h>
#include "MemBlock.h"


CMemBlock::CMemBlock()
{
	length	= 0;
	ptr		= NULL;
}

CMemBlock::CMemBlock(DWORD size)
{
	Alloc(size);
}

CMemBlock::~CMemBlock()
{
	if(ptr)
		free(ptr);

	length = 0;
}

bool CMemBlock::Alloc(DWORD size)
{
	ptr = malloc(size);

	if(!ptr)
		length = 0;
	else
		length = size;

	return (ptr != 0);
}

bool CMemBlock::Clear()
{
	if(!ptr)
		return false;

	::ZeroMemory(ptr,length);

	return true;
}

DWORD CMemBlock::Len()
{
	return length;
}

void* CMemBlock::Ptr()
{
	return ptr;
}

bool CMemBlock::ReSize(DWORD size)
{
	if(!ptr)
		return false;

	ptr = realloc(ptr,size);

	if(!ptr)
		length = 0;
	else
		length = size;

	return (ptr != 0);
}

bool CMemBlock::Free()
{
	if(ptr)
		free(ptr);

	ptr		= NULL;
	length	= 0;

	return true;
}


One of the best things about using a C++ class for memory is that if you use a destructor, you can free the memory if the variable goes out of focus. (Resolves memory leaks)

Hope this helps!
 

Top