- Main Page
- Classes
- Files

- File List
- File Members

# flMemory.h

Go to the documentation of this file.

    00001 // Funclib Memory v1.0.0 (CORE)
    00002 //
    00003 // This module contains functions for memory management.
    00004 //
    00005 // Contributor(s): Flatmush, idea for memory allocation wrapper came from InsertWittyName.
    00006
    00007
    00008
    00009 #ifndef FLMEMORY_H
    00010 #define FLMEMORY_H
    00011
    00012 #include "flGlobal.h"
    00013 #include "flMemoryVMem.h"
    00014
    00015 #define MEMORY_UNCACHED_OFFSET 0x40000000
    00016
    00017 #define MEMORY_USER_BASE 0x08800000
    00018 #define MEMORY_USER_SIZE 0x01800000
    00019
    00020 #define MEMORY_KERNEL_BASE 0x08000000
    00021 #define MEMORY_KERNEL_SIZE 0x00800000
    00022
    00023 #define MEMORY_SCRATCHPAD_BASE 0x00010000
    00024 #define MEMORY_SCRATCHPAD_SIZE 0x00004000
    00025
    00026 #define MEMORY_PARTITION_KERNEL1_BASE 0x88000000
    00027 #define MEMORY_PARTITION_KERNEL1_SIZE 0x00300000
    00028 #define MEMORY_PARTITION_KERNEL2_BASE 0x88300000
    00029 #define MEMORY_PARTITION_KERNEL2_SIZE 0x00100000
    00030 #define MEMORY_PARTITION_KERNEL3_BASE 0x88400000
    00031 #define MEMORY_PARTITION_KERNEL3_SIZE 0x00400000
    00032
    00033 #if FL_MEMORY
    00034
    00035 #ifdef __cplusplus
    00036 extern "C" {
    00037 #endif
    00038
    00039 #if FL_MEMORY_ERRORBYTES
    00040 typedef struct {
    00041      void* memtData;
    00042      u32   memtSize;
    00043      u32   memtAlignment;
    00044      void* memtNext;
    00045 } MemoryTree;
    00046
    00047 extern MemoryTree* memErrorTree;
    00048 #endif
    00049
    00055 extern bool  memInitialized;
    00056
    00062 extern bool  memScratchpadUsed;
    00063
    00064
    00069 extern void  memInit();
    00070
    00075 extern void  memTerm();
    00076
    00077
    00078
    00085 extern u32   memFreeSpace(u32 inAccuracy);
    00086
    00092 extern u32   memFreeSpaceTotal();
    00093
    00102 extern char* memString(u32 inBytes, bool inBinary);
    00103
    00104
    00105
    00106 #if FL_DEBUG_CALLEDFROM
    00107 extern void  memSetByteFrom(void* inPtr, int inVal, unsigned int inSize, const char* inFile, const char* inFunc, int inLine);
    00108 extern void  memSetShortFrom(void* inPtr, int inVal, unsigned int inSize, const char* inFile, const char* inFunc, int inLine);
    00109 extern void  memClearFrom(void* inPtr, unsigned int inSize, const char* inFile, const char* inFunc, int inLine);
    00110 extern void  memCopyFrom(void* inDest, void* inSrc, unsigned int inSize, const char* inFile, const char* inFunc, int inLine);
    00111 extern int   memCompareFrom(void* inSrc0, void* inSrc1, unsigned int inSize, const char* inFile, const char* inFunc, int inLine);
    00112 #define memSetByte(inPtr, inVal, inSize) memSetByteFrom(inPtr, inVal, inSize, __FILE__, __FUNCTION__, __LINE__)
    00113 #define memSetShort(inPtr, inVal, inSize) memSetShortFrom(inPtr, inVal, inSize, __FILE__, __FUNCTION__, __LINE__)
    00114 #define memClear(inPtr, inSize) memClearFrom(inPtr, inSize, __FILE__, __FUNCTION__, __LINE__)
    00115 #define memCopy(inDest, inSrc, inSize) memCopyFrom(inDest, inSrc, inSize, __FILE__, __FUNCTION__, __LINE__)
    00116 #define memCompare(inSrc0, inSrc1, inSize) memCompareFrom(inSrc0, inSrc1, inSize, __FILE__, __FUNCTION__, __LINE__)
    00117 #else
    00118 extern void  memSetByte(void* inPtr, int inVal, unsigned int inSize);
    00119 extern void  memSetShort(void* inPtr, int inVal, unsigned int inSize);
    00120 extern void  memClear(void* inPtr, unsigned int inSize);
    00121 extern void  memCopy(void* inDest, void* inSrc, unsigned int inSize);
    00122 extern int   memCompare(void* inSrc0, void* inSrc1, unsigned int inSize);
    00123 #endif
    00124
    00125
    00126
    00127 #if FL_DEBUG_CALLEDFROM
    00128
    00137 extern void* memAllocFrom(int inSize, const char* inFile, const char* inFunc, int inLine);
    00138 extern void* memCallocFrom(int inSize0, int inSize1, const char* inFile, const char* inFunc, int inLine);
    00139 extern void* memQallocFrom(int inSize, const char* inFile, const char* inFunc, int inLine);
    00140 extern void* memAllocUncachedFrom(int inSize, const char* inFile, const char* inFunc, int inLine);
    00141 extern void* memReallocFrom(void* inData, int inSize, const char* inFile, const char* inFunc, int inLine);
    00142 extern void* memAlignFrom(int inBoundry, int inSize, const char* inFile, const char* inFunc, int inLine);
    00143 extern void  memFreeFrom(void* inData, const char* inFile, const char* inFunc, int inLine);
    00144 extern void* memUncachedPtrFrom(void* inPtr, const char* inFile, const char* inFunc, int inLine);
    00155 #define memAlloc(inSize) memAllocFrom(inSize, __FILE__, __FUNCTION__, __LINE__)
    00156 #define memCalloc(inSize0, inSize1) memCallocFrom(inSize0, inSize1, __FILE__, __FUNCTION__, __LINE__)
    00157 #define memQalloc(inSize) memQallocFrom(inSize, __FILE__, __FUNCTION__, __LINE__)
    00158 #define memAllocUncached(inSize) memAllocUncachedFrom(inSize, __FILE__, __FUNCTION__, __LINE__)
    00159 #define memRealloc(inData, inSize) memReallocFrom(inData, inSize, __FILE__, __FUNCTION__, __LINE__)
    00160 #define memAlign(inBoundry, inSize) memAlignFrom(inBoundry, inSize, __FILE__, __FUNCTION__, __LINE__)
    00161 #define memFree(inData) memFreeFrom(inData, __FILE__, __FUNCTION__, __LINE__)
    00162 #define memUncachedPtr(inPtr) memUncachedPtrFrom(inPtr, __FILE__, __FUNCTION__, __LINE__)
    00163 #else
    00164
    00172 extern void* memAlloc(int inSize);
    00173 extern void* memQalloc(int inSize);
    00174 extern void* memCalloc(int inSize0, int inSize1);
    00175 extern void* memAllocUncached(int inSize);
    00176 extern void* memRealloc(void* inData, int inSize);
    00177 extern void* memAlign(int inBoundry, int inSize);
    00178 extern void  memFree(void* inData);
    00179 extern void* memUncachedPtr(void* inPtr);
    00180 #endif
    00181
    00182
    00188 extern u32  memAddressAlignment(void* inPtr);
    00189
    00195 extern bool memAddressValid(void* inPtr);
    00196
    00203 extern bool memBlockValid(void* inPtr, u32 inSize);
    00204
    00205
    00206
    00207 #if FL_MEMORY_PAGE
    00208 extern unsigned int memToPage(void* inData, unsigned int inSize);
    00209 extern void*        memFromPage(unsigned int inPageID);
    00210 #endif
    00211
    00212
    00213
    00214 #if FL_MEMORY_ERRORBYTES
    00215 #if FL_DEBUG_CALLEDFROM
    00216 extern bool memErrorBytesCheckFrom(void* inPtr, const char* inFile, const char* inFunc, int inLine);
    00217 extern void memtAddFrom(MemoryTree** inMemTree, void* inPtr, u32 inSize, u32 inAlignment, const char* inFile, const char* inFunc, int inLine);
    00218 extern void memtDelFrom(MemoryTree** inMemTree, void* inPtr, const char* inFile, const char* inFunc, int inLine);
    00219 extern void memtErrorBytesCheckFrom(MemoryTree** inMemTree, const char* inFile, const char* inFunc, int inLine);
    00220 #define memErrorBytesCheck(inPtr) memErrorBytesCheckFrom(inPtr, __FILE__, __FUNCTION__, __LINE__)
    00221 #define memtAdd(inMemTree, inPtr, inSize, inAlignment) memtAddFrom(inMemTree, inPtr, inSize, inAlignment, __FILE__, __FUNCTION__, __LINE__)
    00222 #define memtDel(inMemTree, inPtr) memtDelFrom(inMemTree, inPtr, __FILE__, __FUNCTION__, __LINE__)
    00223 #define memtErrorBytesCheck(inMemTree) memtErrorBytesCheckFrom(inMemTree, __FILE__, __FUNCTION__, __LINE__)
    00224 #else
    00225 extern bool memErrorBytesCheck(void* inPtr);
    00226 extern void memtAdd(MemoryTree** inMemTree, void* inPtr, u32 inSize, u32 inAlignment);
    00227 extern void memtDel(MemoryTree** inMemTree, void* inPtr);
    00228 extern void memtErrorBytesCheck(MemoryTree** inMemTree);
    00229 #endif
    00230 #endif
    00231
    00232 #ifdef __cplusplus
    00233 }
    00234 #endif
    00235
    00236 #else
    00237
    00238 #include <malloc.h>
    00239 #define memAlloc(inSize) memalign(FL_MEMORY_ALIGNMENT_DEFAULT, (inSize))
    00240 #define memAlign(inBoundry, inSize) memalign(max(FL_MEMORY_ALIGNMENT_DEFAULT, (inBoundry)), (inSize))
    00241 #define memRealloc(inData, inSize) realloc((inData), (inSize))
    00242 #define memCalloc(inSize0, inSize1) memAlloc((inSize0) * (inSize1))
    00243 #define memQalloc(inSize) memAlloc(inSize)
    00244 #define memFree(inData) free(inData)
    00245 #define memCopy(inDest, inSrc, inSize) memmove((inDest), (inSrc), (inSize))
    00246 #define memClear(inPtr, inSize) memset(inPtr, 0x00, inSize)
    00247 #define memSetByte(inPtr, inVal, inSize) memset(inPtr, inVal, inSize)
    00248 #define memUncachedPtr(inPtr) ((void*)((unsigned int)(inPtr) | MEMORY_UNCACHED_OFFSET))
    00249
    00250 //#define memBlockValid(inPtr) ...TODO
    00251 //#define memBlockValid(inPtr, inSize) (memAddressValid(inPtr) && memAddressValid((void*)((u32)(inPtr) + (inSize))))
    00252
    00253 #endif
    00254
    00255 #define memCachedPtr(inPtr) ((void*)((unsigned int)(inPtr) & ~MEMORY_UNCACHED_OFFSET))
    00256 #define memIsUncachedPtr(inPtr) (((unsigned int)(inPtr) & MEMORY_UNCACHED_OFFSET) != 0)
    00257 #define memIsCachedPtr(inPtr) (((unsigned int)(inPtr) & MEMORY_UNCACHED_OFFSET) == 0)
    00258 #define memCachedPointer(inPtr) memCachedPtr(inPtr);
    00259 #define memUncachedPointer(inPtr) memUncachedPtr(inPtr);
    00260 #define memIsUncachedPointer(inPtr) memIsUncachedPtr(inPtr)
    00261 #define memIsCachedPointer(inPtr) memIsCachedPtr(inPtr)
    00262
    00263 #endif