Page 1 of 1

Useful C++ Heap Source

Posted: Fri Jan 20, 2006 12:13 am
by Kevin McGuire
Here is a useful heap written in C++. To convert it to C, you should only have to append struct in frount of struct name, and type casts, and replace typdefs with defines.

C++ struct code
tmyStruct * myStruct
tmyStruct * myStruct = (tmyStruct*)...
typdef unsigned int MYTYPE
C struct code converted from C++
struct tmyStruct * myStruct
struct tmyStruct * myStruct = (struct tmyStruct*)..
#define MYTYPE unsigned int

This is a basic heap. It has no optimization function written for it, but one may appear on the link someday. It stores allocation information inside its self. Making it a nice and compact type.
http://mcguire.sytes.net:6080/hfmos/Sou ... ource.html
This is a slightly advanced heap used for shared data between threads or different processes. It supports garbage collection, and consolidation of free space. It however, uses the basic heap to store allocation information. So you need the basic one too when using this one.
http://mcguire.sytes.net:6080/hfmos/Sou ... ource.html

Note: The advanced heap uses the basic heap to seperate allocation information from data. To prevent heap corruption, however, when sharing data corruption is still possible.

The advanced heap also uses a different system for allocation, and deallocation. It takes a group ID, that you select or randomly generate for each allocation. Instead of calling the deallocation function with a pointer, you give it a group ID and it deallocates all allocations made using that ID previously.

Also, using the advanced heap. You can use a macro like this.

Code: Select all

RELEASE(myPointer);
This sets a secret byte behind all allocations to zero, so when the Optimize function is called it will deallocate these entries. This allows you to free individual allocations, instead of by group.

Code: Select all

LOCK(myPointer);
UNLOCK(myPointer);
This sets the secret byte, so that a RELEASE will not cause the garbage collector to free the pointer. UNLOCK reverses this. This is useful when the process that did the allocation wants to allow another process to free something, but is still somehow using something. So during this time a RELEASE can be called, then a UNLOCK, and the garbage collector takes care of it.

Also, instead of using the RELEASE, or you can use the CLAIM macros and time arguments. It would be better to setup the optimize function to be called on a interval, via a seperate thread or timer. This allows the allocation functions to use a time argument. Every time the optimize is called, its time argument has been incremented. Once a group has expired it is automaticly garbage collected unless entries have been CLAIMed. Each entry/allocation with-in the group can be calimed 252 times. This could be nice for threads sharing data, but could also be a disaster in waiting. It could be used for a single thread, when it only needs select items from the heap, but does not want to waste time RELEASEing them.
If a RELEASE is called using the time feature, it will over-ride the time and perform a deallocation on the next optimize run.

If those pages do not work, because you're browser is superannuated or non-orthodox you will just have to download the zipped source (with-out line numbers). You did not need those anyway. :)

This code is free to use. I do not reserve any rights to this material.

Re:Useful C++ Heap Source

Posted: Fri Jan 20, 2006 12:23 am
by Candy
kmcguire wrote: If those pages do not work, because you're browser is superannuated or non-orthodox you will just have to download the zipped source (with-out line numbers). You did not need those anyway. :)
Your server serves the files not as HTML web pages but as HTML downloadable files. IE just plain ignores it (which isn't correct) and therefore lots of people don't care. You should care. Just make your webserver comply with the standards and all browsers will have no problems accessing it.

Re:Useful C++ Heap Source

Posted: Fri Jan 20, 2006 12:32 am
by Phugoid
typedefs are legal in C and may be used when declaring structures to avoid having to insert struct before every reference to the type name: declare an anonymous structure type in a typedef (the typedef gives it a name).

Re:Useful C++ Heap Source

Posted: Fri Jan 20, 2006 12:53 am
by Kevin McGuire
Candy wrote: Your server serves the files not as HTML web pages but as HTML downloadable files. IE just plain ignores it (which isn't correct) and therefore lots of people don't care. You should care. Just make your webserver comply with the standards and all browsers will have no problems accessing it.
hehe, that was funny now that you point out it is my screw up. My IE and Opera do not have any problem. Thanks, Let me figure this out.

I appreciate you and pype for taking the time to tell me.

Re:Useful C++ Heap Source

Posted: Fri Jan 20, 2006 4:36 am
by Kevin McGuire
It is working now. If anyone has any problems with that heap code I would be happy to help, even if it is a really simple question. :)