File:  [OS/2 SDKs] / os2sdk / demos / examples / suballoc / suballoc.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:26:14 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: os2sdk-1988, HEAD
Microsoft OS/2 SDK 03-01-1988

/* This example illustrates the use of the following MS OS/2 functions:
 *	DosSubSet, DosSubAlloc, DosSubFree
 *
 * This example allocates a segment <A>, and initialises it for
 * suballocation. It suballocates a block <B> from <A>, and writes
 * some value into a word in the block <B>. It then grows segment
 * <A>, and notifies the suballocator of the new size. It checks
 * that the value written into block <B> is still there, frees block
 * <B>, frees segment <A>, and terminates.
 *
 * This example program is intended to illustrate the possible functions
 * rather than do anything useful. It can run as a Family API program.
 *
 * Compile as: cl -AL -G0 -Lp suballoc.c
 *
 * Created by Microsoft Corp. 1986
 */

#define INCL_DOSMEMMGR

#include	<os2def.h>     /* SELECTOROF and OFFSETOF macros  */
#include	<bsedos.h>     /* MS OS/2 function declarations   */

/* definitions used in DosAllocSeg call */
#define	SIZEA	16000		/* bytes to allocate */
#define PRIVATE 0		/* segment will not be shared */

/* definitions used in DosSubSet call */
#define INIT	 1		/* initialise size of subset in a segment*/
#define INCRSIZE 0		/* increase size of subset in a segment  */

#define	SUBSIZEA 10000		/* size to suballocate */
#define SIZER	40000		/* size to reallocate */
#define BLKSIZE  100		/* size of block within the subset       */
#define SUBSIZEB 20000		/* size to suballocate after realloc     */
#define VALUEA   5		/* value to write to first word in block */

main ()
{
	SEL		Selector;	/* selector for segment allocated    */
	USHORT		BlockOffset,	/* offset to block in segment subset */
			*addr;		/*32 bit address to a word in segment*/

	DosAllocSeg (SIZEA, &Selector, PRIVATE); /* allocate a segment */

	/* subset the segment to size SUBSIZEA ( must be =< SIZEA ) */
	DosSubSet (Selector, INIT, SUBSIZEA);

	DosSubAlloc(Selector, &BlockOffset, BLKSIZE); /* suballocate block B */

	/* write VALUEA in to first word in block B of the subset */
	SELECTOROF(addr) = Selector;
	OFFSETOF(addr) = BlockOffset;
	*addr = VALUEA;
	
	DosReallocSeg (SIZER, Selector); /* grow the segment to size SIZER */

	/* subset the segment to size SUBSIZEB where,
         * (SUBSIZEB > SUBSIZEA) and (SUBSIZEB =< SIZER) */
	DosSubSet(Selector, INCRSIZE, SUBSIZEB);

	/* first word in block B should still contain VALUEA */
	if (*addr != VALUEA)
          printf ("*** error: unexpected value in suballocated block ***\n");

	DosSubFree (Selector, BlockOffset, BLKSIZE); /* free block B */

	DosFreeSeg (Selector);		/* free the segment */
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.