home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / VDEL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  3.3 KB  |  96 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - vdel.cpp
  3.  * C++ vector_delete
  4.  * Called internally by the compiler to deallocate arrays of classes
  5.  * having destructors
  6.  *
  7.  * NOTE: This is the old version used in TC++ 1.0 and BC++ 2.0
  8.  *-----------------------------------------------------------------------*/
  9.  
  10. /*
  11.  *      C/C++ Run Time Library - Version 5.0
  12.  *
  13.  *      Copyright (c) 1990, 1992 by Borland International
  14.  *      All Rights Reserved.
  15.  *
  16.  */
  17.  
  18.  
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21.  
  22. typedef void (near * destNNC)(void near*, int);
  23. typedef void (near * destNFC)(void far*, int);
  24. typedef void pascal (near * destNNP)(void near*, int);
  25. typedef void pascal (near * destNFP)(void far*, int);
  26. typedef void (far * destFNC)(void near*, int);
  27. typedef void (far * destFFC)(void far*, int);
  28. typedef void pascal (far * destFNP)(void near*, int);
  29. typedef void pascal (far * destFFP)(void far*, int);
  30.  
  31. extern "C"
  32. void _vector_delete_(void far *ptr,     // address of array (always needed)
  33.              size_t size,               // size of each object
  34.              unsigned count,            // how many objects
  35.              unsigned mode,             // How to call
  36.              ...
  37.             )
  38. /* This routine is used to destroy an array of class type.  If mode is
  39.    set, it deallocates the space for the array afterwards. Since the
  40.    destructor for the class may be of either memory model, and take
  41.    an argument of any memory model, we are forced to pass a mode parameter
  42.    to tell us how to cast it.  Since we must pass a near pointer for near
  43.    functions and a far pointer for far functions, we can't even know the
  44.    argument type until runtime, so we have to use varargs to get at it.
  45.  
  46.    The interpretation of the mode parameter is:
  47.     far function    0x01
  48.     pascal call     0x02
  49.     far pointer     0x04
  50.     deallocate      0x08
  51.     stored count    0x10
  52. */
  53. {
  54.     va_list ap;     /* for access to parameters */
  55.     destNNC np;     /* Near pointer version */
  56.     destFNC fp;     /* Far pointer version */
  57.     int dealloc = mode & 8;
  58.     int stored_count = mode & 0x10;
  59.  
  60.     if ( ptr == 0 )
  61.         return;
  62.  
  63.     va_start(ap, mode);
  64.  
  65.     if (mode & 1)
  66.         fp = va_arg(ap, destFNC);
  67.     else
  68.         np = va_arg(ap, destNNC);
  69.  
  70.     if (stored_count)   // If count is stored then use it
  71.         count = *((unsigned far *)ptr - 1);
  72.  
  73.     mode &= 0x07;   // strip out all flags except call type
  74.  
  75.     for( char far *p = (char far *) ptr + size * (count - 1);
  76.             count-- > 0; p -= size )
  77.         switch (mode)
  78.             {
  79.             case 0: np((void near *) p, 2); break;
  80.             case 1: fp((void near *) p, 2); break;
  81.             case 2: (*(destNNP) np)((void near *) p, 2); break;
  82.             case 3: (*(destFNP) fp)((void near *) p, 2); break;
  83.             case 4: (*(destNFC) np)((void far  *) p, 2); break;
  84.             case 5: (*(destFFC) fp)((void far  *) p, 2); break;
  85.             case 6: (*(destNFP) np)((void far  *) p, 2); break;
  86.             default:(*(destFFP) fp)((void far  *) p, 2); break;
  87.             }
  88.  
  89.     if( dealloc )
  90.         {
  91.         if (stored_count)
  92.             ptr = (char far *)ptr - sizeof(count);
  93.         operator delete((void *)ptr);
  94.         }
  95. }
  96.