init
This commit is contained in:
85
pg_include/lib/dllist.h
Executable file
85
pg_include/lib/dllist.h
Executable file
@@ -0,0 +1,85 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* dllist.h
|
||||
* simple doubly linked list primitives
|
||||
* the elements of the list are void* so the lists can contain anything
|
||||
* Dlelem can only be in one list at a time
|
||||
*
|
||||
*
|
||||
* Here's a small example of how to use Dllists:
|
||||
*
|
||||
* Dllist *lst;
|
||||
* Dlelem *elt;
|
||||
* void *in_stuff; -- stuff to stick in the list
|
||||
* void *out_stuff
|
||||
*
|
||||
* lst = DLNewList(); -- make a new dllist
|
||||
* DLAddHead(lst, DLNewElem(in_stuff)); -- add a new element to the list
|
||||
* with in_stuff as the value
|
||||
* ...
|
||||
* elt = DLGetHead(lst); -- retrieve the head element
|
||||
* out_stuff = (void*)DLE_VAL(elt); -- get the stuff out
|
||||
* DLRemove(elt); -- removes the element from its list
|
||||
* DLFreeElem(elt); -- free the element since we don't
|
||||
* use it anymore
|
||||
*
|
||||
*
|
||||
* It is also possible to use Dllist objects that are embedded in larger
|
||||
* structures instead of being separately malloc'd. To do this, use
|
||||
* DLInitElem() to initialize a Dllist field within a larger object.
|
||||
* Don't forget to DLRemove() each field from its list (if any) before
|
||||
* freeing the larger object!
|
||||
*
|
||||
*
|
||||
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* src/include/lib/dllist.h
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#ifndef DLLIST_H
|
||||
#define DLLIST_H
|
||||
|
||||
struct Dllist;
|
||||
struct Dlelem;
|
||||
|
||||
typedef struct Dlelem
|
||||
{
|
||||
struct Dlelem *dle_next; /* next element */
|
||||
struct Dlelem *dle_prev; /* previous element */
|
||||
void *dle_val; /* value of the element */
|
||||
struct Dllist *dle_list; /* what list this element is in */
|
||||
} Dlelem;
|
||||
|
||||
typedef struct Dllist
|
||||
{
|
||||
Dlelem *dll_head;
|
||||
Dlelem *dll_tail;
|
||||
} Dllist;
|
||||
|
||||
extern Dllist *DLNewList(void); /* allocate and initialize a list header */
|
||||
extern void DLInitList(Dllist *list); /* init a header alloced by caller */
|
||||
extern void DLFreeList(Dllist *list); /* free up a list and all the nodes in
|
||||
* it */
|
||||
extern Dlelem *DLNewElem(void *val);
|
||||
extern void DLInitElem(Dlelem *e, void *val);
|
||||
extern void DLFreeElem(Dlelem *e);
|
||||
extern void DLRemove(Dlelem *e); /* removes node from list */
|
||||
extern void DLAddHead(Dllist *list, Dlelem *node);
|
||||
extern void DLAddTail(Dllist *list, Dlelem *node);
|
||||
extern Dlelem *DLRemHead(Dllist *list); /* remove and return the head */
|
||||
extern Dlelem *DLRemTail(Dllist *list);
|
||||
extern void DLMoveToFront(Dlelem *e); /* move node to front of its list */
|
||||
|
||||
/* These are macros for speed */
|
||||
#define DLGetHead(list) ((list)->dll_head)
|
||||
#define DLGetTail(list) ((list)->dll_tail)
|
||||
#define DLGetSucc(elem) ((elem)->dle_next)
|
||||
#define DLGetPred(elem) ((elem)->dle_prev)
|
||||
#define DLGetListHdr(elem) ((elem)->dle_list)
|
||||
|
||||
#define DLE_VAL(elem) ((elem)->dle_val)
|
||||
|
||||
#endif /* DLLIST_H */
|
||||
156
pg_include/lib/stringinfo.h
Executable file
156
pg_include/lib/stringinfo.h
Executable file
@@ -0,0 +1,156 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* stringinfo.h
|
||||
* Declarations/definitions for "StringInfo" functions.
|
||||
*
|
||||
* StringInfo provides an indefinitely-extensible string data type.
|
||||
* It can be used to buffer either ordinary C strings (null-terminated text)
|
||||
* or arbitrary binary data. All storage is allocated with palloc().
|
||||
*
|
||||
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* src/include/lib/stringinfo.h
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#ifndef STRINGINFO_H
|
||||
#define STRINGINFO_H
|
||||
|
||||
/*-------------------------
|
||||
* StringInfoData holds information about an extensible string.
|
||||
* data is the current buffer for the string (allocated with palloc).
|
||||
* len is the current string length. There is guaranteed to be
|
||||
* a terminating '\0' at data[len], although this is not very
|
||||
* useful when the string holds binary data rather than text.
|
||||
* maxlen is the allocated size in bytes of 'data', i.e. the maximum
|
||||
* string size (including the terminating '\0' char) that we can
|
||||
* currently store in 'data' without having to reallocate
|
||||
* more space. We must always have maxlen > len.
|
||||
* cursor is initialized to zero by makeStringInfo or initStringInfo,
|
||||
* but is not otherwise touched by the stringinfo.c routines.
|
||||
* Some routines use it to scan through a StringInfo.
|
||||
*-------------------------
|
||||
*/
|
||||
typedef struct StringInfoData
|
||||
{
|
||||
char *data;
|
||||
int len;
|
||||
int maxlen;
|
||||
int cursor;
|
||||
} StringInfoData;
|
||||
|
||||
typedef StringInfoData *StringInfo;
|
||||
|
||||
|
||||
/*------------------------
|
||||
* There are two ways to create a StringInfo object initially:
|
||||
*
|
||||
* StringInfo stringptr = makeStringInfo();
|
||||
* Both the StringInfoData and the data buffer are palloc'd.
|
||||
*
|
||||
* StringInfoData string;
|
||||
* initStringInfo(&string);
|
||||
* The data buffer is palloc'd but the StringInfoData is just local.
|
||||
* This is the easiest approach for a StringInfo object that will
|
||||
* only live as long as the current routine.
|
||||
*
|
||||
* To destroy a StringInfo, pfree() the data buffer, and then pfree() the
|
||||
* StringInfoData if it was palloc'd. There's no special support for this.
|
||||
*
|
||||
* NOTE: some routines build up a string using StringInfo, and then
|
||||
* release the StringInfoData but return the data string itself to their
|
||||
* caller. At that point the data string looks like a plain palloc'd
|
||||
* string.
|
||||
*-------------------------
|
||||
*/
|
||||
|
||||
/*------------------------
|
||||
* makeStringInfo
|
||||
* Create an empty 'StringInfoData' & return a pointer to it.
|
||||
*/
|
||||
extern StringInfo makeStringInfo(void);
|
||||
|
||||
/*------------------------
|
||||
* initStringInfo
|
||||
* Initialize a StringInfoData struct (with previously undefined contents)
|
||||
* to describe an empty string.
|
||||
*/
|
||||
extern void initStringInfo(StringInfo str);
|
||||
|
||||
/*------------------------
|
||||
* resetStringInfo
|
||||
* Clears the current content of the StringInfo, if any. The
|
||||
* StringInfo remains valid.
|
||||
*/
|
||||
extern void resetStringInfo(StringInfo str);
|
||||
|
||||
/*------------------------
|
||||
* appendStringInfo
|
||||
* Format text data under the control of fmt (an sprintf-style format string)
|
||||
* and append it to whatever is already in str. More space is allocated
|
||||
* to str if necessary. This is sort of like a combination of sprintf and
|
||||
* strcat.
|
||||
*/
|
||||
extern void
|
||||
appendStringInfo(StringInfo str, const char *fmt,...)
|
||||
/* This extension allows gcc to check the format string */
|
||||
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
|
||||
|
||||
/*------------------------
|
||||
* appendStringInfoVA
|
||||
* Attempt to format text data under the control of fmt (an sprintf-style
|
||||
* format string) and append it to whatever is already in str. If successful
|
||||
* return true; if not (because there's not enough space), return false
|
||||
* without modifying str. Typically the caller would enlarge str and retry
|
||||
* on false return --- see appendStringInfo for standard usage pattern.
|
||||
*/
|
||||
extern bool
|
||||
appendStringInfoVA(StringInfo str, const char *fmt, va_list args)
|
||||
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 0)));
|
||||
|
||||
/*------------------------
|
||||
* appendStringInfoString
|
||||
* Append a null-terminated string to str.
|
||||
* Like appendStringInfo(str, "%s", s) but faster.
|
||||
*/
|
||||
extern void appendStringInfoString(StringInfo str, const char *s);
|
||||
|
||||
/*------------------------
|
||||
* appendStringInfoChar
|
||||
* Append a single byte to str.
|
||||
* Like appendStringInfo(str, "%c", ch) but much faster.
|
||||
*/
|
||||
extern void appendStringInfoChar(StringInfo str, char ch);
|
||||
|
||||
/*------------------------
|
||||
* appendStringInfoCharMacro
|
||||
* As above, but a macro for even more speed where it matters.
|
||||
* Caution: str argument will be evaluated multiple times.
|
||||
*/
|
||||
#define appendStringInfoCharMacro(str,ch) \
|
||||
(((str)->len + 1 >= (str)->maxlen) ? \
|
||||
appendStringInfoChar(str, ch) : \
|
||||
(void)((str)->data[(str)->len] = (ch), (str)->data[++(str)->len] = '\0'))
|
||||
|
||||
/*------------------------
|
||||
* appendStringInfoSpaces
|
||||
* Append a given number of spaces to str.
|
||||
*/
|
||||
extern void appendStringInfoSpaces(StringInfo str, int count);
|
||||
|
||||
/*------------------------
|
||||
* appendBinaryStringInfo
|
||||
* Append arbitrary binary data to a StringInfo, allocating more space
|
||||
* if necessary.
|
||||
*/
|
||||
extern void appendBinaryStringInfo(StringInfo str,
|
||||
const char *data, int datalen);
|
||||
|
||||
/*------------------------
|
||||
* enlargeStringInfo
|
||||
* Make sure a StringInfo's buffer can hold at least 'needed' more bytes.
|
||||
*/
|
||||
extern void enlargeStringInfo(StringInfo str, int needed);
|
||||
|
||||
#endif /* STRINGINFO_H */
|
||||
Reference in New Issue
Block a user