home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / s / s001 / 1.ddi / TS / SRC / TSMALLOC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-06  |  3.8 KB  |  126 lines

  1.                 /******************************************
  2.            *        TSMALLOC.C             *
  3.                * Copyright TimeSlice, Inc. 1985, 86, 87. *
  4.                ******************************************/
  5.  
  6.  
  7. #include <ts.h>
  8.  
  9. #if defined(TURBOC) && ( defined(MS) || defined(MP) )
  10.   short ts_brklev , _tstc_sp ;
  11. #define swap(a,b) ((int)a^=(int)b,(int)b^=(int)a,(int)a^=(int)b)
  12. #endif
  13.  
  14.  
  15. /***
  16. * _TS_FREE( PTR )
  17. * Frees allocated ptr (in near segment for Microsoft large).
  18. ***/
  19. void _ts_free( off, seg )
  20. unsigned off, seg ;
  21. {
  22. int free() ;
  23.  
  24.   critstart(DOS_CRCLASS);/*become critical */
  25. #if ( defined(ML) || defined(MD) ) && defined(MICROSOFT)
  26.     _nfree( off ) ;
  27. #else
  28. #if defined(TURBOC) && ( defined(MS) || defined(MP) )
  29.   ssleep() ;        /*put slicer to sleep */
  30.   asm mov cx, off    /*get the pointer value */
  31.   asm pushf        /* save current flags */
  32.   asm cli        ;    /* disable interrupts */
  33.   asm push __brklvl ;    /* Save current stack overflow/near heap base marker */
  34.   asm mov  ax, sp   ;   /* Get current value of stack pointer */  
  35.   asm mov  bx, ts_brklev
  36.   asm mov  __brklvl,bx  /*let the break level become what it used to be */
  37.   asm mov  sp, _tstc_sp /* point to the original stack */
  38.   asm push ax        /* save real sp */
  39.   asm push bp        /*..and real bp */
  40.   asm mov  bp, sp    /*make new bp */
  41.   asm push cx        /*push offset */
  42. #if defined(MS)
  43.   asm call free    /*call free */
  44. #else
  45.   asm call far ptr free    /*call free */
  46. #endif
  47.   asm pop  cx        /*remove param */
  48.   asm pop bp        /*get real bp back */
  49.   asm pop cx        /*get the real sp back */
  50.   asm mov sp, cx    /*point to real stack */
  51.   asm pop cx        /*get real __brklvl back */
  52.   asm mov bx, __brklvl  /*get newly updated heap marker */
  53.   asm mov ts_brklev,bx  /*save it for the next time */
  54.   asm mov __brklvl, cx  /*restore the current heap marker */
  55.   asm popf        /* restore cpuflags register */
  56.   swake() ;
  57. #else
  58.   free( off, seg );
  59. #endif
  60. #endif
  61.   critend(DOS_CRCLASS) ;
  62.  
  63. }
  64.     
  65.  
  66. /***
  67. * _TS_MALLOC( NBYTES )
  68. * Allocate nbytes (in near segment for Microsoft large).
  69. ***/
  70. char *_ts_malloc( nbytes ) {
  71.  
  72. #if ( defined(ML) || defined(MD)) && defined(MICROSOFT)
  73.   char near *_nmalloc();
  74.   union {
  75.     char *ptr;
  76.     struct {
  77.       int off, seg;
  78.     }i;
  79.   } genptr;
  80.  
  81.   genptr.i.seg = ( genptr.i.off = (unsigned short)cpcrit( DOS_CRCLASS,_nmalloc , nbytes ) ) ? _datseg : 0 ;
  82.   if (genptr.ptr) memset(genptr.ptr, 0, nbytes);
  83.   return genptr.ptr;
  84. #else
  85.     char *malloc();
  86.     char *cp ;
  87. #if defined(TURBOC) && ( defined(MS) || defined(MP) )
  88.   critstart(DOS_CRCLASS);/*become critical */
  89.   ssleep() ;        /*put slicer to sleep */
  90.   asm mov cx, nbytes    /*get the number of bytes to allocate */
  91.   asm pushf        /* save current flags */
  92.   asm cli        ;    /* disable interrupts */
  93.   asm push __brklvl ;    /* Save current stack overflow/near heap base marker */
  94.   asm mov  ax, sp   ;   /* Get current value of stack pointer */  
  95.   asm mov  bx, ts_brklev
  96.   asm mov  __brklvl,bx  /*let the break level become what it used to be */
  97.   asm mov  sp, _tstc_sp /* point to the original stack */
  98.   asm push ax        /* save real sp */
  99.   asm push bp        /*..and real bp */
  100.   asm mov  bp, sp    /*make new bp */
  101.   asm push cx        /*push nbytes */
  102. #if defined(MS)
  103.   asm call malloc/*call malloc */
  104. #else
  105.   asm call far ptr malloc/*call malloc */
  106. #endif
  107.   asm pop  cx        /*remove param */
  108.   asm pop bp        /*get real bp back */
  109.   asm pop cx        /*get the real sp back */
  110.   asm mov sp, cx    /*point to real stack */
  111.   asm pop cx        /*get real __brklvl back */
  112.   asm mov bx, __brklvl  /*get newly updated heap marker */
  113.   asm mov ts_brklev,bx  /*save it for the next time */
  114.   asm mov __brklvl, cx  /*restore the current heap marker */
  115.   asm mov cp , ax    
  116.   asm popf        /* restore cpuflags register */
  117.   swake() ;
  118.   critend(DOS_CRCLASS) ;
  119.   return cp ;
  120. #else
  121.     return cpcrit( DOS_CRCLASS , malloc , nbytes );
  122. #endif
  123. #endif
  124. }
  125.     
  126.