You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by lusob <lu...@gmail.com> on 2008/07/10 09:41:15 UTC

Instance a c++ object on Apache shared memory

Hello,
I'm developing an apache c++ module and I need to create a global persistent
object for all the processes.

I have tried to  declare  it as global, but it doesn't work

Apache only has shared functions for C object, but no for C++ object.
How can I do it?

Thanks in advance ;)
-- 
View this message in context: http://www.nabble.com/Instance-a-c%2B%2B-object-on-Apache-shared-memory-tp18377523p18377523.html
Sent from the Apache HTTP Server - Module Writers mailing list archive at Nabble.com.


Re: Instance a c++ object on Apache shared memory

Posted by Sorin Manolache <so...@gmail.com>.
On Thu, Jul 10, 2008 at 12:17, lusob <lu...@gmail.com> wrote:
>
> Hello, and thanks for the example!
> My class has another class as data member (instead a struct). How can I
> allocate it in shared memory?

As far as I know you cannot. But maybe I'm wrong.

S

> Sorin Manolache wrote:
>>
>> On Thu, Jul 10, 2008 at 09:41, lusob <lu...@gmail.com> wrote:
>>>
>>> Hello,
>>> I'm developing an apache c++ module and I need to create a global
>>> persistent
>>> object for all the processes.
>>
>> Hi,
>>
>> I'm doing something similar.
>>
>> I declare
>>
>> static MyClass *my_object
>>
>> globally in your module c++ file.
>>
>>
>> Then I hook post_config. There, I do
>>
>> MyClass *my_object = new MyClass(config_pool);
>>
>>
>> In the constructor of MyClass I create a shared memory segment:
>>
>> class MyClass {
>> public:
>>    MyClass(apr_pool_t *config_pool) {
>>        apr_shm_create(&m, sizeof(MyStruct), 0, config_pool);
>>        shared = apr_shm_baseaddr_get(m);
>>        // when config_pool is destroyed, the shared memory segment and
>> this object are destroyed too
>>        apr_pool_cleanup_register(config_pool, this, (apr_status_t
>> (*)(void *))my_cleanup, NULL);
>>        init(shared);
>>    }
>>    ~MyClass() {
>>        tear_down(shared);
>>        apr_shm_destroy(m);
>>    }
>> private:
>>    apr_shm_t *m;
>>    MyStruct *shared;
>> };
>>
>> apr_status_t my_cleanup(MyClass *obj) {
>>    delete obj;
>> }
>>
>> --
>> S
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Instance-a-c%2B%2B-object-on-Apache-shared-memory-tp18377523p18379811.html
> Sent from the Apache HTTP Server - Module Writers mailing list archive at Nabble.com.
>
>

Re: Instance a c++ object on Apache shared memory

Posted by Manfred Rebentisch <mr...@comparat.de>.
Am Donnerstag 10 Juli 2008 12:17:04 schrieb lusob:
> Hello, and thanks for the example!
> My class has another class as data member (instead a struct). How can I
> allocate it in shared memory?
> Thanks again!!

I think, it is better to use C-structs for data only in shared memory. It is 
not a memory for allocate and free many times, because you may reuse memory 
lecks by yourself.

Manfred

Re: Instance a c++ object on Apache shared memory

Posted by lusob <lu...@gmail.com>.
Hello, and thanks for the example!
My class has another class as data member (instead a struct). How can I
allocate it in shared memory?
Thanks again!!


Sorin Manolache wrote:
> 
> On Thu, Jul 10, 2008 at 09:41, lusob <lu...@gmail.com> wrote:
>>
>> Hello,
>> I'm developing an apache c++ module and I need to create a global
>> persistent
>> object for all the processes.
> 
> Hi,
> 
> I'm doing something similar.
> 
> I declare
> 
> static MyClass *my_object
> 
> globally in your module c++ file.
> 
> 
> Then I hook post_config. There, I do
> 
> MyClass *my_object = new MyClass(config_pool);
> 
> 
> In the constructor of MyClass I create a shared memory segment:
> 
> class MyClass {
> public:
>    MyClass(apr_pool_t *config_pool) {
>        apr_shm_create(&m, sizeof(MyStruct), 0, config_pool);
>        shared = apr_shm_baseaddr_get(m);
>        // when config_pool is destroyed, the shared memory segment and
> this object are destroyed too
>        apr_pool_cleanup_register(config_pool, this, (apr_status_t
> (*)(void *))my_cleanup, NULL);
>        init(shared);
>    }
>    ~MyClass() {
>        tear_down(shared);
>        apr_shm_destroy(m);
>    }
> private:
>    apr_shm_t *m;
>    MyStruct *shared;
> };
> 
> apr_status_t my_cleanup(MyClass *obj) {
>    delete obj;
> }
> 
> --
> S
> 
> 

-- 
View this message in context: http://www.nabble.com/Instance-a-c%2B%2B-object-on-Apache-shared-memory-tp18377523p18379811.html
Sent from the Apache HTTP Server - Module Writers mailing list archive at Nabble.com.


Re: Instance a c++ object on Apache shared memory

Posted by Sorin Manolache <so...@gmail.com>.
On Thu, Jul 10, 2008 at 09:41, lusob <lu...@gmail.com> wrote:
>
> Hello,
> I'm developing an apache c++ module and I need to create a global persistent
> object for all the processes.

Hi,

I'm doing something similar.

I declare

static MyClass *my_object

globally in your module c++ file.


Then I hook post_config. There, I do

MyClass *my_object = new MyClass(config_pool);


In the constructor of MyClass I create a shared memory segment:

class MyClass {
public:
   MyClass(apr_pool_t *config_pool) {
       apr_shm_create(&m, sizeof(MyStruct), 0, config_pool);
       shared = apr_shm_baseaddr_get(m);
       // when config_pool is destroyed, the shared memory segment and
this object are destroyed too
       apr_pool_cleanup_register(config_pool, this, (apr_status_t
(*)(void *))my_cleanup, NULL);
       init(shared);
   }
   ~MyClass() {
       tear_down(shared);
       apr_shm_destroy(m);
   }
private:
   apr_shm_t *m;
   MyStruct *shared;
};

apr_status_t my_cleanup(MyClass *obj) {
   delete obj;
}

--
S