Use a self-defined memory acquirement function instead of realloc(3) in the l_alloc function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Use a self-defined memory acquirement function instead of realloc(3) in the l_alloc function

孙世龙 sunshilong
Hi, list

I want to use a self-defined memory acquirement function instead of
using realloc(3) in the l_alloc function.
Since I am a newbie, I don't know if there are some potential problems.
Could somebody shed some light on this problem?
Thank you for your attention to this matter.

For your convenience, here is the original l_alloc() function for lua.
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
  (void)ud; (void)osize;  /* not used */
  if (nsize == 0) {
    free(ptr);
    return NULL;
  }
  else
    return realloc(ptr, nsize);
}

Best regards.
Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: Use a self-defined memory acquirement function instead of realloc(3) in the l_alloc function

Roberto Ierusalimschy
> I want to use a self-defined memory acquirement function instead of
> using realloc(3) in the l_alloc function.
> Since I am a newbie, I don't know if there are some potential problems.
> Could somebody shed some light on this problem?

All you need to know is documented here:

  https://www.lua.org/manual/5.3/manual.html#lua_Alloc

As long as you meet those requirements, everything should be fine.

As already pointed out and documented in the above link, Lua assumes
that its l_alloc function cannot fail when shrinking a block. If your
collector does not assure it, it is trivial to change l_alloc so that it
returns the original block when a shrinking would fail:

static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
  (void)ud;  (void)osize;  /* not used */
  if (nsize == 0) {
    free(ptr);
    return NULL;
  }
  else {
    void *newptr = realloc(ptr, nsize);
    if (newptr == NULL && ptr != NULL && nsize <= osize)
      return ptr;  /* keep the original block */
    else
      return newptr;
  }
}

-- Roberto
Reply | Threaded
Open this post in threaded view
|

Re: Use a self-defined memory acquirement function instead of realloc(3) in the l_alloc function

孙世龙 sunshilong
Thank you for your reply.

I think it's a solution, but I still have a question.

>void *newptr = realloc(ptr, nsize);
>   if (newptr == NULL && ptr != NULL && nsize <= osize)
>      return ptr;  /* keep the original block */

If it runs into such code path.
Which value would you pass to the l_alloc function when invoking it
next time,  nsize or osize(i.e. nsize <= osize)?
You don't exactly know whether the expression (i.e. newptr == NULL &&
ptr != NULL && nsize <= osize) is true or not (when this function was
called last time).

Best Regards.
Sunshilong

On Mon, Aug 10, 2020 at 10:53 PM Roberto Ierusalimschy
<[hidden email]> wrote:

>
> > I want to use a self-defined memory acquirement function instead of
> > using realloc(3) in the l_alloc function.
> > Since I am a newbie, I don't know if there are some potential problems.
> > Could somebody shed some light on this problem?
>
> All you need to know is documented here:
>
>   https://www.lua.org/manual/5.3/manual.html#lua_Alloc
>
> As long as you meet those requirements, everything should be fine.
>
> As already pointed out and documented in the above link, Lua assumes
> that its l_alloc function cannot fail when shrinking a block. If your
> collector does not assure it, it is trivial to change l_alloc so that it
> returns the original block when a shrinking would fail:
>
> static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
>   (void)ud;  (void)osize;  /* not used */
>   if (nsize == 0) {
>     free(ptr);
>     return NULL;
>   }
>   else {
>     void *newptr = realloc(ptr, nsize);
>     if (newptr == NULL && ptr != NULL && nsize <= osize)
>       return ptr;  /* keep the original block */
>     else
>       return newptr;
>   }
> }
>
> -- Roberto
Reply | Threaded
Open this post in threaded view
|

Re: Use a self-defined memory acquirement function instead of realloc(3) in the l_alloc function

Roberto Ierusalimschy
> I think it's a solution, but I still have a question.
>
> >void *newptr = realloc(ptr, nsize);
> >   if (newptr == NULL && ptr != NULL && nsize <= osize)
> >      return ptr;  /* keep the original block */
>
> If it runs into such code path.
> Which value would you pass to the l_alloc function when invoking it
> next time,  nsize or osize(i.e. nsize <= osize)?
> You don't exactly know whether the expression (i.e. newptr == NULL &&
> ptr != NULL && nsize <= osize) is true or not (when this function was
> called last time).

I assume your question is about the 'osize' value . As the contract
says, 'osize' will be the 'nsize' passed in the previous call.  Note
that 'osize' is not used by 'realloc', so its value is irrelevant in this
standard implementation of 'l_alloc'.

-- Roberto