C++ module

Shuxin Yang shuxinyang.oss at gmail.com
Thu Jun 16 06:41:06 UTC 2016

On 06/15/2016 09:18 PM, Rajalakshmi Iyer wrote:
> Thanks everyone.
> I have been able to compile a C++ module fine. But I am primarily 
> trying to figure out if it's safe to use features like STL containers 
> or Boost libraries within an Nginx module.
In my project, I used STL. it works fine.

> I am asking this because if I try to allocate an std::string within 
> say the module context, I see a segmentation fault. I assume it's 
> because of the Nginx pool mechanism?

Not sure what is "say the module context", and how you allocate 
std::string on Nginx pool. I believe there
are following combinations of "allocating std::string" and Nginx-pool usage.

    1).  does not use pool at all, statement like:

          std::string *p = new std::string("haha"); // instance 
allocated from heap
          std::string s("haha") // instance allocated from stack.

    2)  use pool to allocate std::string instance,  but use heap to 
allocate data: like following

        the instance is allocate on pool via placement new, but the data 
is allocated from heap.

         void *p = ngx_palloc(enough size)
         std::string *s = new(p) std::string("haha")

         it is fine so long as string instance's life time is not longer 
than pool

     3). if you want to allocate data on top of ngx pool (for efficiency 
reasons), it is bit involved.
          first, you need to define an allocation class like this
          (NOTE that the MEM_POOL is similar to Nginx pool in many 
ways). and feed to allocator
          to construction.

            e.g. typedef mempool_allocator<node_type> my_allocator;
                   tyepdef std::vector<node_type, my_alloctor> my_vector_t;

                   ngx_pool_t* the_ngx_pool = ....

                   my_vector_t vect(myalloctor(the_ngx_pool))

                so the element will allocate from the nginx pool.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20160615/c81c18fd/attachment.html>

More information about the nginx-devel mailing list