[Bf-blender-cvs] [44bb40b] temp-python-bgl: No need to use allocated array for method declarations

Campbell Barton noreply at git.blender.org
Fri Jul 24 00:56:09 CEST 2015


Commit: 44bb40b47c341a66e803622483993984328cac85
Author: Campbell Barton
Date:   Fri Jul 24 08:49:46 2015 +1000
Branches: temp-python-bgl
https://developer.blender.org/rB44bb40b47c341a66e803622483993984328cac85

No need to use allocated array for method declarations

Just use static vars, also move assignment into a static function,
resolves leak from not freeing the dynamic array and gives ~20% reduction in generated code size.

===================================================================

M	source/blender/python/generic/bgl.c
M	source/blender/python/generic/bgl.h

===================================================================

diff --git a/source/blender/python/generic/bgl.c b/source/blender/python/generic/bgl.c
index 968860d..ca1153c 100644
--- a/source/blender/python/generic/bgl.c
+++ b/source/blender/python/generic/bgl.c
@@ -1651,13 +1651,6 @@ static struct PyMethodDef BGL_methods[] = {
 	{NULL, NULL, 0, NULL}
 };
 
-static PyMethodDef* dyn_methods = NULL;
-
-void BGL_free(void *UNUSED(obj)){
-	if( dyn_methods != NULL)
-		free(dyn_methods);
-}
-
 static struct PyModuleDef BGL_module_def = {
 	PyModuleDef_HEAD_INIT,
 	"bgl",  /* m_name */
@@ -1667,7 +1660,7 @@ static struct PyModuleDef BGL_module_def = {
 	NULL,  /* m_reload */
 	NULL,  /* m_traverse */
 	NULL,  /* m_clear */
-	&BGL_free,  /* m_free */
+	NULL,  /* m_free */
 };
 
 static void expp_addconst_int(PyObject *dict, const char *name, int value)
@@ -1677,6 +1670,19 @@ static void expp_addconst_int(PyObject *dict, const char *name, int value)
 	Py_DECREF(item);
 }
 
+static void method_add_impl(PyObject *submodule, PyObject *dict, PyMethodDef *method_def, bool is_valid)
+{
+	if (is_valid) {
+		PyObject *m;
+		m = PyCFunction_NewEx(method_def, NULL, submodule);
+		PyDict_SetItemString(dict, method_def->ml_name, m);
+		Py_DECREF(m);
+	}
+	else {
+		PyDict_SetItemString(dict, method_def->ml_name, Py_None);
+	}
+}
+
 bool BGL_add_deprecated = true;
 
 void BGL_disable_deprecated(void)
@@ -1687,8 +1693,6 @@ void BGL_disable_deprecated(void)
 PyObject *BPyInit_bgl(void)
 {
 	PyObject *submodule, *dict;
-	PyMethodDef *dyn_methods;
-	int dyn_meth_offset = 0;
 
 	submodule = PyModule_Create(&BGL_module_def);
 	dict = PyModule_GetDict(submodule);
@@ -1699,25 +1703,14 @@ PyObject *BPyInit_bgl(void)
 	PyModule_AddObject(submodule, "Buffer", (PyObject *)&BGL_bufferType);
 	Py_INCREF((PyObject *)&BGL_bufferType);
 
-#define BGL_NUM_DYNAMIC_METHODS 556
-	dyn_methods = MEM_mallocN(sizeof(PyMethodDef) * (BGL_NUM_DYNAMIC_METHODS + 1), "BGL methods");
-
+/* needed since some function pointers won't be NULL */
 #pragma GCC diagnostic ignored "-Waddress"
-#define MethodAdd(func) { \
-	if ( gl##func != NULL) {\
-		PyObject* m; \
-		dyn_methods[dyn_meth_offset].ml_name = "gl"#func; \
-		dyn_methods[dyn_meth_offset].ml_meth = Method_##func; \
-		dyn_methods[dyn_meth_offset].ml_flags = METH_VARARGS; \
-		dyn_methods[dyn_meth_offset].ml_doc = "no string"; \
-		m = PyCFunction_NewEx(&(dyn_methods[dyn_meth_offset]), (PyObject*)NULL, submodule); \
-		PyDict_SetItemString(dict, dyn_methods[dyn_meth_offset].ml_name , m); \
-		Py_DECREF(m); \
-		dyn_meth_offset++; \
-	} else { \
-		PyDict_SetItemString(dict, "gl"#func, Py_False); \
-	} \
-	}
+
+#define MethodAdd(func) \
+	{ \
+		static PyMethodDef method_def = {"gl"#func, Method_##func, METH_VARARGS}; \
+		method_add_impl(submodule, dict, &method_def, (gl##func != NULL)); \
+	} ((void)0)
 
 	// GL_VERSION_1_0
 	MethodAdd(BlendFunc);
diff --git a/source/blender/python/generic/bgl.h b/source/blender/python/generic/bgl.h
index 6a32fde..f148c12 100644
--- a/source/blender/python/generic/bgl.h
+++ b/source/blender/python/generic/bgl.h
@@ -44,8 +44,6 @@ struct _Buffer *BGL_MakeBuffer(int type, int ndimensions, int *dimensions, void
 /*@ returns -1 otherwise */
 int BGL_typeSize(int type);
 
-void BGL_free(void*);
-
 /*@ Buffer Object */
 /*@ For Python access to OpenGL functions requiring a pointer. */
 typedef struct _Buffer {




More information about the Bf-blender-cvs mailing list