xref: /freebsd/sys/contrib/openzfs/module/lua/lmem.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: MIT
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy ** $Id: lmem.h,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $
4eda14cbcSMatt Macy ** Interface to Memory Manager
5eda14cbcSMatt Macy ** See Copyright Notice in lua.h
6eda14cbcSMatt Macy */
7eda14cbcSMatt Macy 
8eda14cbcSMatt Macy #ifndef lmem_h
9eda14cbcSMatt Macy #define lmem_h
10eda14cbcSMatt Macy 
11eda14cbcSMatt Macy 
12eda14cbcSMatt Macy #include "llimits.h"
13eda14cbcSMatt Macy #include <sys/lua/lua.h>
14eda14cbcSMatt Macy 
15eda14cbcSMatt Macy 
16eda14cbcSMatt Macy /*
17eda14cbcSMatt Macy ** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is
18eda14cbcSMatt Macy ** always constant.
19eda14cbcSMatt Macy ** The macro is somewhat complex to avoid warnings:
20eda14cbcSMatt Macy ** +1 avoids warnings of "comparison has constant result";
21eda14cbcSMatt Macy ** cast to 'void' avoids warnings of "value unused".
22eda14cbcSMatt Macy */
23eda14cbcSMatt Macy #define luaM_reallocv(L,b,on,n,e) \
24eda14cbcSMatt Macy   (cast(void, \
25eda14cbcSMatt Macy      (cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \
26eda14cbcSMatt Macy    luaM_realloc_(L, (b), (on)*(e), (n)*(e)))
27eda14cbcSMatt Macy 
28eda14cbcSMatt Macy #define luaM_freemem(L, b, s)	luaM_realloc_(L, (b), (s), 0)
29eda14cbcSMatt Macy #define luaM_free(L, b)		luaM_realloc_(L, (b), sizeof(*(b)), 0)
30eda14cbcSMatt Macy #define luaM_freearray(L, b, n)   luaM_reallocv(L, (b), n, 0, sizeof((b)[0]))
31eda14cbcSMatt Macy 
32eda14cbcSMatt Macy #define luaM_malloc(L,s)	luaM_realloc_(L, NULL, 0, (s))
33eda14cbcSMatt Macy #define luaM_new(L,t)		cast(t *, luaM_malloc(L, sizeof(t)))
34eda14cbcSMatt Macy #define luaM_newvector(L,n,t) \
35eda14cbcSMatt Macy 		cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))
36eda14cbcSMatt Macy 
37eda14cbcSMatt Macy #define luaM_newobject(L,tag,s)	luaM_realloc_(L, NULL, tag, (s))
38eda14cbcSMatt Macy 
39eda14cbcSMatt Macy #define luaM_growvector(L,v,nelems,size,t,limit,e) \
40eda14cbcSMatt Macy           if ((nelems)+1 > (size)) \
41eda14cbcSMatt Macy             ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))
42eda14cbcSMatt Macy 
43eda14cbcSMatt Macy #define luaM_reallocvector(L, v,oldn,n,t) \
44eda14cbcSMatt Macy    ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t))))
45eda14cbcSMatt Macy 
46eda14cbcSMatt Macy LUAI_FUNC l_noret luaM_toobig (lua_State *L);
47eda14cbcSMatt Macy 
48eda14cbcSMatt Macy /* not to be called directly */
49eda14cbcSMatt Macy LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
50eda14cbcSMatt Macy                                                           size_t size);
51eda14cbcSMatt Macy LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size,
52eda14cbcSMatt Macy                                size_t size_elem, int limit,
53eda14cbcSMatt Macy                                const char *what);
54eda14cbcSMatt Macy 
55eda14cbcSMatt Macy #endif
56