Skip to main content
gamedev.net
🔒 Locked

Managed memory in C++

Started by caesar4 Aug 8, 2006 at 6:49 PM 7 replies 1.8k views
Original Post
caesar4
caesar4
ok i'm starting work on a memory manager that i think should minimize fragmentation in my engine -my idea is to first allocate a table of 10000 or so handles (simple structures no more than 10 bytes each, not too big an overhead) -and a large shared memory pool, say 20 MB -then, when an object is to be newed or allocated, a chunk of that pool is set apart -whenever there are spare cpu cycles, an optimizer thread runs, defragmenting the pool -nonmanager code (NMC) rarely uses pointers directly, only handles, which are permanent pointers -however, when NMC needs to use a pointer, it tries to lock a handle (tells the defragmenter not to touch that memory chunk), and when NMC is done, unlocks that handle -this will all be done with elaborate use of templates and some algorithms ill need to devise now here comes the real question/problem to begin with, the memory pool pointer is not an absolute pointer, since only the OS works in absolute memory so my question is, how do i determine and align chunks of the shared pool to, say a 32 or 128 bit boundary?
Cartman's definition of sexual harrasement:"When you are trying to have intercourse with a lady friend, and some other guy comes up and tickles your balls from behind"(watch South Park, it rocks)
tendifo
tendifo
I don't get the point of having a memory manager. As far as I know, objects are rarely "fragmented" (if at all), and trying to implement your own allocation table is just crazy. If you're worried about memory management and having to deal with releasing pointers or what not, just used smart pointers.
caesar4
caesar4
memory objects themselves can't be fragmented, but memory itself can get very fragmented, with unusable memory between allocated chunks

and the way certain components work, those unusable memory chunks add up to quite a bit

plus, having such a manager will allow me to gather statistics on certain objects

and btw, my question wasnt on the practicality or feasability of my manager, but on alignment

how can i check and align a memory address to a 32 or 128 bit boundary?
Cartman's definition of sexual harrasement:"When you are trying to have intercourse with a lady friend, and some other guy comes up and tickles your balls from behind"(watch South Park, it rocks)
gumpy
gumpy
align (C++)

Quote:

Microsoft Specific

Use __declspec(align(#)) to precisely control the alignment of user-defined data (for example, static allocations or automatic data in a function).
This space for rent.
caesar4
caesar4
not exactly what i had in mind


heres basically what i wonna know

int *buff = (int *) malloc(size);int *ptr1 = buff+offset;//int *ptr1=buff+offset+alignoffset;


how can i check if ptr1 is 32 or 128 bit aligned?
how can i calculate alignment offset, such that ptr1 is 32 or 128 bit aligned?
Cartman's definition of sexual harrasement:"When you are trying to have intercourse with a lady friend, and some other guy comes up and tickles your balls from behind"(watch South Park, it rocks)
jkleinecke
jkleinecke
My memory manager uses these #defines to align memory.

#define FF_ALIGN(_v, _align)	(((size_t)(_v)+(_align)-1) & (~(_align)-1))#define FF_ALIGN_UP(_v, _align)	(FF_ALIGN((_v), (_align)) + (_align))#define FF_ALIGN_16(_v)			FF_ALIGN(_v,16)#define FF_ALIGN_32(_v)			FF_ALIGN(_v,32)#define FF_ALIGN_64(_v)			FF_ALIGN(_v,64)#define FF_ALIGN_128(_v)		FF_ALIGN(_v,128)


You should take a look at Cross Platform Game Programming. I based my memory manager on the one presented in there. It also introduces a few other really good tips for any software application, not just games.
-----------------------------------Indium Studios, Inc.
jkleinecke
jkleinecke
Oh, and one more thing... The memory manager also uses a header and footer to help pad all the memory. The final memory 'block' actually looks like this

BlockHeader
|
AlignmentPadding
|
BlockFooter
|
Data

It doesn't really make sense at first, but after you read the explanation in the book it works out.
-----------------------------------Indium Studios, Inc.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.