[AD] Patches to add an _al_ustrdup() function (4.3.10)

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Hi all.

In my continuing seek and destroy effort on memory leaks exposed by my
memory tracking class, I have added an internal use only _al_ustrdup()
function.  A lot of internal code was using ustrdup() to allocate strings
and was then freeing it with _AL_FREE().  This is no good because it uses
mismatching allocation and deallocation functions.  ustrdup() should only
be used by external code, which then uses free() to free the result. 
Conversely, all internal code should use _al_ustrdup() and the matching
_AL_FREE().  The attached patches update the code to do this.

aintern.diff is the patch to add the _al_ustrdup() function prototype to
allegro/internal/aintern.h.
unicode.diff is the patch to add the _al_ustrdup() function implementation
to src/unicode.c.

The rest of the patches fix up the files that call ustrdup() so that they
call the proper _al_strdup() function.

For Allegro 5 we definitely need to think about this kind of thing - maybe
we should get rid of separate internal and external functions for this kind
of thing and make a public al_malloc() and al_free() that can be used that
use whatever memory tracking system a particular port supports.  Then you
just make a rule that anything allocated by the Allegro library (whether by
internal or external code) should be freed by al_free().

-- 
/-------------------------------------------------------------------\
[Hitman/Code HQ - 6502/z80/68000/604e/80x86/ARM coder - Amiga rulez!]
[VZ-200/VIC-20/MZ-700/c16/c64*10/c128*8/Plus-4/CPC464/CD32/500*2    ]
[600/1000/1200*2/A4000/SNES/N64/Dreamcast/Athlon 1100/AmigaOne      ]
[Assembly Language: The most fun you can have with your clothes on! ]
\-------------------------------------------------------------------/
--- include/allegro/internal/aintern.h	(revision 11109)
+++ include/allegro/internal/aintern.h	(working copy)
@@ -66,7 +66,8 @@
 AL_FUNC(void *, _al_malloc, (size_t size));
 AL_FUNC(void, _al_free, (void *mem));
 AL_FUNC(void *, _al_realloc, (void *mem, size_t size));
-AL_FUNC(char *, _al_strdup, (const char *string));
+AL_FUNC(char *, _al_strdup, (AL_CONST char *string));
+AL_FUNC(char *, _al_ustrdup, (AL_CONST char *string));
 
 
 
--- src/config.c	(revision 11109)
+++ src/config.c	(working copy)
@@ -348,7 +348,7 @@
 	 c = ugetc(buf+i);
       }
 
-      *val = ustrdup(buf+i);
+	  *val = _al_ustrdup(buf+i);
       if (!(*val)) {
 	 _AL_FREE(name);
 	 _AL_FREE(buf);
@@ -363,7 +363,7 @@
    else {
       /* blank line or comment */
       *name = NULL;
-      *val = ustrdup(buf);
+	  *val = _al_ustrdup(buf);
       if (!(*val)) {
 	 _AL_FREE(buf);
 	 return -1;
@@ -403,7 +403,7 @@
    (*config)->dirty = FALSE;
 
    if (filename) {
-      (*config)->filename = ustrdup(filename);
+	  (*config)->filename = _al_ustrdup(filename);
       if (!(*config)->filename) {
 	 _AL_FREE(*config);
 	 *config = NULL;
@@ -674,7 +674,7 @@
    if (!hook)
       return;
 
-   hook->section = ustrdup(section_name);
+   hook->section = _al_ustrdup(section_name);
    if (!(hook->section)) {
       _AL_FREE(hook);
       return;
@@ -1033,12 +1033,12 @@
       return NULL;
 
    if (name)
-      n->name = ustrdup(name);
+	  n->name = _al_ustrdup(name);
    else
       n->name = NULL;
 
    if (data)
-      n->data = ustrdup(data);
+	  n->data = _al_ustrdup(data);
    else
       n->data = NULL;
 
@@ -1099,7 +1099,7 @@
 	    if (p->data)
 	       _AL_FREE(p->data);
 
-	    p->data = ustrdup(val);
+		p->data = _al_ustrdup(val);
 	 }
 	 else {
 	    /* delete variable */
@@ -1236,7 +1236,7 @@
 void _reload_config(void)
 {
    if (config[0]) {
-      char *name = ustrdup(config[0]->filename);
+	  char *name = _al_ustrdup(config[0]->filename);
       set_config_file(name);
       _AL_FREE(name);
    }
@@ -1268,7 +1268,7 @@
    name = get_config_string(uconvert_ascii("system", tmp1), uconvert_ascii("language", tmp2), NULL);
 
    if ((name) && (ugetc(name))) {
-      namecpy = ustrdup(name);
+	  namecpy = _al_ustrdup(name);
       ustrlwr (namecpy);
       if ((ustrlen(namecpy)<4) || (ustricmp(namecpy+uoffset(namecpy, -4), uconvert_ascii("text", tmp1)) != 0))
 	 ext = uconvert_ascii("text.cfg", tmp1);
Index: src/datafile.c
===================================================================
--- src/datafile.c	(revision 11109)
+++ src/datafile.c	(working copy)
@@ -1442,7 +1442,7 @@
       return NULL;
    }
 
-   index->filename = ustrdup(filename);
+   index->filename = _al_ustrdup(filename);
    if (!index->filename) {
       pack_fclose(f);
       _AL_FREE(index);
Index: src/file.c
===================================================================
--- src/file.c	(revision 11109)
+++ src/file.c	(working copy)
@@ -182,7 +182,7 @@
 	    /* easy */
 	    home = getenv("HOME");
 	    if (home)
-	       home = strdup(home);
+	       home = _al_strstrdup(home);
 	 }
 	 else {
 	    /* harder */
@@ -219,7 +219,7 @@
 	       _AL_FREE(ascii_username);
 
 	       if (pwd)
-		  home = strdup(pwd->pw_dir);
+		  home = _al_strdup(pwd->pw_dir);
 
 	       endpwent();
 	    }
@@ -373,11 +373,11 @@
    if (ugetc(path) != ugetc(filename))
       return NULL;
 
-   my_path = ustrdup(path);
+   my_path = _al_ustrdup(path);
    if (!my_path)
       return NULL;
 
-   my_filename = ustrdup(filename);
+   my_filename = _al_ustrdup(filename);
    if (!my_filename) {
       _AL_FREE(my_path);
       return NULL;
@@ -1972,20 +1972,20 @@
 
          /* Try various possible locations to store the temporary file */
          if (getenv("TEMP")) {
-            tmp_dir = strdup(getenv("TEMP"));
+            tmp_dir = _al_strdup(getenv("TEMP"));
          }
          else if (getenv("TMP")) {
-            tmp_dir = strdup(getenv("TMP"));
+            tmp_dir = _al_strdup(getenv("TMP"));
          }
          else if (file_exists("/tmp", FA_DIREC, NULL)) {
-            tmp_dir = strdup("/tmp");
+            tmp_dir = _al_strdup("/tmp");
          }
          else if (getenv("HOME")) {
-            tmp_dir = strdup(getenv("HOME"));
+            tmp_dir = _al_strdup(getenv("HOME"));
          }
          else {
             /* Give up - try current directory */
-            tmp_dir = strdup(".");
+            tmp_dir = _al_strdup(".");
          }
 
       #endif
@@ -2029,7 +2029,7 @@
       chunk = _pack_fdopen(tmp_fd, (pack ? F_WRITE_PACKED : F_WRITE_NOPACK));
 
       if (chunk) {
-         chunk->normal.filename = ustrdup(name);
+         chunk->normal.filename = _al_ustrdup(name);
 
 	 if (pack)
 	    chunk->normal.parent->normal.parent = f;
--- src/fli.c	(revision 11109)
+++ src/fli.c	(working copy)
@@ -997,7 +997,7 @@
       fli_filename = NULL;
    }
 
-   fli_filename = ustrdup(filename);
+   fli_filename = _al_ustrdup(filename);
    if (!fli_filename)
       return FLI_ERROR;
 
--- src/fsel.c	(revision 11109)
+++ src/fsel.c	(working copy)
@@ -646,7 +646,7 @@
    char *last, *p, *attrb_p;
    int c, c2, i;
 
-   fext = ustrdup(ext);
+   fext = _al_ustrdup(ext);
    if (!fext)
       return;
 
@@ -848,7 +848,7 @@
    ASSERT(message);
    ASSERT(path);
 
-   backup = ustrdup(path);
+   backup = _al_ustrdup(path);
    if (!backup)
       return FALSE;
 
--- src/gui.c	(revision 11109)
+++ src/gui.c	(working copy)
@@ -1400,7 +1400,7 @@
    char *buf, *last;
    char tmp[16];
 
-   buf = ustrdup(s);
+   buf = _al_ustrdup(s);
    *tok1 = ustrtok_r(buf, uconvert_ascii("\t", tmp), &last);
    *tok2 = ustrtok_r(NULL, empty_string, &last);
 
--- src/text.c	(revision 11109)
+++ src/text.c	(working copy)
@@ -99,7 +99,7 @@
    usetc(toks+i, 0);
 
    /* count words and measure min length (without spaces) */ 
-   strbuf = ustrdup(str);
+   strbuf = _al_ustrdup(str);
    if (!strbuf) {
       /* Can't justify ! */
       f->vtable->render(f, str, color, bg, bmp, x1, y);
--- src/unicode.c	(revision 11109)
+++ src/unicode.c	(working copy)
@@ -1724,7 +1724,8 @@
 
 /* ustrdup:
  *  Returns a newly allocated copy of the src string, which must later be
- *  freed by the caller.
+ *  freed by the caller.  This function is for external use by Allegro
+ *  clients only.  Internal code should use _al_ustrdup().
  */
 char *_ustrdup(AL_CONST char *src, AL_METHOD(void *, malloc_func, (size_t)))
 {
@@ -1746,6 +1747,27 @@
 
 
 
+/* _al_ustrdup:
+ *  Returns a newly allocated copy of the src string, which must later be
+ *  freed by the caller using _AL_FREE().  This function is for internal use
+ *  by Allegro clients only.  External code should use ustrdup().
+ */
+char *_al_ustrdup(AL_CONST char *src)
+{
+   int size;
+   ASSERT(src);
+
+   size = ustrsizez(src);
+   char *newstring = _AL_MALLOC(size);
+
+   if (newstring)
+	  ustrzcpy(newstring, size, src);
+
+   return newstring;
+}
+
+
+
 /* ustrsize:
  *  Returns the size of the specified string in bytes, not including the
  *  trailing zero.


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/