home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377a.lha / libraries / intuition / other / remembertest.c next >
Encoding:
C/C++ Source or Header  |  1980-02-04  |  5.8 KB  |  177 lines

  1. /* RememberTest.c                                                    */
  2. /* Illustrates the use of AllocRemember() and FreeRemember().        */
  3. /* Compiled with Lattice C v5.02                                     */
  4. /* Compiler invoked with: lc -b1 -cfist -L -v -w                     */
  5.  
  6. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  7.  *
  8.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  9.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  10.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  11.  * information on the correct usage of the techniques and operating system
  12.  * functions presented in this example.  The source and executable code of
  13.  * this example may only be distributed in free electronic form, via bulletin
  14.  * board or as part of a fully non-commercial and freely redistributable
  15.  * diskette.  Both the source and executable code (including comments) must
  16.  * be included, without modification, in any copy.  This example may not be
  17.  * published in printed form or distributed with any commercial product.
  18.  * However, the programming techniques and support routines set forth in
  19.  * this example may be used in the development of original executable
  20.  * software products for Commodore Amiga computers.
  21.  * All other rights reserved.
  22.  * This example is provided "as-is" and is subject to change; no warranties
  23.  * are made.  All use is at your own risk.  No liability or responsibility
  24.  * is assumed.
  25.  */
  26.  
  27. #include <exec/types.h>
  28. #include <exec/memory.h>
  29. #include <intuition/intuition.h>
  30. #include <intuition/intuitionbase.h>
  31.  
  32. #include <proto/all.h>
  33. #include <stdlib.h>
  34. int CXBRK(void) {return(0);}
  35.  
  36. struct IntuitionBase *IntuitionBase;
  37.  
  38. #define SIZE_A 100L
  39. #define SIZE_B 200L
  40.  
  41. #define FLAGS_A (MEMF_CLEAR | MEMF_PUBLIC)
  42. #define FLAGS_B MEMF_PUBLIC
  43.  
  44. VOID methodOne(VOID), methodTwo(VOID);
  45.  
  46. VOID main(int argc, char *argv[])
  47. {
  48. int exitVal = RETURN_OK;
  49.  
  50. /* Open Intuition */
  51. IntuitionBase = (struct Intuition *) OpenLibrary("intuition.library", 33L);
  52. if (IntuitionBase)
  53.     {
  54.     methodOne();
  55.  
  56.     methodTwo();
  57.  
  58.     CloseLibrary((struct Library *)IntuitionBase);
  59.     }
  60. else
  61.     exitVal = RETURN_FAIL;
  62.  
  63. exit(exitVal);
  64. }
  65.  
  66.  
  67. /* MethodOne
  68.    Illustrates using AllocRemember() to allocate all memory and
  69.    FreeRemember() to free it all.
  70. */
  71. VOID methodOne(VOID)
  72. {
  73. CPTR memBlockA = NULL, memBlockB = NULL;
  74. struct Remember *rememberKey = NULL;
  75.  
  76. memBlockA = (CPTR)AllocRemember(&rememberKey, SIZE_A, FLAGS_A);
  77. if (memBlockA)
  78.     {
  79.     /*  The memBlockA allocation succeeded; try for memBlockB.  */
  80.     memBlockB = (CPTR)AllocRemember(&rememberKey, SIZE_B, FLAGS_B);
  81.     if (memBlockB)
  82.         {
  83.         /*  Both memory allocations succeeded.
  84.             The program may now use this memory.
  85.         */
  86.         }
  87.     }
  88.  
  89. /*
  90.    It is not necessary to keep track of the status of each allocation.
  91.    Intuition has kept track of all successful allocations by updating its
  92.    linked list of Remember nodes.  The following call to FreeRemember() will
  93.    deallocate any and all of the memory that was successfully allocated.
  94.    The memory blocks as well as the link nodes will be deallocated because
  95.    the "ReallyForget" parameter is TRUE.
  96. */
  97.  
  98. FreeRemember(&rememberKey, TRUE);
  99.  
  100. /*
  101.    It is possible to have reached the above call to FreeRemember()
  102.    in one of three states.  Here they are, along with their results.
  103.  
  104.    1. Both memory allocations failed.
  105.          RememberKey is still NULL.  FreeRemember() will do nothing.
  106.    2. The memBlockA allocation succeeded but the memBlockB allocation failed.
  107.          FreeRemember() will free the memory block pointed to by memBlockA.
  108.    3. Both memory allocations were successful.
  109.          FreeRemember() will free the memory blocks pointed to by
  110.          memBlockA and memBlockB.
  111. */
  112.  
  113. }
  114.  
  115.  
  116. /* MethodTwo
  117.    Illustrates using AllocRemember() to allocate all memory,
  118.    FreeRemember() to free the link nodes, and FreeMem() to
  119.    free the actual memory blocks.
  120. */
  121. VOID methodTwo(VOID)
  122. {
  123. CPTR memBlockA = NULL, memBlockB = NULL;
  124. struct Remember *rememberKey = NULL;
  125.  
  126. memBlockA = (CPTR)AllocRemember(&rememberKey, SIZE_A, FLAGS_A);
  127. if (memBlockA)
  128.     {
  129.     /*  The memBlockA allocation succeeded; try for memBlockB.  */
  130.     memBlockB = (CPTR)AllocRemember(&rememberKey, SIZE_B, FLAGS_B);
  131.     if (memBlockB)
  132.         {
  133.         /*  Both memory allocations succeeded. */
  134.  
  135.         /*  For the purpose of illustration, FreeRemember()
  136.             is called at this point, but only to free the
  137.             link nodes.  The memory pointed to by memBlockA
  138.             and memBlockB is retained.
  139.         */
  140.         FreeRemember(&rememberKey, FALSE);
  141.  
  142.         /*  Pretend that memBlockA was needed only temporarily.
  143.             It can now be freed.  The Exec FreeMem() call must
  144.             be used, as the link nodes are no longer available.
  145.         */
  146.         FreeMem((VOID *)memBlockA, SIZE_A);
  147.  
  148.         /*  The memory pointed to by memBlockB is used by the program.  */
  149.  
  150.  
  151.         /*  All memory blocks allocated with AllocRemember() must be
  152.             freed individually, now that the link nodes are gone.
  153.         */
  154.         FreeMem((VOID *)memBlockB, SIZE_B);
  155.  
  156.         }
  157.     }
  158.  
  159. FreeRemember(&rememberKey, TRUE);
  160.  
  161. /*
  162.    It is possible to have reached the above call to FreeRemember()
  163.    in one of three states.  Here they are, along with their results.
  164.  
  165.    1. Both memory allocations failed.
  166.          RememberKey is still NULL.  FreeRemember() will do nothing.
  167.    2. The memBlockA allocation succeeded but the memBlockB allocation failed.
  168.          FreeRemember() will free the memory block pointed to by memBlockA.
  169.    3. Both memory allocations were successful.
  170.          If this is the case, the program has already freed the link nodes
  171.          with FreeRemember() and the memory blocks with FreeMem().
  172.          When FreeRemember() freed the link nodes, it reset RememberKey
  173.          to NULL.  This (second) call to FreeRemember() will do nothing.
  174. */
  175.  
  176. }
  177.