Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Buffer Pool \ Object Pool

, 18 Jan 2003
An article on optimization of the use of dynamic memory.
#ifndef LINK_H
#define LINK_H
/*
SLINK - single link
*/

typedef struct _SLINK{
   _SLINK* _next;
} SLINK,*PSLINK;

///////////////////////////////////////////////////////////////////////////////////
/////////////////////////SINGLE////LINK////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

//initialize the link 
//void SLINK_Initialize(PSLINK)
#define SLINK_Initialize(_head)           ((_head)->_next = NULL)

//check if list is empty
//bool SLINK_IsEmpty(PSLINK)
#define SLINK_IsEmpty(_head)              ((_head)->_next == NULL)

//pop the first item in list  
//PSLINK SLINK_Pop(PSLINK)                                              
#define SLINK_Pop(_head)                  (_head)->_next;\
                                          (_head)->_next =  (_head)->_next->_next;
//push an item to the head of list    
//VOID SLINK_Push(PSLINK,PSLINK)                                            
#define SLINK_Push(_head,_link)           (_link)->_next =  (_head)->_next;\
                                          (_head)->_next =  (_link)


///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////DOUBLE///LINK/////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

/*
DLINK - double link
*/
typedef struct _DLINK{
   _DLINK* _prev;
   _DLINK* _next;
} DLINK,*PDLINK;

//initialize the link 
//void DLINK_Initialize(PDLINK)
#define DLINK_Initialize(_head)            ((_head)->_next = (_head)->_prev = (_head))

//check if list is empty
//bool DLINK_IsEmpty(PDLINK) 
#define DLINK_IsEmpty(_head)               ((_head)->_next == (_head))

//insert item after the _head item
//void DLINK_InsertNext(PDLINK,PDLINK)
#define DLINK_InsertNext(_head,_dlink)     (_dlink)->_next = (_head)->_next;\
                                           (_dlink)->_prev = (_head);\
                                           (_head)->_next->_prev = (_dlink);\
                                           (_head)->_next = (_dlink);
//insert item previous to the _head item         
//void DLINK_InsertPrev(PDLINK,PDLINK)                             
#define DLINK_InsertPrev(_head,_dlink)     (_dlink)->_prev = (_head)->_prev;\
                                           (_dlink)->_next = (_head);\
                                           (_head)->_prev->_next = (_dlink);\
                                           (_head)->_prev = (_dlink);
//remove the item from list
//void DLINK_Remove(PDLINK)
#define DLINK_Remove(_dlink)               (_dlink)->_prev->_next = (_dlink)->_next;\
                                           (_dlink)->_next->_prev = (_dlink)->_prev
//extract the item previous to _head
//PDLINK DLINK_ExtructPrev(PDLINK)
#define DLINK_ExtructPrev(_head)           (_head)->_prev;\
                                           DLINK_Remove((_head)->_prev)
//extract the item after the _head item
//PDLINK DLINK_ExtructNext(PDLINK)
#define DLINK_ExtructNext(_head)           (_head)->_next;\
                                           DLINK_Remove((_head)->_next)

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

//get the object of type "type" that contains the field "field" stating in address "address"
#ifndef CONTAINING_RECORD

#define CONTAINING_RECORD(address, type, field) ((type *)( \
                                                 (PCHAR)(address) - \
                                                 (ULONG_PTR)(&((type *)0)->field)))

#endif
#endif // ALINK_H

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Uri Twig
Web Developer
Israel Israel
4 years expirience coding C++ with MFC & STL and coding C for Windows XP/2K internals (Drivers).
 
I Love what I do.

For pastime activities:
Fun & Games

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 19 Jan 2003
Article Copyright 2003 by Uri Twig
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid