5353 cast_charp (luaM_saferealloc_(L, (b), (on)*sizeof(char ), (n)*sizeof(char )))
5454
5555#define luaM_freemem (L, b, s ) luaM_free_(L, (b), (s))
56- #define luaM_free (L , b ) luaM_free_(L, (b), sizeof(*(b)))
57- #define luaM_freearray (L , b , n ) luaM_free_(L, (b), (n)*sizeof(*(b)))
58-
59- #define luaM_new (L ,t ) cast(t*, luaM_malloc_(L, sizeof(t), 0))
60- #define luaM_newvector (L ,n ,t ) \
61- cast(t*, luaM_malloc_(L, cast_sizet(n)*sizeof(t), 0))
62- #define luaM_newvectorchecked (L ,n ,t ) \
63- (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t))
64-
65- #define luaM_newobject (L ,tag ,s ) luaM_malloc_(L, (s), tag)
66-
67- #define luaM_newblock (L , size ) luaM_newvector(L, size, char)
68-
69- #define luaM_growvector (L ,v ,nelems ,size ,t ,limit ,e ) \
70- ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \
71- luaM_limitN(limit,t),e)))
72-
73- #define luaM_reallocvector (L , v ,oldn ,n ,t ) \
74- (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \
75- cast_sizet(n) * sizeof(t))))
76-
77- #define luaM_shrinkvector (L ,v ,size ,fs ,t ) \
78- ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t))))
7956
57+ /* Forward declarations of underlying memory functions */
8058LUAI_FUNC l_noret luaM_toobig (lua_State *L);
81-
82- /* not to be called directly */
8359LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
8460 size_t size);
8561LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize,
@@ -92,5 +68,70 @@ LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem,
9268 int final_n, unsigned size_elem);
9369LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag);
9470
71+ /*
72+ ** Template-based memory management functions for type safety.
73+ ** These replace the old macros with proper C++ templates.
74+ */
75+
76+ /* Free a single object of type T */
77+ template <typename T>
78+ inline void luaM_free (lua_State* L, T* b) noexcept {
79+ luaM_free_ (L, static_cast <void *>(b), sizeof (T));
80+ }
81+
82+ /* Free an array of n objects of type T */
83+ template <typename T>
84+ inline void luaM_freearray (lua_State* L, T* b, size_t n) noexcept {
85+ luaM_free_ (L, static_cast <void *>(b), n * sizeof (T));
86+ }
87+
88+ /* Allocate a single object of type T */
89+ template <typename T>
90+ inline T* luaM_new (lua_State* L) {
91+ return static_cast <T*>(luaM_malloc_ (L, sizeof (T), 0 ));
92+ }
93+
94+ /* Allocate an array of n objects of type T */
95+ template <typename T>
96+ inline T* luaM_newvector (lua_State* L, size_t n) {
97+ return static_cast <T*>(luaM_malloc_ (L, cast_sizet (n) * sizeof (T), 0 ));
98+ }
99+
100+ /* Allocate an array with size check */
101+ template <typename T>
102+ inline T* luaM_newvectorchecked (lua_State* L, size_t n) {
103+ luaM_checksize (L, n, sizeof (T));
104+ return luaM_newvector<T>(L, n);
105+ }
106+
107+ #define luaM_newobject (L,tag,s ) luaM_malloc_(L, (s), tag)
108+
109+ /* Allocate a block of size bytes (char array) */
110+ inline char * luaM_newblock (lua_State* L, size_t size) {
111+ return luaM_newvector<char >(L, size);
112+ }
113+
114+ /* Reallocate an array from oldn to n elements */
115+ template <typename T>
116+ inline T* luaM_reallocvector (lua_State* L, T* v, size_t oldn, size_t n) {
117+ return static_cast <T*>(luaM_realloc_ (L, v, cast_sizet (oldn) * sizeof (T),
118+ cast_sizet (n) * sizeof (T)));
119+ }
120+
121+ /* Grow a vector, updating size and checking limit */
122+ template <typename T>
123+ inline void luaM_growvector (lua_State* L, T*& v, int nelems, int & size, int limit, const char * e) {
124+ v = static_cast <T*>(luaM_growaux_ (L, v, nelems, &size, sizeof (T),
125+ luaM_limitN (limit, T), e));
126+ }
127+
128+ /* Shrink a vector to final_n elements, updating size */
129+ template <typename T>
130+ inline void luaM_shrinkvector (lua_State* L, T*& v, int & size, int final_n) {
131+ v = static_cast <T*>(luaM_shrinkvector_ (L, v, &size, final_n, sizeof (T)));
132+ }
133+
134+ /* Note: Function declarations moved above template definitions */
135+
95136#endif
96137
0 commit comments