00001 /* 00002 FALCON - The Falcon Programming Language. 00003 FILE: flc_itempage.h 00004 00005 Definition of the page that holds items. 00006 ------------------------------------------------------------------- 00007 Author: Giancarlo Niccolai 00008 Begin: lun ott 4 2004 00009 00010 ------------------------------------------------------------------- 00011 (C) Copyright 2004: the FALCON developers (see list in AUTHORS file) 00012 00013 See LICENSE file for licensing details. 00014 */ 00015 00025 #ifndef flc_flc_spage_H 00026 #define flc_flc_spage_H 00027 00028 #include <falcon/heap.h> 00029 #include <falcon/types.h> 00030 #include <falcon/llist.h> 00031 00032 namespace Falcon { 00033 00037 class MemPage:public LinkableElement 00038 { 00039 protected: 00040 uint16 m_lastFree; 00041 00042 public: 00043 00044 MemPage( MemPage *prev=0, MemPage *next=0 ): 00045 LinkableElement( prev, next ) 00046 {} 00047 00048 void *operator new( size_t ) throw() { return HeapMem::getPage(); } 00049 void *operator new( size_t, void *pos ) throw() { return pos; } 00050 void operator delete( void *page ) { HeapMem::freePage( page ); } 00051 }; 00052 00053 class MemPageList: public LinkedList< MemPage > {}; 00054 00064 template <class _T> 00065 class SegregatedPage: public MemPage 00066 { 00067 public: 00068 00069 SegregatedPage( SegregatedPage<_T> *prev=0, SegregatedPage<_T> *next=0 ): 00070 MemPage( prev, next ) 00071 { 00072 m_lastFree = sizeof( SegregatedPage<_T> ); 00073 } 00074 00075 void *nextItem() { 00076 if ( m_lastFree + sizeof(_T) > PAGE_SIZE ) 00077 return 0; 00078 void *data= (void *)( ((char *) this) + m_lastFree ); 00079 m_lastFree += sizeof(_T); 00080 return data; 00081 } 00082 00083 void backItem() { 00084 if ( m_lastFree > sizeof(SegregatedPage<_T>) ) 00085 m_lastFree -= sizeof( _T ); 00086 } 00087 00088 void resetPage() { m_lastFree = sizeof( SegregatedPage<_T> ); } 00089 00090 _T *currentItem() 00091 { 00092 if ( m_lastFree == sizeof( SegregatedPage<_T> ) ) 00093 return 0; 00094 return ((_T *) (((char *) this)+m_lastFree)) -1; 00095 } 00096 00097 _T *firstItem() { 00098 return (_T *) ((char*)this + sizeof( SegregatedPage<_T> ) ); 00099 } 00100 00101 _T *lastItem() { 00102 return ((_T *) ((char*)this + sizeof( SegregatedPage<_T> ))) + 00103 ( (PAGE_SIZE - sizeof( SegregatedPage<_T> )) / sizeof( _T) ); 00104 } 00105 }; 00106 00107 } 00108 00109 #endif 00110 00111 /* end of flc_spage.h */