Re: [AD] mode-list updates.]

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


On Thursday 04 October 2001 06:02, Henrik Stokseth wrote:

> What if i provide an
> al_realloc() which works exactly like libc realloc() just that it's
> behaviour is guaranteed? in other words: if ptr=NULL -> malloc, if size=0
> -> free, otherwise call realloc and if failure -> free.

untested patch attached.

--
Sincerely Henrik Stokseth.
-----------------------------------------------------------------------
E-mail: hstokset@xxxxxxxxxx  Homepage: http://hstokset.n3.net
diff -ur ../allegro/docs/allegro._tx allegro/docs/allegro._tx
--- ../allegro/docs/allegro._tx	Wed Oct  3 18:13:34 2001
+++ allegro/docs/allegro._tx	Wed Oct  3 22:39:28 2001
@@ -297,6 +297,13 @@
    instruction was available (if this is set, the other CPU variables are 
    100% reliable, otherwise there may be some mistakes).
 
+@@extern void *@al_realloc(void *ptr, size_t size);
+   A 100% portable version of the C library's realloc() function. If ptr is
+   NULL, this function works like malloc(). If size is 0, this function works
+   like free(). Otherwise it works like realloc(). If re-allocation fails
+   this function frees previously allocated memory and returns NULL. But
+   normally this function returns a pointer to the newly allocated memory.
+
 
 
 @heading
diff -ur ../allegro/include/allegro.h allegro/include/allegro.h
--- ../allegro/include/allegro.h	Wed Oct  3 18:13:39 2001
+++ allegro/include/allegro.h	Wed Oct  3 22:05:48 2001
@@ -207,6 +207,8 @@
 AL_ARRAY(_DRIVER_INFO, _system_driver_list);
 
 
+AL_FUNC(void *, al_realloc, (void *ptr, size_t size));
+
 
 /******************************************/
 /************ Unicode routines ************/
diff -ur ../allegro/src/libc.c allegro/src/libc.c
--- ../allegro/src/libc.c	Mon Sep 17 17:04:02 2001
+++ allegro/src/libc.c	Wed Oct  3 21:58:36 2001
@@ -12,6 +12,8 @@
  *
  *      By Michael Bukin.
  *
+ *      Henrik Stokseth added al_realloc() function.
+ *
  *      See readme.txt for copyright information.
  */
 
@@ -98,3 +100,27 @@
 #endif
 
 
+
+/* al_realloc:
+ *  Resizes allocated memory area while keeping the contents.
+ */
+void *al_realloc(void *ptr, size_t size)
+{
+   void *tmp_ptr;
+
+   tmp_ptr = NULL;
+
+   if (ptr && size) {
+      tmp_ptr = realloc(ptr, size);
+      if (!tmp_ptr && ptr) free(ptr);
+   }
+   else if (!size) {
+      tmp_ptr = NULL;
+      if (ptr) free(ptr);
+   }
+   else if (!ptr) {
+      tmp_ptr = malloc(size);
+   }
+   
+   return tmp_ptr;
+}
diff -ur ../allegro/src/modesel.c allegro/src/modesel.c
--- ../allegro/src/modesel.c	Sun Sep 30 02:21:01 2001
+++ allegro/src/modesel.c	Wed Oct  3 22:07:13 2001
@@ -229,7 +229,7 @@
          /* add resolution. */
          if (!res_exist) {
             driver_list_entry->mode_count++;
-            temp_mode_list = realloc(temp_mode_list, sizeof(MODE_LIST) * (driver_list_entry->mode_count));
+            temp_mode_list = al_realloc(temp_mode_list, sizeof(MODE_LIST) * (driver_list_entry->mode_count));
             if (!temp_mode_list) return -1;
             mode = driver_list_entry->mode_count - 1;
 
@@ -252,7 +252,7 @@
       }
 
       /* terminate mode list */
-      temp_mode_list = realloc(temp_mode_list, sizeof(MODE_LIST) * (driver_list_entry->mode_count + 1));
+      temp_mode_list = al_realloc(temp_mode_list, sizeof(MODE_LIST) * (driver_list_entry->mode_count + 1));
       if (!temp_mode_list) return -1;
       temp_mode_list[driver_list_entry->mode_count].w = 0;
       temp_mode_list[driver_list_entry->mode_count].h = 0;
@@ -309,7 +309,7 @@
    driver_count = 0;
 
    while(driver_info[driver_count].driver) {
-      driver_list = realloc(driver_list, sizeof(DRIVER_LIST) * (driver_count + 4));
+      driver_list = al_realloc(driver_list, sizeof(DRIVER_LIST) * (driver_count + 4));
       if (!driver_list) return -1;
       driver_list[driver_count+3].id = driver_info[driver_count].id;
       gfx_driver = driver_info[driver_count].driver;
diff -ur ../allegro/src/win/wddmode.c allegro/src/win/wddmode.c
--- ../allegro/src/win/wddmode.c	Sun Sep 30 19:14:42 2001
+++ allegro/src/win/wddmode.c	Wed Oct  3 22:01:46 2001
@@ -272,7 +272,7 @@
    gfx_mode_list = (GFX_MODE_LIST *) gfx_mode_list_ptr;
 
    /* build gfx mode-list */
-   gfx_mode_list->mode = realloc(gfx_mode_list->mode, sizeof(GFX_MODE) * (gfx_mode_list->num_modes + 1));
+   gfx_mode_list->mode = al_realloc(gfx_mode_list->mode, sizeof(GFX_MODE) * (gfx_mode_list->num_modes + 1));
    if (!gfx_mode_list->mode)
       return DDENUMRET_CANCEL;
 
@@ -337,7 +337,7 @@
    }
 
    /* terminate mode list */
-   gfx_mode_list->mode = realloc(gfx_mode_list->mode, sizeof(GFX_MODE) * (gfx_mode_list->num_modes + 1));
+   gfx_mode_list->mode = al_realloc(gfx_mode_list->mode, sizeof(GFX_MODE) * (gfx_mode_list->num_modes + 1));
 
    gfx_mode_list->mode[gfx_mode_list->num_modes].width  = 0;
    gfx_mode_list->mode[gfx_mode_list->num_modes].height = 0;


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