[Freeswitch-dev] freeswitch development question (memory allocation?) [SOLVED]

Michael Jerris mike at jerris.com
Thu Jan 29 05:50:31 PST 2009


On Jan 29, 2009, at 8:23 AM, Apostolos Pantsiopoulos wrote:

> The question is : which pool should I use for each different thing I  
> am trying to accomplish?
>
> For instance the code below is part of my mod_radius_cdr  
> experimentation.
> When I am making use of a pool within a module should I :
>
> a) create a new pool?

Depends what scope you need it for.
>
> b) make use of an existing one?
Depends what scope you need it for.

> c) what kind of pool should I use?

There are not different kinds of pools
>
> d) is it really just one pool (accessed through different handlers)  
> and I am making myself look like a fool so far?

Depends what you mean by it, but there are many pools.


A memory pool is an object that you can use to make allocations that  
can all be freed at a later time as a single call to pool destroy.  It  
essentially balls up all your malloc calls and tracks them on a single  
handle to be freed later.  It also has some performance benefits over  
malloc in that it allocates in larger chunks usually then dishes out  
from those chunks on demand.  Because a pool is freed all together, we  
think of pools as having a scope.  For example, each FreeSWITCH  
session has a pool, and we use that pool to allocate things that need  
to be around for the life of the session such as the strings used in  
the caller profile.  It would be horribly complex to have to go and  
clean those things up later if they were malloc'ed strings, so the  
pool makes that easy for us.  So most of your questions above depend  
on what the scope of what you are trying to allocate should be.  If it  
is something that should live for the life of a session, you should  
probably use the session pool, if it is something your just using  
inside one function, in most cases you should just stack allocate, if  
its something that needs to live for a finite time but goes through  
many functions in complex interactions, (such as a file handle and all  
its file operations) you might want to stick your own new pool in your  
structure that you can destroy when you destroy that handle.

Mike

>
>
>
>
>
>
> Michael Jerris wrote:
>>
>> I am not sure even after re-reading what your question is, could you
>> try to rephrase?
>>
>> Mike
>>
>> On Jan 29, 2009, at 8:00 AM, Apostolos Pantsiopoulos wrote:
>>
>>
>>> I found it out by myself!
>>> (why is it that we always come with the solution right after posting
>>> to
>>> the list?)
>>>
>>> I inserted :
>>>
>>> thread_params = switch_core_session_alloc(session,
>>> sizeof(*thread_params));
>>>
>>> before the pool initialization.
>>>
>>> But still,  can I get some answers to the questions bellow about
>>> how to effectively handle memory allocations?
>>>
>>>
>>>
>>> Apostolos Pantsiopoulos wrote:
>>>
>>>> I have the code below :
>>>>
>>>> struct radacct_thread_handle {
>>>>        switch_core_session_t *session;
>>>>        switch_mutex_t *mutex;
>>>>        switch_thread_cond_t *cond;
>>>> };
>>>>
>>>> static switch_status_t my_on_routing(switch_core_session_t  
>>>> *session){
>>>>
>>>>        switch_thread_t *thread;
>>>>        switch_threadattr_t *thd_attr = NULL;
>>>>
>>>>        switch_memory_pool_t *pool;
>>>>
>>>>        struct radacct_thread_handle *thread_params = NULL;
>>>>
>>>>        pool = switch_core_session_get_pool(session);
>>>>
>>>>        thread_params->session = session;
>>>>
>>>>        ...
>>>>
>>>> }
>>>>
>>>> when the program reaches the last line (thread_params->session =
>>>> session;)
>>>> I get a core dump. Is this a memory allocation error? Is it because
>>>> I am
>>>> making
>>>> use of the wrong pool? Please enlighten me because I am not an
>>>> experienced c
>>>> programmer, and I am struggling to get familiar with the FS API.
>>>>
>>>>
>>>>
>>



More information about the Freeswitch-dev mailing list