📄 system.cpp
字号:
/**
@file System.cpp
@maintainer Morgan McGuire, matrix@graphics3d.com
Note: every routine must call init() first.
There are two kinds of detection used in this file. At compile
time, the _MSC_VER #define is used to determine whether x86 assembly
can be used at all. At runtime, processor detection is used to
determine if we can safely call the routines that use that assembly.
@cite Rob Wyatt http://www.gamasutra.com/features/wyatts_world/19990709/processor_detection_01.htm
@cite Benjamin Jurke http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-ProcessorDetectionClass&forum=cotd&id=-1
@cite Michael Herf http://www.stereopsis.com/memcpy.html
@created 2003-01-25
@edited 2006-05-17
*/
#include "G3D/platform.h"
#include "G3D/System.h"
#include "G3D/debug.h"
#include "G3D/format.h"
#ifdef G3D_WIN32
#include <conio.h>
#include <sys/timeb.h>
#include "G3D/RegistryUtil.h"
#elif defined(G3D_LINUX)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/select.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <pthread.h>
// #include <assert.h>
#elif defined(G3D_OSX)
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
#include <pthread.h>
#include <mach-o/arch.h>
#include <sstream>
#include <CoreServices/CoreServices.h>
#endif
#if defined(SSE)
#include <xmmintrin.h>
#endif
namespace G3D {
static char versionCstr[1024];
System::OutOfMemoryCallback System::outOfMemoryCallback = NULL;
void System::init() {
// Cannot use most G3D data structures or utility functions in here because
// they are not initialized.
static bool initialized = false;
if (initialized) {
return;
}
initialized = true;
if ((G3D_VER % 100) != 0) {
sprintf(versionCstr, "G3D %d.%02d beta %d",
G3D_VER / 10000,
(G3D_VER / 100) % 100,
G3D_VER % 100);
} else {
sprintf(versionCstr, "G3D %d.%02d",
G3D_VER / 10000,
(G3D_VER / 100) % 100);
}
}
void System::memcpy(void* dst, const void* src, size_t numBytes) {
::memcpy(dst, src, numBytes);
}
void System::memset(void* dst, uint8 value, size_t numBytes) {
::memset(dst, value, numBytes);
}
////////////////////////////////////////////////////////////////
class BufferPool {
public:
/** Only store buffers up to these sizes (in bytes) in each pool->
Different pools have different management strategies.
A large block is preallocated for tiny buffers; they are used with
tremendous frequency. Other buffers are allocated as demanded.
*/
enum {tinyBufferSize = 128, smallBufferSize = 1024, medBufferSize = 4096};
/**
Most buffers we're allowed to store.
64000 * 128 = 8 MB (preallocated)
1024 * 1024 = 1 MB (allocated on demand)
1024 * 4096 = 4 MB (allocated on demand)
*/
enum {maxTinyBuffers = 64000, maxSmallBuffers = 1024, maxMedBuffers = 1024};
private:
class MemBlock {
public:
void* ptr;
size_t bytes;
inline MemBlock() : ptr(NULL), bytes(0) {}
inline MemBlock(void* p, size_t b) : ptr(p), bytes(b) {}
};
MemBlock smallPool[maxSmallBuffers];
int smallPoolSize;
MemBlock medPool[maxMedBuffers];
int medPoolSize;
/** The tiny pool is a single block of storage into which all tiny
objects are allocated. This provides better locality for
small objects and avoids the search time, since all tiny
blocks are exactly the same size. */
void* tinyPool[maxTinyBuffers];
int tinyPoolSize;
/** Pointer to the data in the tiny pool */
void* tinyHeap;
# ifdef G3D_WIN32
CRITICAL_SECTION mutex;
# else
pthread_mutex_t mutex;
# endif
/** Provide synchronization between threads */
void lock() {
# ifdef G3D_WIN32
EnterCriticalSection(&mutex);
# else
pthread_mutex_lock(&mutex);
# endif
}
void unlock() {
# ifdef G3D_WIN32
LeaveCriticalSection(&mutex);
# else
pthread_mutex_unlock(&mutex);
# endif
}
/**
Malloc out of the tiny heap.
*/
inline void* tinyMalloc(size_t bytes) {
// Note that we ignore the actual byte size
// and create a constant size block.
(void)bytes;
debugAssert(tinyBufferSize >= bytes);
void* ptr = NULL;
if (tinyPoolSize > 0) {
--tinyPoolSize;
// Return the last one
ptr = tinyPool[tinyPoolSize];
}
return ptr;
}
/** Returns true if this is a pointer into the tiny heap. */
bool inTinyHeap(void* ptr) {
return (ptr >= tinyHeap) &&
(ptr < (uint8*)tinyHeap + maxTinyBuffers * tinyBufferSize);
}
void tinyFree(void* ptr) {
debugAssert(tinyPoolSize < maxTinyBuffers);
// Put the pointer back into the free list
tinyPool[tinyPoolSize] = ptr;
++tinyPoolSize;
}
void flushPool(MemBlock* pool, int& poolSize) {
for (int i = 0; i < poolSize; ++i) {
::free(pool->ptr);
pool->ptr = NULL;
pool->bytes = 0;
}
poolSize = 0;
}
/** Allocate out of a specific pool-> Return NULL if no suitable
memory was found.
*/
void* malloc(MemBlock* pool, int& poolSize, size_t bytes) {
// OPT: find the smallest block that satisfies the request.
// See if there's something we can use in the buffer pool->
// Search backwards since usually we'll re-use the last one.
for (int i = (int)poolSize - 1; i >= 0; --i) {
if (pool[i].bytes >= bytes) {
// We found a suitable entry in the pool->
// No need to offset the pointer; it is already offset
void* ptr = pool[i].ptr;
// Remove this element from the pool
--poolSize;
pool[i] = pool[poolSize];
return ptr;
}
}
return NULL;
}
public:
/** Count of memory allocations that have occurred. */
int totalMallocs;
int mallocsFromTinyPool;
int mallocsFromSmallPool;
int mallocsFromMedPool;
/** Amount of memory currently allocated (according to the application).
This does not count the memory still remaining in the buffer pool,
but does count extra memory required for rounding off to the size
of a buffer.
Primarily useful for detecting leaks.*/
// TODO: make me an atomic int!
int bytesAllocated;
BufferPool() {
totalMallocs = 0;
mallocsFromTinyPool = 0;
mallocsFromSmallPool = 0;
mallocsFromMedPool = 0;
bytesAllocated = true;
tinyPoolSize = 0;
tinyHeap = NULL;
smallPoolSize = 0;
medPoolSize = 0;
// Initialize the tiny heap as a bunch of pointers into one
// pre-allocated buffer.
tinyHeap = ::malloc(maxTinyBuffers * tinyBufferSize);
for (int i = 0; i < maxTinyBuffers; ++i) {
tinyPool[i] = (uint8*)tinyHeap + (tinyBufferSize * i);
}
tinyPoolSize = maxTinyBuffers;
# ifdef G3D_WIN32
InitializeCriticalSection(&mutex);
# else
pthread_mutex_init(&mutex, NULL);
# endif
}
~BufferPool() {
::free(tinyHeap);
# ifdef G3D_WIN32
DeleteCriticalSection(&mutex);
# else
// No destruction on pthreads
# endif
}
void* realloc(void* ptr, size_t bytes) {
if (ptr == NULL) {
return malloc(bytes);
}
if (inTinyHeap(ptr)) {
if (bytes <= tinyBufferSize) {
// The old pointer actually had enough space.
return ptr;
} else {
// Free the old pointer and malloc
void* newPtr = malloc(bytes);
System::memcpy(newPtr, ptr, tinyBufferSize);
tinyFree(ptr);
return newPtr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -