Skip to content

Commit d8a90aa

Browse files
committed
allocate the argparsing stacks managed
1 parent 3effdd3 commit d8a90aa

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

graalpython/com.oracle.graal.python.cext/src/modsupport.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
typedef struct _positional_argstack {
4646
PyObject* argv;
47-
int argnum;
47+
long argnum;
4848
struct _positional_argstack* prev;
4949
} positional_argstack;
5050

@@ -93,9 +93,10 @@ int _PyArg_ParseTupleAndKeywords_SizeT(PyObject *argv, PyObject *kwds, const cha
9393
unsigned char rest_optional = 0;
9494
unsigned char rest_keywords_only = 0;
9595

96-
positional_argstack *v = (positional_argstack*)calloc(1, sizeof(positional_argstack));
96+
positional_argstack *v = (positional_argstack*)truffle_managed_malloc(sizeof(positional_argstack));
9797
v->argv = argv;
9898
v->argnum = 0;
99+
v->prev = NULL;
99100
positional_argstack *next;
100101

101102
char c = format[format_idx];
@@ -320,7 +321,7 @@ int _PyArg_ParseTupleAndKeywords_SizeT(PyObject *argv, PyObject *kwds, const cha
320321
PyErr_Format(PyExc_TypeError, "expected tuple, got %R", Py_TYPE(arg));
321322
return 0;
322323
}
323-
next = (positional_argstack*)calloc(1, sizeof(positional_argstack));
324+
next = (positional_argstack*)truffle_managed_malloc(sizeof(positional_argstack));
324325
next->argv = arg;
325326
next->argnum = 0;
326327
next->prev = v;
@@ -330,9 +331,7 @@ int _PyArg_ParseTupleAndKeywords_SizeT(PyObject *argv, PyObject *kwds, const cha
330331
if (v->prev == NULL) {
331332
PyErr_SetString(PyExc_SystemError, "')' without '(' in argument parsing");
332333
} else {
333-
next = v;
334334
v = v->prev;
335-
free(next);
336335
}
337336
break;
338337
case '|':
@@ -354,7 +353,6 @@ int _PyArg_ParseTupleAndKeywords_SizeT(PyObject *argv, PyObject *kwds, const cha
354353
}
355354

356355
end:
357-
free(v);
358356
return 1;
359357
}
360358

@@ -371,9 +369,10 @@ PyObject* _Py_BuildValue_SizeT(const char *format, ...) {
371369
char argchar[2] = {'\0'};
372370
unsigned int value_idx = 1;
373371
unsigned int format_idx = 0;
374-
build_stack *v = (build_stack*)calloc(1, sizeof(build_stack));
372+
build_stack *v = (build_stack*)truffle_managed_malloc(sizeof(build_stack));
375373
build_stack *next;
376374
v->list = PyList_New(0);
375+
v->prev = NULL;
377376

378377
char c = format[format_idx];
379378
while (c != '\0') {
@@ -484,7 +483,7 @@ PyObject* _Py_BuildValue_SizeT(const char *format, ...) {
484483
}
485484
break;
486485
case '(':
487-
next = (build_stack*)calloc(1, sizeof(build_stack));
486+
next = (build_stack*)truffle_managed_malloc(sizeof(build_stack));
488487
next->list = PyList_New(0);
489488
next->prev = v;
490489
v = next;
@@ -494,13 +493,11 @@ PyObject* _Py_BuildValue_SizeT(const char *format, ...) {
494493
PyErr_SetString(PyExc_SystemError, "')' without '(' in Py_BuildValue");
495494
} else {
496495
PyList_Append(v->prev->list, PyList_AsTuple(v->list));
497-
next = v;
498496
v = v->prev;
499-
free(next);
500497
}
501498
break;
502499
case '[':
503-
next = (build_stack*)calloc(1, sizeof(build_stack));
500+
next = (build_stack*)truffle_managed_malloc(sizeof(build_stack));
504501
next->list = PyList_New(0);
505502
next->prev = v;
506503
v = next;
@@ -510,13 +507,11 @@ PyObject* _Py_BuildValue_SizeT(const char *format, ...) {
510507
PyErr_SetString(PyExc_SystemError, "']' without '[' in Py_BuildValue");
511508
} else {
512509
PyList_Append(v->prev->list, v->list);
513-
next = v;
514510
v = v->prev;
515-
free(next);
516511
}
517512
break;
518513
case '{':
519-
next = (build_stack*)calloc(1, sizeof(build_stack));
514+
next = (build_stack*)truffle_managed_malloc(sizeof(build_stack));
520515
next->list = PyList_New(0);
521516
next->prev = v;
522517
v = next;
@@ -526,9 +521,7 @@ PyObject* _Py_BuildValue_SizeT(const char *format, ...) {
526521
PyErr_SetString(PyExc_SystemError, "'}' without '{' in Py_BuildValue");
527522
} else {
528523
PyList_Append(v->prev->list, to_sulong(polyglot_invoke(PY_TRUFFLE_CEXT, "dict_from_list", to_java(v->list))));
529-
next = v;
530524
v = v->prev;
531-
free(next);
532525
}
533526
break;
534527
case ':':

0 commit comments

Comments
 (0)