getsize.c

Sat, 10 Oct 2009 17:30:57 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 10 Oct 2009 17:30:57 +0100
changeset 0
dc6c564e4d0a
child 1
fcb41eb42326
permissions
-rw-r--r--

Initial commit

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 printf("Size of array: %p\n", h->array);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 lua_pushinteger(L, sizeof(Table) + sizeof(TValue) * h->sizearray +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 sizeof(Node) * sizenode(h));
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 case LUA_TFUNCTION: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 Closure *cl = clvalue(o);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 lua_pushinteger(L, (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) :
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 sizeLclosure(cl->l.nupvalues));
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 case LUA_TTHREAD: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 lua_State *th = thvalue(o);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 lua_pushinteger(L, sizeof(lua_State) + sizeof(TValue) * th->stacksize +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 sizeof(CallInfo) * th->size_ci);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 case LUA_TPROTO: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 Proto *p = pvalue(o);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 lua_pushinteger(L, sizeof(Proto) + sizeof(Instruction) * p->sizecode +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 sizeof(Proto *) * p->sizep +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 sizeof(TValue) * p->sizek +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 sizeof(int) * p->sizelineinfo +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 sizeof(LocVar) * p->sizelocvars +
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 sizeof(TString *) * p->sizeupvalues);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 /* Non-containers */
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 case LUA_TUSERDATA: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 lua_pushnumber(L, uvalue(o)->len);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 case LUA_TLIGHTUSERDATA: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 lua_pushnumber(L, sizeof(void*));
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 case LUA_TSTRING: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 TString *s = rawtsvalue(o);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 lua_pushinteger(L, sizeof(TString) + s->tsv.len + 1);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 case LUA_TNUMBER: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 lua_pushinteger(L, sizeof(lua_Number));
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 case LUA_TBOOLEAN: {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 lua_pushinteger(L, sizeof(int));
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 break;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 default: return 0;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 return 1;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 }
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 int luaopen_getsize(lua_State* L)
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 {
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 lua_getglobal(L, "debug");
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 lua_pushcfunction(L, debug_getsize);
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 lua_setfield(L, -2, "getsize");
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 return 0;
dc6c564e4d0a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 }

mercurial