Commit 1cfdb1cb authored by Tatsuo Ishii's avatar Tatsuo Ishii

Fix memory leak in BufFileCreateShared().

Also this commit unifies some duplicated code in makeBufFile() and
BufFileOpenShared() into new function makeBufFileCommon().

Author: Antonin Houska
Reviewed-By: Thomas Munro, Tatsuo Ishii
Discussion: https://postgr.es/m/16139.1529049566%40localhost
parent ff03112b
...@@ -99,6 +99,7 @@ struct BufFile ...@@ -99,6 +99,7 @@ struct BufFile
char buffer[BLCKSZ]; char buffer[BLCKSZ];
}; };
static BufFile *makeBufFileCommon(int nfiles);
static BufFile *makeBufFile(File firstfile); static BufFile *makeBufFile(File firstfile);
static void extendBufFile(BufFile *file); static void extendBufFile(BufFile *file);
static void BufFileLoadBuffer(BufFile *file); static void BufFileLoadBuffer(BufFile *file);
...@@ -106,21 +107,16 @@ static void BufFileDumpBuffer(BufFile *file); ...@@ -106,21 +107,16 @@ static void BufFileDumpBuffer(BufFile *file);
static int BufFileFlush(BufFile *file); static int BufFileFlush(BufFile *file);
static File MakeNewSharedSegment(BufFile *file, int segment); static File MakeNewSharedSegment(BufFile *file, int segment);
/* /*
* Create a BufFile given the first underlying physical file. * Create BufFile and perform the common initialization.
* NOTE: caller must set isInterXact if appropriate.
*/ */
static BufFile * static BufFile *
makeBufFile(File firstfile) makeBufFileCommon(int nfiles)
{ {
BufFile *file = (BufFile *) palloc(sizeof(BufFile)); BufFile *file = (BufFile *) palloc(sizeof(BufFile));
file->numFiles = 1; file->numFiles = nfiles;
file->files = (File *) palloc(sizeof(File)); file->offsets = (off_t *) palloc0(sizeof(off_t) * nfiles);
file->files[0] = firstfile;
file->offsets = (off_t *) palloc(sizeof(off_t));
file->offsets[0] = 0L;
file->isInterXact = false; file->isInterXact = false;
file->dirty = false; file->dirty = false;
file->resowner = CurrentResourceOwner; file->resowner = CurrentResourceOwner;
...@@ -128,6 +124,21 @@ makeBufFile(File firstfile) ...@@ -128,6 +124,21 @@ makeBufFile(File firstfile)
file->curOffset = 0L; file->curOffset = 0L;
file->pos = 0; file->pos = 0;
file->nbytes = 0; file->nbytes = 0;
return file;
}
/*
* Create a BufFile given the first underlying physical file.
* NOTE: caller must set isInterXact if appropriate.
*/
static BufFile *
makeBufFile(File firstfile)
{
BufFile *file = makeBufFileCommon(1);
file->files = (File *) palloc(sizeof(File));
file->files[0] = firstfile;
file->readOnly = false; file->readOnly = false;
file->fileset = NULL; file->fileset = NULL;
file->name = NULL; file->name = NULL;
...@@ -246,23 +257,12 @@ BufFileCreateShared(SharedFileSet *fileset, const char *name) ...@@ -246,23 +257,12 @@ BufFileCreateShared(SharedFileSet *fileset, const char *name)
{ {
BufFile *file; BufFile *file;
file = (BufFile *) palloc(sizeof(BufFile)); file = makeBufFileCommon(1);
file->fileset = fileset; file->fileset = fileset;
file->name = pstrdup(name); file->name = pstrdup(name);
file->numFiles = 1;
file->files = (File *) palloc(sizeof(File)); file->files = (File *) palloc(sizeof(File));
file->files[0] = MakeNewSharedSegment(file, 0); file->files[0] = MakeNewSharedSegment(file, 0);
file->offsets = (off_t *) palloc(sizeof(off_t));
file->offsets[0] = 0L;
file->isInterXact = false;
file->dirty = false;
file->resowner = CurrentResourceOwner;
file->curFile = 0;
file->curOffset = 0L;
file->pos = 0;
file->nbytes = 0;
file->readOnly = false; file->readOnly = false;
file->name = pstrdup(name);
return file; return file;
} }
...@@ -283,7 +283,6 @@ BufFileOpenShared(SharedFileSet *fileset, const char *name) ...@@ -283,7 +283,6 @@ BufFileOpenShared(SharedFileSet *fileset, const char *name)
File *files; File *files;
int nfiles = 0; int nfiles = 0;
file = (BufFile *) palloc(sizeof(BufFile));
files = palloc(sizeof(File) * capacity); files = palloc(sizeof(File) * capacity);
/* /*
...@@ -317,16 +316,8 @@ BufFileOpenShared(SharedFileSet *fileset, const char *name) ...@@ -317,16 +316,8 @@ BufFileOpenShared(SharedFileSet *fileset, const char *name)
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not open BufFile \"%s\"", name))); errmsg("could not open BufFile \"%s\"", name)));
file->numFiles = nfiles; file = makeBufFileCommon(nfiles);
file->files = files; file->files = files;
file->offsets = (off_t *) palloc0(sizeof(off_t) * nfiles);
file->isInterXact = false;
file->dirty = false;
file->resowner = CurrentResourceOwner; /* Unused, can't extend */
file->curFile = 0;
file->curOffset = 0L;
file->pos = 0;
file->nbytes = 0;
file->readOnly = true; /* Can't write to files opened this way */ file->readOnly = true; /* Can't write to files opened this way */
file->fileset = fileset; file->fileset = fileset;
file->name = pstrdup(name); file->name = pstrdup(name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment