home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / remembertest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  4.5 KB  |  149 lines

  1. ;/* remembertest.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 remembertest.c
  3. Blink FROM LIB:c.o,remembertest.o TO remembertest LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  
  6. ** RememberTest - Illustrates the use of AllocRemember() and FreeRemember().
  7. */
  8. #define INTUI_V36_NAMES_ONLY
  9.  
  10. #include <exec/types.h>
  11. #include <exec/memory.h>
  12. #include <dos/dos.h>
  13. #include <intuition/intuition.h>
  14.  
  15. #include <clib/exec_protos.h>
  16. #include <clib/intuition_protos.h>
  17.  
  18. #include <stdlib.h>
  19.  
  20. #ifdef LATTICE
  21. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  22. int chkabort(void) { return(0); }  /* really */
  23. #endif
  24.  
  25. /* our function prototypes */
  26. VOID methodOne(VOID);
  27. VOID methodTwo(VOID);
  28.  
  29. struct IntuitionBase *IntuitionBase;
  30.  
  31. /* random sizes to demonstrate the Remember functions. */
  32. #define SIZE_A 100L
  33. #define SIZE_B 200L
  34.  
  35.  
  36. /*
  37. ** main() - Initialize everything.
  38. */
  39. VOID main(int argc, char **argv)
  40. {
  41. LONG exitVal = RETURN_OK;
  42.  
  43. IntuitionBase = OpenLibrary("intuition.library", 33L);
  44. if (IntuitionBase == NULL)
  45.     exitVal = RETURN_FAIL;
  46. else
  47.     {
  48.     methodOne();
  49.     methodTwo();
  50.  
  51.     CloseLibrary(IntuitionBase);
  52.     }
  53. exit(exitVal);
  54. }
  55.  
  56.  
  57. /*
  58. ** MethodOne
  59. ** Illustrates using AllocRemember() to allocate all memory and
  60. ** FreeRemember() to free it all.
  61. */
  62. VOID methodOne(VOID)
  63. {
  64. APTR memBlockA = NULL, memBlockB = NULL;
  65. struct Remember *rememberKey = NULL;
  66.  
  67. memBlockA = AllocRemember(&rememberKey, SIZE_A, MEMF_CLEAR | MEMF_PUBLIC);
  68. if (memBlockA)
  69.     {
  70.     /*  The memBlockA allocation succeeded; try for memBlockB.  */
  71.     memBlockB = AllocRemember(&rememberKey, SIZE_B, MEMF_CLEAR | MEMF_PUBLIC);
  72.     if (memBlockB)
  73.         {
  74.         /*  Both memory allocations succeeded.
  75.         **  The program may now use this memory.
  76.         */
  77.         }
  78.     }
  79.  
  80. /* It is not necessary to keep track of the status of each allocation.
  81. ** Intuition has kept track of all successful allocations by updating its
  82. ** linked list of Remember nodes.  The following call to FreeRemember() will
  83. ** deallocate any and all of the memory that was successfully allocated.
  84. ** The memory blocks as well as the link nodes will be deallocated because
  85. ** the "ReallyForget" parameter is TRUE.
  86. **
  87. ** It is possible to have reached the call to FreeRemember()
  88. ** in one of three states.  Here they are, along with their results.
  89. **
  90. ** 1. Both memory allocations failed.
  91. **       RememberKey is still NULL.  FreeRemember() will do nothing.
  92. ** 2. The memBlockA allocation succeeded but the memBlockB allocation failed.
  93. **       FreeRemember() will free the memory block pointed to by memBlockA.
  94. ** 3. Both memory allocations were successful.
  95. **       FreeRemember() will free the memory blocks pointed to by
  96. **       memBlockA and memBlockB.
  97. */
  98. FreeRemember(&rememberKey, TRUE);
  99. }
  100.  
  101. /*
  102. ** MethodTwo
  103. ** Illustrates using AllocRemember() to allocate all memory,
  104. ** FreeRemember() to free the link nodes, and FreeMem() to
  105. ** free the actual memory blocks.
  106. */
  107. VOID methodTwo(VOID)
  108. {
  109. APTR memBlockA = NULL, memBlockB = NULL;
  110. struct Remember *rememberKey = NULL;
  111.  
  112. memBlockA = AllocRemember(&rememberKey, SIZE_A, MEMF_CLEAR | MEMF_PUBLIC);
  113. if (memBlockA)
  114.     {
  115.     /*  The memBlockA allocation succeeded; try for memBlockB.  */
  116.     memBlockB = AllocRemember(&rememberKey, SIZE_B, MEMF_CLEAR | MEMF_PUBLIC);
  117.     if (memBlockB)
  118.         {
  119.         /* Both memory allocations succeeded.
  120.         ** For the purpose of illustration, FreeRemember() is called at
  121.         ** this point, but only to free the link nodes.  The memory pointed
  122.         ** to by memBlockA and memBlockB is retained.
  123.         */
  124.         FreeRemember(&rememberKey, FALSE);
  125.  
  126.         /* Individually free the two memory blocks. The Exec FreeMem()
  127.         ** call must be used, as the link nodes are no longer available.
  128.         */
  129.         FreeMem((VOID *)memBlockA, SIZE_A);
  130.         FreeMem((VOID *)memBlockB, SIZE_B);
  131.         }
  132.     }
  133.  
  134. /* It is possible to have reached the call to FreeRemember()
  135. ** in one of three states.  Here they are, along with their results.
  136. **
  137. ** 1. Both memory allocations failed.
  138. **    RememberKey is still NULL.  FreeRemember() will do nothing.
  139. ** 2. The memBlockA allocation succeeded but the memBlockB allocation failed.
  140. **    FreeRemember() will free the memory block pointed to by memBlockA.
  141. ** 3. Both memory allocations were successful.
  142. **    If this is the case, the program has already freed the link nodes
  143. **    with FreeRemember() and the memory blocks with FreeMem().
  144. **    When FreeRemember() freed the link nodes, it reset RememberKey
  145. **    to NULL.  This (second) call to FreeRemember() will do nothing.
  146. */
  147. FreeRemember(&rememberKey, TRUE);
  148. }
  149.