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++ 3.0r4 - <b>the page package</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
The Page Package

   The Page package allows the programmer to turn a block of memory from
   almost any source into a heap, from which memory can be dynamically
   allocated and freed.

   The Page package is a set of functions which allow the programmer to
   create and manipulate a heap (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 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 <io.h>
   #include <page.h>
   #include <stdlib.h>

   /* For Small Data Models only */

   #define HEAPSIZE 0x4000

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

   int 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");

       printf(" 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");
       printf(" is %04x bytes\n",maxsize);

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





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