Memory.c 981 Bytes
Newer Older
ASCP's avatar
ASCP committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
/*
 *	Flow
 *	Copyright (c) 1991 New Horizons Software, Inc.
 *
 *	Memory handling routines
 */

#include <Toolbox/Memory.h>

#include <proto/exec.h>

/*
 *	Local variables and definitions
 */

#define CHUNK_SIZE	8	/* Physical alloc chunks when allocating handles */

typedef struct {
	LONG	Size;		/* Actual data follows */
} PtrHdr;

/*
 *	NewPtr
 *	Allocate a new Ptr, return NULL if size <= 0 or error
 */

Ptr NewPtr(LONG size)
{
	register PtrHdr *ptrHdr;

	if (size > 0 &&
		(ptrHdr = MemAlloc(size + sizeof(PtrHdr), 0)) != NULL) {
		ptrHdr->Size = size;
		return ((Ptr) (ptrHdr + 1));
	}
	return (NULL);
}

/*
 *	DisposePtr
 *	Free memory allocated by Ptr
 */

void DisposePtr(Ptr ptr)
{
	register PtrHdr *ptrHdr = ptr;

	if (ptrHdr--)
		MemFree(ptrHdr, ptrHdr->Size + sizeof(PtrHdr));
}

/*
 *	GetPtrSize
 *	Return the logical size of Ptr
 *	Return 0 if Ptr is NULL
 */

LONG GetPtrSize(Ptr ptr)
{
	register PtrHdr *ptrHdr = ptr;

	return ((ptrHdr--) ? ptrHdr->Size : 0);
}