At 10:23 AM 8/3/99 -0500, you wrote:
>
>I'm not sure the best way to allocate memory for the search using the fewest
>resizes, locks and unlocks.  Having no idea how big the result will be means
>I can't allocate a specific size array.  How do people do this?  Can someone
>suggest a design for this type of thing on the Palm?
>

Don't know about a "correct" design, but this works:

// don't bother with C++ const declarations - they live in global space
#define ALLOC_CHUNK             20
#define ZERO_BYTE               0xFF

// sanity defines
#define memset(x, char, num) MemSet(x, num, char)
#define malloc(x) MemPtrNew(x)
#define memcpy(dest, src, size) MemMove(dest, src, size)
#define free(x) MemPtrFree(x)

func()
{
  ... blah
  Word wAlloc = ALLOC_CHUNK * 2;
  Word wInserts = 0;
  Word size1 = ALLOC_CHUNK * sizeof(MyStruct);
  MyStructPtr pStructs = (MyStructPtr)malloc(wAlloc * sizeof(MyStruct));
  memset(pdwStops, ZERO_BYTE, wAlloc * sizeof(MyStruct));
  for(i=0;i<n;i++)
  {
        VoidHand hRec;
        SomeRecordStructPtr pRec;

        hRec = DmGetRecord(rDB, i);
        pRec = (SomeRecordStructPtr)MemHandleLock(hRec);
        if (RecordTestFunction(pRec, stuff))
        {
                if (wInserts == wAlloc)
                {
                        MyStructPtr* pFree;
                        Word size2 = wAlloc * sizeof(MyStruct);

                        pFree = pStructs;
                        // don't bother using a var for the add, optimizer should do it
                        pStructs = (MyStructPtr)malloc(size1 + size2);
                        memset(pStructs, ZERO_BYTE, size1 + size2);
                        memcpy(pStructs, pFree, size2);
                        free(pFree);
                        wAlloc += ALLOC_CHUNK;
                }
                SomeInitializerFunc(pStructs[wInserts++], pRec);
        }
        MemHandleUnlock(hRec);
        DmReleaseRecord(rDB, i, false);
  }
  ... blah
}


Reply via email to