lmem.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. ** $Id: lmem.h,v 1.34 2009/04/17 14:40:13 roberto Exp roberto $
  3. ** Interface to Memory Manager
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef lmem_h
  7. #define lmem_h
  8. #include <stddef.h>
  9. #include "llimits.h"
  10. #include "lua.h"
  11. #define MEMERRMSG "not enough memory"
  12. #define luaM_reallocv(L,b,on,n,e) \
  13. ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \
  14. luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \
  15. luaM_toobig(L))
  16. #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
  17. #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
  18. #define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0]))
  19. #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s))
  20. #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t)))
  21. #define luaM_newvector(L,n,t) \
  22. cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))
  23. #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s))
  24. #define luaM_growvector(L,v,nelems,size,t,limit,e) \
  25. if ((nelems)+1 > (size)) \
  26. ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))
  27. #define luaM_reallocvector(L, v,oldn,n,t) \
  28. ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t))))
  29. LUAI_FUNC void *luaM_toobig (lua_State *L);
  30. /* not to be called directly */
  31. LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
  32. size_t size);
  33. LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size,
  34. size_t size_elem, int limit,
  35. const char *what);
  36. #endif