![](/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//**************************************************
*
* MemMan 3.0.0.0 beta
*
* Copyright (C) 2007 - 2008 by Len3d
* All rights reserved.
*
*************************************************/
![](/Images/OutliningIndicators/None.gif)
#ifndef __MEMMAN__
#define __MEMMAN__
![](/Images/OutliningIndicators/None.gif)
#include <malloc.h>
#include <vector>
#include <algorithm>
#include <iostream>
![](/Images/OutliningIndicators/None.gif)
#pragma pack(push,1)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) namespace mem {
![](/Images/OutliningIndicators/InBlock.gif)
#define mem_inline __forceinline
![](/Images/OutliningIndicators/InBlock.gif)
#define mem_pool_pad 16
#define mem_page_pad 48
#define mem_page_size ( 1 << 18 )
#define mem_align_size 16
#define mem_align __declspec( align( mem_align_size ) )
![](/Images/OutliningIndicators/InBlock.gif)
typedef unsigned int prior_type;
typedef unsigned long time_type;
typedef unsigned long size_type;
typedef unsigned char byte;
![](/Images/OutliningIndicators/InBlock.gif)
#define mem_max( a, b ) ( ( (a) > (b) ) ? (a) : (b) )
#define mem_min( a, b ) ( ( (a) < (b) ) ? (a) : (b) )
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) enum {
ENTRY_PRIORITY = 0,
};
![](/Images/OutliningIndicators/InBlock.gif)
extern time_type num_stream_out;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) class heap {
public:
mem_inline heap( size_type max_size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
allocated_size = 0;
available_size = max_size;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline ~heap()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void *alloc( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( size == 0 )
return NULL;
![](/Images/OutliningIndicators/InBlock.gif)
allocated_size += size;
available_size -= size;
![](/Images/OutliningIndicators/InBlock.gif)
return sys_alloc( size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void dealloc( void *ptr, size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( ptr && size != 0 )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
sys_dealloc( ptr );
![](/Images/OutliningIndicators/InBlock.gif)
allocated_size -= size;
available_size += size;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void *aligned_alloc( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( size == 0 )
return NULL;
![](/Images/OutliningIndicators/InBlock.gif)
allocated_size += size;
available_size -= size;
![](/Images/OutliningIndicators/InBlock.gif)
return sys_aligned_alloc( size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void aligned_dealloc( void *ptr, size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( ptr && size != 0 )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
sys_aligned_dealloc( ptr );
![](/Images/OutliningIndicators/InBlock.gif)
allocated_size -= size;
available_size += size;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline size_type available() const
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return available_size;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void print()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
std::cout << std::endl;
std::cout << "========== heap Statistics ==========" << std::endl;
std::cout << std::endl;
![](/Images/OutliningIndicators/InBlock.gif)
std::cout << std::endl;
std::cout << "=====================================" << std::endl;
std::cout << std::endl;
}
![](/Images/OutliningIndicators/InBlock.gif)
private:
mem_inline void *sys_alloc( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return malloc( size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void sys_dealloc( void *ptr )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
free( ptr );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void *sys_aligned_alloc( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return _aligned_malloc( size, mem_align_size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void sys_aligned_dealloc( void *ptr )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
_aligned_free( ptr );
}
![](/Images/OutliningIndicators/InBlock.gif)
private:
size_type allocated_size,
available_size;
};
template < typename ALLOC >
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) class pool {
private:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) class page {
public:
mem_inline page( size_type _size, page *old )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
base = get_base();
_next = old;
size = _size;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline byte *get_base()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return ( (byte *) this + sizeof(page) );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline size_type get_size()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return static_cast<size_type>( base - get_base() + size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline size_type available() const
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return size;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void *alloc( size_type _size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
byte *ptr = base;
base += _size;
size -= _size;
return ptr;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline page *next()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return _next;
}
![](/Images/OutliningIndicators/InBlock.gif)
private:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) mem_align union {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) struct {
byte *base;
page *_next;
size_type size;
};
byte pad[mem_pool_pad];
};
};
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) class chunk {
public:
mem_inline chunk( size_type _size, chunk *old )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
next = old;
size = _size;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline size_type available() const
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return size;
}
![](/Images/OutliningIndicators/InBlock.gif)
public:
chunk *next;
size_type size;
};
![](/Images/OutliningIndicators/InBlock.gif)
public:
mem_inline pool()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
pages = NULL;
chunks = NULL;
allocated = 0;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline ~pool()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
destory();
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void destory()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
page *p;
while( ( p = pages ) != NULL )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
pages = p->next();
al.dealloc( p, p->get_size() );
}
![](/Images/OutliningIndicators/InBlock.gif)
pages = NULL;
chunks = NULL;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline bool search_chunk( void * & ptr, size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( chunks && ( size <= chunks->available() ) )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ptr = (void *) chunks;
chunks = chunks->next;
![](/Images/OutliningIndicators/InBlock.gif)
return true;
}
else
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline bool search_page( void * & ptr, size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( pages && ( size <= pages->available() ) )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ptr = pages->alloc( size );
![](/Images/OutliningIndicators/InBlock.gif)
return true;
}
else
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void allocate_page( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
size_type asize = mem_max( size, mem_page_size );
![](/Images/OutliningIndicators/InBlock.gif)
pages = new (al.alloc( sizeof(page) + asize )) page( asize, pages );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void *alloc( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
void *ptr;
![](/Images/OutliningIndicators/InBlock.gif)
++ allocated;
![](/Images/OutliningIndicators/InBlock.gif)
if( search_chunk( ptr, size ) )
return ptr;
else if( search_page( ptr, size ) )
return ptr;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) else {
allocate_page( size );
if( search_page( ptr, size ) )
return ptr;
else
return NULL;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void dealloc( void *ptr, size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
-- allocated;
![](/Images/OutliningIndicators/InBlock.gif)
if( size >= sizeof(chunk) )
chunks = new (ptr) chunk( size, chunks );
if( allocated == 0 )
destory();
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void print()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
std::cout << std::endl;
std::cout << "========== pool Statistics ==========" << std::endl;
std::cout << std::endl;
![](/Images/OutliningIndicators/InBlock.gif)
std::cout << std::endl;
std::cout << "=====================================" << std::endl;
std::cout << std::endl;
}
![](/Images/OutliningIndicators/InBlock.gif)
private:
page *pages;
chunk *chunks;
size_type allocated;
ALLOC al;
};
![](/Images/OutliningIndicators/InBlock.gif)
template < typename ALLOC >
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) class cache {
public:
class node;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) class entry {
public:
mem_inline entry()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
priority = ENTRY_PRIORITY;
last_use_time = pl.get_current_time();
locked = false;
node_ptr = NULL;
}
mem_inline virtual ~entry()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void *alloc( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return pl.alloc( size, this );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void dealloc( void *ptr, size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
pl.dealloc( ptr, size, this );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void stream_begin()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
locked = true;
stream_in();
last_use_time = pl.get_current_time();
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void stream_end()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
locked = false;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline bool is_locked()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return locked;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline bool operator < ( const entry & right ) const
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( priority == right.priority )
return ( last_use_time < right.last_use_time );
else
return ( priority < right.priority );
}
![](/Images/OutliningIndicators/InBlock.gif)
public:
virtual void stream_in() = 0;
![](/Images/OutliningIndicators/InBlock.gif)
virtual size_type stream_size() = 0;
![](/Images/OutliningIndicators/InBlock.gif)
virtual void *stream_out() = 0;
![](/Images/OutliningIndicators/InBlock.gif)
public:
static cache< ALLOC > pl;
prior_type priority;
time_type last_use_time;
bool locked;
node *node_ptr;
};
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) class node {
public:
mem_inline node( entry *obj, node *old, node *g_old, node *phead, node *g_phead )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ptr = obj;
obj->node_ptr = this;
next = old;
prev = phead;
g_next = g_old;
g_prev = g_phead;
if( next )
next->prev = this;
if( prev )
prev->next = this;
if( g_next )
g_next->g_prev = this;
if( g_prev )
g_prev->g_next = this;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline node()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ptr = NULL;
next = NULL;
prev = NULL;
g_next = NULL;
g_prev = NULL;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline ~node()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
void *p = NULL;
![](/Images/OutliningIndicators/InBlock.gif)
stream_out( p );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline time_type get_last_use_time() const
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( ptr )
return ptr->last_use_time;
else
return 0;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline bool is_locked() const
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( ptr )
return ptr->is_locked();
else
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline size_type stream_size()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( ptr )
return ptr->stream_size();
else
return 0;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline bool stream_out( void * & p )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( ptr )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
p = ptr->stream_out();
![](/Images/OutliningIndicators/InBlock.gif)
ptr->node_ptr = NULL;
![](/Images/OutliningIndicators/InBlock.gif)
++ num_stream_out;
![](/Images/OutliningIndicators/InBlock.gif)
return true;
}
else
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void detach()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( prev )
prev->next = next;
if( next )
next->prev = prev;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void g_detach()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( g_prev )
g_prev->g_next = g_next;
if( g_next )
g_next->g_prev = g_prev;
}
![](/Images/OutliningIndicators/InBlock.gif)
public:
entry *ptr;
node *next, *prev;
node *g_next, *g_prev;
};
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) class page {
public:
mem_inline page( size_type _size, page *old )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
size = _size;
base = get_base();
next = old;
prev = NULL;
if( next )
next->prev = this;
num_nodes = 0;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline ~page()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
node *p = head.next;
![](/Images/OutliningIndicators/InBlock.gif)
while( p )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
head.next = p->next;
![](/Images/OutliningIndicators/InBlock.gif)
p->g_detach();
p->~node();
node_pool.dealloc( p, sizeof(node) );
![](/Images/OutliningIndicators/InBlock.gif)
p = head.next;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void recycle( page *old )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
node *p = head.next;
![](/Images/OutliningIndicators/InBlock.gif)
while( p )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
head.next = p->next;
![](/Images/OutliningIndicators/InBlock.gif)
p->g_detach();
p->~node();
node_pool.dealloc( p, sizeof(node) );
p = head.next;
}
![](/Images/OutliningIndicators/InBlock.gif)
head.next = NULL;
![](/Images/OutliningIndicators/InBlock.gif)
size = get_size();
base = get_base();
next = old;
prev = NULL;
if( next )
next->prev = this;
num_nodes = 0;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline byte *get_base()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return (byte *) this + sizeof(page);
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline size_type get_size()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return static_cast<size_type>( base - get_base() + size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline size_type available() const
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return size;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void *alloc( size_type req_size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
void *ptr = (void *) base;
![](/Images/OutliningIndicators/InBlock.gif)
base += req_size;
size -= req_size;
return ptr;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline time_type priority() const
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
time_type last_use_time = 0;
![](/Images/OutliningIndicators/InBlock.gif)
node *p = head.next;
![](/Images/OutliningIndicators/InBlock.gif)
while( p )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
last_use_time += p->get_last_use_time();
![](/Images/OutliningIndicators/InBlock.gif)
p = p->next;
}
![](/Images/OutliningIndicators/InBlock.gif)
if( num_nodes != 0 )
last_use_time /= static_cast<time_type>( num_nodes );
![](/Images/OutliningIndicators/InBlock.gif)
return last_use_time;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline bool operator < ( const page & right ) const
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return ( priority() < right.priority() );
}
![](/Images/OutliningIndicators/InBlock.gif)
public:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) mem_align union {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) struct {
byte *base;
page *next;
page *prev;
size_type size;
node head;
size_type num_nodes;
};
byte pad[mem_page_pad];
};
};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) class chunk {
public:
mem_inline chunk( size_type _size, chunk *old )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
size = _size;
next = old;
prev = NULL;
if( next )
next->prev = this;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline size_type available() const
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return size;
}
![](/Images/OutliningIndicators/InBlock.gif)
public:
chunk *next;
chunk *prev;
size_type size;
};
![](/Images/OutliningIndicators/InBlock.gif)
public:
mem_inline cache()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
current_time = 0;
pages = NULL;
chunks = NULL;
allocated = 0;
g_num_nodes = 0;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline ~cache()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
destory();
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline time_type get_current_time()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return ( ++ current_time );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void *alloc( size_type size, entry *obj )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
void *ptr = NULL;
![](/Images/OutliningIndicators/InBlock.gif)
++ allocated;
![](/Images/OutliningIndicators/InBlock.gif)
obj->node_ptr = NULL;
![](/Images/OutliningIndicators/InBlock.gif)
if( search_chunk( ptr, size ) )
return ptr;
else if( search_page( ptr, size, obj ) )
return ptr;
else if( (size + sizeof(page)) > al.available() )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( search_entry( ptr, size, obj ) )
return ptr;
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
recycle_page( size );
![](/Images/OutliningIndicators/InBlock.gif)
if( search_page( ptr, size, obj ) )
return ptr;
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
std::cout << "fatal: failed to allocate memory!" << std::endl;
return NULL;
}
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
allocate_page( size );
![](/Images/OutliningIndicators/InBlock.gif)
if( search_page( ptr, size, obj ) )
return ptr;
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
std::cout << "fatal: failed to allocate memory!" << std::endl;
return NULL;
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void dealloc( void *ptr, size_type size, entry *obj )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
-- allocated;
![](/Images/OutliningIndicators/InBlock.gif)
if( obj->node_ptr )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
obj->node_ptr->detach();
obj->node_ptr->g_detach();
obj->node_ptr->~node();
node_pool.dealloc( obj->node_ptr, sizeof(node) );
obj->node_ptr = NULL;
![](/Images/OutliningIndicators/InBlock.gif)
-- g_num_nodes;
}
![](/Images/OutliningIndicators/InBlock.gif)
if( size >= sizeof(chunk) )
chunks = new (ptr) chunk( size, chunks );
if( allocated == 0 )
destory();
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void print()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
std::cout << std::endl;
std::cout << "========== cache Statistics ==========" << std::endl;
std::cout << std::endl;
![](/Images/OutliningIndicators/InBlock.gif)
std::cout << std::endl;
std::cout << "======================================" << std::endl;
std::cout << std::endl;
}
![](/Images/OutliningIndicators/InBlock.gif)
private:
mem_inline void destory()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
page *p = pages;
while( p )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
pages = p->next;
p->~page();
al.dealloc( p, p->get_size() );
p = pages;
}
![](/Images/OutliningIndicators/InBlock.gif)
chunks = NULL;
![](/Images/OutliningIndicators/InBlock.gif)
g_num_nodes = 0;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline bool search_chunk( void * & ptr, size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
chunk *p = chunks;
while( p )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( size <= p->available() )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ptr = (void *) p;
if( p->prev )
p->prev->next = p->next;
if( p->next )
p->next->prev = p->prev;
return true;
}
![](/Images/OutliningIndicators/InBlock.gif)
p = p->next;
}
![](/Images/OutliningIndicators/InBlock.gif)
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline bool search_page( void * & ptr, size_type size, entry *obj )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
page *p = pages;
while( p )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( size <= p->available() )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ptr = p->alloc( size );
![](/Images/OutliningIndicators/InBlock.gif)
new (node_pool.alloc( sizeof(node) )) node( obj, p->head.next, g_head.g_next, &p->head, &g_head );
++ p->num_nodes;
++ g_num_nodes;
return true;
}
![](/Images/OutliningIndicators/InBlock.gif)
p = p->next;
}
![](/Images/OutliningIndicators/InBlock.gif)
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline bool search_entry( void * & ptr, size_type size, entry *obj )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
node *old = NULL;
node *p = g_head.g_next;
![](/Images/OutliningIndicators/InBlock.gif)
while( p )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( !p->is_locked() &&
p->stream_size() >= size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( old )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( *p->ptr < *old->ptr )
old = p;
}
else
old = p;
}
![](/Images/OutliningIndicators/InBlock.gif)
p = p->g_next;
}
![](/Images/OutliningIndicators/InBlock.gif)
if( old && old->stream_out( ptr ) )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
old->ptr = obj;
![](/Images/OutliningIndicators/InBlock.gif)
obj->node_ptr = old;
return true;
}
![](/Images/OutliningIndicators/InBlock.gif)
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void recycle_page( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( pages )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
page *p, *old;
![](/Images/OutliningIndicators/InBlock.gif)
old = pages;
p = pages->next;
![](/Images/OutliningIndicators/InBlock.gif)
while( p )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( *p < *old )
old = p;
![](/Images/OutliningIndicators/InBlock.gif)
p = p->next;
}
![](/Images/OutliningIndicators/InBlock.gif)
if( old != pages )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( old->prev )
old->prev->next = old->next;
if( old->next )
old->next->prev = old->prev;
![](/Images/OutliningIndicators/InBlock.gif)
old->recycle( pages );
pages = old;
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void allocate_page( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
size_type asize = mem_min( mem_max( sizeof(page) + size, mem_page_size ), al.available() );
![](/Images/OutliningIndicators/InBlock.gif)
pages = new (al.alloc( asize )) page( asize - sizeof(page), pages );
}
![](/Images/OutliningIndicators/InBlock.gif)
private:
time_type current_time;
time_type allocated;
ALLOC al;
node g_head;
size_type g_num_nodes;
page *pages;
chunk *chunks;
static pool< ALLOC > node_pool;
};
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) class man {
public:
mem_inline man()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_heap = NULL;
![](/Images/OutliningIndicators/InBlock.gif)
upl = NULL;
apl = NULL;
![](/Images/OutliningIndicators/InBlock.gif)
num_stream_out = 0;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline ~man()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( apl )
delete apl;
if( upl )
delete upl;
![](/Images/OutliningIndicators/InBlock.gif)
if( m_heap )
delete m_heap;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void begin( size_type heap_size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_heap = new heap( heap_size );
![](/Images/OutliningIndicators/InBlock.gif)
upl = new pool< allocator >;
apl = new pool< aligned_allocator >;
![](/Images/OutliningIndicators/InBlock.gif)
num_stream_out = 0;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void end()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
delete apl;
apl = NULL;
delete upl;
upl = NULL;
![](/Images/OutliningIndicators/InBlock.gif)
delete m_heap;
m_heap = NULL;
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void *alloc( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return upl->alloc( size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void dealloc( void *ptr, size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return upl->dealloc( ptr, size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void *aligned_alloc( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return apl->alloc( size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void aligned_dealloc( void *ptr, size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return apl->dealloc( ptr, size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void print()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
std::cout << std::endl;
std::cout << "********** MemMan Statistics **********" << std::endl;
std::cout << std::endl;
![](/Images/OutliningIndicators/InBlock.gif)
if( m_heap )
m_heap->print();
![](/Images/OutliningIndicators/InBlock.gif)
if( upl )
upl->print();
![](/Images/OutliningIndicators/InBlock.gif)
if( apl )
apl->print();
![](/Images/OutliningIndicators/InBlock.gif)
cache< mem::man::allocator >::entry::pl.print();
![](/Images/OutliningIndicators/InBlock.gif)
cache< mem::man::aligned_allocator >::entry::pl.print();
![](/Images/OutliningIndicators/InBlock.gif)
std::cout << std::endl;
std::cout << "number of stream_out: " << num_stream_out << std::endl;
std::cout << std::endl;
![](/Images/OutliningIndicators/InBlock.gif)
std::cout << std::endl;
std::cout << "***************************************" << std::endl;
std::cout << std::endl;
}
![](/Images/OutliningIndicators/InBlock.gif)
public:
static heap *m_heap;
![](/Images/OutliningIndicators/InBlock.gif)
public:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) class allocator {
public:
mem_inline void *alloc( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return m_heap->alloc( size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void dealloc( void *ptr, size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_heap->dealloc( ptr, size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline size_type available() const
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return m_heap->available();
}
};
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) class aligned_allocator {
public:
mem_inline void *alloc( size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return m_heap->aligned_alloc( size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline void dealloc( void *ptr, size_type size )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_heap->aligned_dealloc( ptr, size );
}
![](/Images/OutliningIndicators/InBlock.gif)
mem_inline size_type available() const
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return m_heap->available();
}
};
![](/Images/OutliningIndicators/InBlock.gif)
public:
pool< allocator > *upl;
pool< aligned_allocator > *apl;
};
![](/Images/OutliningIndicators/InBlock.gif)
typedef cache< man::allocator >::entry resource;
typedef cache< man::aligned_allocator >::entry aligned_resource;
![](/Images/OutliningIndicators/InBlock.gif)
} // namespace mem
![](/Images/OutliningIndicators/None.gif)
extern mem::man memman;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
#pragma pack(pop)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
#endif // __MEMMAN__
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//**************************************************
*
* MemMan 3.0.0.0 beta
*
* Copyright (C) 2007 - 2008 by Len3d
* All rights reserved.
*
*************************************************/
![](/Images/OutliningIndicators/None.gif)
#ifndef __MEMMAN_DEF__
#define __MEMMAN_DEF__
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
#include "memman3.h"
![](/Images/OutliningIndicators/None.gif)
mem::man memman;
mem::heap *mem::man::m_heap;
mem::pool< mem::man::allocator > mem::cache< mem::man::allocator >::node_pool;
mem::pool< mem::man::aligned_allocator > mem::cache< mem::man::aligned_allocator >::node_pool;
mem::time_type mem::num_stream_out;
mem::cache< mem::man::allocator > mem::cache< mem::man::allocator >::entry::pl;
mem::cache< mem::man::aligned_allocator > mem::cache< mem::man::aligned_allocator >::entry::pl;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
#endif // __MEMMAN_DEF__
|