richter     00/12/17 05:16:56

  Modified:    .        Tag: Embperl2c epdom.c epdom.h
  Log:
  Embperl 2 - improve memory management
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.4.2.19  +128 -135  embperl/Attic/epdom.c
  
  Index: epdom.c
  ===================================================================
  RCS file: /home/cvs/embperl/Attic/epdom.c,v
  retrieving revision 1.4.2.18
  retrieving revision 1.4.2.19
  diff -u -r1.4.2.18 -r1.4.2.19
  --- epdom.c   2000/12/17 10:24:49     1.4.2.18
  +++ epdom.c   2000/12/17 13:16:55     1.4.2.19
  @@ -43,10 +43,123 @@
   tIndex xCheckpointCache[512] ;
   
   
  -//#define dom_malloc(s)  (nMemUsage += s, malloc(s))
  +tUInt8 * MemFree[256] ;
  +tUInt8 * pMemLast = NULL ;
  +tUInt8 * pMemEnd = NULL ;
   
  -void * dom_malloc (size_t  n)
  +struct tPad
       {
  +    tNodeData Nodes [1024] ;
  +    } ;
  +
  +typedef struct tPad tPad ;
  +
  +
  +/* ------------------------------------------------------------------------ */
  +/*                                                                          */
  +/* mydie                                                                    */
  +/*                                                                          */
  +/* Fatal Error                                                              */
  +/*                                                                          */
  +/* ------------------------------------------------------------------------ */
  +
  +int mydie (char *  msg)
  +    {
  +    strncpy (pCurrReq -> errdat1, msg, sizeof (pCurrReq -> errdat1)) ;
  +    LogError (pCurrReq, 9999) ;
  +    puts (msg) ;
  +    exit (1) ;
  +    }
  +
  +/* ------------------------------------------------------------------------ */
  +/*                                                                          */
  +/* Node memory management                                                   */
  +/*                                                                          */
  +/* ------------------------------------------------------------------------ */
  +
  +
  +tNodeData * dom_malloc (size_t  nSize)
  +    {
  +    int          nFree = (nSize+15)>>4 ;
  +    void *  pNew ;
  +    
  +    if (nFree > sizeof (MemFree) / sizeof (void *))
  +     mydie ("Node to huge for dom_malloc") ;
  +
  +    if (pNew = MemFree[nFree])
  +     { /* --- take one entry off the free list --- */
  +     MemFree[nFree] = *((tUInt8 * *)pNew) ;
  +     return pNew ;
  +     }
  +    
  +    nSize = nFree * 16 ; /* --- make dividable by 16 --- */
  +    if (pMemLast + nSize < pMemEnd)
  +     { /* --- take space at the end of the pad --- */
  +     pNew = pMemLast ;
  +     pMemLast += nSize ;
  +     return pNew ;
  +     }
  +    
  +    /* --- Pad full -> alloc new one --- */
  +    
  +    pMemLast = malloc(sizeof (tPad)) ;
  +
  +    nMemUsage += sizeof (tPad) ;
  +
  +    pMemEnd = pMemLast + sizeof (tPad) ;
  +    pNew = pMemLast ;
  +    pMemLast += nSize ;
  +    return pNew ;
  +    }
  +
  +
  +void dom_free (tNodeData * pNode)
  +    {
  +    int          nSize = sizeof (tNodeData) + pNode -> numAttr * sizeof (tAttrData) 
;
  +    int          nFree = (nSize+15)>>4 ;
  +    void *  pFree ;
  +    
  +    if (nFree > sizeof (MemFree) / sizeof (void *))
  +     mydie ("Node to huge for dom_malloc") ;
  +
  +    /* --- add to the free list --- */
  +    pFree = MemFree[nFree] ;
  +    MemFree[nFree] = (tUInt8 *)pNode ;
  +    *((tUInt8 * *)pNode) = pFree ;
  +    return ;
  +    }
  +
  +
  +
  +tNodeData *  dom_realloc (tNodeData * pNode, size_t  nSize)
  +    {
  +    int          nOldSize = sizeof (tNodeData) + pNode -> numAttr * sizeof 
(tAttrData) ;
  +    tNodeData * pNew ;
  +    
  +    if (((tUInt8 *)pNode) + nOldSize == pMemLast)
  +     { /* --- expand --- */
  +     if (((tUInt8 *)pNode) + nSize < pMemEnd)
  +         { /* --- take space at the end of the pad --- */
  +         pMemLast = ((tUInt8 *)pNode) + nSize ;
  +         return pNode ;
  +         }
  +     }
  +    
  +    pNew = dom_malloc (nSize) ;
  +    memcpy (pNew, pNode, nOldSize) ;
  +    dom_free (pNode) ;
  +    return pNew ;
  +    }
  +
  +
  +/* ------------------------------------------------------------------------ */
  +/*                                                                          */
  +/* general memory management                                                */
  +/*                                                                          */
  +/* ------------------------------------------------------------------------ */
  +
  +void * str_malloc (size_t  n)
  +    {
       void * m = malloc(n + sizeof (size_t)) ;
       if (m)
        {
  @@ -56,8 +169,10 @@
   
       return m ;
       }
  +
   
  -void * dom_realloc (void * s, size_t  n)
  +
  +void * str_realloc (void * s, size_t  n)
       {
       void * m = ((size_t *)s) - 1 ;
       nMemUsage -= *((size_t *)m) ;
  @@ -71,7 +186,7 @@
   
   //#define dom_free(s)    (free(s))
   
  -void dom_free (void * s)
  +void str_free (void * s)
       {
       void * m = ((size_t *)s) - 1 ;
       nMemUsage -= *((size_t *)m) ;
  @@ -87,22 +202,7 @@
   static int DomTree_free (SV * pSV, MAGIC * mg) ;
   
   
  -/* ------------------------------------------------------------------------ */
  -/*                                                                          */
  -/* mydie                                                                    */
  -/*                                                                          */
  -/* Fatal Error                                                              */
  -/*                                                                          */
  -/* ------------------------------------------------------------------------ */
   
  -int mydie (char *  msg)
  -    {
  -    strncpy (pCurrReq -> errdat1, msg, sizeof (pCurrReq -> errdat1)) ;
  -    LogError (pCurrReq, 9999) ;
  -    puts (msg) ;
  -    exit (1) ;
  -    }
  -
   
   
   /* ------------------------------------------------------------------------ */
  @@ -121,7 +221,7 @@
       {
       struct tArrayCtrl * pNew ;
       
  -    if ((pNew = dom_malloc (nAdd * nElementSize + sizeof (struct tArrayCtrl))) == 
NULL)
  +    if ((pNew = str_malloc (nAdd * nElementSize + sizeof (struct tArrayCtrl))) == 
NULL)
        return 0 ;
       
       memset (pNew, 0, nAdd * nElementSize + sizeof (struct tArrayCtrl)) ; 
  @@ -149,7 +249,7 @@
       struct tArrayCtrl * pCtrl = ((struct tArrayCtrl *)(*(void * *)pArray)) - 1 ;
   
       if (pCtrl)
  -        dom_free (pCtrl) ;
  +        str_free (pCtrl) ;
   
       (*(void * *)pArray) = NULL ;
   
  @@ -173,7 +273,7 @@
       struct tArrayCtrl * pCtrl = ((struct tArrayCtrl *)(*(void * *)pOrgArray)) - 1 ;
       int    size = pCtrl -> nFill * pCtrl -> nElementSize + sizeof (struct 
tArrayCtrl) ;
       
  -    if ((pNew = dom_malloc (size)) == NULL)
  +    if ((pNew = str_malloc (size)) == NULL)
        return 0 ;
       
       memcpy (pNew, pCtrl, size) ; 
  @@ -206,7 +306,7 @@
        struct tArrayCtrl * pNew ;
        int                 nNewMax = pCtrl -> nFill + numElements + pCtrl -> nAdd ;
        
  -     if ((pNew = dom_realloc (pCtrl, nNewMax * pCtrl -> nElementSize + sizeof 
(struct tArrayCtrl))) == NULL)
  +     if ((pNew = str_realloc (pCtrl, nNewMax * pCtrl -> nElementSize + sizeof 
(struct tArrayCtrl))) == NULL)
            return 0 ;
        
        *(void * *)pArray = (struct tArray *)(pNew + 1) ;
  @@ -270,7 +370,7 @@
        if (nNewMax < numElements)
            nNewMax = numElements + pCtrl -> nAdd ;
        
  -     if ((pNew = dom_realloc (pCtrl, nNewMax * pCtrl -> nElementSize + sizeof 
(struct tArrayCtrl))) == NULL)
  +     if ((pNew = str_realloc (pCtrl, nNewMax * pCtrl -> nElementSize + sizeof 
(struct tArrayCtrl))) == NULL)
            return 0 ;
        
        p = (char *)(pNew + 1) ;
  @@ -542,10 +642,10 @@
       xDocumentFraq = String2Ndx ("DocumentFraq", 12) ;
       xOrderIndexAttr   = String2Ndx ("<orderindex>", 10) ;
   
  -    ArrayNew (&pDomTrees, 16, sizeof (tDomTree)) ; 
  +    ArrayNew (&pDomTrees, 64, sizeof (tDomTree)) ; 
       ArrayAdd (&pDomTrees, 1) ;
       memset (&pDomTrees[0], 0, sizeof (tDomTree)) ;
  -    ArrayNew (&pFreeDomTrees, 16, sizeof (tIndex)) ;
  +    ArrayNew (&pFreeDomTrees, 64, sizeof (tIndex)) ;
       }
   
   
  @@ -627,10 +727,10 @@
       tDomTree * pDomTree ;
       pDomTree = DomTree_alloc () ;
   
  -    ArrayNew (&pDomTree -> pLookup, 128, sizeof (struct tNodeData *)) ; 
  +    ArrayNew (&pDomTree -> pLookup, 256, sizeof (struct tNodeData *)) ; 
       ArrayAdd (&pDomTree -> pLookup, 1) ;
   
  -    ArrayNew (&pDomTree -> pOrder, 128, sizeof (tDomTreeOrder)) ; 
  +    ArrayNew (&pDomTree -> pOrder, 256, sizeof (tDomTreeOrder)) ; 
   
       *pNewLookup = pDomTree  ;
   
  @@ -1732,113 +1832,6 @@
       }
   
   
  -#if 0 /* macro in epdom.h */
  -
  -/* ------------------------------------------------------------------------ */
  -/*                                                                          */
  -/* Node_parentNode                                                          */
  -/*                                                                          */
  -/* Get parent node                                                       */
  -/*                                                                          */
  -/* ------------------------------------------------------------------------ */
  -
  -
  -tNode Node_parentNode  (/*in*/  tDomTree *   pDomTree,
  -                     /*in*/  tNode            xNode) 
  -
  -    {
  -    struct tNodeData *       pNode = Node_self (pDomTree, xNode) ;
  -    struct tNodePad *        pPad    = (struct tNodePad * )(((tUInt8 *)pNode) - 
pNode -> nPadOffset) ;
  -    return pPad -> xParent ;
  -    }
  -
  -/* ------------------------------------------------------------------------ */
  -/*                                                                          */
  -/* Node_selfParentNode                                                      */
  -/*                                                                          */
  -/* Get parent node                                                       */
  -/*                                                                          */
  -/* ------------------------------------------------------------------------ */
  -
  -
  -tNodeData * Node_selfParentNode  (/*in*/  tDomTree * pDomTree,
  -                               /*in*/  tNodeData *   pNode) 
  -
  -    {
  -    struct tNodePad *        pPad    = (struct tNodePad * )(((tUInt8 *)pNode) - 
pNode -> nPadOffset) ;
  -    return Node_self (pDomTree, pPad -> xParent) ;
  -    }
  -
  -
  -
  -/* ------------------------------------------------------------------------ */
  -/*                                                                          */
  -/* Node_selfParentNode                                                      */
  -/*                                                                          */
  -/* Get parent node                                                       */
  -/*                                                                          */
  -/* ------------------------------------------------------------------------ */
  -
  -
  -tNode xNode_selfParentNode  (/*in*/  tNodeData *     pNode) 
  -
  -    {
  -    tNodePad *       pPad    = (struct tNodePad * )(((tUInt8 *)pNode) - pNode -> 
nPadOffset) ;
  -    return pPad -> xParent ;
  -    }
  -
  -
  -/* ------------------------------------------------------------------------ */
  -/*                                                                          */
  -/* Node_firstChild                                                          */
  -/*                                                                          */
  -/* Get first child node                                                          */
  -/*                                                                          */
  -/* ------------------------------------------------------------------------ */
  -
  -
  -tNode Node_firstChild  (/*in*/  tDomTree *   pDomTree,
  -                     /*in*/  tNode            xNode) 
  -
  -    {
  -    struct tNodePad *        pPad = NodePad_self (pDomTree, Node_self (pDomTree, 
xNode) -> xChilds) ;
  -    tNodeData * pNode ;
  -    if (pPad == NULL)
  -     return 0 ;
  -    
  -    pNode = ((tNodeData *)(pPad + 1)) ;
  -    if (!pNode -> bFlags)
  -     pNode = Node_selfNextSibling (pDomTree, pNode) ;
  -
  -    return pNode && pNode -> bFlags?pNode -> xNdx:0 ;
  -    }
  -
  -/* ------------------------------------------------------------------------ */
  -/*                                                                          */
  -/* Node_selfFirstChild                                                          */
  -/*                                                                          */
  -/* Get first child node                                                          */
  -/*                                                                          */
  -/* ------------------------------------------------------------------------ */
  -
  -
  -tNodeData * Node_selfFirstChild  (/*in*/  tDomTree *   pDomTree,
  -                               /*in*/  tNodeData *  pNode) 
  -
  -    {
  -    struct tNodePad *        pPad = NodePad_self (pDomTree, pNode -> xChilds) ;
  -    tNodeData * pChildNode ;
  -    if (pPad == NULL)
  -     return 0 ;
  -    
  -    pChildNode = ((tNodeData *)(pPad + 1)) ;
  -    if (!pChildNode -> bFlags)
  -     pChildNode = Node_selfNextSibling (pDomTree, pChildNode) ;
  -
  -    return pChildNode ;
  -    }
  -
  -#endif
   
   
   /* ------------------------------------------------------------------------ */
  
  
  
  1.4.2.10  +1 -43     embperl/Attic/epdom.h
  
  Index: epdom.h
  ===================================================================
  RCS file: /home/cvs/embperl/Attic/epdom.h,v
  retrieving revision 1.4.2.9
  retrieving revision 1.4.2.10
  diff -u -r1.4.2.9 -r1.4.2.10
  --- epdom.h   2000/12/15 15:08:28     1.4.2.9
  +++ epdom.h   2000/12/17 13:16:55     1.4.2.10
  @@ -38,27 +38,7 @@
   typedef tIndex               tNode    ;
   typedef tIndex               tAttr    ;
   
  -#if 0
   
  -struct tNodePad
  -    {
  -    tNodeType                nType ;
  -    tIndex           xNdx ;
  -    tIndex           xParent ;
  -    tIndex           xPrev ;
  -    tIndex           xNext ;
  -    tIndex           xLast ;
  -    tIndex           xFirst ;
  -    tIndex           xDomTree ;
  -    tUInt16          numChilds ;
  -    tUInt16          nFill ;
  -    tUInt16          nMax ;
  -    } ;
  -
  -typedef struct tNodePad tNodePad ;
  -
  -#endif
  -
   struct tNodeData
       {
       tNodeType                nType ;
  @@ -124,7 +104,6 @@
   
   enum tNodeType
       {
  -    ntypPad          = -1,
       ntypTag          = 1,
       ntypStartTag        = 1 + 0x20,
       ntypEndTag               = 1 + 0x40,
  @@ -176,7 +155,7 @@
   
   struct tDomTree
       {
  -    void * *     pLookup ;   /* table for converting tNode, tNodePad and tAttr to 
pointers */
  +    void * *     pLookup ;   /* table for converting tNode and tAttr to pointers */
       tDomTreeOrder * pOrder ; /* Order of dom tree after execution of code */
       tIndex       xNdx ;      /* Index of Dom Tree */
       tNode        xDocument ; /* root document node */
  @@ -281,22 +260,6 @@
                        /*in*/  int              nLinenumber) ;
   
   
  -#if 0
  -
  -tNode Node_parentNode  (/*in*/ tDomTree *  pDomTree,
  -                     /*in*/  tNode            xNode) ;
  -
  -tNodeData * Node_selfParentNode  (/*in*/  tDomTree * pDomTree,
  -                               /*in*/  tNodeData *   pNode) ;
  -
  -tNode xNode_selfParentNode  (/*in*/  tNodeData *     pNode) ;
  -
  -tNode Node_firstChild  (/*in*/ tDomTree *  pDomTree,
  -                     /*in*/  tNode            xNode) ;
  -
  -tNodeData * Node_selfFirstChild  (/*in*/  tDomTree *   pDomTree,
  -                               /*in*/  tNodeData *  pNode) ;
  -#endif
   
   tNodeData * Node_selfNthChild (/*in*/ tDomTree *  pDomTree,
                                      /*in*/ struct tNodeData * pNode,
  @@ -312,9 +275,6 @@
                        /*in*/ tNode       xNode) ;
   
   #define DomTree_self(xDomTree)                   (&pDomTrees[xDomTree]) 
  -#define NodePad_self(pDomTree,xNode)     ((struct tNodePad *)(pDomTree -> 
pLookup[xNode]))
  -#define NodePad_selfDomTree(pPad)        (pPad -> xDomTree)
  -#define NodePad_selfFirstChild(pDomTree,pNodePad)        ((tNodeData *)(pNodePad + 
1))
   
   #define Node_self(pDomTree,xNode)        ((struct tNodeData *)(pDomTree -> 
pLookup[xNode]))
   
  @@ -328,8 +288,6 @@
   #define Node_selfNodeNameNdx(pNode)      ((pNode) -> nText) ;
   #define Node_selfNodeName(pNode)         (Ndx2String ((pNode) -> nText))
   #define Node_nodeName(pDomTree,pNode)            (Ndx2String (Node_self 
(pDomTree,xNode) -> nText))
  -#define Node_selfPad(pNode)        ((struct tNodePad * )(((tUInt8 *)(pNode)) - 
(pNode) -> nPadOffset))
  -#define Node_selfDomTree(pNode)                  NodePad_selfDomTree 
(Node_selfPad(pNode))
   #define Node_selfFirstAttr(pNode)        ((tAttrData *)((pNode) + 1))
   
   tNodeData * Node_selfCloneNode (/*in*/ tDomTree *      pDomTree,
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to