1996-08-28 03:59:28 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* memutils.h
|
1997-09-07 07:04:48 +02:00
|
|
|
* this file contains general memory alignment, allocation
|
|
|
|
* and manipulation stuff that used to be spread out
|
|
|
|
* between the following files:
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* align.h alignment macros
|
|
|
|
* aset.h memory allocation set stuff
|
|
|
|
* oset.h (used by aset.h)
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2000-05-21 04:23:30 +02:00
|
|
|
* $Id: memutils.h,v 1.35 2000/05/21 02:23:28 tgl Exp $
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef MEMUTILS_H
|
|
|
|
#define MEMUTILS_H
|
|
|
|
|
1999-03-25 04:49:34 +01:00
|
|
|
/* ----------------
|
|
|
|
* Alignment macros: align a length or address appropriately for a given type.
|
|
|
|
*
|
1999-03-25 20:05:19 +01:00
|
|
|
* There used to be some incredibly crufty platform-dependent hackery here,
|
|
|
|
* but now we rely on the configure script to get the info for us. Much nicer.
|
1999-03-25 04:49:34 +01:00
|
|
|
*
|
1999-06-17 17:16:09 +02:00
|
|
|
* NOTE: TYPEALIGN will not work if ALIGNVAL is not a power of 2.
|
1999-03-25 20:05:19 +01:00
|
|
|
* That case seems extremely unlikely to occur in practice, however.
|
1999-03-25 04:49:34 +01:00
|
|
|
* ----------------
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
#define TYPEALIGN(ALIGNVAL,LEN) (((long)(LEN) + (ALIGNVAL-1)) & ~(ALIGNVAL-1))
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1999-06-17 17:16:09 +02:00
|
|
|
#define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
|
|
|
|
#define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
|
|
|
|
#define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
|
|
|
|
#define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
|
|
|
|
#define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/*****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
* oset.h -- Fixed format ordered set definitions. *
|
1996-08-28 03:59:28 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
/* Note:
|
1997-09-07 07:04:48 +02:00
|
|
|
* Fixed format ordered sets are <EXPLAIN>.
|
|
|
|
* XXX This is a preliminary version. Work is needed to explain
|
|
|
|
* XXX semantics of the external definitions. Otherwise, the
|
|
|
|
* XXX functional interface should not change.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct OrderedElemData OrderedElemData;
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef OrderedElemData *OrderedElem;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
typedef struct OrderedSetData OrderedSetData;
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef OrderedSetData *OrderedSet;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
struct OrderedElemData
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
OrderedElem next; /* Next elem or &this->set->dummy */
|
|
|
|
OrderedElem prev; /* Previous elem or &this->set->head */
|
|
|
|
OrderedSet set; /* Parent set */
|
1996-08-28 03:59:28 +02:00
|
|
|
};
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
struct OrderedSetData
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
OrderedElem head; /* First elem or &this->dummy */
|
|
|
|
OrderedElem dummy; /* (hack) Terminator == NULL */
|
|
|
|
OrderedElem tail; /* Last elem or &this->head */
|
|
|
|
Offset offset; /* Offset from struct base to elem */
|
1997-09-07 07:04:48 +02:00
|
|
|
/* this could be signed short int! */
|
1996-08-28 03:59:28 +02:00
|
|
|
};
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void OrderedSetInit(OrderedSet set, Offset offset);
|
|
|
|
extern Pointer OrderedSetGetHead(OrderedSet set);
|
|
|
|
extern Pointer OrderedElemGetPredecessor(OrderedElem elem);
|
|
|
|
extern Pointer OrderedElemGetSuccessor(OrderedElem elem);
|
|
|
|
extern void OrderedElemPop(OrderedElem elem);
|
|
|
|
extern void OrderedElemPushInto(OrderedElem elem, OrderedSet Set);
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/*****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
* aset.h -- Allocation set definitions. *
|
1996-08-28 03:59:28 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* Description:
|
1997-09-07 07:04:48 +02:00
|
|
|
* An allocation set is a set containing allocated elements. When
|
|
|
|
* an allocation is requested for a set, memory is allocated and a
|
|
|
|
* pointer is returned. Subsequently, this memory may be freed or
|
|
|
|
* reallocated. In addition, an allocation set may be reset which
|
1999-05-23 01:19:37 +02:00
|
|
|
* will cause all memory allocated within it to be freed.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
1999-08-24 22:11:19 +02:00
|
|
|
* XXX The following material about allocation modes is all OUT OF DATE.
|
|
|
|
* aset.c currently implements only one allocation strategy,
|
|
|
|
* DynamicAllocMode, and that's the only one anyone ever requests anyway.
|
|
|
|
* If we ever did have more strategies, the new ones might or might
|
|
|
|
* not look like what is described here...
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Allocations may occur in four different modes. The mode of
|
|
|
|
* allocation does not affect the behavior of allocations except in
|
|
|
|
* terms of performance. The allocation mode is set at the time of
|
|
|
|
* set initialization. Once the mode is chosen, it cannot be changed
|
|
|
|
* unless the set is reinitialized.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* "Dynamic" mode forces all allocations to occur in a heap. This
|
|
|
|
* is a good mode to use when small memory segments are allocated
|
|
|
|
* and freed very frequently. This is a good choice when allocation
|
|
|
|
* characteristics are unknown. This is the default mode.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
1999-05-23 01:19:37 +02:00
|
|
|
* "Static" mode attempts to allocate space as efficiently as possible
|
1997-09-07 07:04:48 +02:00
|
|
|
* without regard to freeing memory. This mode should be chosen only
|
|
|
|
* when it is known that many allocations will occur but that very
|
|
|
|
* little of the allocated memory will be explicitly freed.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* "Tunable" mode is a hybrid of dynamic and static modes. The
|
|
|
|
* tunable mode will use static mode allocation except when the
|
|
|
|
* allocation request exceeds a size limit supplied at the time of set
|
|
|
|
* initialization. "Big" objects are allocated using dynamic mode.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* "Bounded" mode attempts to allocate space efficiently given a limit
|
|
|
|
* on space consumed by the allocation set. This restriction can be
|
|
|
|
* considered a "soft" restriction, because memory segments will
|
|
|
|
* continue to be returned after the limit is exceeded. The limit is
|
|
|
|
* specified at the time of set initialization like for tunable mode.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
* Note:
|
1997-09-07 07:04:48 +02:00
|
|
|
* Allocation sets are not automatically reset on a system reset.
|
|
|
|
* Higher level code is responsible for cleaning up.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
1999-05-23 01:19:37 +02:00
|
|
|
* There may be other modes in the future.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* AllocPointer
|
1997-09-07 07:04:48 +02:00
|
|
|
* Aligned pointer which may be a member of an allocation set.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
|
|
|
typedef Pointer AllocPointer;
|
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* AllocMode
|
1997-09-07 07:04:48 +02:00
|
|
|
* Mode of allocation for an allocation set.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
* Note:
|
1999-08-24 22:11:19 +02:00
|
|
|
* See above for a description of the various modes.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef enum AllocMode
|
|
|
|
{
|
|
|
|
DynamicAllocMode, /* always dynamically allocate */
|
|
|
|
StaticAllocMode, /* always "statically" allocate */
|
|
|
|
TunableAllocMode, /* allocations are "tuned" */
|
|
|
|
BoundedAllocMode /* allocations bounded to fixed usage */
|
1997-09-08 22:59:27 +02:00
|
|
|
} AllocMode;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#define DefaultAllocMode DynamicAllocMode
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1999-08-24 22:11:19 +02:00
|
|
|
typedef struct AllocSetData *AllocSet;
|
|
|
|
typedef struct AllocBlockData *AllocBlock;
|
|
|
|
typedef struct AllocChunkData *AllocChunk;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AllocSet
|
|
|
|
* Allocation set.
|
|
|
|
*/
|
|
|
|
typedef struct AllocSetData
|
|
|
|
{
|
|
|
|
AllocBlock blocks; /* head of list of blocks in this set */
|
|
|
|
#define ALLOCSET_NUM_FREELISTS 8
|
2000-04-12 19:17:23 +02:00
|
|
|
AllocChunk freelist[ALLOCSET_NUM_FREELISTS]; /* free chunk lists */
|
1999-08-24 22:11:19 +02:00
|
|
|
/* Note: this will change in the future to support other modes */
|
|
|
|
} AllocSetData;
|
|
|
|
|
1999-02-06 17:50:34 +01:00
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* AllocBlock
|
1999-08-24 22:11:19 +02:00
|
|
|
* An AllocBlock is the unit of memory that is obtained by aset.c
|
2000-04-12 19:17:23 +02:00
|
|
|
* from malloc(). It contains one or more AllocChunks, which are
|
1999-08-24 22:11:19 +02:00
|
|
|
* the units requested by palloc() and freed by pfree(). AllocChunks
|
|
|
|
* cannot be returned to malloc() individually, instead they are put
|
|
|
|
* on freelists by pfree() and re-used by the next palloc() that has
|
|
|
|
* a matching request size.
|
|
|
|
*
|
|
|
|
* AllocBlockData is the header data for a block --- the usable space
|
|
|
|
* within the block begins at the next alignment boundary.
|
1999-02-06 17:50:34 +01:00
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
typedef struct AllocBlockData
|
|
|
|
{
|
1999-08-24 22:11:19 +02:00
|
|
|
AllocSet aset; /* aset that owns this block */
|
|
|
|
AllocBlock next; /* next block in aset's blocks list */
|
|
|
|
char *freeptr; /* start of free space in this block */
|
|
|
|
char *endptr; /* end of space in this block */
|
1999-05-26 00:43:53 +02:00
|
|
|
} AllocBlockData;
|
1999-02-06 17:50:34 +01:00
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* AllocChunk
|
1999-02-06 17:50:34 +01:00
|
|
|
* The prefix of each piece of memory in an AllocBlock
|
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
typedef struct AllocChunkData
|
|
|
|
{
|
1999-05-23 01:19:37 +02:00
|
|
|
/* aset is the owning aset if allocated, or the freelist link if free */
|
1999-05-25 18:15:34 +02:00
|
|
|
void *aset;
|
1999-08-24 22:11:19 +02:00
|
|
|
/* size is always the size of the usable space in the chunk */
|
1999-05-25 18:15:34 +02:00
|
|
|
Size size;
|
1999-05-26 00:43:53 +02:00
|
|
|
} AllocChunkData;
|
1999-02-06 17:50:34 +01:00
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* AllocPointerIsValid
|
1997-09-07 07:04:48 +02:00
|
|
|
* True iff pointer is valid allocation pointer.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
|
|
|
#define AllocPointerIsValid(pointer) PointerIsValid(pointer)
|
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* AllocSetIsValid
|
1997-09-07 07:04:48 +02:00
|
|
|
* True iff set is valid allocation set.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
#define AllocSetIsValid(set) PointerIsValid(set)
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void AllocSetInit(AllocSet set, AllocMode mode, Size limit);
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void AllocSetReset(AllocSet set);
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
extern bool AllocSetContains(AllocSet set, AllocPointer pointer);
|
1996-08-28 03:59:28 +02:00
|
|
|
extern AllocPointer AllocSetAlloc(AllocSet set, Size size);
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void AllocSetFree(AllocSet set, AllocPointer pointer);
|
1998-09-01 06:40:42 +02:00
|
|
|
extern AllocPointer AllocSetRealloc(AllocSet set, AllocPointer pointer,
|
1997-09-07 07:04:48 +02:00
|
|
|
Size size);
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void AllocSetDump(AllocSet set);
|
2000-05-21 04:23:30 +02:00
|
|
|
extern void AllocSetStats(AllocSet set, const char *ident);
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
#endif /* MEMUTILS_H */
|