diff options
Diffstat (limited to 'lstate.h')
-rw-r--r-- | lstate.h | 51 |
1 files changed, 34 insertions, 17 deletions
@@ -64,28 +64,45 @@ | |||
64 | 64 | ||
65 | /* | 65 | /* |
66 | ** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of | 66 | ** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of |
67 | ** how many "C calls" it can do in the C stack, to avoid C-stack overflow. | 67 | ** how many "C calls" it still can do in the C stack, to avoid C-stack |
68 | ** This count is very rough approximation; it considers only recursive | 68 | ** overflow. This count is very rough approximation; it considers only |
69 | ** functions inside the interpreter, as non-recursive calls can be | 69 | ** recursive functions inside the interpreter, as non-recursive calls |
70 | ** considered using a fixed (although unknown) amount of stack space. | 70 | ** can be considered using a fixed (although unknown) amount of stack |
71 | ** space. | ||
71 | ** | 72 | ** |
72 | ** The count itself has two parts: the lower part is the count itself; | 73 | ** The count has two parts: the lower part is the count itself; the |
73 | ** the higher part counts the number of non-yieldable calls in the stack. | 74 | ** higher part counts the number of non-yieldable calls in the stack. |
75 | ** (They are together so that we can change both with one instruction.) | ||
74 | ** | 76 | ** |
75 | ** Because calls to external C functions can use of unkown amount | 77 | ** Because calls to external C functions can use of unkown amount |
76 | ** of space (e.g., functions using an auxiliary buffer), calls | 78 | ** of space (e.g., functions using an auxiliary buffer), calls |
77 | ** to these functions add more than one to the count. | 79 | ** to these functions add more than one to the count (see CSTACKCF). |
78 | ** | 80 | ** |
79 | ** The proper count also includes the number of CallInfo structures | 81 | ** The proper count excludes the number of CallInfo structures allocated |
80 | ** allocated by Lua, as a kind of "potential" calls. So, when Lua | 82 | ** by Lua, as a kind of "potential" calls. So, when Lua calls a function |
81 | ** calls a function (and "consumes" one CallInfo), it needs neither to | 83 | ** (and "consumes" one CallInfo), it needs neither to decrement nor to |
82 | ** increment nor to check 'nCcalls', as its use of C stack is already | 84 | ** check 'nCcalls', as its use of C stack is already accounted for. |
83 | ** accounted for. | ||
84 | */ | 85 | */ |
85 | 86 | ||
86 | /* number of "C stack slots" used by an external C function */ | 87 | /* number of "C stack slots" used by an external C function */ |
87 | #define CSTACKCF 10 | 88 | #define CSTACKCF 10 |
88 | 89 | ||
90 | |||
91 | /* | ||
92 | ** The C-stack size is sliced in the following zones: | ||
93 | ** - larger than CSTACKERR: normal stack; | ||
94 | ** - [CSTACKMARK, CSTACKERR]: buffer zone to signal a stack overflow; | ||
95 | ** - [CSTACKCF, CSTACKERRMARK]: error-handling zone; | ||
96 | ** - below CSTACKERRMARK: buffer zone to signal overflow during overflow; | ||
97 | ** (Because the counter can be decremented CSTACKCF at once, we need | ||
98 | ** the so called "buffer zones", with at least that size, to properly | ||
99 | ** detect a change from one zone to the next.) | ||
100 | */ | ||
101 | #define CSTACKERR (8 * CSTACKCF) | ||
102 | #define CSTACKMARK (CSTACKERR - (CSTACKCF + 2)) | ||
103 | #define CSTACKERRMARK (CSTACKCF + 2) | ||
104 | |||
105 | |||
89 | /* true if this thread does not have non-yieldable calls in the stack */ | 106 | /* true if this thread does not have non-yieldable calls in the stack */ |
90 | #define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0) | 107 | #define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0) |
91 | 108 | ||
@@ -99,11 +116,11 @@ | |||
99 | /* Decrement the number of non-yieldable calls */ | 116 | /* Decrement the number of non-yieldable calls */ |
100 | #define decnny(L) ((L)->nCcalls -= 0x10000) | 117 | #define decnny(L) ((L)->nCcalls -= 0x10000) |
101 | 118 | ||
102 | /* Increment the number of non-yieldable calls and nCcalls */ | 119 | /* Increment the number of non-yieldable calls and decrement nCcalls */ |
103 | #define incXCcalls(L) ((L)->nCcalls += 0x10000 + CSTACKCF) | 120 | #define incXCcalls(L) ((L)->nCcalls += 0x10000 - CSTACKCF) |
104 | 121 | ||
105 | /* Decrement the number of non-yieldable calls and nCcalls */ | 122 | /* Decrement the number of non-yieldable calls and increment nCcalls */ |
106 | #define decXCcalls(L) ((L)->nCcalls -= 0x10000 + CSTACKCF) | 123 | #define decXCcalls(L) ((L)->nCcalls -= 0x10000 - CSTACKCF) |
107 | 124 | ||
108 | 125 | ||
109 | 126 | ||
@@ -336,7 +353,7 @@ LUAI_FUNC void luaE_enterCcall (lua_State *L); | |||
336 | LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); | 353 | LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); |
337 | 354 | ||
338 | 355 | ||
339 | #define luaE_exitCcall(L) ((L)->nCcalls--) | 356 | #define luaE_exitCcall(L) ((L)->nCcalls++) |
340 | 357 | ||
341 | #endif | 358 | #endif |
342 | 359 | ||