home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 092.lha / ASDG / LowMemoryServer / lms-example.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-21  |  3.9 KB  |  139 lines

  1. /* :ts=8 */
  2.  
  3. /*
  4. ; Programming Example Of Using The ASDG Low Memory Server
  5. ;
  6. ; Copyright 1987 By ASDG Incorporated
  7. ;
  8. ; For  non-commercial distribution  only. Commercial distribution
  9. ; or use is  strictly  forbidden  except under license from ASDG.
  10. ; Author: Perry S. Kivolowitz
  11. ; ASDG shall in no way be held responsible for any damage or loss
  12. ; of data which may result from the use or misuse of this program
  13. ; or data. ASDG makes no warranty with respect to the correct and
  14. ; propnctioning of this code or data. However, it is the be-
  15. ; lief of ASDG that this  program and  data is  correct and shall
  16. ; function properly with correct use.
  17. ;
  18. ; These modules were written for  use  with  Manx C.  Manx C is a
  19. ; product  of  the  Manx  Software Systems company whose language
  20. ; tools are used  exclusively by  ASDG  for all its software pro-
  21. ; ducts. Yes - this is an unsolicited plug for Manx - Perry K.
  22. ;
  23.     dseg
  24. ;
  25. ; you must provide  a LowMemBase  in your  C programs similar in
  26. ; concept to ExecBase or IntuitionBase etc.
  27. ;
  28.  
  29.     public    _LowMemBase
  30.  
  31. ;
  32. ; RegLowMemReq
  33. ;
  34. ; Register a message port with the low-memory notification service. From
  35. ; C this routine would be called as in:
  36. ;
  37. ;    res = RegLowMemReq(PortName , Space)
  38. ;                  A0        A1
  39. ;    where:
  40. ;
  41. ;    PortName is  a pointer  to a null terminated string representing 
  42. ;         the name  of  your port to which the low-memory service
  43. ;         will attempt to send a message.
  44. ;    Space     is a pointer to an initialized he printf's after the Wait may not work.
  45. */
  46.  
  47. #define    RipCordSize    16384
  48. char   *RipCord   = NULL;
  49.  
  50. CloseAll()
  51. {
  52.     if (LowMemoryPort) DeletePort(LowMemoryPort);
  53.     if (LowMemBase) CloseLibrary(LowMemBase);
  54.     if (RipCord) FreeMem(RipCord , RipCordSize);
  55.     printf("ASDG Low Memory Server Example (exiting)\n");
  56.     exit(0);
  57. }
  58.  
  59. main(argc , argv)
  60. char *argv[];
  61. {
  62.     int Result;
  63.  
  64.     if (!(RipCord = AllocMem(RipCordSize , 0L))) {
  65.         printf("Was  unable  to allocate  a rip  cord. You\n");
  66.         printf("must not have very  much memory  available\n");
  67.         printf("right now. You should consume memory while\n");
  68.         printf("this  example  is  running - NOT before it\n");
  69.         printf("starts!\n");
  70.         CloseAll();
  71.     }
  72.     printf("RipCord area has been allocated.\n");
  73.  
  74.     /* library base must be named LoeMemBase */
  75.     if (!(LowMemBase = OpenLibrary(LMSName , 0L))) {
  76.         printf("Was unable to open library: %s\n" , LMSName);
  77.         printf("Are you sure it is in ``libs:''?\n");
  78.         CloseAll();
  79.     }
  80.     printf("Successfully opened the ASDG Low Memory Server.\n");
  81.  
  82.     if (!(LowMemoryPort = CreatePort(MyPortName , 0L))) {
  83.         printf("Was unable to create a message port for use with\n");
  84.         printf("the ASDG Low Memory Server. Maybe you really are\n");
  85.         printf("low on memory?\n");
  86.         CloseAll();
  87.     }
  88.     LowMemSig = 1L << LowMemoryPort->mp_SigBit;
  89.     printf("Successfully created port whose name is: %s\n" , MyPortName);
  90.  
  91.     /* Important! Initialize lm_flag to LM_CONDITION_ACKNOWLEDGED
  92.     ** or no messages will be sent  to  you! This is the only ini-
  93.     ** tialization you need do.
  94.     */
  95.     LMM.lm_flag = LM_CONDITION_ACKNOWLEDGED;
  96.     printf("Low Memory Server Message initialized.\n");
  97.  
  98.     printf("Calling RegLowMemReq Now...\n");
  99.  
  100.     Result = RegLowMemReq(MyPortName , &LMM);
  101.  
  102.     printf("Result is: %d\n" , Result);
  103.     printf("This means: ");
  104.     switch (Result) {
  105.  
  106.     case LM_NOMEM:
  107.         printf("Not enough memory to store registration.\n");
  108.         break;
  109.  
  110.     case LM_BADNAME:
  111.         printf("Try another port name, that one is used already\n");
  112.         break;
  113.  
  114.     case 0: printf("All went well. Registration Accepted\n");
  115.         break;
  116.  
  117.     default:
  118.         printf("A bogus return value has come back to you!\n");
  119.         break;
  120.     }
  121.     if (Result < 0) CloseAll();
  122.  
  123.     printf("Waiting for you to run out of memory now.\n");
  124.     printf("Run something which will cause that situation to arise.\n");
  125.     (void) Wait(LowMemSig);
  126.  
  127.     FreeMem(RipCord , RipCordSize);
  128.     RipCord = NULL; /* don't free twice */
  129.     printf("Message received. Rip Cord Pulled\n");
  130.  
  131.     DeRegLowMemReq(MyPortName);
  132.     printf("Low memory registration has been canceled\n");
  133.  
  134.     CloseAll();
  135. }
  136.  
  137.