home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / VDEL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  3.1 KB  |  83 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.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C++ Run Time Library - Version 1.0                 |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1990 by Borland International              |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18. #include <stdlib.h>
  19. #include <stdarg.h>
  20.  
  21. typedef void (near * destNNC)(void near*, int);
  22. typedef void (near * destNFC)(void far*, int);
  23. typedef void pascal (near * destNNP)(void near*, int);
  24. typedef void pascal (near * destNFP)(void far*, int);
  25. typedef void (far * destFNC)(void near*, int);
  26. typedef void (far * destFFC)(void far*, int);
  27. typedef void pascal (far * destFNP)(void near*, int);
  28. typedef void pascal (far * destFFP)(void far*, int);
  29.  
  30. extern "C"
  31. void _vector_delete_(void far *ptr,    // address of array (always needed)
  32.              size_t size,    // size of each object
  33.              int count,        // how many objects
  34.              int mode,        // How to call
  35.              ...
  36.             )
  37. /* This routine is used to destroy an array of class type.  If mode is 
  38.    set, it deallocates the space for the array afterwards. Since the
  39.    destructor for the class may be of either memory model, and take
  40.    an argument of any memory model, we are forced to pass a mode parameter
  41.    to tell us how to cast it.  Since we must pass a near pointer for near
  42.    functions and a far pointer for far functions, we can't even know the
  43.    argument type until runtime, so we have to use varargs to get at it.
  44.  
  45.    The interpretation of the mode parameter is:
  46.     far function        0x01
  47.     pascal call        0x02
  48.     far pointer        0x04
  49.     deallocate        0x08
  50. */
  51. {
  52.     int dealloc = mode & 8;
  53.     va_list ap;        /* for access to parameters */
  54.     destNNC np;        /* Near pointer version */
  55.     destFNC fp;        /* Far pointer version */
  56.  
  57.     if ( ptr == 0 )
  58.     return;
  59.  
  60.     mode &= 7;            // strip out the deallocate flag
  61.     va_start(ap, mode);
  62.  
  63.     if (mode & 1)
  64.     fp = va_arg(ap, destFNC);
  65.     else
  66.     np = va_arg(ap, destNNC);
  67.  
  68.     for( char far *p = (char far *) ptr;  --count >= 0;  p += size )
  69.     switch (mode) {
  70.         case 0: np((void near *) p, 2); break;
  71.         case 1: fp((void near *) p, 2); break;
  72.         case 2: (*(destNNP) np)((void near *) p, 2); break;
  73.         case 3: (*(destFNP) fp)((void near *) p, 2); break;
  74.         case 4: (*(destNFC) np)((void far  *) p, 2); break;
  75.         case 5: (*(destFFC) fp)((void far  *) p, 2); break;
  76.         case 6: (*(destNFP) np)((void far  *) p, 2); break;
  77.         default:(*(destFFP) fp)((void far  *) p, 2); break;
  78.     }
  79.  
  80.     if( dealloc )
  81.     operator delete(ptr, size*count);
  82. }
  83.