aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/mmgr/palloc.c
blob: e4d822156f2c6b068c786e79dced831d264b9a51 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*-------------------------------------------------------------------------
 *
 * palloc.c--
 *	  POSTGRES memory allocator code.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/utils/mmgr/Attic/palloc.c,v 1.6 1998/02/26 04:38:22 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */

#include <string.h>

#include "c.h"

#include "utils/mcxt.h"
#include "utils/elog.h"
#include "utils/palloc.h"

#include "nodes/memnodes.h"

#include "utils/palloc.h"

/* ----------------------------------------------------------------
 *		User library functions
 * ----------------------------------------------------------------
 */

#undef palloc
#undef pfree
#undef MemoryContextAlloc
#undef MemoryContextFree
#undef malloc
#undef free

/* define PALLOC_IS_MALLOC if you want palloc to go straight to the
   raw malloc, without concern for the extra bookkeeping needed to
   ensure garbage is collected at the end of transactions - jolly 1/12/94 */


/*
 * palloc --
 *		Returns pointer to aligned memory of specified size.
 *
 * Exceptions:
 *		BadArgument if size < 1 or size >= MaxAllocSize.
 *		ExhaustedMemory if allocation fails.
 *		NonallocatedPointer if pointer was not returned by palloc or repalloc
 *				or may have been freed already.
 *
 * pfree --
 *		Frees memory associated with pointer returned from palloc or repalloc.
 *
 * Exceptions:
 *		BadArgument if pointer is invalid.
 *		FreeInWrongContext if pointer was allocated in a different "context."
 *		NonallocatedPointer if pointer was not returned by palloc or repalloc
 *				or may have been subsequently freed.
 */
void *
palloc(Size size)
{
#ifdef PALLOC_IS_MALLOC
	return malloc(size);
#else
	return (MemoryContextAlloc(CurrentMemoryContext, size));
#endif							/* PALLOC_IS_MALLOC */
}

void
pfree(void *pointer)
{
#ifdef PALLOC_IS_MALLOC
	free(pointer);
#else
	MemoryContextFree(CurrentMemoryContext, pointer);
#endif							/* PALLOC_IS_MALLOC */
}

/*
 * repalloc --
 *		Returns pointer to aligned memory of specified size.
 *
 * Side effects:
 *		The returned memory is first filled with the contents of *pointer
 *		up to the minimum of size and psize(pointer).  Pointer is freed.
 *
 * Exceptions:
 *		BadArgument if pointer is invalid or size < 1 or size >= MaxAllocSize.
 *		ExhaustedMemory if allocation fails.
 *		NonallocatedPointer if pointer was not returned by palloc or repalloc
 *				or may have been freed already.
 */
void *
repalloc(void *pointer, Size size)
{
#ifdef PALLOC_IS_MALLOC
	return realloc(pointer, size);
#else
	return (MemoryContextRealloc(CurrentMemoryContext, pointer, size));
#endif
}

/* pstrdup
	allocates space for and copies a string
	just like strdup except it uses palloc instead of malloc */
char *
pstrdup(char *string)
{
	char	   *nstr;

	nstr = (char *) palloc(strlen(string) + 1);
	strcpy(nstr, string);

	return nstr;
}