util-src/table.c
author Kim Alvefur <zash@zash.se>
Sun, 07 Aug 2016 20:49:50 +0200
changeset 7541 a3a4ed0d34f4
parent 7522 d278a770eddc
child 7821 54669df178c2
permissions -rw-r--r--
util.table: Move loop variable to top of function for C89 compatibility
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6613
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
#include <lua.h>
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
#include <lauxlib.h>
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
static int Lcreate_table(lua_State* L) {
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
	lua_createtable(L, luaL_checkinteger(L, 1), luaL_checkinteger(L, 2));
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
	return 1;
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
}
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
7522
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
     9
static int Lpack(lua_State* L) {
7541
a3a4ed0d34f4 util.table: Move loop variable to top of function for C89 compatibility
Kim Alvefur <zash@zash.se>
parents: 7522
diff changeset
    10
	int arg;
7522
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    11
	unsigned int n_args = lua_gettop(L);
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    12
	lua_createtable(L, n_args, 1);
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    13
	lua_insert(L, 1);
7541
a3a4ed0d34f4 util.table: Move loop variable to top of function for C89 compatibility
Kim Alvefur <zash@zash.se>
parents: 7522
diff changeset
    14
	for(arg = n_args; arg >= 1; arg--) {
7522
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    15
		lua_rawseti(L, 1, arg);
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    16
	}
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    17
	lua_pushinteger(L, n_args);
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    18
	lua_setfield(L, -2, "n");
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    19
	return 1;
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    20
}
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    21
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    22
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6613
diff changeset
    23
int luaopen_util_table(lua_State* L) {
6613
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
	lua_newtable(L);
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
	lua_pushcfunction(L, Lcreate_table);
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
	lua_setfield(L, -2, "create");
7522
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    27
	lua_pushcfunction(L, Lpack);
d278a770eddc util.table: Add pack() function (this is already available in Lua 5.2+)
Matthew Wild <mwild1@gmail.com>
parents: 6618
diff changeset
    28
	lua_setfield(L, -2, "pack");
6613
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
	return 1;
7c4cf87f4dff util.table, Makefile: New C module that allows pre-allocation of tables to improve performance and decrease memory fragmentation
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
}