wxWidgets/Win32: Helps when I include the necessary files (HeapAllocator.cpp / HeapAllocator.h)

git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1989 96395faa-99c1-11dd-bbfe-3dabce05a288
This commit is contained in:
Jake.Stine 2009-10-08 23:23:35 +00:00
parent 5cf69e328d
commit ca49f2edc0
2 changed files with 240 additions and 0 deletions

View File

@ -0,0 +1,164 @@
#pragma once
#include <limits>
extern void _allocateHeap_wxString();
extern void _destroyHeap_wxString();
extern void* _allocHeap_wxString( size_t size );
extern void _freeHeap_wxString( void* ptr );
extern void _allocateHeap_wxObject();
extern void _destroyHeap_wxObject();
extern void* _allocHeap_wxObject( size_t size );
extern void _freeHeap_wxObject( void* ptr );
template<typename T>
class wxStringAllocator
{
public :
// typedefs
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
public :
// convert an allocator<T> to allocator<U>
template<typename U>
struct rebind {
typedef wxStringAllocator<U> other;
};
public :
wxStringAllocator()
{
_allocateHeap_wxString();
}
~wxStringAllocator()
{
_destroyHeap_wxString();
}
wxStringAllocator(wxStringAllocator const&) {}
template<typename U>
explicit wxStringAllocator(wxStringAllocator<U> const&) {}
// address
pointer address(reference r) { return &r; }
const_pointer address(const_reference r) { return &r; }
// memory allocation
pointer allocate(size_type cnt,
typename std::allocator<void>::const_pointer = 0)
{
return reinterpret_cast<pointer>( _allocHeap_wxString(cnt * sizeof(T)) );
}
void deallocate(pointer p, size_type)
{
_freeHeap_wxString( p );
}
// size
size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof(T);
}
// construction/destruction
// standard placement-new syntax to initialize the object:
void construct(pointer p, const T& t) { new(p) T(t); }
// standard placement destructor:
void destroy(pointer p) { p->~T(); }
bool operator==(wxStringAllocator const&) { return true; }
bool operator!=(wxStringAllocator const& a) { return !operator==(a); }
}; // end of class Allocator
// --------------------------------------------------------------------------------------
//
// --------------------------------------------------------------------------------------
template<typename T>
class wxObjectAllocator
{
public :
// typedefs
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
public :
// convert an allocator<T> to allocator<U>
template<typename U>
struct rebind {
typedef wxObjectAllocator<U> other;
};
public :
wxObjectAllocator()
{
_allocateHeap_wxObject();
}
~wxObjectAllocator()
{
_destroyHeap_wxObject();
}
wxObjectAllocator(wxObjectAllocator const&) {}
template<typename U>
explicit wxObjectAllocator(wxObjectAllocator<U> const&) {}
// address
pointer address(reference r) { return &r; }
const_pointer address(const_reference r) { return &r; }
// memory allocation
pointer allocate(size_type cnt,
typename std::allocator<void>::const_pointer = 0)
{
return reinterpret_cast<pointer>( _allocHeap_wxObject(cnt * sizeof(T)) );
}
void deallocate(pointer p, size_type)
{
_freeHeap_wxObject( p );
}
// size
size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof(T);
}
// construction/destruction
// standard placement-new syntax to initialize the object:
void construct(pointer p, const T& t) { new(p) T(t); }
// standard placement destructor:
void destroy(pointer p) { p->~T(); }
bool operator==(wxObjectAllocator const&) { return true; }
bool operator!=(wxObjectAllocator const& a) { return !operator==(a); }
}; // end of class Allocator

View File

@ -0,0 +1,76 @@
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/string.h"
#include "wx/intl.h"
#include "wx/thread.h"
#endif
#include <ctype.h>
#ifndef __WXWINCE__
#include <errno.h>
#endif
#include <string.h>
#include <stdlib.h>
static HANDLE win32_string_heap = INVALID_HANDLE_VALUE;
static int win32_string_heap_refcount;
static HANDLE win32_object_heap = INVALID_HANDLE_VALUE;
static int win32_object_heap_refcount;
void _allocateHeap_wxString()
{
if( win32_string_heap == INVALID_HANDLE_VALUE )
{
//wxASSERT( win32_string_heap_refcount == 0 );
win32_string_heap = HeapCreate( HEAP_NO_SERIALIZE, 0x200000, 0 );
}
}
void _destroyHeap_wxString()
{
if( win32_string_heap == INVALID_HANDLE_VALUE ) return;
}
void* _allocHeap_wxString( size_t size )
{
return (void*)HeapAlloc( win32_string_heap, 0, size );
}
void _freeHeap_wxString( void* ptr )
{
HeapFree( win32_string_heap, 0, ptr );
}
void _allocateHeap_wxObject()
{
if( win32_object_heap == INVALID_HANDLE_VALUE )
{
//wxASSERT( win32_object_heap_refcount == 0 );
win32_object_heap = HeapCreate( HEAP_NO_SERIALIZE, 0x200000, 0 );
}
}
void _destroyHeap_wxObject()
{
if( win32_object_heap == INVALID_HANDLE_VALUE ) return;
}
void* _allocHeap_wxObject( size_t size )
{
return (void*)HeapAlloc( win32_object_heap, 0, size );
}
void _freeHeap_wxObject( void* ptr )
{
HeapFree( win32_object_heap, 0, ptr );
}