how to resize with pjlib?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Mar 27, 2008 at 12:23 PM, Nigel Hsiung <nigelcz at hotmail.com> wrote:
>
>  Hi list,
>
> I've got a few questions -
>
> 1. What is the pjlib equivalent to a realloc()?

Nothing. If you configure the pool to automatically resize (by setting
the "increment size" argument to non zero when you create the pool),
the pool will resize itself automatically anytime you allocate a
memory chunk from the pool and there's no space left in the pool. If
you don't configure pool to automatically resize, or if the resize
operation fails, then the pool will throw PJ_NO_MEMORY_EXCEPTION
exception.

> 2. Can i do this -
>
>     char str1[32] = {"123456"};
>     char str2[32] ={"123456789012345"};
>     char str3[32] ={"1234"};
>     char str4[32] ={"1234678"};
>
>     pj_str_t  pj_string;
>
>     //1st call
>     pj_strdup2_with_null(pool, &pj_string, str1); // no problem here
>
>     //2nd call
>     if(pj_string.slen > strlen(str2))
>         pj_strcpy2(&pj_string, str2);   //no problem here too
>     else
>         pj_strdup2_with_null(pool, &pj_string, str2); // ok to call
> pj_strdup2 again?

Yes it's okay.

>    //3rd call with str3 ...
>
>    //4th call with str4
>    // cannot use pj_string.slen to decide whether to pj_strcpy or pj_strdup,
> right?

In general, yes. The "slen" member of the pj_str_t only says about the
string length, and not the size of the buffer allocated by the pool
and which was assigned to "ptr" member of pj_str_t. I think there are
three ways to solve this:

1. always call pj_strdup() to assign your string.
2. allocate a fixed, large size capacity to the string, e.g.:

     pj_string.ptr = pj_pool_alloc(pool, MAX_LEN);

     if (strlen(str1) <= MAX_LEN) pj_strcpy2(&pj_string, str1);
     else error();

     if (strlen(str2) <= MAX_LEN) pj_strcpy2(&pj_string, str2);
     else error();

3. maintain the capacity information yourself, e.g.:

    int capacity = MAX_LEN;
    pj_string.ptr = pj_pool_alloc(pool, capacity);

    if (strlen(str1) <= capacity) pj_strcpy2(&pj_string, str1);
    else capacity=strlen(str1), pj_strdup2(pool, &pj_string, str1);

    if (strlen(str2) <= capacity) pj_strcpy2(&pj_string, str2);
    else capacity=strlen(str2), pj_strdup2(pool, &pj_string, str2);

Cheers
 Benny


>     Please help!
>
> thanks,
> Nigel



[Index of Archives]     [Asterisk Users]     [Asterisk App Development]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [Linux API]
  Powered by Linux