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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - vnewn.cpp
  3.  * C++ VECTOR_NEW
  4.  * Called internally by the compiler to allocate arrays of classes
  5.  *  having constructors
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1990, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #include <stdlib.h>
  18. #include <stdarg.h>
  19. #include <_vector.h>
  20.  
  21. #pragma argsused
  22.  
  23. void near * _vector_new_(void near *ptr, // address of array, 0 means allocate
  24.         size_t size,                     // size of each object
  25.         unsigned count,                  // how many objects
  26.         unsigned mode,                   // actual type of constructor
  27.         ...
  28.         )
  29. /* This routine is used to initialize an array of class type.  If ptr is
  30.    NULL, it allocates the space for the array first.  Since the constructor
  31.    for the class may be of either memory model, and take an argument of any
  32.    memory model, we are forced to pass a mode parameter to tell us how to
  33.    cast it.  Since we must pass a near pointer for near functions and a
  34.    far call for far functions, we can't even know the argument type until
  35.    runtime, so we have to use varargs to get at it.
  36.  
  37.    If the constructor pointer is NULL no constructors are called.
  38.  
  39.    The interpretation of the mode parameter is:
  40.        far function    0x01     VFARCALL
  41.        pascal call     0x02     VPASCAL
  42.        far pointer     0x04     VFARPTR
  43.        deallocate      0x08     VDEALLOC
  44.        stored count    0x10     VSTORECNT
  45.        fastcall        0x20     VFASTCALL
  46.        huge vector     0x40     VHUGEVECT
  47.        fastthis        0x80     VFASTTHIS
  48.        this last       0x100    VTHISLAST
  49. */
  50. {
  51. #if defined(__LARGE__) || defined(__HUGE__) || defined(__COMPACT__)
  52.     abort();
  53.     return 0;
  54. #else
  55.     va_list ap;         // for access to parameters
  56.     constNNC np;        // near call version
  57.     constFNC fp;        // far call version
  58.     int construct = 1;  // flag whether to call constructors
  59.  
  60.     va_start(ap, mode);
  61.  
  62.     if (mode & VFARCALL)
  63.         {
  64.         fp = va_arg(ap, constFNC);
  65.         if (!fp)
  66.             construct = 0;
  67.         }
  68.     else
  69.         {
  70.         np = va_arg(ap, constNNC);
  71.         if (!np)
  72.             construct = 0;
  73.         }
  74.  
  75.     if( ptr == 0 )
  76.         {
  77.         unsigned long Length = size * count;
  78.  
  79.         if (mode & VSTORECNT)       // stored count
  80.             Length += sizeof(count);
  81.  
  82.         // check that additional bytes don't make it go over 64K
  83.         if (Length > 0xFFFF)
  84.             return 0;
  85.  
  86.         ptr = operator new((unsigned)Length);
  87.  
  88.         if (!ptr)
  89.             return 0;
  90.  
  91.         // have ptr at either final location or count before
  92.         if (mode & VSTORECNT)       // store count
  93.             {
  94.             *(unsigned *)ptr = count;
  95.             ((unsigned *)ptr)++;
  96.             }
  97.         }
  98.  
  99.     mode &= VCALLTYPE;      // strip out all flags except call type
  100.  
  101.     if (mode & VFASTTHIS)   // this-last has no effect with fast-this
  102.         mode &= ~(VTHISLAST);
  103.  
  104.     if (construct)
  105.         {
  106.         for( char *p = (char *) ptr;  count-- > 0;  p += size )
  107.             switch (mode)
  108.                 {
  109.                 case 0:
  110.                     (*(constNNC) np)((void near *) p); break;
  111.                 case (VFARCALL):
  112.                     (*(constFNC) fp)((void near *) p); break;
  113.                 case (VPASCAL):
  114.                 case (VPASCAL | VTHISLAST):
  115.                     (*(constNNP) np)((void near *) p); break;
  116.                 case (VFARCALL | VPASCAL):
  117.                 case (VFARCALL | VPASCAL | VTHISLAST):
  118.                     (*(constFNP) fp)((void near *) p); break;
  119.                 case (VFASTCALL):
  120.                 case (VFASTCALL | VTHISLAST):
  121.                     (*(constNNF) np)((void near *) p); break;
  122.                 case (VFARCALL | VFASTCALL):
  123.                 case (VFARCALL | VFASTCALL | VTHISLAST):
  124.                     (*(constFNF) fp)((void near *) p); break;
  125.                 case (0 | VFASTTHIS):
  126.                     LOAD_NEAR_THIS(p);
  127.                     (*(constNNCT) np)();
  128.                     END_NEAR_THIS();
  129.                     break;
  130.                 case (VFARCALL | VFASTTHIS):
  131.                     LOAD_NEAR_THIS(p);
  132.                     (*(constFNCT) fp)();
  133.                     END_NEAR_THIS();
  134.                     break;
  135.                 case (VPASCAL | VFASTTHIS):
  136.                     LOAD_NEAR_THIS(p);
  137.                     (*(constNNPT) np)();
  138.                     END_NEAR_THIS();
  139.                     break;
  140.                 case (VFARCALL | VPASCAL | VFASTTHIS):
  141.                     LOAD_NEAR_THIS(p);
  142.                     (*(constFNPT) fp)();
  143.                     END_NEAR_THIS();
  144.                     break;
  145.                 case (VFASTCALL | VFASTTHIS):
  146.                     LOAD_NEAR_THIS(p);
  147.                     (*(constNNFT) np)();
  148.                     END_NEAR_THIS();
  149.                     break;
  150.                 case (VFARCALL | VFASTCALL | VFASTTHIS):
  151.                     LOAD_NEAR_THIS(p);
  152.                     (*(constFNFT) fp)();
  153.                     END_NEAR_THIS();
  154.                     break;
  155.                 default:
  156.                     abort(); break;
  157.                 }
  158.         }
  159.     return ptr;
  160. #endif
  161. }
  162.