librelist archives

« back to archive

Using customized memory allocation function

Using customized memory allocation function

From:
Zheng Yang
Date:
2011-05-11 @ 08:52
Hi,

Can I know how can I set a customized memory allocation function?
I'm not sure about what should I pass to the ctx.

I tried this:

yajl_alloc_funcs* funcs = (yajl_alloc_funcs *) palloc(sizeof(yajl_alloc_funcs));
funcs->malloc = palloc_wrapper;
funcs->realloc = repalloc_wrapper;
funcs->free = pfree_wrapper;
//funcs->ctx = NULL;

handle = yajl_alloc(&callbacks, funcs, (void *) ctx);


I got segmentation fault. Can anyone help why it's not working?

Thanks,
Yang

Re: [yajl] Using customized memory allocation function

From:
Lloyd Hilaiel
Date:
2011-05-11 @ 13:37
On May 11, 2011, at 2:52 AM, Zheng Yang wrote:
> Hi,
> 
> Can I know how can I set a customized memory allocation function?
> I'm not sure about what should I pass to the ctx.
> 
> I tried this:
> 
> yajl_alloc_funcs* funcs = (yajl_alloc_funcs *) palloc(sizeof(yajl_alloc_funcs));
> funcs->malloc = palloc_wrapper;
> funcs->realloc = repalloc_wrapper;
> funcs->free = pfree_wrapper;
> //funcs->ctx = NULL;
> 
> handle = yajl_alloc(&callbacks, funcs, (void *) ctx);
> 
> 
> I got segmentation fault. Can anyone help why it's not working?

This *looks* correct, and this feature is tested by the unit tests (used 
to test for memory leaks).  Is it possible that the segv is happening 
inside your allocation routines?

Do you have a stack trace?

very best,
lloyd

> Thanks,
> Yang
> 

Re: [yajl] Using customized memory allocation function

From:
Zheng Yang
Date:
2011-05-11 @ 15:45
Oh, there is one thing I forget to mention.


void* palloc_wrapper (void *ctx, size_t sz) {
	return palloc( sz);
}

void* repalloc_wrapper (void *ctx, void *ptr, size_t sz) {
	return repalloc(ptr, sz);
}

void pfree_wrapper(void *ctx, void *ptr) {
	return pfree(ptr);
}


The memory allocation functions are wrappers of postgresql palloc family. 
One thing I'm not sure about is the memory context. The palloc family 
doesn't seem to require a memory context.

Regard,
Yang


On 11-May-2011, at 9:37 PM, Lloyd Hilaiel wrote:

> On May 11, 2011, at 2:52 AM, Zheng Yang wrote:
>> Hi,
>> 
>> Can I know how can I set a customized memory allocation function?
>> I'm not sure about what should I pass to the ctx.
>> 
>> I tried this:
>> 
>> yajl_alloc_funcs* funcs = (yajl_alloc_funcs *) 
palloc(sizeof(yajl_alloc_funcs));
>> funcs->malloc = palloc_wrapper;
>> funcs->realloc = repalloc_wrapper;
>> funcs->free = pfree_wrapper;
>> //funcs->ctx = NULL;
>> 
>> handle = yajl_alloc(&callbacks, funcs, (void *) ctx);
>> 
>> 
>> I got segmentation fault. Can anyone help why it's not working?
> 
> This *looks* correct, and this feature is tested by the unit tests (used
to test for memory leaks).  Is it possible that the segv is happening 
inside your allocation routines?
> 
> Do you have a stack trace?
> 
> very best,
> lloyd
> 
>> Thanks,
>> Yang
>> 
> 

Re: [yajl] Using customized memory allocation function

From:
Hatem Nassrat
Date:
2011-06-28 @ 22:15
On Wed, May 11, 2011 at 12:45 PM, Zheng Yang <zhengyang4k@gmail.com> wrote:

>
> The memory allocation functions are wrappers of postgresql palloc family.
> One thing I'm not sure about is the memory context. The palloc family
> doesn't seem to require a memory context.
>
>
You can seee an example usage of the alloc functions at
https://github.com/lloyd/yajl/blob/master/test/yajl_test.c#L25 the ctx is
something Yajl adds to give you the ability to pass around a context object
(ptr) when parsing.

-- 
Hatem Nassrat