PostgreSQL Source Code git master
Functions
alignedalloc.c File Reference
#include "postgres.h"
#include "utils/memdebug.h"
#include "utils/memutils_memorychunk.h"
Include dependency graph for alignedalloc.c:

Go to the source code of this file.

Functions

void  AlignedAllocFree (void *pointer)
 
void *  AlignedAllocRealloc (void *pointer, Size size, int flags)
 
 
Size  AlignedAllocGetChunkSpace (void *pointer)
 

Function Documentation

AlignedAllocFree()

void AlignedAllocFree ( void *  pointer )

Definition at line 29 of file alignedalloc.c.

30{
31 MemoryChunk *chunk = PointerGetMemoryChunk(pointer);
32 void *unaligned;
33
35
37
38 /* obtain the original (unaligned) allocated pointer */
39 unaligned = MemoryChunkGetBlock(chunk);
40
41#ifdef MEMORY_CONTEXT_CHECKING
42 /* Test for someone scribbling on unused space in chunk */
43 if (!sentinel_ok(pointer, chunk->requested_size))
44 elog(WARNING, "detected write past chunk end in %s %p",
45 GetMemoryChunkContext(unaligned)->name, chunk);
46#endif
47
48 /*
49 * Create a dummy vchunk covering the start of the unaligned chunk, but
50 * not overlapping the aligned chunk. This will be freed while pfree'ing
51 * the unaligned chunk, keeping Valgrind happy. Then when we return to
52 * the outer pfree, that will clean up the vchunk for the aligned chunk.
53 */
55 (char *) pointer - (char *) unaligned);
56
57 /* Recursively pfree the unaligned chunk */
58 pfree(unaligned);
59}
#define WARNING
Definition: elog.h:36
#define elog(elevel,...)
Definition: elog.h:226
Assert(PointerIsAligned(start, uint64))
void pfree(void *pointer)
Definition: mcxt.c:1594
MemoryContext GetMemoryChunkContext(void *pointer)
Definition: mcxt.c:753
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
#define VALGRIND_MEMPOOL_ALLOC(context, addr, size)
Definition: memdebug.h:29
static bool MemoryChunkIsExternal(MemoryChunk *chunk)
static void * MemoryChunkGetBlock(MemoryChunk *chunk)
#define PointerGetMemoryChunk(p)
const char * name

References Assert(), elog, GetMemoryChunkContext(), MemoryChunkGetBlock(), MemoryChunkIsExternal(), name, pfree(), PointerGetMemoryChunk, VALGRIND_MAKE_MEM_DEFINED, VALGRIND_MEMPOOL_ALLOC, and WARNING.

AlignedAllocGetChunkContext()

MemoryContext AlignedAllocGetChunkContext ( void *  pointer )

Definition at line 154 of file alignedalloc.c.

155{
156 MemoryChunk *redirchunk = PointerGetMemoryChunk(pointer);
157 MemoryContext cxt;
158
159 VALGRIND_MAKE_MEM_DEFINED(redirchunk, sizeof(MemoryChunk));
160
161 Assert(!MemoryChunkIsExternal(redirchunk));
162
164
165 VALGRIND_MAKE_MEM_NOACCESS(redirchunk, sizeof(MemoryChunk));
166
167 return cxt;
168}
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27

References Assert(), GetMemoryChunkContext(), MemoryChunkGetBlock(), MemoryChunkIsExternal(), PointerGetMemoryChunk, VALGRIND_MAKE_MEM_DEFINED, and VALGRIND_MAKE_MEM_NOACCESS.

AlignedAllocGetChunkSpace()

Size AlignedAllocGetChunkSpace ( void *  pointer )

Definition at line 176 of file alignedalloc.c.

177{
178 MemoryChunk *redirchunk = PointerGetMemoryChunk(pointer);
179 void *unaligned;
180 Size space;
181
182 VALGRIND_MAKE_MEM_DEFINED(redirchunk, sizeof(MemoryChunk));
183
184 unaligned = MemoryChunkGetBlock(redirchunk);
185 space = GetMemoryChunkSpace(unaligned);
186
187 VALGRIND_MAKE_MEM_NOACCESS(redirchunk, sizeof(MemoryChunk));
188
189 return space;
190}
size_t Size
Definition: c.h:610
Size GetMemoryChunkSpace(void *pointer)
Definition: mcxt.c:767

References GetMemoryChunkSpace(), MemoryChunkGetBlock(), PointerGetMemoryChunk, VALGRIND_MAKE_MEM_DEFINED, and VALGRIND_MAKE_MEM_NOACCESS.

AlignedAllocRealloc()

void * AlignedAllocRealloc ( void *  pointer,
Size  size,
int  flags 
)

Definition at line 70 of file alignedalloc.c.

71{
72 MemoryChunk *redirchunk = PointerGetMemoryChunk(pointer);
73 Size alignto;
74 void *unaligned;
75 MemoryContext ctx;
76 Size old_size;
77 void *newptr;
78
79 VALGRIND_MAKE_MEM_DEFINED(redirchunk, sizeof(MemoryChunk));
80
81 alignto = MemoryChunkGetValue(redirchunk);
82 unaligned = MemoryChunkGetBlock(redirchunk);
83
84 /* sanity check this is a power of 2 value */
85 Assert((alignto & (alignto - 1)) == 0);
86
87 /*
88 * Determine the size of the original allocation. We can't determine this
89 * exactly as GetMemoryChunkSpace() returns the total space used for the
90 * allocation, which for contexts like aset includes rounding up to the
91 * next power of 2. However, this value is just used to memcpy() the old
92 * data into the new allocation, so we only need to concern ourselves with
93 * not reading beyond the end of the original allocation's memory. The
94 * drawback here is that we may copy more bytes than we need to, which
95 * only amounts to wasted effort. We can safely subtract the extra bytes
96 * that we requested to allow us to align the pointer. We must also
97 * subtract the space for the unaligned pointer's MemoryChunk since
98 * GetMemoryChunkSpace should have included that. This does assume that
99 * all context types use MemoryChunk as a chunk header.
100 */
101 old_size = GetMemoryChunkSpace(unaligned) -
102 PallocAlignedExtraBytes(alignto) - sizeof(MemoryChunk);
103
104#ifdef MEMORY_CONTEXT_CHECKING
105 /* check that GetMemoryChunkSpace returned something realistic */
106 Assert(old_size >= redirchunk->requested_size);
107#endif
108
109 /*
110 * To keep things simple, we always allocate a new aligned chunk and copy
111 * data into it. Because of the above inaccuracy, this may end in copying
112 * more data than was in the original allocation request size, but that
113 * should be OK.
114 */
115 ctx = GetMemoryChunkContext(unaligned);
116 newptr = MemoryContextAllocAligned(ctx, size, alignto, flags);
117
118 /* Cope cleanly with OOM */
119 if (unlikely(newptr == NULL))
120 {
121 VALGRIND_MAKE_MEM_NOACCESS(redirchunk, sizeof(MemoryChunk));
122 return MemoryContextAllocationFailure(ctx, size, flags);
123 }
124
125 /*
126 * We may memcpy more than the original allocation request size, which
127 * would result in trying to copy trailing bytes that the original
128 * MemoryContextAllocAligned call marked NOACCESS. So we must mark the
129 * entire old_size as defined. That's slightly annoying, but probably not
130 * worth improving.
131 */
132 VALGRIND_MAKE_MEM_DEFINED(pointer, old_size);
133 memcpy(newptr, pointer, Min(size, old_size));
134
135 /*
136 * Create a dummy vchunk covering the start of the old unaligned chunk,
137 * but not overlapping the aligned chunk. This will be freed while
138 * pfree'ing the old unaligned chunk, keeping Valgrind happy. Then when
139 * we return to repalloc, it will move the vchunk for the aligned chunk.
140 */
141 VALGRIND_MEMPOOL_ALLOC(ctx, unaligned,
142 (char *) pointer - (char *) unaligned);
143
144 pfree(unaligned);
145
146 return newptr;
147}
#define Min(x, y)
Definition: c.h:1003
#define unlikely(x)
Definition: c.h:402
void * MemoryContextAllocAligned(MemoryContext context, Size size, Size alignto, int flags)
Definition: mcxt.c:1460
void * MemoryContextAllocationFailure(MemoryContext context, Size size, int flags)
Definition: mcxt.c:1195
#define PallocAlignedExtraBytes(alignto)
static Size MemoryChunkGetValue(MemoryChunk *chunk)
struct MemoryChunk MemoryChunk

References Assert(), GetMemoryChunkContext(), GetMemoryChunkSpace(), MemoryChunkGetBlock(), MemoryChunkGetValue(), MemoryContextAllocAligned(), MemoryContextAllocationFailure(), Min, PallocAlignedExtraBytes, pfree(), PointerGetMemoryChunk, unlikely, VALGRIND_MAKE_MEM_DEFINED, VALGRIND_MAKE_MEM_NOACCESS, and VALGRIND_MEMPOOL_ALLOC.

AltStyle によって変換されたページ (->オリジナル) /