Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - page_package http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   page_package
   The  Page_package is a set of functions which allow the programmer  to
   create and manipulate a heap, that is, a block of memory set aside for
   dynamic  allocation.  The  heap is created  by  passing  the  function
   page_initialize the address and size of the block of memory that is to
   become  the  heap. The programmer can then use calls  to  page_malloc,
   page_calloc,  page_realloc and page_free to allocate memory from  this
   heap in a similar fashion to the standard memory allocation routines.

   All  of these functions require a far pointer to the page heap  as  an
   argument  so that they know which heap to act on. The page  allocation
   functions  return  the offset of the allocated block within  the  page
   heap rather than a pointer. The macro page_toptr can be used to obtain
   a far pointer to a location in the heap.

   Example
   #include <stdio.h>
   #include <dos.h>
   #include <io.h>
   #include <page.h>

   /* S and M models */
   #define HEAPSIZE 0x4000

   static char buffer[HEAPSIZE];
   /* a 16k static buffer */

   main()
   {
   unsigned maxsize, offset;
   char far *baseptr;
   int far *fp, i;

        baseptr = MK_FP(getDS(),buffer);
        maxsize = page_initialize(baseptr,0x4000);
        printf("The maximum allocatable size is %04x bytes\n",maxsize);

        if((offset = page_malloc(baseptr,0x800)) == 0)
             printf("Page_malloc failed\n ");
        else
             printf("Allocated 0x800 bytes successfully\n");

        fp = page_toptr(baseptr,offset);
        for (i = 0; i < 255; i++)
             fp[i] = i;

        printf("fp[50] = %d\n",*(fp+50));

        if((offset = page_realloc(baseptr,offset,0x1000))==0)
             printf("Page_realloc failed\n ");
        else
             printf("Re-allocated to 0x1000 bytes\n");

        fp = page_toptr(baseptr,offset);
        printf("fp[75] = %d\n",*(fp+75));
        maxsize = page_maxfree(baseptr);
        printf("Maximum free block remaining is %04x bytes\n",maxsize);

        if(page_free(baseptr,offset) == -1)
             printf("Page_free failed\n");
        else
             printf("Page freed successfully\n");
   }


Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson