返回介绍

lua_Alloc

发布于 2019-08-25 13:16:46 字数 1691 浏览 1147 评论 0 收藏 0

typedef void * (*lua_Alloc) (void *ud,
 void *ptr,
 size_t osize,
 size_t nsize);

The type of the memory-allocation function used by Lua states. The allocator function must provide a functionality similar to realloc, but not exactly the same. Its arguments are ud, an opaque pointer passed to lua_newstate; ptr, a pointer to the block being allocated/reallocated/freed; osize, the original size of the block; nsize, the new size of the block. ptr is NULL if and only if osize is zero. When nsize is zero, the allocator must return NULL; if osize is not zero, it should free the block pointed to by ptr. When nsize is not zero, the allocator returns NULL if and only if it cannot fill the request. When nsize is not zero and osize is zero, the allocator should behave like malloc. When nsize and osize are not zero, the allocator behaves like realloc. Lua assumes that the allocator never fails when osize >= nsize.

Here is a simple implementation for the allocator function. It is used in the auxiliary library by lua_newstate.

 static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
 (void)ud; /* not used */
 (void)osize; /* not used */
 if (nsize == 0) {
 free(ptr); /* ANSI requires that free(NULL) has no effect */
 return NULL;
 }
 else
 /* ANSI requires that realloc(NULL, size) == malloc(size) */
 return realloc(ptr, nsize);
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文