head	1.13;
access;
symbols
	release_4_2:1.13
	aix_ok:1.11
	Version_2_1:1.7
	C_Demo_1:1.5;
locks; strict;
comment	@ * @;


1.13
date	94.06.16.03.23.06;	author aoki;	state Exp;
branches;
next	1.12;

1.12
date	94.01.30.19.12.40;	author sunita;	state Exp;
branches;
next	1.11;

1.11
date	91.11.12.20.20.29;	author mer;	state Exp;
branches;
next	1.10;

1.10
date	91.11.07.01.26.48;	author hong;	state Exp;
branches;
next	1.9;

1.9
date	91.04.28.09.14.05;	author cimarron;	state Exp;
branches;
next	1.8;

1.8
date	91.04.01.08.50.13;	author hong;	state Exp;
branches;
next	1.7;

1.7
date	91.02.24.00.48.02;	author cimarron;	state Exp;
branches;
next	1.6;

1.6
date	90.09.25.16.53.32;	author kemnitz;	state Exp;
branches;
next	1.5;

1.5
date	89.09.05.17.29.02;	author mao;	state C_Demo_1;
branches;
next	1.4;

1.4
date	89.05.07.09.36.24;	author hirohama;	state Exp;
branches;
next	1.2;

1.2
date	89.05.05.22.42.13;	author hirohama;	state Exp;
branches;
next	1.1;

1.1
date	89.05.05.22.27.15;	author hirohama;	state Exp;
branches;
next	;


desc
@@


1.13
log
@remove bogus proto
@
text
@/*
 * aset.c --
 *	Allocation set definitions.
 *
 * Note:
 *	XXX This is a preliminary implementation which lacks fail-fast
 *	XXX validity checking of arguments.
 */

#include "tmp/c.h"

RcsId("$Header: /import/faerie/aoki/postgres/src/backend/utils/mmgr/RCS/aset.c,v 1.12 1994/01/30 19:12:40 sunita Exp aoki $");

#include "utils/excid.h"	/* for ExhaustedMemory */
#include "utils/memutils.h"
#include "utils/log.h"

#undef AllocSetReset
#undef malloc
#undef free

/*
 * Internal type definitions
 */

/*
 * AllocElem --
 *	Allocation element.
 */
typedef struct AllocElemData {
	OrderedElemData	elemData;	/* elem in AllocSet */
	Size		size;
} AllocElemData;

typedef AllocElemData *AllocElem;


/*
 * Private method definitions
 */

/*
 * AllocPointerGetAllocElem --
 *	Returns allocation (internal) elem given (external) pointer.
 */
#define AllocPointerGetAllocElem(pointer)	(&((AllocElem)(pointer))[-1])

/*
 * AllocElemGetAllocPointer --
 *	Returns allocation (external) pointer given (internal) elem.
 */
#define AllocElemGetAllocPointer(alloc)	((AllocPointer)&(alloc)[1])

/*
 * AllocElemIsValid --
 *	True iff alloc is valid.
 */
#define AllocElemIsValid(alloc)	PointerIsValid(alloc)

/*
 * AllocSetGetFirst --
 *	Returns "first" allocation pointer in a set.
 *
 * Note:
 *	Assumes set is valid.
 */
static
AllocPointer
AllocSetGetFirst ARGS((
	AllocSet	set
));

/*
 * AllocPointerGetNext --
 *	Returns "successor" allocation pointer.
 *
 * Note:
 *	Assumes pointer is valid.
 */
static
AllocPointer
AllocPointerGetNext ARGS((
	AllocPointer	pointer
));

/*
 * Public routines
 */

/* 
 *	AllocPointerIsValid(pointer)
 * 	AllocSetIsValid(set)    
 *
 *		.. are now macros in aset.h -cim 4/27/91
 */

void
AllocSetInit(set, mode, limit)
	AllocSet	set;
	AllocMode	mode;
	Size		limit;
{
	AssertArg(PointerIsValid(set));
	AssertArg((int)DynamicAllocMode <= (int)mode);
	AssertArg((int)mode <= (int)BoundedAllocMode);

	/*
	 * XXX mode is currently ignored and treated as DynamicAllocMode.
	 * XXX limit is also ignored.  This affects this whole file.
	 */

	OrderedSetInit(&set->setData, offsetof(AllocElemData, elemData));
}

void
AllocSetReset(set)
	AllocSet	set;
{
	AllocPointer	pointer;

	AssertArg(AllocSetIsValid(set));

	while (AllocPointerIsValid(pointer = AllocSetGetFirst(set))) {
	    AllocSetFree(set, pointer);
	}
}

void
AllocSetReset_debug(file, line, set)
    String      file;
    int	        line;
    AllocSet	set;
{
    AllocPointer	pointer;

    AssertArg(AllocSetIsValid(set));

    while (AllocPointerIsValid(pointer = AllocSetGetFirst(set))) {
	alloc_set_message(file, line, pointer, set);
	AllocSetFree(set, pointer);
    }
}

bool
AllocSetContains(set, pointer)
	AllocSet	set;
	AllocPointer	pointer;
{
	AssertArg(AllocSetIsValid(set));
	AssertArg(AllocPointerIsValid(pointer));

	return (OrderedSetContains(&set->setData,
		&AllocPointerGetAllocElem(pointer)->elemData));
}

AllocPointer
AllocSetAlloc(set, size)
	AllocSet	set;
	Size		size;
{
	AllocElem	alloc;

	AssertArg(AllocSetIsValid(set));

	/* allocate */
	alloc = (AllocElem)malloc(sizeof (*alloc) + size);

	if (!PointerIsValid(alloc)) {
		elog (FATAL, "palloc failure: memory exhausted");
	}

	/* add to allocation list */
	OrderedElemPushInto(&alloc->elemData, &set->setData);

	/* set size */
	alloc->size = size;

	return (AllocElemGetAllocPointer(alloc));
}

void
AllocSetFree(set, pointer)
	AllocSet	set;
	AllocPointer	pointer;
{
	AllocElem	alloc;

	/* AssertArg(AllocSetIsValid(set)); */
	/* AssertArg(AllocPointerIsValid(pointer)); */
	AssertArg(AllocSetContains(set, pointer));

	alloc = AllocPointerGetAllocElem(pointer);

	/* remove from allocation set */
	OrderedElemPop(&alloc->elemData);

	/* free storage */
	delete(alloc);
}

AllocPointer
AllocSetRealloc(set, pointer, size)
	AllocSet	set;
	AllocPointer	pointer;
	Size		size;
{
	AllocPointer	newPointer;
	AllocElem	alloc;

	/* AssertArg(AllocSetIsValid(set)); */
	/* AssertArg(AllocPointerIsValid(pointer)); */
	AssertArg(AllocSetContains(set, pointer));

	/*
	 * Calling realloc(3) directly is not be possible (unless we use
	 * our own hacked version of malloc) since we must keep the
	 * allocations in the allocation set.
	 */

	alloc = AllocPointerGetAllocElem(pointer);

	/* allocate new pointer */
	newPointer = AllocSetAlloc(set, size);

	/* fill new memory */
	bcopy(pointer, newPointer, (alloc->size < size) ? alloc->size : size);

	/* free old pointer */
	AllocSetFree(set, pointer);

	return (newPointer);
}

Count
AllocSetIterate(set, function)
	AllocSet	set;
	void		(*function) ARGS((AllocPointer pointer));
{
	Count		count = 0;
	AllocPointer	pointer;

	AssertArg(AllocSetIsValid(set));

	for (pointer = AllocSetGetFirst(set);
			AllocPointerIsValid(pointer);
			pointer = AllocPointerGetNext(pointer)) {

		if (PointerIsValid(function)) {
			(*function)(pointer);
		}
		count += 1;
	}

	return (count);
}

Count
AllocSetCount(set)
AllocSet set;
{
	Count		count = 0;
	AllocPointer	pointer;

	AssertArg(AllocSetIsValid(set));

	for (pointer = AllocSetGetFirst(set);
			AllocPointerIsValid(pointer);
			pointer = AllocPointerGetNext(pointer)) {
		count++;
	}
	return count;
}

/*
 * Private routines
 */

static
AllocPointer
AllocSetGetFirst(set)
	AllocSet	set;
{
	AllocElem	alloc;

	alloc = (AllocElem)OrderedSetGetHead(&set->setData);

	if (!AllocElemIsValid(alloc)) {
		return (NULL);
	}

	return (AllocElemGetAllocPointer(alloc));
}

static
AllocPointer
AllocPointerGetNext(pointer)
	AllocPointer	pointer;
{
	AllocElem	alloc;

	alloc = (AllocElem)OrderedElemGetSuccessor(
		&AllocPointerGetAllocElem(pointer)->elemData);

	if (!AllocElemIsValid(alloc)) {
		return (NULL);
	}

	return (AllocElemGetAllocPointer(alloc));
}


/*
 * Debugging routines
 */

/*
 * XXX AllocPointerDump --
 *	Displays allocated pointer.
 */
void
AllocPointerDump(pointer)
	AllocPointer	pointer;
{
	printf("\t%-10d@@ %0#x\n", ((long*)pointer)[-1], pointer); /* XXX */
}

/*
 * AllocSetDump --
 *	Displays allocated set.
 */
void
AllocSetDump(set)
	AllocSet	set;
{
	int count;
	count = AllocSetIterate(set, AllocPointerDump);
	printf("\ttotal %d allocations\n", count);
}
@


1.12
log
@malloc failure generates a elog (FATAL, ..) rather than trap.
@
text
@d12 1
a12 1
RcsId("$Header: /private/src/postgres/src/backend/utils/mmgr/RCS/aset.c,v 1.11 1991/11/12 20:20:29 mer Exp sunita $");
a17 2
extern void bcopy();	/* XXX use header */

@


1.11
log
@prototyping changes
@
text
@d12 1
a12 1
RcsId("$Header: /users/mer/postgres/src/utils/mmgr/RCS/aset.c,v 1.10 1991/11/07 01:26:48 hong Exp mer $");
d16 1
a168 1
	Trap(!PointerIsValid(alloc), ExhaustedMemory);
d170 4
a200 1
	/* pg_free(alloc); */
@


1.10
log
@added and modified debug routines
@
text
@d12 1
a12 1
RcsId("$Header: RCS/aset.c,v 1.9 91/04/28 09:14:05 cimarron Exp $");
a13 1
#include "tmp/aset.h"
@


1.9
log
@Converted IsValid code into macros and added an improved NodeIsType scheme
@
text
@d12 1
a12 1
RcsId("$Header: RCS/aset.c,v 1.8 91/04/01 08:50:13 hong Exp Locker: cimarron $");
d257 6
d264 10
d335 3
a337 1
	AllocSetIterate(set, AllocPointerDump);
@


1.8
log
@we now also catches directly malloc's for memory leaks debugging
@
text
@d12 1
a12 1
RcsId("$Header: RCS/aset.c,v 1.7 91/02/24 00:48:02 cimarron Exp Locker: hong $");
d14 1
d92 6
a97 13
bool
AllocPointerIsValid(pointer)
	AllocPointer	pointer;
{
	return (PointerIsValid(pointer));
}

bool
AllocSetIsValid(set)
	AllocSet	set;
{
	return (PointerIsValid(set));	/* XXX incomplete */
}
@


1.7
log
@added palloc_debug tracing facility
@
text
@d12 1
a12 1
RcsId("$Header: RCS/aset.c,v 1.6 90/09/25 16:53:32 kemnitz Exp Locker: cimarron $");
d20 2
@


1.6
log
@Updating from revision 1.5 to revision 1.9
@
text
@d12 1
a12 1
RcsId("$Header: RCS/aset.c,v 1.9 90/08/18 00:42:44 cimarron Exp $");
d19 2
d130 1
a130 1
		AllocSetFree(set, pointer);
d132 16
@


1.5
log
@Working version of C-only demo
@
text
@d10 1
a10 1
#include "c.h"
d12 1
a12 1
RcsId("$Header: /usr6/postgres/mao/postgres/src/utils/mmgr/RCS/aset.c,v 1.4 89/05/07 09:36:24 hirohama Exp $");
d14 2
a15 2
#include "excid.h"	/* for ExhaustedMemory */
#include "oset.h"
a16 2
#include "aset.h"

d184 1
@


1.4
log
@renamed ...Step -> ...Iterate
@
text
@d12 1
a12 1
RcsId("$Header: aset.c,v 1.3 89/05/06 13:04:44 hirohama Locked $");
@


1.2
log
@*** empty log message ***
@
text
@d12 1
a12 1
RcsId("$Header: aset.c,v 1.1 89/05/05 22:27:15 hirohama Locked $");
d109 1
a109 1
	AssertArg(AllocSetIsValid(set));
d222 1
a222 1
AllocSetStep(set, function)
d306 1
a306 1
	AllocSetStep(set, AllocPointerDump);
@


1.1
log
@Initial revision
@
text
@d12 1
a12 1
RcsId("$Header$");
d110 2
a111 1
	AssertArg(DynamicAllocMode <= mode && mode <= BoundedAllocMode);
@
