mremap and bus error

Lists: pgsql-hackers
From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: mremap and bus error
Date: 2010-03-31 15:05:58
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

I playing with mmap. I have a problem with mremap function. It
doesn't allocate correct memory. All others work well.

static void *
mmap_realloc(void *ptr, Size size)
{
Size oldsize;
void *result;
int i;
char *x;

ptr = (char *) ptr - MAXALIGN(sizeof(Size));

oldsize = *((Size *) ptr);
size = MAXALIGN(size) + MAXALIGN(sizeof(Size));

ptr = mremap(ptr, oldsize,
size,
MREMAP_MAYMOVE);

if (ptr == MAP_FAILED)
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed on request of size %lu.",
(unsigned long) size)));

*((Size*) ptr) = size;
result = (char *) ptr + MAXALIGN(sizeof(Size));

/* check allocated memory */
x = ptr;

fprintf(stderr, "allocated memory %ld\n", size);
for (i = 0; i < size; i++)
{
if (i > oldsize)
{
*x = 0;
}
x++;
}
fprintf(stderr, "memory checked\n");

return result;
}

log:
>>>> 1 <<<>>> 1048584
>>>> 3 <<<>>> 2097216
>>>> 3 <<<>>> 9024
allocated memory 10304
memory checked
allocated memory 11584
memory checked
allocated memory 12864

gdb

Program terminated with signal 7, Bus error.
#0 mmap_realloc (size=12864, ptr=<value optimized out>) at mmap_alloc.c:357
357 *x = 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.10.2-1.x86_64

(gdb) print x
$1 = 0x7f92055df000 ""
(gdb) print i
$2 = 12289

Can somebody help me?

Regards
Pavel Stehule


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: mremap and bus error
Date: 2010-04-01 17:16:19
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Mar 31, 2010 at 11:05 AM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
> Can somebody help me?

I think it's pretty hopeless to try to debug this without the complete
source code...

...Robert


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: mremap and bus error
Date: 2010-04-01 17:28:47
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

2010/4/1 Robert Haas <robertmhaas(at)gmail(dot)com>:
> On Wed, Mar 31, 2010 at 11:05 AM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
>> Can somebody help me?
>
> I think it's pretty hopeless to try to debug this without the complete
> source code...

I am sorry. I am able to write test case, but I leaved it. Simply, I
don't use a function mremap. I searched about it on net, and I think
it is known issue on Linux :(.

Regards
Pavel Stehule

>
> ...Robert
>