Skip to content

Instantly share code, notes, and snippets.

@postwait
Created June 2, 2016 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save postwait/a2483861c6e7557560c6be0c1b6855f6 to your computer and use it in GitHub Desktop.
Save postwait/a2483861c6e7557560c6be0c1b6855f6 to your computer and use it in GitHub Desktop.
diff --git a/lptree.c b/lptree.c
index 6973aac..36f301e 100644
--- a/lptree.c
+++ b/lptree.c
@@ -7,6 +7,7 @@
#include <limits.h>
#include <string.h>
#include <stdlib.h>
+#include <pthread.h>
#include "lua.h"
@@ -1147,8 +1148,14 @@ static size_t initposition (lua_State *L, size_t len) {
/*
** Main match function
*/
+static int stack_on_heap_for_lp_match_initialized;
+static pthread_key_t stack_on_heap_for_lp_match;
static int lp_match (lua_State *L) {
- Capture *capture = calloc(INITCAPSIZE, sizeof(Capture));
+ Capture *capture = pthread_getspecific(stack_on_heap_for_lp_match);
+ if(capture == NULL) {
+ capture = calloc(INITCAPSIZE, sizeof(Capture));
+ pthread_setspecific(stack_on_heap_for_lp_match, capture);
+ }
const char *r;
size_t l;
int rv;
@@ -1287,6 +1294,10 @@ static struct luaL_Reg metareg[] = {
int luaopen_circlpeg (lua_State *L);
int luaopen_circlpeg (lua_State *L) {
+ if(!stack_on_heap_for_lp_match_initialized) {
+ stack_on_heap_for_lp_match_initialized = 1;
+ pthread_key_create(&stack_on_heap_for_lp_match, free);
+ }
luaL_newmetatable(L, PATTERN_T);
lua_pushnumber(L, MAXBACK); /* initialize maximum backtracking */
lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment