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

  1. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  2.  *
  3.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  4.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  5.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  6.  * information on the correct usage of the techniques and operating system
  7.  * functions presented in this example.  The source and executable code of
  8.  * this example may only be distributed in free electronic form, via bulletin
  9.  * board or as part of a fully non-commercial and freely redistributable
  10.  * diskette.  Both the source and executable code (including comments) must
  11.  * be included, without modification, in any copy.  This example may not be
  12.  * published in printed form or distributed with any commercial product.
  13.  * However, the programming techniques and support routines set forth in
  14.  * this example may be used in the development of original executable
  15.  * software products for Commodore Amiga computers.
  16.  * All other rights reserved.
  17.  * This example is provided "as-is" and is subject to change; no warranties
  18.  * are made.  All use is at your own risk.  No liability or responsibility
  19.  * is assumed.
  20.  */
  21.  
  22. /* Compiled with Lattice 5.04: LC -b1 -cfist -v -y
  23.  * Linkage: blink from lib:c.o+allocate.o to Allocate lib lib:lc.lib lib:amiga.lib
  24.  */
  25.  
  26. #include <exec/types.h>
  27. #include <exec/memory.h>
  28.  
  29. #ifdef LATTICE
  30. #include <proto/all.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #endif
  34.  
  35. #define BLOCKSIZE 4000     /* or whatever you want */
  36. VOID main(VOID);
  37.  
  38. VOID main(VOID)
  39. {
  40.     struct MemHeader *mh;
  41.     struct MemChunk  *mc;
  42.     APTR   block1;
  43.     APTR   block2;
  44.  
  45.     /* Get the MemHeader needed to keep track of our new block */
  46.     mh = (struct MemHeader *)AllocMem((LONG)sizeof(struct MemHeader), MEMF_CLEAR);
  47.     if (!mh)
  48.         exit(10);
  49.  
  50.     /* Get the actual block the above MemHeader will manage */
  51.     mc = (struct MemChunk *)AllocMem(BLOCKSIZE, 0);
  52.     if (!mc)
  53.         {
  54.         FreeMem(mh, (LONG)sizeof(struct MemHeader)); exit(10);
  55.         }
  56.  
  57.     mh->mh_Node.ln_Type = NT_MEMORY;
  58.     mh->mh_Node.ln_Name = "myname";
  59.     mh->mh_First = mc;
  60.     mh->mh_Lower = (APTR)mc;
  61.     mh->mh_Upper = (APTR)(BLOCKSIZE + (ULONG)mc);
  62.     mh->mh_Free  = BLOCKSIZE;
  63.  
  64.     /* Set up first chunk in the freelist */
  65.     mc->mc_Next  = NULL;
  66.     mc->mc_Bytes = BLOCKSIZE;
  67.  
  68.     block1 = (APTR)Allocate(mh,20);
  69.     block2 = (APTR)Allocate(mh, 314);
  70.  
  71.     printf("mh = $%lx mc=$%lx\n", mh, mc);
  72.     printf("block1 = $%lx block2 = $%lx\n", block1, block2);
  73.  
  74.     FreeMem(mh, (LONG)sizeof(struct MemHeader));
  75.     FreeMem(mc, (LONG)BLOCKSIZE);
  76. }
  77.