1323530Savg/*
2323530Savg** $Id: lmem.h,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $
3323530Savg** Interface to Memory Manager
4323530Savg** See Copyright Notice in lua.h
5323530Savg*/
6323530Savg
7323530Savg#ifndef lmem_h
8323530Savg#define lmem_h
9323530Savg
10323530Savg
11323530Savg#include <sys/zfs_context.h>
12323530Savg
13323530Savg#include "llimits.h"
14323530Savg#include "lua.h"
15323530Savg
16323530Savg
17323530Savg/*
18323530Savg** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is
19323530Savg** always constant.
20323530Savg** The macro is somewhat complex to avoid warnings:
21323530Savg** +1 avoids warnings of "comparison has constant result";
22323530Savg** cast to 'void' avoids warnings of "value unused".
23323530Savg*/
24323530Savg#define luaM_reallocv(L,b,on,n,e) \
25323530Savg  (cast(void, \
26323530Savg     (cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \
27323530Savg   luaM_realloc_(L, (b), (on)*(e), (n)*(e)))
28323530Savg
29323530Savg#define luaM_freemem(L, b, s)	luaM_realloc_(L, (b), (s), 0)
30323530Savg#define luaM_free(L, b)		luaM_realloc_(L, (b), sizeof(*(b)), 0)
31323530Savg#define luaM_freearray(L, b, n)   luaM_reallocv(L, (b), n, 0, sizeof((b)[0]))
32323530Savg
33323530Savg#define luaM_malloc(L,s)	luaM_realloc_(L, NULL, 0, (s))
34323530Savg#define luaM_new(L,t)		cast(t *, luaM_malloc(L, sizeof(t)))
35323530Savg#define luaM_newvector(L,n,t) \
36323530Savg		cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))
37323530Savg
38323530Savg#define luaM_newobject(L,tag,s)	luaM_realloc_(L, NULL, tag, (s))
39323530Savg
40323530Savg#define luaM_growvector(L,v,nelems,size,t,limit,e) \
41323530Savg          if ((nelems)+1 > (size)) \
42323530Savg            ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))
43323530Savg
44323530Savg#define luaM_reallocvector(L, v,oldn,n,t) \
45323530Savg   ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t))))
46323530Savg
47323530SavgLUAI_FUNC l_noret luaM_toobig (lua_State *L);
48323530Savg
49323530Savg/* not to be called directly */
50323530SavgLUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
51323530Savg                                                          size_t size);
52323530SavgLUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size,
53323530Savg                               size_t size_elem, int limit,
54323530Savg                               const char *what);
55323530Savg
56323530Savg#endif
57323530Savg
58