home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / Sylia / VectorHeap.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  920 b   |  51 lines

  1. #include <assert.h>
  2. #include <stddef.h>
  3. #include <malloc.h>
  4.  
  5. #include "VectorHeap.h"
  6.  
  7. VectorHeap::VectorHeap(long chunk_size) {
  8.     lChunkSize = chunk_size;
  9.     first = last = NULL;
  10. }
  11.  
  12. VectorHeap::~VectorHeap() {
  13.     VectorHeapHeader *vhh_cur, *vhh_next;
  14.  
  15.     vhh_cur = first;
  16.  
  17.     while(vhh_cur) {
  18.         vhh_next = vhh_cur->next;
  19.         free(vhh_cur);
  20.         vhh_cur = vhh_next;
  21.     }
  22. }
  23.  
  24. void *VectorHeap::Allocate(long lBytes) {
  25.     long lp;
  26.  
  27.     lBytes = (lBytes + 7) & ~7;
  28.  
  29.     if (!last || last->lSize - last->lPoint < lBytes) {
  30.         VectorHeapHeader *vhh;
  31.  
  32.         vhh = (VectorHeapHeader *)malloc(lChunkSize);
  33.         if (!vhh) return NULL;
  34.  
  35.         vhh->next    = NULL;
  36.         vhh->lSize    = lChunkSize - offsetof(VectorHeapHeader, heap);
  37.         vhh->lPoint    = lBytes;
  38.  
  39.         if (last) last->next    = vhh;
  40.         last        = vhh;
  41.         if (!first) first = vhh;
  42.  
  43.         return vhh->heap;
  44.     }
  45.  
  46.     lp = last->lPoint;
  47.     last->lPoint += lBytes;
  48.  
  49.     return &last->heap[lp];
  50. }
  51.