- Main Page
- Classes
- Files

- File List
- File Members

# flMemoryPool.c

Go to the documentation of this file.

    00001 #include "flGlobal.h"
    00002
    00003 #if FL_MEMORY != 0
    00004 #include <string.h>
    00005 #include <stdio.h>
    00006 #include <pspkernel.h>
    00007
    00008 #if FL_INCLUDE_ALL_C == 0
    00009 #include "flMemoryPool.h"
    00010
    00011 #if FL_DEBUG != 0
    00012 #include "flDebug.h"
    00013 #endif
    00014 #endif
    00015
    00016 MemoryPool* mempCreate(u32 inSize, u32 inType) {
    00017      if(!inSize) {
    00018           #if FL_DEBUG_WARNING != 0
    00019           debugWarning("Cannot create memory pool.\nTrying to create a memory pool of size 0.");
    00020           #endif
    00021           return NULL;
    00022      }
    00023      if(!inType || (inType > 2)) {
    00024           #if FL_DEBUG_WARNING != 0
    00025           debugWarning("Cannot create memory pool.\nTrying to create a memory pool with an invalid type parameter.");
    00026           #endif
    00027           return NULL;
    00028      }
    00029      MemoryPool* tempOut = (MemoryPool*)memAlloc(inSize + sizeof(MemoryPool));
    00030      if(!tempOut) {
    00031           #if FL_DEBUG_WARNING != 0
    00032           debugWarning("Cannot allocate memory pool.\nProbably not enough free memory.");
    00033           #endif
    00034           return NULL;
    00035      }
    00036      while(inSize & 15)
    00037           inSize++;
    00038      tempOut->mempData = (void*)((u32)tempOut + sizeof(MemoryPool));
    00039      tempOut->mempSize = inSize;
    00040      tempOut->mempType = inType;
    00041      tempOut->mempAllocData = NULL;
    00042
    00043      return tempOut;
    00044 }
    00045
    00046 void mempDestroy(MemoryPool* inMemPool) {
    00047      if(!inMemPool) {
    00048           #if FL_DEBUG_WARNING != 0
    00049           debugWarning("Trying to destroy NULL memory pool.");
    00050           #endif
    00051           return;
    00052      }
    00053      if(inMemPool->mempType == MEMORY_POOL_TYPE_TABLE)
    00054           memFree(inMemPool->mempAllocData);
    00055      memFree(inMemPool);
    00056 }
    00057
    00058
    00059
    00060 void* mempAlloc(MemoryPool* inMemPool, u32 inSize) {
    00061      if(!inMemPool) {
    00062           #if FL_DEBUG_WARNING != 0
    00063           debugWarning("Trying to allocate from NULL memory pool.");
    00064           #endif
    00065           return NULL;
    00066      }
    00067      if(!inSize) {
    00068           #if FL_DEBUG_WARNING != 0
    00069           debugWarning("Trying to allocate 0 bytes from memory pool.");
    00070           #endif
    00071           return NULL;
    00072      }
    00073      if(inMemPool->mempType == MEMORY_POOL_TYPE_LINKLIST) {
    00074           MemoryPoolHdrLL* tempHeader = NULL;
    00075           MemoryPoolHdrLL* tempPrevHeader = NULL;
    00076           if(!inMemPool->mempAllocData) {
    00077                inMemPool->mempAllocData = inMemPool->mempData;
    00078                tempHeader = (MemoryPoolHdrLL*)inMemPool->mempAllocData;
    00079           } else {
    00080                tempPrevHeader = (MemoryPoolHdrLL*)inMemPool->mempAllocData;
    00081                while(tempPrevHeader->mempHLLNext != NULL)
    00082                     tempPrevHeader = (MemoryPoolHdrLL*)tempPrevHeader->mempHLLNext;
    00083                tempHeader = (MemoryPoolHdrLL*)((u32)tempPrevHeader + sizeof(MemoryPoolHdrLL) + tempPrevHeader->mempHLLSize);
    00084                tempPrevHeader->mempHLLNext = (void*)tempHeader;
    00085           }
    00086           tempHeader->mempHLLData = (void*)((u32)tempHeader + sizeof(MemoryPoolHdrLL));
    00087           tempHeader->mempHLLSize = inSize;
    00088           tempHeader->mempHLLNext = NULL;
    00089           tempHeader->mempHLLPrev = (void*)tempPrevHeader;
    00090
    00091           return tempHeader->mempHLLData;
    00092      }
    00093
    00094      #if FL_DEBUG_WARNING != 0
    00095      debugWarning("Trying to allocate from memory pool with an invalid type.");
    00096      #endif
    00097      return NULL;
    00098 }
    00099
    00100 void* mempCalloc(MemoryPool* inMemPool, u32 inSize0, u32 inSize1) {
    00101      void* tempOut = mempAlloc(inMemPool, (inSize0 * inSize1));
    00102      if(!tempOut)
    00103           return NULL;
    00104      memClear(tempOut, (inSize0 * inSize1));
    00105      return tempOut;
    00106 }
    00107
    00108 void* mempRealloc(MemoryPool* inMemPool, void* inPtr, u32 inSize) {
    00109      return NULL;
    00110 }
    00111
    00112 void* mempAlign(MemoryPool* inMemPool, u32 inBoundry, u32 inSize) {
    00113      return NULL;
    00114 }
    00115
    00116 void mempFree(MemoryPool* inMemPool, void* inPtr) {
    00117
    00118 }
    00119
    00120 #endif