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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - vnew.cpp
  3.  * C++ VECTOR_NEW
  4.  * Called internally by the compiler to allocate arrays of classes
  5.  * having constructors
  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 * constNNC)(void near*);
  23. typedef void (near * constNFC)(void far*);
  24. typedef void pascal (near * constNNP)(void near*);
  25. typedef void pascal (near * constNFP)(void far*);
  26. typedef void (far * constFNC)(void near*);
  27. typedef void (far * constFFC)(void far*);
  28. typedef void pascal (far * constFNP)(void near*);
  29. typedef void pascal (far * constFFP)(void far*);
  30.  
  31. extern "C"
  32. void far * _vector_new_(void far *ptr,  // address of array, 0 means allocate it
  33.         size_t size,                    // size of each object
  34.         unsigned count,                 // how many objects
  35.         unsigned mode,                  // actual type of constructor
  36.         ...
  37.         )
  38. /* This routine is used to initialize an array of class type.  If ptr is
  39.    NULL, it allocates the space for the array first.  Since the constructor
  40.    for the class may be of either memory model, and take an argument of any
  41.    memory model, we are forced to pass a mode parameter to tell us how to
  42.    cast it.  Since we must pass a near pointer for near functions and a
  43.    far call for far functions, we can't even know the argument type until
  44.    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. */
  51. {
  52.     va_list ap;     /* for access to parameters */
  53.     constNNC np;    /* Near pointer version */
  54.     constFNC fp;    /* Far pointer version */
  55.     int stored_count;
  56.  
  57.     va_start(ap, mode);
  58.  
  59.     if (mode & 1)
  60.         fp = va_arg(ap, constFNC);
  61.     else
  62.         np = va_arg(ap, constNNC);
  63.  
  64.     if( ptr == 0 )
  65.         {
  66.         stored_count = mode & 0x10;
  67.         if (stored_count)
  68.             ptr = operator new((size * count)+sizeof(count));
  69.         else
  70.             ptr = operator new(size * count);
  71.  
  72.         if (ptr == 0)
  73.             return 0;
  74.  
  75.         if (stored_count)
  76.             {
  77.             *(unsigned far *)ptr = count;
  78.             ptr = ((char far *)ptr + sizeof(count));
  79.             }
  80.         }
  81.  
  82.     mode &= 0x07;   // strip out all flags except call type
  83.  
  84.     for( char far*p = (char far*) ptr;  count-- > 0;  p += size )
  85.        switch (mode)
  86.            {
  87.            case 0: np((void near *) p); break;
  88.            case 1: fp((void near *) p); break;
  89.            case 2: (*(constNNP) np)((void near *) p); break;
  90.            case 3: (*(constFNP) fp)((void near *) p); break;
  91.            case 4: (*(constNFC) np)((void far  *) p); break;
  92.            case 5: (*(constFFC) fp)((void far  *) p); break;
  93.            case 6: (*(constNFP) np)((void far  *) p); break;
  94.            default:(*(constFFP) fp)((void far  *) p); break;
  95.            }
  96.     return ptr;
  97. }
  98.