I think that the approach put forth by Brian and amplified by James is an
elegant one, provided you are using a GCC compiler of sufficient recency to
support the notion of a placement new.
For me I discovered that I couldn't get the compiler to accept the one liner:
pData = new ((TransformationData *) TSmalloc(sizeof(TransformationData)))
TransformationData();
and ended up having to use:
TransformationData* pData = (TransformationData*)
Tsmalloc(sizeof(TransformationData));
pData = new (pData) TransformationData();
Also I had to change:
typedef struct {
int state;
TSVIO output_vio;
TSIOBuffer output_buffer;
TSIOBufferReader output_reader;
std::string foo;
} TransformationData;
To
typedef struct TransformationData {
int state;
TSVIO output_vio;
TSIOBuffer output_buffer;
TSIOBufferReader output_reader;
std::string foo;
TransformationData() {}
} TransformationData;
In retrospect I could have probably gotten by with the one liner after making
the change to the typedef. If time permits I will test that out tomorrow.
From: Manas Agarwal <[email protected]<mailto:[email protected]>>
Reply-To:
"[email protected]<mailto:[email protected]>"
<[email protected]<mailto:[email protected]>>
Date: Thu, 24 Jan 2013 22:21:51 -0800
To: "[email protected]<mailto:[email protected]>"
<[email protected]<mailto:[email protected]>>
Subject: Re: Question on TSMalloc
I too faced similar issue.
I replaced string with string*. I think the problem is TSMalloc doesn't create
and initialize the object as it is based on malloc.
On Fri, Jan 25, 2013 at 10:49 AM, James Peach
<[email protected]<mailto:[email protected]>> wrote:
On 24/01/2013, at 3:14 PM, Brian Geffon
<[email protected]<mailto:[email protected]>> wrote:
> Try just using a placement new.
>
> pData = new ((TransformationData *) TSmalloc(sizeof(TransformationData)))
> TransformationData();
and don't forget to call the destructor before calling TSfree:
pData->~TransformationData();
TSfree(pData);
>
> Brian
>
> On Thu, Jan 24, 2013 at 2:32 PM, Owens, Steve
> <[email protected]<mailto:[email protected]>> wrote:
>
> I ran into a bit of an issue today related to TSMalloc
>
> Suppose I have a plugin which defines a plugin data structure such as:
>
> typedef struct {
> int state;
> TSVIO output_vio;
> TSIOBuffer output_buffer;
> TSIOBufferReader output_reader;
> std::string foo;
> } TransformationData;
>
>
> And I have some plugin code which does this:
>
> void allocTransformationData(){
> TransformationData *pData;
>
> pData = (TransformationData *) TSmalloc(sizeof(TransformationData));
> pData->state = STATE_START;
> pData->output_buffer = NULL;
> pData->output_vio = NULL;
> pData->output_reader = NULL;
>
> pData->foo.assign("bar");
>
> return pData;
> }
>
> The line pData->foo.assign("bar");
>
>
> Will result in a segmentation fault.
>
> Is there a best practice idiom for using std::string items in custom
> continuation data? For example should I be using
>
> typedef struct {
> int state;
> TSVIO output_vio;
> TSIOBuffer output_buffer;
> TSIOBufferReader output_reader;
> std::string *foo;
> } TransformationData;
>
> And
>
>
> Void allocTransformationData(){
> TransformationData *pData;
>
> pData = (TransformationData *) TSmalloc(sizeof(TransformationData));
> pData->state = STATE_START;
> pData->output_buffer = NULL;
> pData->output_vio = NULL;
> pData->output_reader = NULL;
>
>
> pData->foo = new std::string;
> pData->foo.assign("bar");
>
> return pData;
> }
>
> Or should I use:
>
> Void allocTransformationData(){
> TransformationData *pData;
>
> pData = (TransformationData *) TSmalloc(sizeof(TransformationData));
> pData->state = STATE_START;
> pData->output_buffer = NULL;
> pData->output_vio = NULL;
> pData->output_reader = NULL;
>
>
> pData->foo = (std::string*) Tsmalloc(sizeof(std::string));
> pData->foo.assign("bar");
>
> return pData;
> }
>
>
>
>
>
>