Skip to content

Commit

Permalink
Allocate memory in the correct context
Browse files Browse the repository at this point in the history
Commit 5a1bc58 introduced a new context for AsyncSource's needs,
but failed to allocate the memory for buffer during initializion in
that context.  Fix that.

Also, add some comments.
  • Loading branch information
amitlan committed Dec 27, 2016
1 parent 5cfa642 commit 6f6b540
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,29 @@ static Source *
CreateAsyncSource(const char *path, TupleDesc desc)
{
AsyncSource *self = palloc0(sizeof(AsyncSource));
MemoryContext oldcxt;

self->base.read = (SourceReadProc) AsyncSourceRead;
self->base.close = (SourceCloseProc) AsyncSourceClose;

self->size = INITIAL_BUF_LEN;
self->begin = 0;
self->end = 0;
self->buffer = palloc0(self->size);
self->errmsg[0] = '\0';

/* Create a dedicated context for our allocation needs */
self->context = AllocSetContextCreate(
CurrentMemoryContext,
"AsyncSource",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);

/* Must allocate memory for self->buffer in self->context */
oldcxt = MemoryContextSwitchTo(self->context);
self->buffer = palloc0(self->size);
MemoryContextSwitchTo(oldcxt);

self->eof = false;
self->fd = AllocateFile(path, "r");
if (self->fd == NULL)
Expand Down Expand Up @@ -202,6 +209,8 @@ AsyncSourceRead(AsyncSource *self, void *buffer, size_t len)
newsize = (len * 4 - 1) -
((len * 4 - 1) / READ_UNIT_SIZE) +
READ_UNIT_SIZE;

/* Switch to the dedicated context for our allocation needs */
oldcxt = MemoryContextSwitchTo(self->context);
newbuf = palloc0(newsize);

Expand Down

0 comments on commit 6f6b540

Please sign in to comment.