home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 3.ddi / TASK.ZIP / TASKMEM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-22  |  1.3 KB  |  51 lines

  1. /*****************************************************
  2. File: TASKMEM.CPP   Copyright 1989 by Dlugosz Software
  3.    make new and delete lock under multitasking
  4. *****************************************************/
  5.  
  6. #include "usual.hpp"
  7. #include "task.hpp"
  8. #include "sem.hpp"
  9. #include <stdlib.h>
  10.    //for malloc() and free()
  11.  
  12. /* link this into a multitasking program so that new and delete
  13.    will get an exclusive lock when processing.  Otherwise, you
  14.    could clobber the heap when a malloc() is pre-empted and
  15.    another malloc() is called in another task!  */
  16.  
  17. void (* _new_handler)()= NULL;
  18.    /* I must define this, because Zortech's library defines it in the
  19.       same module as operator new.  So if this symbol is fetched from
  20.       the library, operator new comes with it and I get a linker
  21.       error.  */
  22.  
  23. semaphore MEM;
  24.  
  25.  
  26. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  27.  
  28. void* operator new (long size)
  29. {
  30. if (size == 0) return NULL;
  31. void* p;
  32. {  resource_lock x(MEM);
  33.    p= malloc((unsigned)size);
  34.    }
  35. if (!p && _new_handler) {
  36.    // call handler and then retry
  37.    _new_handler();
  38.    resource_lock x(MEM);
  39.    p= malloc((unsigned)size);
  40.    }
  41. return p;
  42. }
  43.  
  44. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  45.  
  46. void operator delete (void* p)
  47. {
  48. resource_lock x(MEM);
  49. free (p);
  50. }
  51.