Lua alloc function with just malloc and free causes crash

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

Lua alloc function with just malloc and free causes crash

John D. Ramsdell-2
I'm using Lua 5.1.1 in an environment that provides only malloc and
free.  Initially, I wrote my Lua allocator so that whenever Lua
requested that the size of an object be reduced, it simply returned
the object unaltered.  This allocator works well.

Today, I tried to allow Lua to better compact its memory usage by
allocating a reduced sized copy of an object when Lua requests that
the size of an object be reduced, and then freeing the original.  This
version of the allocator causes a horrible crash.

The Lua documentation states that Lua assumes that the allocator never
fails when osize >= nsize.  Does Lua also assume that the following
expression always evaluates to true when osize >= nsize?

  ptr == my_alloc(void *ud, void *ptr, size_t osize, size_t nsize)

If so, please update the documentation.  If not, I'll start debugging
the implementation of malloc and free I'm using.  Thanks in advance.

John

#include <stddef.h>
#include <string.h>
void *malloc(size_t size);
void free(void *ptr);

void *my_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
    if (nsize == 0) {
	free(ptr);
	return NULL;
    }
    else if (ptr == NULL)
	return malloc(nsize);
#if defined COMPACT
    else if (osize == nsize) {
        return ptr;
    else if (osize > nsize) {
	void *p = malloc(nsize);
	if (p == NULL)
	    return ptr;
	memcpy(p, ptr, osize);
	free(ptr);
	return p;
    }
#else
    else if (osize >= nsize)
	return ptr;
#endif
    else {
	void *p = malloc(nsize);
	if (p == NULL)
	    return p;
	memcpy(p, ptr, osize);
	free(ptr);
	return p;
    }
}

Reply | Threaded
Open this post in threaded view
|

Re: Lua alloc function with just malloc and free causes crash

Klaus Ripke
On Sat, Aug 05, 2006 at 10:16:47AM -0400, John D. Ramsdell wrote:
>     else if (osize > nsize) {
> 	void *p = malloc(nsize);
> 	if (p == NULL)
> 	    return ptr;
> 	memcpy(p, ptr, osize);
how about using nsize here?

Reply | Threaded
Open this post in threaded view
|

Re: Lua alloc function with just malloc and free causes crash

John D. Ramsdell-2
Klaus Ripke <[hidden email]> writes:

> On Sat, Aug 05, 2006 at 10:16:47AM -0400, John D. Ramsdell wrote:
> >     else if (osize > nsize) {
> > 	void *p = malloc(nsize);
> > 	if (p == NULL)
> > 	    return ptr;
> > 	memcpy(p, ptr, osize);
> how about using nsize here?

This fixes the problem.  Please ignore my original post.  Thank you
Klaus. 

John

Reply | Threaded
Open this post in threaded view
|

Re: Lua alloc function with just malloc and free causes crash

Roberto Ierusalimschy
In reply to this post by John D. Ramsdell-2
> The Lua documentation states that Lua assumes that the allocator never
> fails when osize >= nsize.  Does Lua also assume that the following
> expression always evaluates to true when osize >= nsize?
> 
>   ptr == my_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
> 

No. You can move the block when shrinking it.

-- Roberto