Branch: refs/heads/master Author: Colomban Wendling ban@herbesfolles.org Committer: Colomban Wendling ban@herbesfolles.org Date: Sat, 20 Feb 2016 18:47:27 UTC Commit: 15bc6c48c2fe0873a69edad297da566f9d12bd4e https://github.com/geany/geany-plugins/commit/15bc6c48c2fe0873a69edad297da56...
Log Message: ----------- pretty-printer: Switch to the GLib allocator
This is not really useful per se, but will allow to pass a GLib-allocated buffer to processXMLPrettyPrinting() without having to worry about mismatching allocators.
Modified Paths: -------------- pretty-printer/src/PluginEntry.c pretty-printer/src/PrettyPrinter.c
Modified: pretty-printer/src/PluginEntry.c 5 lines changed, 2 insertions(+), 3 deletions(-) =================================================================== @@ -112,7 +112,7 @@ void xml_format(GtkMenuItem* menuitem, gpointer gdata) GeanyEditor* editor; ScintillaObject* sco; int length; - char* buffer; + gchar* buffer; xmlDoc* parsedDocument; int result; int xOffset; @@ -129,8 +129,7 @@ void xml_format(GtkMenuItem* menuitem, gpointer gdata) /* prepare the buffer that will contain the text * from the scintilla object */ length = sci_get_length(sco)+1; - buffer = (char*)malloc(length*sizeof(char)); - if (buffer == NULL) { exit(-1); } /* malloc error */ + buffer = (char*)g_malloc(length*sizeof(char));
/* retrieves the text */ sci_get_text(sco, length, buffer);
Modified: pretty-printer/src/PrettyPrinter.c 20 lines changed, 10 insertions(+), 10 deletions(-) =================================================================== @@ -116,7 +116,7 @@ int processXMLPrettyPrinting(char** buffer, int* length, PrettyPrintingOptions* inputBufferLength = *length;
xmlPrettyPrintedLength = *length; - xmlPrettyPrinted = (char*)malloc(sizeof(char)*(*length)); + xmlPrettyPrinted = (char*)g_try_malloc(sizeof(char)*(*length)); if (xmlPrettyPrinted == NULL) { PP_ERROR("Allocation error (initialisation)"); return PRETTY_PRINTING_SYSTEM_ERROR; }
/* go to the first char */ @@ -129,29 +129,29 @@ int processXMLPrettyPrinting(char** buffer, int* length, PrettyPrintingOptions* putCharInBuffer('\0');
/* adjust the final size */ - reallocated = (char*)realloc(xmlPrettyPrinted, xmlPrettyPrintedIndex); + reallocated = (char*)g_try_realloc(xmlPrettyPrinted, xmlPrettyPrintedIndex); if (reallocated == NULL) { PP_ERROR("Allocation error (reallocation size is %d)", xmlPrettyPrintedIndex); - free(xmlPrettyPrinted); + g_free(xmlPrettyPrinted); xmlPrettyPrinted = NULL; return PRETTY_PRINTING_SYSTEM_ERROR; } xmlPrettyPrinted = reallocated;
/* freeing the unused values */ - if (freeOptions) { free(options); } + if (freeOptions) { g_free(options); }
/* if success, then update the values */ if (result == PRETTY_PRINTING_SUCCESS) { - free(*buffer); + g_free(*buffer); *buffer = xmlPrettyPrinted; *length = xmlPrettyPrintedIndex-2; /* the '\0' is not in the length */ } /* else clean the other values */ else { - free(xmlPrettyPrinted); + g_free(xmlPrettyPrinted); }
/* updating the pointers for the using into the caller function */ @@ -166,7 +166,7 @@ int processXMLPrettyPrinting(char** buffer, int* length, PrettyPrintingOptions*
PrettyPrintingOptions* createDefaultPrettyPrintingOptions(void) { - PrettyPrintingOptions* defaultOptions = (PrettyPrintingOptions*)malloc(sizeof(PrettyPrintingOptions)); + PrettyPrintingOptions* defaultOptions = (PrettyPrintingOptions*)g_try_malloc(sizeof(PrettyPrintingOptions)); if (defaultOptions == NULL) { PP_ERROR("Unable to allocate memory for PrettyPrintingOptions"); @@ -213,7 +213,7 @@ void putCharInBuffer(char charToAdd)
if (charToAdd == '\0') { ++xmlPrettyPrintedLength; } else { xmlPrettyPrintedLength += inputBufferLength; } - reallocated = (char*)realloc(xmlPrettyPrinted, xmlPrettyPrintedLength); + reallocated = (char*)g_try_realloc(xmlPrettyPrinted, xmlPrettyPrintedLength); if (reallocated == NULL) { PP_ERROR("Allocation error (char was %c)", charToAdd); return; } xmlPrettyPrinted = reallocated; } @@ -621,7 +621,7 @@ void processNode(void) }
/* store the name */ - nodeName = (char*)malloc(sizeof(char)*nodeNameLength+1); + nodeName = (char*)g_try_malloc(sizeof(char)*nodeNameLength+1); if (nodeName == NULL) { PP_ERROR("Allocation error (node name length is %d)", nodeNameLength); return ; } nodeName[nodeNameLength] = '\0'; for (i=0 ; i<nodeNameLength ; ++i) @@ -730,7 +730,7 @@ void processNode(void) lastNodeOpen = FALSE;
/* freeeeeeee !!! */ - free(nodeName); + g_free(nodeName); nodeName = NULL; currentNodeName = NULL; }
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).
plugins-commits@lists.geany.org