Thursday, February 19, 2015

Learning C Functions : allocmem, _dos_allocmem ( Dos Only )

C Programming :
Function :
allocmem, _dos_allocmem

Syntax :

#include <dos.h>
int allocmem(unsigned size, unsigned *segp);
unsigned _dos_allocmem(unsigned size, unsigned *segp);

Description :

allocmem and _dos_allocmem use the DOS system call 0x48 to allocate a block of free memory and return the segment address of the allocated block .
size is the desired size in paragraphs (a paragraph is 16 bytes). segp is a pointer to a word that will be assigned the segment address of the newly
allocated block .
For allocmem, if not enough room is available, no assignment is made to the word pointed to by segp.
For _dos_allocmem, if not enough room is available, the size of the largest available block will be stored in the word pointed to by segp.
All allocated blocks are paragraph-aligned.
allocmem and _dos_allocmem cannot coexist with malloc.

Return Value:

allocmem returns -1 on success. In the event of error, allocmem returns a number indicating the size in paragraphs of the largest available block.
_dos_allocmem returns 0 on success. In the event of error, _dos_allocmem returns the DOS error code and sets the word pointed to by segp to the
size in paragraphs of the largest available block.
An error return from allocmem or _dos_allocmem sets the global variable _doserrno and sets the global variable errno to:

ENOMEM    Not enough memory

 Example :





/*   _dos_allocmem example    */

#include <dos.h>
#include <stdio.h>

int main(void)
{
  unsigned int size, segp, err, maxb;
  size = 64; /* (64 x 16) = 1024 bytes */
  err = _dos_allocmem(size, &segp);
  if (err == 0)
    printf("Allocated memory at segment: %x\n", segp);
  else {
    perror("Unable to allocate block ");
    printf("Maximum no. of paragraphs available is %u\n", segp);
    return 1;
  }
  if (_dos_setblock(size * 2, segp, &maxb) == 0)
    printf(" Expanded memory block at segment: %X\n", segp);
  else {
    perror("Unable to expand block ");
    printf("Maximum no. of paragraphs  available is %u\n", maxb);
  }
  _dos_freemem(segp);
  return 0;
}

No comments:

Post a Comment