Cleanup, add qsort function

svn-id: r8393
This commit is contained in:
Chris Apers 2003-06-08 09:28:18 +00:00
parent 8cc50d2460
commit 2600628339
2 changed files with 6 additions and 177 deletions

View File

@ -21,12 +21,6 @@
*/ */
#include "stdlib.h" #include "stdlib.h"
#include "MemGlue.h"
///////////////////////////////////////////////////////////////////////////////
/*void qsort(void *base, UInt32 nmemb, UInt32 size, ComparF *compar) {
SysQSort(base, nmemb, size, compar);
}*/
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void *bsearch(const void *key, const void *base, UInt32 nmemb, void *bsearch(const void *key, const void *base, UInt32 nmemb,
UInt32 size, int (*compar)(const void *, const void *)) { UInt32 size, int (*compar)(const void *, const void *)) {
@ -38,136 +32,10 @@ void *bsearch(const void *key, const void *base, UInt32 nmemb,
return NULL; return NULL;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static DmOpenRef gExtMemory = NULL; MemPtr calloc(UInt32 nelem, UInt32 elsize) {
MemPtr newP;
static UInt16 MemFindHeapID(UInt32 size)
{
UInt32 nFree, maxChunk ;
UInt16 maxCards = 1; //MemNumCards(); process only first card for now
UInt16 heapID = -1; // no heap avaliable
UInt16 cardNo;
UInt16 maxHeaps, heapIndex;
for (cardNo = 0; cardNo < maxCards; cardNo++)
{
if (MemNumRAMHeaps(cardNo) > 0)
{
maxHeaps = MemNumHeaps(cardNo);
for (heapIndex = 0; heapIndex < maxHeaps; heapIndex++)
{
// Obtain the ID of the heap.
heapID = MemHeapID(cardNo, heapIndex);
if (!(MemHeapFlags(heapID) & memHeapFlagReadOnly))
{
MemHeapFreeBytes( heapID, &nFree, &maxChunk );
if (maxChunk > size)
return heapID;
}
}
}
}
return heapID;
}
void MemExtInit()
{
if (!gExtMemory)
{
LocalID localID = DmFindDatabase(0, "ScummVM-Memory");
if (localID) DmDeleteDatabase(0, localID);
if (DmCreateDatabase (0, "ScummVM-Memory", 'ScVM', 'DATA', false) != errNone)
return;
localID = DmFindDatabase(0, "ScummVM-Memory");
gExtMemory = DmOpenDatabase(0, localID, dmModeReadWrite|dmModeExclusive);
}
}
void MemExtCleanup()
{
if (gExtMemory) {
DmCloseDatabase(gExtMemory);
LocalID localID = DmFindDatabase(0, "ScummVM-Memory");
if (localID)
DmDeleteDatabase(0, localID);
}
}
//#define USE_EXTENDEDMEM
#ifdef USE_EXTENDEDMEM
MemPtr calloc(UInt32 nelem, UInt32 elsize)
{
UInt32 size = nelem*elsize; UInt32 size = nelem*elsize;
MemPtr newP = NULL;
UInt16 heapID = MemFindHeapID(size);
if (heapID != NO_HEAP_FOUND)
{
if (MemHeapDynamic(heapID) && size < 65536-8) // 8 = chunk header size
newP = MemPtrNew(size);
else
{
SysAppInfoPtr appInfoP;
UInt16 ownerID, large, nmovable;
UInt16 attr;
ownerID = ((SysAppInfoPtr)SysGetAppInfo(&appInfoP, &appInfoP))->memOwnerID;
large = ((size > 65536-8) ? memNewChunkFlagAllowLarge : 0);
nmovable= (MemHeapDynamic(heapID) ? memNewChunkFlagNonMovable : memNewChunkFlagPreLock);
attr = ownerID|large|nmovable;
//MEMORY_RESERVE_ACCESS
newP = MemChunkNew(heapID, size, attr);
//MEMORY_RELEASE_ACCESS
if (newP && MemPtrDataStorage(newP)) { // if storage heap ?
if (!gExtMemory) { // if memory DB doesn't exist
MemChunkFree(newP);
return NULL;
}
UInt16 index = dmMaxRecordIndex; // used for record purpose
MemHandle newH = MemPtrRecoverHandle(newP); // exists
if (DmAttachRecord(gExtMemory, &index, newH, NULL) != errNone) // attach to DB
{
MemChunkFree(newP); // error
return NULL;
}
}
}
}
if (newP)
MemSet(newP,size,0);
return newP;
}
#else
MemPtr calloc(UInt32 nelem, UInt32 elsize)
{
UInt32 size = nelem*elsize;
MemPtr newP = NULL;
/* if (size < 65536-8) // 8 = chunk header size
newP = MemPtrNew(size);
else*/
/* {
SysAppInfoPtr appInfoP;
UInt16 ownerID;
UInt16 attr;
ownerID = ((SysAppInfoPtr)SysGetAppInfo(&appInfoP, &appInfoP))->memOwnerID;
attr = ownerID|memNewChunkFlagAllowLarge|memNewChunkFlagNonMovable;
newP = MemChunkNew(0, size, attr);
}
*/
newP = MemGluePtrNew(size); newP = MemGluePtrNew(size);
if (newP) if (newP)
@ -175,39 +43,12 @@ MemPtr calloc(UInt32 nelem, UInt32 elsize)
return newP; return newP;
} }
#endif
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#ifdef USE_EXTENDEDMEM Err free(MemPtr memP) {
Err free(MemPtr memP)
{
Err err = memErrInvalidParam;
if (!memP)
return err;
if (MemPtrDataStorage(memP)) { // if storage heap ?
if (gExtMemory) { // if memory DB exists
DmOpenRef where;
MemHandle newH = MemPtrRecoverHandle(memP);
UInt16 index = DmSearchRecord(newH, &where);
err = DmRemoveRecord(gExtMemory, index);
}
}
else
err = MemChunkFree(memP);
return err;
}
#else
Err free(MemPtr memP)
{
if (memP) if (memP)
return MemPtrFree(memP); return MemPtrFree(memP);
return memErrInvalidParam; return memErrInvalidParam;
} }
#endif
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
MemPtr realloc(MemPtr oldP, UInt32 size) MemPtr realloc(MemPtr oldP, UInt32 size)
{ {

View File

@ -24,31 +24,19 @@
#define STDLIB_H #define STDLIB_H
#include <PalmOS.h> #include <PalmOS.h>
#include "MemGlue.h"
#include "mathlib.h" #include "mathlib.h"
//#define memNewChunkFlagNonMovable 0x0200
#define memNewChunkFlagAllowLarge 0x1000 // this is not in the sdk *g*
#define memHeapFlagReadOnly 0x0001
#define NO_HEAP_FOUND -1
SysAppInfoPtr SysGetAppInfo(SysAppInfoPtr *uiAppPP, SysAppInfoPtr *actionCodeAppPP)
SYS_TRAP(sysTrapSysGetAppInfo);
#define atoi StrAToI #define atoi StrAToI
#define atol StrAToI #define atol StrAToI
#define abs(a) ((a) < 0 ? -(a) : (a)) #define abs(a) ((a) < 0 ? -(a) : (a))
//#define abs fabs //#define abs fabs
#define malloc(a) calloc(a,1) #define malloc(a) MemGluePtrNew(a)
//#define free MemPtrFree //#define free MemPtrFree
#define strtol(a,b,c) StrAToI(a) #define strtol(a,b,c) StrAToI(a)
#define qsort(a,b,c,d) #define qsort(a,b,c,d) SysQSort((a), (b), (c), (CmpFuncPtr)(&d), 0);
#define rand() SysRandom(0) #define rand() SysRandom(0)
void MemExtInit();
void MemExtCleanup();
MemPtr realloc(MemPtr oldP, UInt32 size); MemPtr realloc(MemPtr oldP, UInt32 size);
MemPtr calloc(UInt32 nelem, UInt32 elsize); MemPtr calloc(UInt32 nelem, UInt32 elsize);
Err free(MemPtr memP); Err free(MemPtr memP);