getsize.c

Fri, 23 Oct 2009 22:46:40 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 23 Oct 2009 22:46:40 +0100
changeset 1
fcb41eb42326
parent 0
dc6c564e4d0a
permissions
-rw-r--r--

Remove a stray printf

0
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 /* lua-getsize
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 Author: (C) 2009 Matthew Wild
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 License: MIT/X11 license
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 Description: Adds a debug.getsize() function which
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 returns the size in bytes of a Lua object
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 */
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 #include <stdio.h>
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 #include <lua.h>
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 #include <lstate.h>
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 #include <lobject.h>
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 #include <lfunc.h>
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 int debug_getsize(lua_State* L)
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 TValue* o = L->base;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 switch (o->tt) {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 /* Container types */
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 case LUA_TTABLE: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 Table *h = hvalue(o);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 lua_pushinteger(L, sizeof(Table) + sizeof(TValue) * h->sizearray +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 sizeof(Node) * sizenode(h));
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 case LUA_TFUNCTION: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 Closure *cl = clvalue(o);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 lua_pushinteger(L, (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) :
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 sizeLclosure(cl->l.nupvalues));
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 case LUA_TTHREAD: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 lua_State *th = thvalue(o);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 lua_pushinteger(L, sizeof(lua_State) + sizeof(TValue) * th->stacksize +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 sizeof(CallInfo) * th->size_ci);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 case LUA_TPROTO: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 Proto *p = pvalue(o);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 lua_pushinteger(L, sizeof(Proto) + sizeof(Instruction) * p->sizecode +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 sizeof(Proto *) * p->sizep +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 sizeof(TValue) * p->sizek +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 sizeof(int) * p->sizelineinfo +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 sizeof(LocVar) * p->sizelocvars +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 sizeof(TString *) * p->sizeupvalues);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 /* Non-containers */
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 case LUA_TUSERDATA: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 lua_pushnumber(L, uvalue(o)->len);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 case LUA_TLIGHTUSERDATA: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 lua_pushnumber(L, sizeof(void*));
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 case LUA_TSTRING: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 TString *s = rawtsvalue(o);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 lua_pushinteger(L, sizeof(TString) + s->tsv.len + 1);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 case LUA_TNUMBER: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 lua_pushinteger(L, sizeof(lua_Number));
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 case LUA_TBOOLEAN: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 lua_pushinteger(L, sizeof(int));
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 default: return 0;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 return 1;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 int luaopen_getsize(lua_State* L)
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 lua_getglobal(L, "debug");
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 lua_pushcfunction(L, debug_getsize);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 lua_setfield(L, -2, "getsize");
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 return 0;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 }

mercurial