lua-users home
lua-l archive

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


I've been puzzling over lzio, because I'm thinking of using it, and I
noticed that the rather bizarrely luaZ_openspace (how about
"luaZ_ensurebuffer"?) could be simplified in terms of luaZ_resizebuffer:

since we have

#define luaZ_resizebuffer(L, buff, size) \
	(luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size,
char), \
	(buff)->buffsize = size)

we can simplify

char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
  if (n > buff->buffsize) {
    if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
    luaM_reallocvector(L, buff->buffer, buff->buffsize, n, char);
    buff->buffsize = n;
  }
  return buff->buffer;
}

to

char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
  if (n > luaZ_sizebuffer(buff)) {
    if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
    luaZ_resizebuffer(L, buff, n);
  }
  return luaZ_buffer(buff);
}

I've also used luaZ_ macros so that now this is an entirely "derived"
function, written in terms of the public API (but actually exactly the
same code underneath).

-- 
http://www.mupsych.org/~rrt/ | Psalm 19:12 <-- tagline for the guilty