qemacs-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemacs-commit] qemacs buffer.c charset.c dired.c display.c dis...


From: Charlie Gordon
Subject: [Qemacs-commit] qemacs buffer.c charset.c dired.c display.c dis...
Date: Wed, 12 Dec 2007 11:49:04 +0000

CVSROOT:        /cvsroot/qemacs
Module name:    qemacs
Changes by:     Charlie Gordon <chqrlie>        07/12/12 11:49:04

Modified files:
        .              : buffer.c charset.c dired.c display.c display.h 
                         fbfrender.c html2png.c image.c input.c qe.c 
                         qe.h shell.c tty.c unicode_join.c unix.c util.c 
                         video.c win32.c x11.c 
        libqhtml       : css.c cssparse.c xmlparse.c 

Log message:
        added wrappers and utilities for safer memory allocation:
        
        type *qe_malloc(type);
        type *qe_mallocz(type);
        type *qe_malloc_array(type, count);
        type *qe_mallocz_array(type, count);
        type *qe_malloc_hack(type, extra_bytes);
        type *qe_mallocz_hack(type, extra_bytes);
        void qe_free(&ptr);
        void *qe_realloc(&ptr, new_size);
        char *qe_strdup(const char *str);
        void *qe_malloc_bytes(size_t size);
        void *qe_mallocz_bytes(size_t size);
        void *qe_malloc_dup(const void *src, size_t size);
        
        pass pointer address to bmp_free()

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/qemacs/buffer.c?cvsroot=qemacs&r1=1.14&r2=1.15
http://cvs.savannah.gnu.org/viewcvs/qemacs/charset.c?cvsroot=qemacs&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/qemacs/dired.c?cvsroot=qemacs&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/qemacs/display.c?cvsroot=qemacs&r1=1.8&r2=1.9
http://cvs.savannah.gnu.org/viewcvs/qemacs/display.h?cvsroot=qemacs&r1=1.8&r2=1.9
http://cvs.savannah.gnu.org/viewcvs/qemacs/fbfrender.c?cvsroot=qemacs&r1=1.6&r2=1.7
http://cvs.savannah.gnu.org/viewcvs/qemacs/html2png.c?cvsroot=qemacs&r1=1.9&r2=1.10
http://cvs.savannah.gnu.org/viewcvs/qemacs/image.c?cvsroot=qemacs&r1=1.11&r2=1.12
http://cvs.savannah.gnu.org/viewcvs/qemacs/input.c?cvsroot=qemacs&r1=1.13&r2=1.14
http://cvs.savannah.gnu.org/viewcvs/qemacs/qe.c?cvsroot=qemacs&r1=1.40&r2=1.41
http://cvs.savannah.gnu.org/viewcvs/qemacs/qe.h?cvsroot=qemacs&r1=1.32&r2=1.33
http://cvs.savannah.gnu.org/viewcvs/qemacs/shell.c?cvsroot=qemacs&r1=1.24&r2=1.25
http://cvs.savannah.gnu.org/viewcvs/qemacs/tty.c?cvsroot=qemacs&r1=1.23&r2=1.24
http://cvs.savannah.gnu.org/viewcvs/qemacs/unicode_join.c?cvsroot=qemacs&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/qemacs/unix.c?cvsroot=qemacs&r1=1.4&r2=1.5
http://cvs.savannah.gnu.org/viewcvs/qemacs/util.c?cvsroot=qemacs&r1=1.22&r2=1.23
http://cvs.savannah.gnu.org/viewcvs/qemacs/video.c?cvsroot=qemacs&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/qemacs/win32.c?cvsroot=qemacs&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/qemacs/x11.c?cvsroot=qemacs&r1=1.14&r2=1.15
http://cvs.savannah.gnu.org/viewcvs/qemacs/libqhtml/css.c?cvsroot=qemacs&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/qemacs/libqhtml/cssparse.c?cvsroot=qemacs&r1=1.8&r2=1.9
http://cvs.savannah.gnu.org/viewcvs/qemacs/libqhtml/xmlparse.c?cvsroot=qemacs&r1=1.11&r2=1.12

Patches:
Index: buffer.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/buffer.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -b -r1.14 -r1.15
--- buffer.c    6 Dec 2007 17:43:47 -0000       1.14
+++ buffer.c    12 Dec 2007 11:49:02 -0000      1.15
@@ -63,11 +63,10 @@
 
     /* if the page is read only, copy it */
     if (p->flags & PG_READ_ONLY) {
-        buf = malloc(p->size);
+        buf = qe_malloc_dup(p->data, p->size);
         /* XXX: should return an error */
         if (!buf)
             return;
-        memcpy(buf, p->data, p->size);
         p->data = buf;
         p->flags &= ~PG_READ_ONLY;
     }
@@ -147,7 +146,8 @@
             len = size;
         if (len > 0) {
             update_page(p);
-            p->data = realloc(p->data, p->size + len);
+            /* CG: probably faster with qe_malloc + qe_free */
+            qe_realloc(&p->data, p->size + len);
             memmove(p->data + len, p->data, p->size);
             memcpy(p->data, buf + size - len, len);
             size -= len;
@@ -159,18 +159,16 @@
     n = (size + MAX_PAGE_SIZE - 1) / MAX_PAGE_SIZE;
     if (n > 0) {
         b->nb_pages += n;
-        b->page_table = realloc(b->page_table, b->nb_pages * sizeof(Page));
+        qe_realloc(&b->page_table, b->nb_pages * sizeof(Page));
         p = b->page_table + page_index;
-        memmove(p + n, p,
-                sizeof(Page) * (b->nb_pages - n - page_index));
+        memmove(p + n, p, sizeof(Page) * (b->nb_pages - n - page_index));
         while (size > 0) {
             len = size;
             if (len > MAX_PAGE_SIZE)
                 len = MAX_PAGE_SIZE;
             p->size = len;
-            p->data = malloc(len);
+            p->data = qe_malloc_dup(buf, len);
             p->flags = 0;
-            memcpy(p->data, buf, len);
             buf += len;
             size -= len;
             p++;
@@ -211,7 +209,7 @@
             p = b->page_table + page_index;
             update_page(p);
             p->size += len - len_out;
-            p->data = realloc(p->data, p->size);
+            qe_realloc(&p->data, p->size);
             memmove(p->data + offset + len, 
                     p->data + offset, p->size - (offset + len));
             memcpy(p->data + offset, buf, len);
@@ -272,7 +270,7 @@
                realloced */
             q = dest->page_table + page_index - 1;
             update_page(q);
-            q->data = realloc(q->data, dest_offset);
+            qe_realloc(&q->data, dest_offset);
             q->size = dest_offset;
         }
     } else {
@@ -294,11 +292,9 @@
     if (n > 0) {
         /* add the pages */
         dest->nb_pages += n;
-        dest->page_table = realloc(dest->page_table,
-                                   dest->nb_pages * sizeof(Page));
+        qe_realloc(&dest->page_table, dest->nb_pages * sizeof(Page));
         q = dest->page_table + page_index;
-        memmove(q + n, q, 
-                sizeof(Page) * (dest->nb_pages - n - page_index));
+        memmove(q + n, q, sizeof(Page) * (dest->nb_pages - n - page_index));
         p = p_start;
         while (n > 0) {
             len = p->size;
@@ -310,8 +306,7 @@
             } else {
                 /* allocate a new page */
                 q->flags = 0;
-                q->data = malloc(len);
-                memcpy(q->data, p->data, len);
+                q->data = qe_malloc_dup(p->data, len);
             }
             n--;
             p++;
@@ -366,7 +361,7 @@
                 del_start = p;
             /* we cannot free if read only */
             if (!(p->flags & PG_READ_ONLY))
-                free(p->data);
+                qe_free(&p->data);
             p++;
             offset = 0;
             n++;
@@ -375,7 +370,7 @@
             memmove(p->data + offset, p->data + offset + len, 
                     p->size - offset - len);
             p->size -= len;
-            p->data = realloc(p->data, p->size);
+            qe_realloc(&p->data, p->size);
             offset += len;
             if (offset >= p->size) {
                 p++;
@@ -390,7 +385,7 @@
         b->nb_pages -= n;
         memmove(del_start, del_start + n, 
                 (b->page_table + b->nb_pages - del_start) * sizeof(Page));
-        b->page_table = realloc(b->page_table, b->nb_pages * sizeof(Page));
+        qe_realloc(&b->page_table, b->nb_pages * sizeof(Page));
     }
 
     /* the page cache is no longer valid */
@@ -433,10 +428,9 @@
     QEmacsState *qs = &qe_state;
     EditBuffer *b;
 
-    b = malloc(sizeof(EditBuffer));
+    b = qe_mallocz(EditBuffer);
     if (!b)
         return NULL;
-    memset(b, 0, sizeof(EditBuffer));
 
     pstrcpy(b->name, sizeof(b->name), name);
     b->flags = flags;
@@ -476,7 +470,7 @@
     /* free each callback */
     for (l = b->first_callback; l != NULL;) {
         l1 = l->next;
-        free(l);
+        qe_free(&l);
         l = l1;
     }
     b->first_callback = NULL;
@@ -502,7 +496,7 @@
     if (b == trace_buffer)
         trace_buffer = NULL;
 
-    free(b);
+    qe_free(&b);
 }
 
 EditBuffer *eb_find(const char *name)
@@ -613,7 +607,7 @@
 {
     EditBufferCallbackList *l;
 
-    l = malloc(sizeof(EditBufferCallbackList));
+    l = qe_malloc(EditBufferCallbackList);
     if (!l)
         return -1;
     l->callback = cb;
@@ -632,7 +626,7 @@
         l = *pl;
         if (l->callback == cb && l->opaque == opaque) {
             *pl = l->next;
-            free(l);
+            qe_free(&l);
             break;
        }
     }
@@ -1157,14 +1151,13 @@
     /* cannot load a buffer if already I/Os or readonly */
     if (b->flags & (BF_LOADING | BF_SAVING | BF_READONLY))
         return -1;
-    s = malloc(sizeof(BufferIOState));
+    s = qe_malloc(BufferIOState);
     if (!s)
         return -1;
     b->io_state = s;
     h = url_new();
     if (!h) {
-        free(b->io_state);
-        b->io_state = NULL;
+        qe_free(&b->io_state);
         return -1;
     }
     s->handle = h;
@@ -1228,8 +1221,7 @@
     }
     url_close(s->handle);
     s->completion_cb(s->opaque, err);
-    free(s);
-    b->io_state = NULL;
+    qe_free(&b->io_state);
 }
 #endif
 
@@ -1276,7 +1268,7 @@
         return -1;
     }
     n = (file_size + MAX_PAGE_SIZE - 1) / MAX_PAGE_SIZE;
-    p = malloc(n * sizeof(Page));
+    p = qe_malloc_array(Page, n);
     if (!p) {
         close(fd);
         return -1;

Index: charset.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/charset.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- charset.c   10 Dec 2007 13:28:08 -0000      1.10
+++ charset.c   12 Dec 2007 11:49:02 -0000      1.11
@@ -300,7 +300,7 @@
     
     s->table = NULL; /* fail safe */
     if (charset->table_alloc) {
-        table = malloc(256 * sizeof(unsigned short));
+        table = qe_malloc_array(unsigned short, 256);
         if (!table) {
             charset = &charset_8859_1;
         } else {
@@ -317,7 +317,7 @@
 {
     if (s->charset->table_alloc &&
         s->charset != &charset_8859_1)
-        free(s->table);
+        qe_free(&s->table);
     /* safety */
     memset(s, 0, sizeof(CharsetDecodeState));
 }

Index: dired.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/dired.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- dired.c     6 Dec 2007 17:43:47 -0000       1.10
+++ dired.c     12 Dec 2007 11:49:02 -0000      1.11
@@ -63,7 +63,7 @@
     
     /* free opaques */
     for (i = 0; i < ds->items.nb_items; i++) {
-        free(ds->items.items[i]->opaque);
+        qe_free(&ds->items.items[i]->opaque);
     }
 
     free_strings(&ds->items);
@@ -296,7 +296,7 @@
         if (item) {
             DiredItem *dip;
 
-            dip = malloc(sizeof(DiredItem) + strlen(p));
+            dip = qe_malloc_hack(DiredItem, strlen(p));
             dip->state = hs;
             dip->mode = st.st_mode;
             dip->size = st.st_size;

Index: display.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/display.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- display.c   6 Dec 2007 17:43:47 -0000       1.8
+++ display.c   12 Dec 2007 11:49:02 -0000      1.9
@@ -223,23 +223,25 @@
 
     if (!s->dpy.dpy_bmp_alloc)
         return NULL;
-    b = malloc(sizeof(QEBitmap));
+    b = qe_malloc(QEBitmap);
     if (!b)
         return NULL;
     b->width = width;
     b->height = height;
     b->flags = flags;
     if (s->dpy.dpy_bmp_alloc(s, b) < 0) {
-        free(b);
+        qe_free(&b);
         return NULL;
     }
     return b;
 }
 
-void bmp_free(QEditScreen *s, QEBitmap *b)
+void bmp_free(QEditScreen *s, QEBitmap **bp)
 {
-    s->dpy.dpy_bmp_free(s, b);
-    free(b);
+    if (*bp) {
+        s->dpy.dpy_bmp_free(s, *bp);
+        qe_free(bp);
+    }
 }
 
 void bmp_draw(QEditScreen *s, QEBitmap *b, 

Index: display.h
===================================================================
RCS file: /cvsroot/qemacs/qemacs/display.h,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- display.h   3 Dec 2007 16:10:38 -0000       1.8
+++ display.h   12 Dec 2007 11:49:02 -0000      1.9
@@ -222,7 +222,7 @@
 void selection_request(QEditScreen *s);
 
 QEBitmap *bmp_alloc(QEditScreen *s, int width, int height, int flags);
-void bmp_free(QEditScreen *s, QEBitmap *b);
+void bmp_free(QEditScreen *s, QEBitmap **bp);
 void bmp_draw(QEditScreen *s, QEBitmap *b, 
               int dst_x, int dst_y, int dst_w, int dst_h, 
               int offset_x, int offset_y, int flags);

Index: fbfrender.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/fbfrender.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- fbfrender.c 6 Dec 2007 17:43:47 -0000       1.6
+++ fbfrender.c 12 Dec 2007 11:49:02 -0000      1.7
@@ -98,10 +98,10 @@
         while (*pp != p) pp = &(*pp)->hash_next;
         *pp = p->hash_next;
 
-        free(p);
+        qe_free(&p);
     }
 
-    p = malloc(sizeof(GlyphCache) + data_size);
+    p = qe_malloc_hack(GlyphCache, data_size);
     if (!p)
         return NULL;
 
@@ -237,7 +237,7 @@
     UniFontData *fonts[MAX_MATCHES];
     int nb_fonts, i;
 
-    font = malloc(sizeof(QEFont));
+    font = qe_malloc(QEFont);
     if (!font)
         return NULL;
 
@@ -278,17 +278,17 @@
 
 void fbf_close_font(__unused__ QEditScreen *s, QEFont *font)
 {
-    free(font);
+    qe_free(&font);
 }
 
 static void *my_malloc(__unused__ void *opaque, int size)
 {
-    return malloc(size);
+    return qe_malloc_bytes(size);
 }
 
 static void my_free(__unused__ void *opaque, void *ptr)
 {
-    free(ptr);
+    qe_free(&ptr);
 }
 
 
@@ -322,12 +322,11 @@
     if (!f) 
         return -1;
 
-    uf = malloc(sizeof(UniFontData));
+    uf = qe_mallocz(UniFontData);
     if (!uf) {
         fclose(f);
         return -1;
     }
-    memset(uf, 0, sizeof(*uf));
 
     /* init memory */
     uf->mem_opaque = NULL;
@@ -341,7 +340,7 @@
     uf->fbf_getc = my_fbf_getc;
 
     if (fbf_load_font(uf) < 0) {
-        free(uf);
+        qe_free(&uf);
         fclose(f);
         return -1;
     }
@@ -430,19 +429,17 @@
     UniFontData *uf;
     MemoryFile *f;
 
-    f = malloc(sizeof(MemoryFile));
+    f = qe_mallocz(MemoryFile);
     if (!f)
         return -1;
     f->base = data;
     f->size = data_size;
-    f->offset = 0;
     
-    uf = malloc(sizeof(UniFontData));
+    uf = qe_mallocz(UniFontData);
     if (!uf) {
-        free(f);
+        qe_free(&f);
         return -1;
     }
-    memset(uf, 0, sizeof(*uf));
 
     /* init memory */
     uf->mem_opaque = NULL;
@@ -456,8 +453,8 @@
     uf->fbf_getc = my_fbf_getc;
 
     if (fbf_load_font(uf) < 0) {
-        free(uf);
-        free(f);
+        qe_free(&uf);
+        qe_free(&f);
         return -1;
     }
 
@@ -495,8 +492,8 @@
         /* close font data structures */
         fbf_free_font(uf);
         /* close font file */
-        free(uf->infile);
-        free(uf);
+        qe_free(&uf->infile);
+        qe_free(&uf);
     }
     first_font = NULL;
 }

Index: html2png.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/html2png.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- html2png.c  6 Dec 2007 17:43:48 -0000       1.9
+++ html2png.c  12 Dec 2007 11:49:02 -0000      1.10
@@ -136,14 +136,11 @@
 static int ppm_resize(QEditScreen *s, int w, int h)
 {
     CFBContext *cfb = s->private;
-    unsigned char *data;
     
     /* alloc bitmap */
-    data = realloc(cfb->base, w * h * sizeof(int));
-    if (!data) {
+    if (!qe_realloc(&cfb->base, w * h * sizeof(int))) {
         return -1;
     }
-    cfb->base = data;
     cfb->wrap = w * sizeof(int);
     s->width = w;
     s->height = h;
@@ -161,7 +158,7 @@
 
     memcpy(&s->dpy, &ppm_dpy, sizeof(QEDisplay));
 
-    cfb = malloc(sizeof(CFBContext));
+    cfb = qe_malloc(CFBContext);
     if (!cfb)
         return -1;
 
@@ -173,7 +170,8 @@
 
     if (ppm_resize(s, w, h) < 0) {
     fail:
-        free(cfb);
+        qe_free(&cfb->base);
+        qe_free(&s->private);
         return -1;
     }
     return 0;
@@ -183,8 +181,8 @@
 {
     CFBContext *cfb = s->private;
     
-    free(cfb->base);
-    free(cfb);
+    qe_free(&cfb->base);
+    qe_free(&s->private);
 }
 
 static int ppm_save(QEditScreen *s, const char *filename)
@@ -248,7 +246,7 @@
     if (!d.info_ptr)
         goto fail;
 
-    d.row_buf = malloc(3 * s->width);
+    d.row_buf = qe_malloc_array(u8, 3 * s->width);
     if (!d.row_buf)
         goto fail;
 
@@ -298,7 +296,7 @@
             png_destroy_write_struct(&d.png_ptr,
                                      d.info_ptr ? &d.info_ptr : NULL);
         }
-        free(d.row_buf);
+        qe_free(&d.row_buf);
         fclose(d.f);
         return -1;
     }

Index: image.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/image.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -b -r1.11 -r1.12
--- image.c     6 Dec 2007 15:28:30 -0000       1.11
+++ image.c     12 Dec 2007 11:49:02 -0000      1.12
@@ -268,18 +268,17 @@
     int size;
     ImageBuffer *ib;
 
-    ib = malloc(sizeof(ImageBuffer));
+    ib = qe_mallocz(ImageBuffer);
     if (!ib)
         return NULL;
-    memset(ib, 0, sizeof(ImageBuffer));
 
     size = avpicture_get_size(pix_fmt, width, height);
     if (size < 0)
         goto fail;
-    ptr = malloc(size);
+    ptr = qe_malloc_array(u8, size);
     if (!ptr) {
     fail:
-        free(ib);
+        qe_free(&ib);
         return NULL;
     }
 
@@ -292,8 +291,8 @@
 
 static void image_free(ImageBuffer *ib)
 {
-    free(ib->pict.data[0]);
-    free(ib);
+    qe_free(&ib->pict.data[0]);
+    qe_free(&ib);
 }
 
 
@@ -396,7 +395,9 @@
 static void image_buffer_close(EditBuffer *b)
 {
     ImageBuffer *ib = b->data;
+
     image_free(ib);
+    b->data = NULL;
 }
 
 
@@ -410,10 +411,7 @@
     int dst_pix_fmt;
     int i;
 
-    if (is->disp_bmp) {
-        bmp_free(s->screen, is->disp_bmp);
-        is->disp_bmp = NULL;
-    }
+    bmp_free(s->screen, &is->disp_bmp);
 
     /* combine with the appropriate background if alpha is present */
     ib1 = NULL;
@@ -577,10 +575,7 @@
 {
     ImageState *is = s->mode_data;
 
-    if (is->disp_bmp) {
-        bmp_free(s->screen, is->disp_bmp);
-        is->disp_bmp = NULL;
-    }
+    bmp_free(s->screen, &is->disp_bmp);
     eb_free_callback(s->b, image_callback, s);
 }
 

Index: input.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/input.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -b -r1.13 -r1.14
--- input.c     9 Dec 2007 23:31:38 -0000       1.13
+++ input.c     12 Dec 2007 11:49:02 -0000      1.14
@@ -263,7 +263,7 @@
         p += 4;
         if (offset == 0)
             break;
-        m = malloc(sizeof(InputMethod));
+        m = qe_malloc(InputMethod);
         if (m) {
             m->data = file_ptr + offset;
             m->input_match = kmap_input;

Index: qe.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/qe.c,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -b -r1.40 -r1.41
--- qe.c        12 Dec 2007 08:51:02 -0000      1.40
+++ qe.c        12 Dec 2007 11:49:03 -0000      1.41
@@ -100,8 +100,7 @@
         char buf[64], *name;
         int size;
 
-        table = malloc(sizeof(CmdDef) * 2);
-        memset(table, 0, sizeof(CmdDef) * 2);
+        table = qe_mallocz_array(CmdDef, 2);
         table->key = KEY_NONE;
         table->alt_key = KEY_NONE;
 
@@ -112,11 +111,10 @@
         size = strlen(buf) + 1;
         buf[size++] = 'S'; /* constant string parameter */
         buf[size++] = '\0';
-        name = malloc(size);
-        memcpy(name, buf, size);
+        name = qe_malloc_dup(buf, size);
         table->name = name;
         table->action.func = (void*)do_cmd_set_mode;
-        table->val = strdup(m->name);
+        table->val = qe_strdup(m->name);
         qe_register_cmd_table(table, NULL);
     }
 }
@@ -166,7 +164,7 @@
     KeyDef **lp, *p;
 
     /* add key */
-    p = malloc(sizeof(KeyDef) + (nb_keys - 1) * sizeof(unsigned int));
+    p = qe_malloc_hack(KeyDef, (nb_keys - 1) * sizeof(unsigned int));
     if (!p)
         return -1;
     p->nb_keys = nb_keys;
@@ -1602,8 +1600,7 @@
                 saved_data_allocated = 1;
         }
         s->mode->mode_close(s);
-        free(s->mode_data);
-        s->mode_data = NULL;
+        qe_free(&s->mode_data);
         s->mode = NULL;
 
         /* try to remove the raw or mode specific data if it is no
@@ -1656,12 +1653,10 @@
                 reload_buffer(s, b, f1);
         }
         if (size > 0) {
-            s->mode_data = malloc(size);
+            s->mode_data = qe_mallocz_array(u8, size);
             /* safe fall back: use text mode */
             if (!s->mode_data)
                 m = &text_mode;
-            else
-                memset(s->mode_data, 0, size);
         }
         s->mode = m;
         
@@ -1672,7 +1667,7 @@
             s->offset_top = s->mode->text_backward_offset(s, s->offset_top);
     }
     if (saved_data_allocated)
-        free(saved_data);
+        qe_free(&saved_data);
 }
 
 void do_set_mode(EditState *s, ModeDef *m, ModeSavedData *saved_data)
@@ -2357,7 +2352,7 @@
             /* realloc shadow */
             int n = e->shadow_nb_lines;
             e->shadow_nb_lines = n + LINE_SHADOW_INCR;
-            e->line_shadow = realloc(e->line_shadow, 
+            qe_realloc(&e->line_shadow, 
                                      e->shadow_nb_lines * 
sizeof(QELineShadow));
             /* put an impossible value so that we redraw */
             memset(&e->line_shadow[n], 0xff, 
@@ -2916,7 +2911,6 @@
 {
     int len, l, line, col, offset;
     int colorize_state;
-    unsigned char *ptr;
     
     /* invalidate cache if needed */
     if (s->colorize_max_valid_offset != MAXINT) {
@@ -2930,10 +2924,8 @@
     /* realloc line buffer if needed */
     if ((line_num + 2) > s->colorize_nb_lines) {
         s->colorize_nb_lines = line_num + 2 + COLORIZED_LINE_PREALLOC_SIZE;
-        ptr = realloc(s->colorize_states, s->colorize_nb_lines);
-        if (!ptr)
+        if (!qe_realloc(&s->colorize_states, s->colorize_nb_lines))
             return 0;
-        s->colorize_states = ptr;
     }
 
     /* propagate state if needed */
@@ -2988,8 +2980,7 @@
 {
     /* invalidate the previous states & free previous colorizer */
     eb_free_callback(s->b, colorize_callback, s);
-    free(s->colorize_states);
-    s->colorize_states = NULL;
+    qe_free(&s->colorize_states);
     s->colorize_nb_lines = 0;
     s->colorize_nb_valid_lines = 0;
     s->colorize_max_valid_offset = MAXINT;
@@ -3134,8 +3125,7 @@
 
     if (s->display_invalid) {
         /* invalidate the line shadow buffer */
-        free(s->line_shadow);
-        s->line_shadow = NULL;
+        qe_free(&s->line_shadow);
         s->shadow_nb_lines = 0;
         s->display_invalid = 0;
     }
@@ -3419,7 +3409,7 @@
         }
     }
     
-    es = malloc(sizeof(ExecCmdState));
+    es = qe_malloc(ExecCmdState);
     if (!es)
         return;
 
@@ -3485,7 +3475,7 @@
             if (use_argval && es->argval != NO_ARG) {
                 char buf[32];
                 snprintf(buf, sizeof(buf), "%d", es->argval);
-                es->args[es->nb_args].p = strdup(buf);
+                es->args[es->nb_args].p = qe_strdup(buf);
                 es->argval = NO_ARG;
             } else {
                 es->args[es->nb_args].p = NULL;
@@ -3567,11 +3557,11 @@
     for (i = 0;i < es->nb_args; i++) {
         switch (es->args_type[i]) {
         case CMD_ARG_STRING:
-            free(es->args[i].p);
+            qe_free(&es->args[i].p);
             break;
         }
     }
-    free(es);
+    qe_free(&es);
 }
 
 /* when the argument has been typed by the user, this callback is
@@ -3585,7 +3575,7 @@
     if (!str) {
         /* command aborted */
     fail:
-        free(str);
+        qe_free(&str);
         free_cmd(es);
         return;
     }
@@ -3601,8 +3591,8 @@
         break;
     case CMD_ARG_STRING:
         if (str[0] == '\0' && es->default_input[0] != '\0') {
-            free(str);
-            str = strdup(es->default_input);
+            qe_free(&str);
+            str = qe_strdup(es->default_input);
         }
         es->args[index].p = str; /* will be freed at the of the command */
         break;
@@ -3716,8 +3706,7 @@
         return;
     }
     qs->defining_macro = 1;
-    free(qs->macro_keys);
-    qs->macro_keys = NULL;
+    qe_free(&qs->macro_keys);
     qs->nb_macro_keys = 0;
     qs->macro_keys_size = 0;
     put_status(s, "Defining kbd macro...");
@@ -3794,18 +3783,17 @@
     char *macro_name, *p;
 
     size = strlen(name) + 1;
-    macro_name = malloc(size + 2);
+    macro_name = qe_malloc_array(char, size + 2);
     memcpy(macro_name, name, size);
     p = macro_name + size;
     *p++ = 'S';
     *p++ = '\0';
 
-    def = malloc(2 * sizeof(CmdDef));
-    memset(def, 0, sizeof(CmdDef) * 2);
+    def = qe_mallocz_array(CmdDef, 2);
     def->key = def->alt_key = KEY_NONE;
     def->name = macro_name;
     def->action.func = do_execute_macro_keys;
-    def->val = strdup(keys);
+    def->val = qe_strdup(keys);
 
     qe_register_cmd_table(def, NULL);
     do_global_set_key(s, key_bind, name);
@@ -3816,15 +3804,12 @@
 static void macro_add_key(int key)
 {
     QEmacsState *qs = &qe_state;
-    unsigned short *keys;
     int new_size;
 
     if (qs->nb_macro_keys >= qs->macro_keys_size) {
         new_size = qs->macro_keys_size + MACRO_KEY_INCR;
-        keys = realloc(qs->macro_keys, new_size * sizeof(unsigned short));
-        if (!keys)
+        if (!qe_realloc(&qs->macro_keys, new_size * sizeof(unsigned short)))
             return;
-        qs->macro_keys = keys;
         qs->macro_keys_size = new_size;
     }
     qs->macro_keys[qs->nb_macro_keys++] = key;
@@ -4226,10 +4211,9 @@
     EditState *s;
     QEmacsState *qs = &qe_state;
     
-    s = malloc(sizeof(EditState));
+    s = qe_mallocz(EditState);
     if (!s)
         return NULL;
-    memset(s, 0, sizeof(EditState));
     s->qe_state = qs;
     s->screen = qs->screen;
     s->x1 = x1;
@@ -4306,8 +4290,8 @@
     if (qs->active_window == s)
         qs->active_window = qs->first_window;
 
-    free(s->line_shadow);
-    free(s);
+    qe_free(&s->line_shadow);
+    qe_free(&s);
 }
 
 static const char *file_completion_ignore_extensions =
@@ -4370,7 +4354,7 @@
 {
     CompletionEntry **lp, *p;
 
-    p = malloc(sizeof(CompletionEntry));
+    p = qe_malloc(CompletionEntry);
     if (!p)
         return;
     p->name = name;
@@ -4536,10 +4520,9 @@
             return &p->history;
     }
     /* not found: allocate history list */
-    p = malloc(sizeof(HistoryEntry));
+    p = qe_mallocz(HistoryEntry);
     if (!p)
         return NULL;
-    memset(p, 0, sizeof(HistoryEntry));
     pstrcpy(p->name, sizeof(p->name), name);
     p->next = first_history;
     first_history = p;
@@ -4628,14 +4611,13 @@
     if (hist && hist->nb_items > 0) {
         /* if null string, do not insert in history */
         hist->nb_items--;
-        free(hist->items[hist->nb_items]);
-        hist->items[hist->nb_items] = NULL;
+        qe_free(&hist->items[hist->nb_items]);
         if (buf[0] != '\0')
             add_string(hist, buf);
     }
 
     /* free prompt */
-    free(s->prompt);
+    qe_free(&s->prompt);
 
     edit_close(s);
     eb_free(b);
@@ -4658,7 +4640,7 @@
     if (do_abort) {
         cb(opaque, NULL);
     } else {
-        retstr = strdup(buf);
+        retstr = qe_strdup(buf);
         cb(opaque, retstr);
     }
 }
@@ -4692,7 +4674,7 @@
                  qs->screen->width, qs->status_height, 0);
     /* Should insert at end of window list */
     do_set_mode(s, &minibuffer_mode, NULL);
-    s->prompt = strdup(prompt);
+    s->prompt = qe_strdup(prompt);
     s->minibuf = 1;
     s->bidir = 0;
     s->default_style = QE_STYLE_MINIBUF;
@@ -4921,10 +4903,11 @@
 static void kill_buffer_confirm_cb(void *opaque, char *reply)
 {
     int yes_replied;
+
     if (!reply)
         return;
     yes_replied = (strcmp(reply, "yes") == 0);
-    free(reply);
+    qe_free(&reply);
     if (!yes_replied)
         return;
     kill_buffer_noconfirm(opaque);
@@ -5205,10 +5188,11 @@
 static void save_edit_cb(void *opaque, char *filename)
 {
     EditState *s = opaque;
+
     if (!filename)
         return;
     set_filename(s->b, filename);
-    free(filename);
+    qe_free(&filename);
     save_final(s);
 }
 
@@ -5242,7 +5226,7 @@
     QEmacsState *qs = s->qe_state;
     QuitState *is;
 
-    is = malloc(sizeof(QuitState));
+    is = qe_malloc(QuitState);
     if (!is)
         return;
     
@@ -5345,7 +5329,7 @@
         return;
     if (reply[0] == 'y' || reply[0] == 'Y')
         url_exit();
-    free(reply);
+    qe_free(&reply);
 }
 
 
@@ -5578,7 +5562,7 @@
             last_search_string_len = j;
         }
         qe_ungrab_keys();
-        free(is);
+        qe_free(&is);
         return;
     case KEY_CTRL('s'):
         is->dir = 1;
@@ -5648,7 +5632,7 @@
 {
     ISearchState *is;
 
-    is = malloc(sizeof(ISearchState));
+    is = qe_malloc(ISearchState);
     if (!is)
         return;
     is->s = s;
@@ -5716,7 +5700,7 @@
 
     qe_ungrab_keys();
     put_status(NULL, "Replaced %d occurrences", is->nb_reps);
-    free(is);
+    qe_free(&is);
     edit_display(s->qe_state);
     dpy_flush(&global_screen);
 }
@@ -5790,7 +5774,7 @@
     if (s->b->flags & BF_READONLY)
         return;
 
-    is = malloc(sizeof(QueryReplaceState));
+    is = qe_malloc(QueryReplaceState);
     if (!is)
         return;
     is->s = s;
@@ -6601,7 +6585,7 @@
 {
     ModeSavedData *saved_data;
 
-    saved_data = malloc(sizeof(ModeSavedData));
+    saved_data = qe_malloc(ModeSavedData);
     if (!saved_data)
         return NULL;
     saved_data->mode = s->mode;

Index: qe.h
===================================================================
RCS file: /cvsroot/qemacs/qemacs/qe.h,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -b -r1.32 -r1.33
--- qe.h        12 Dec 2007 02:51:05 -0000      1.32
+++ qe.h        12 Dec 2007 11:49:03 -0000      1.33
@@ -74,6 +74,23 @@
 
 /************************/
 
+/* allocation wrappers and utilities */
+void *qe_malloc_bytes(size_t size);
+void *qe_mallocz_bytes(size_t size);
+void *qe_malloc_dup(const void *src, size_t size);
+char *qe_strdup(const char *str);
+void *qe_realloc(void *pp, size_t size);
+#define qe_malloc(t)            ((t *)qe_malloc_bytes(sizeof(t)))
+#define qe_mallocz(t)           ((t *)qe_mallocz_bytes(sizeof(t)))
+#define qe_malloc_array(t, n)   ((t *)qe_malloc_bytes((n) * sizeof(t)))
+#define qe_mallocz_array(t, n)  ((t *)qe_mallocz_bytes((n) * sizeof(t)))
+#define qe_malloc_hack(t, n)    ((t *)qe_malloc_bytes(sizeof(t) + (n)))
+#define qe_mallocz_hack(t, n)   ((t *)qe_mallocz_bytes(sizeof(t) + (n)))
+#define qe_free(pp)      \
+    do { void *_ = (pp); free(*(void **)_); *(void **)_ = NULL; } while (0)
+
+/************************/
+
 typedef unsigned char u8;
 typedef struct EditState EditState;
 typedef struct EditBuffer EditBuffer;
@@ -204,8 +221,7 @@
 }
 
 static inline void qstrfree(QString *q) {
-    free(q->data);
-    q->data = NULL;
+    qe_free(&q->data);
 }
 
 int qmemcat(QString *q, const unsigned char *data1, int len1);

Index: shell.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/shell.c,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -b -r1.24 -r1.25
--- shell.c     12 Dec 2007 02:51:05 -0000      1.24
+++ shell.c     12 Dec 2007 11:49:03 -0000      1.25
@@ -1065,7 +1065,7 @@
     if (s->pty_fd >= 0) {
         set_read_handler(s->pty_fd, NULL, NULL);
     }
-    free(s);
+    qe_free(&s);
 }
 
 EditBuffer *new_shell_buffer(const char *name, const char *path,
@@ -1080,12 +1080,11 @@
     set_buffer_name(b, name); /* ensure that the name is unique */
     eb_set_charset(b, &charset_vt100);
 
-    s = malloc(sizeof(ShellState));
+    s = qe_mallocz(ShellState);
     if (!s) {
         eb_free(b);
         return NULL;
     }
-    memset(s, 0, sizeof(ShellState));
     b->priv_data = s;
     b->close = shell_close;
     eb_add_callback(b, eb_offset_callback, &s->cur_offset);
@@ -1101,7 +1100,7 @@
         b_color = eb_new("*color*", BF_SYSTEM);
         if (!b_color) {
             eb_free(b);
-            free(s);
+            qe_free(&s);
             return NULL;
         }
         /* no undo info in this color buffer */

Index: tty.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/tty.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -b -r1.23 -r1.24
--- tty.c       10 Dec 2007 13:28:08 -0000      1.23
+++ tty.c       12 Dec 2007 11:49:03 -0000      1.24
@@ -271,8 +271,8 @@
     count = s->width * s->height;
     size = count * sizeof(TTYChar);
     /* screen buffer + shadow buffer + extra slot for loop guard */
-    ts->screen = realloc(ts->screen, size * 2 + 1);
-    ts->line_updated = realloc(ts->line_updated, s->height);
+    qe_realloc(&ts->screen, size * 2 + 1);
+    qe_realloc(&ts->line_updated, s->height);
     ts->screen_size = count;
     
     /* Erase shadow buffer to impossible value */
@@ -569,7 +569,7 @@
 {
     QEFont *font;
 
-    font = malloc(sizeof(QEFont));
+    font = qe_malloc(QEFont);
     if (!font)
         return NULL;
 
@@ -581,7 +581,7 @@
 
 static void tty_term_close_font(__unused__ QEditScreen *s, QEFont *font)
 {
-    free(font);
+    qe_free(&font);
 }
 
 /*

Index: unicode_join.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/unicode_join.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- unicode_join.c      12 Dec 2007 08:51:02 -0000      1.7
+++ unicode_join.c      12 Dec 2007 11:49:03 -0000      1.8
@@ -45,7 +45,7 @@
     unsigned short *tab;
     int i;
 
-    tab = malloc(n * sizeof(unsigned short));
+    tab = qe_malloc_array(unsigned short, n);
     if (!tab) 
         return NULL;
     for (i = 0; i < n; i++) {
@@ -86,12 +86,9 @@
     fclose(f);
     return;
  fail:
-    free(subst1);
-    free(ligature2);
-    free(ligature_long);
-    subst1 = NULL;
-    ligature2 = NULL;
-    ligature_long = NULL;
+    qe_free(&subst1);
+    qe_free(&ligature2);
+    qe_free(&ligature_long);
     subst1_count = 0;
     ligature2_count = 0;
     fclose(f);

Index: unix.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/unix.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- unix.c      3 Dec 2007 16:10:39 -0000       1.4
+++ unix.c      12 Dec 2007 11:49:03 -0000      1.5
@@ -97,12 +97,12 @@
         list_for_each(p, &pid_handlers) {
             if (p->pid == pid) {
                 list_del(p);
-                free(p);
+                qe_free(&p);
                 break;
             }
         }
     } else {
-        p = malloc(sizeof(PidHandler));
+        p = qe_malloc(PidHandler);
         if (!p)
             return -1;
         p->pid = pid;
@@ -121,7 +121,7 @@
     BottomHalfEntry *bh;
 
     /* Should not fail */
-    bh = malloc(sizeof(BottomHalfEntry));
+    bh = qe_malloc(BottomHalfEntry);
     bh->cb = cb;
     bh->opaque = opaque;
     list_add(bh, &bottom_halves);
@@ -137,7 +137,7 @@
     list_for_each_safe(bh, bh1, &bottom_halves) {
         if (bh->cb == cb && bh->opaque == opaque) {
             list_del(bh);
-            free(bh);
+            qe_free(&bh);
         }
     }
 }
@@ -146,7 +146,7 @@
 {
     QETimer *ti;
 
-    ti = malloc(sizeof(QETimer));
+    ti = qe_malloc(QETimer);
     if (!ti)
         return NULL;
     ti->timeout = get_clock_ms() + delay;
@@ -164,7 +164,7 @@
     while (*pt != NULL) {
         if (*pt == ti) {
             *pt = ti->next;
-            free(ti);
+            qe_free(&ti);
         } else {
             pt = &(*pt)->next;
         }
@@ -180,7 +180,7 @@
         bh = (BottomHalfEntry *)bottom_halves.prev;
         list_del(bh);
         bh->cb(bh->opaque);
-        free(bh);
+        qe_free(&bh);
     }
 }
 
@@ -209,7 +209,7 @@
             *pt = ti->next;
             /* warning: a new timer can be added in the callback */
             ti->cb(ti->opaque);
-            free(ti);
+            qe_free(&ti);
             call_bottom_halves();
         } else {
             if ((ti->timeout - timeout) < 0)

Index: util.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/util.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -b -r1.22 -r1.23
--- util.c      12 Dec 2007 02:51:05 -0000      1.22
+++ util.c      12 Dec 2007 11:49:03 -0000      1.23
@@ -49,7 +49,7 @@
 {
     FindFileState *s;
 
-    s = malloc(sizeof(FindFileState));
+    s = qe_malloc(FindFileState);
     if (!s)
         return NULL;
     pstrcpy(s->path, sizeof(s->path), path);
@@ -108,7 +108,7 @@
 {
     if (s->dir) 
         closedir(s->dir);
-    free(s);
+    qe_free(&s);
 }
 
 #ifdef WIN32
@@ -695,15 +695,14 @@
 
     /* Make room: reallocate table in chunks of 8 entries */
     if ((nb_custom_colors & 7) == 0) {
-        def = malloc((nb_css_colors + nb_custom_colors + 8) *
-                     sizeof(ColorDef));
+        def = qe_malloc_array(ColorDef, nb_css_colors + nb_custom_colors + 8);
         if (!def)
             return -1;
         memcpy(def, custom_colors,
                (nb_css_colors + nb_custom_colors) * sizeof(ColorDef));
             
         if (custom_colors != css_colors)
-            free(custom_colors);
+            qe_free(&custom_colors);
         custom_colors = def;
     }
     /* Check for redefinition */
@@ -715,7 +714,7 @@
     }
 
     def = &custom_colors[nb_css_colors + nb_custom_colors];
-    def->name = strdup(name);
+    def->name = qe_strdup(name);
     def->color = color;
     nb_custom_colors++;
 
@@ -868,16 +867,17 @@
 StringItem *set_string(StringArray *cs, int index, const char *str)
 {
     StringItem *v;
+
     if (index >= cs->nb_items)
         return NULL;
 
-    v = malloc(sizeof(StringItem) + strlen(str));
+    v = qe_malloc_hack(StringItem, strlen(str));
     if (!v)
         return NULL;
     v->selected = 0;
     strcpy(v->str, str);
     if (cs->items[index])
-        free(cs->items[index]);
+        qe_free(&cs->items[index]);
     cs->items[index] = v;
     return v;
 }
@@ -885,15 +885,12 @@
 /* make a generic array alloc */
 StringItem *add_string(StringArray *cs, const char *str)
 {
-    StringItem **tmp;
     int n;
 
     if (cs->nb_items >= cs->nb_allocated) {
         n = cs->nb_allocated + 32;
-        tmp = realloc(cs->items, n * sizeof(StringItem *));
-        if (!tmp)
+        if (!qe_realloc(&cs->items, n * sizeof(StringItem *)))
             return NULL;
-        cs->items = tmp;
         cs->nb_allocated = n;
     }
     cs->items[cs->nb_items++] = NULL;
@@ -905,8 +902,8 @@
     int i;
 
     for (i = 0; i < cs->nb_items; i++)
-        free(cs->items[i]);
-    free(cs->items);
+        qe_free(&cs->items[i]);
+    qe_free(&cs->items);
     memset(cs, 0, sizeof(StringArray));
 }
 
@@ -920,9 +917,7 @@
 int qmemcat(QString *q, const unsigned char *data1, int len1)
 {
     int new_len, len, alloc_size;
-    unsigned char *data;
 
-    data = q->data;
     len = q->len;
     new_len = len + len1;
     /* see if we got a new power of two */
@@ -936,13 +931,11 @@
         alloc_size |= (alloc_size >> 8);
         alloc_size |= (alloc_size >> 16);
         /* allocate one more byte for end of string marker */
-        data = realloc(data, alloc_size + 1);
-        if (!data)
+        if (!qe_realloc(&q->data, alloc_size + 1))
             return -1;
-        q->data = data;
     }
-    memcpy(data + len, data1, len1);
-    data[new_len] = '\0'; /* we force a trailing '\0' */
+    memcpy(q->data + len, data1, len1);
+    q->data[new_len] = '\0'; /* we force a trailing '\0' */
     q->len = new_len;
     return 0;
 }
@@ -1054,3 +1047,45 @@
 
     return out.pos;
 }
+
+/*---------------- allocation routines ----------------*/
+
+void *qe_malloc_bytes(size_t size)
+{
+    return malloc(size);
+}
+
+void *qe_mallocz_bytes(size_t size)
+{
+    void *p = malloc(size);
+    if (p)
+        memset(p, 0, size);
+    return p;
+}
+
+void *qe_malloc_dup(const void *src, size_t size)
+{
+    void *p = malloc(size);
+    if (p)
+        memcpy(p, src, size);
+    return p;
+}
+
+char *qe_strdup(const char *str)
+{
+    size_t size = strlen(str) + 1;
+    char *p = malloc(size);
+
+    if (p)
+        memcpy(p, str, size);
+    return p;
+}
+
+void *qe_realloc(void *pp, size_t size)
+{
+    void *p = realloc(*(void **)pp, size);
+    if (p)
+        *(void **)pp = p;
+    return p;
+}
+

Index: video.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/video.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- video.c     3 Dec 2007 16:10:39 -0000       1.7
+++ video.c     12 Dec 2007 11:49:03 -0000      1.8
@@ -192,7 +192,7 @@
             q->nb_packets--;
             q->size -= pkt1->pkt.size;
             *pkt = pkt1->pkt;
-            free(pkt1);
+            av_free_packet(pkt1);
             ret = 1;
             break;
         } else if (!block) {
@@ -354,8 +354,8 @@
 
     vp = &is->pictq[is->pictq_windex];
     
-    if (vp->bmp)
-        bmp_free(s->screen, vp->bmp);
+    bmp_free(s->screen, &vp->bmp);
+
     /* XXX: use generic function */
     switch (is->video_st->codec.pix_fmt) {
     case PIX_FMT_YUV420P:
@@ -382,9 +382,7 @@
         if (vp->bmp->width != is->video_st->codec.width ||
             vp->bmp->height != is->video_st->codec.height) {
             is_yuv = 0;
-            if (vp->bmp)
-                bmp_free(s->screen, vp->bmp);
-            vp->bmp = NULL;
+            bmp_free(s->screen, &vp->bmp);
             goto retry;
         }
     } else {
@@ -841,10 +839,7 @@
     /* free all pictures */
     for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE;  i++) {
         vp = &is->pictq[i];
-        if (vp->bmp) {
-            bmp_free(s->screen, vp->bmp);
-            vp->bmp = NULL;
-        }
+        bmp_free(s->screen, &vp->bmp);
     }
 
     if (is->video_timer) {

Index: win32.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/win32.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- win32.c     12 Dec 2007 08:51:02 -0000      1.7
+++ win32.c     12 Dec 2007 11:49:03 -0000      1.8
@@ -56,7 +56,8 @@
    int argc, count;
    char *command_line, *p;
 
-   command_line = malloc(strlen(lpszCmdLine) + sizeof(PROG_NAME) + 1);
+    command_line = qe_malloc_array(char, sizeof(PROG_NAME) +
+                                   strlen(lpszCmdLine) + 1);
    if (!command_line)
        return 0;
    strcpy(command_line, PROG_NAME " ");
@@ -76,7 +77,7 @@
       count++;
    }
 
-   argv = (char **)malloc((count + 1) * sizeof(char *));
+    argv = qe_malloc_array(char *, count + 1);
    if (!argv)
        return 0;
    
@@ -205,7 +206,7 @@
 {
     QEEventQ *e;
     
-    e = malloc(sizeof(QEEventQ));
+    e = qe_malloc(QEEventQ);
     if (!e)
         return;
     e->ev = *ev;
@@ -267,7 +268,7 @@
             
             ignore_wchar_msg = 0;
             
-            scan = (unsigned int) ((lParam >> 16) & 0x1FF);
+            scan = (unsigned int)((lParam >> 16) & 0x1FF);
             switch (scan) {
             case 0x00E:
                 ignore_wchar_msg = 1;
@@ -412,7 +413,7 @@
             first_event = e->next;
             if (!first_event)
                 last_event = NULL;
-            free(e);
+            qe_free(&e);
             break;
         }
 
@@ -448,7 +449,7 @@
     QEFont *font;
     TEXTMETRIC tm;
 
-    font = malloc(sizeof(QEFont));
+    font = qe_malloc(QEFont);
     if (!font)
         return NULL;
     GetTextMetrics(win_ctx.hdc, &tm);
@@ -460,7 +461,7 @@
 
 static void win_close_font(QEditScreen *s, QEFont *font)
 {
-    free(font);
+    qe_free(&font);
 }
 
 static void win_text_metrics(QEditScreen *s, QEFont *font, 

Index: x11.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/x11.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -b -r1.14 -r1.15
--- x11.c       12 Dec 2007 08:51:02 -0000      1.14
+++ x11.c       12 Dec 2007 11:49:03 -0000      1.15
@@ -528,7 +528,7 @@
     XftFont *renderFont;
     QEFont *font;
 
-    font = malloc(sizeof(QEFont));
+    font = qe_malloc(QEFont);
     if (!font)
         return NULL;
 
@@ -558,7 +558,7 @@
                              0);
     if (!renderFont) {
         /* CG: don't know if this can happen, should try fallback? */
-        free(font);
+        qe_free(&font);
         return NULL;
     }
     font->ascent = renderFont->ascent;
@@ -576,7 +576,7 @@
      * close_font.
      */
     memset(font, 0, sizeof(*font));
-    free(font);
+    qe_free(&font);
 }
 
 static int term_glyph_width(QEditScreen *s, QEFont *font, unsigned int cc)
@@ -665,7 +665,7 @@
     char **list;
     const char *p;
 
-    font = malloc(sizeof(QEFont));
+    font = qe_malloc(QEFont);
     if (!font)
         return NULL;
     
@@ -684,7 +684,7 @@
         p = strchr(p, ',');
         if (!p) {
             /* no font found */
-            free(font);
+            qe_free(&font);
             return NULL;
         }
         p++;
@@ -768,7 +768,7 @@
     return font;
  fail:
     XFreeFontNames(list);
-    free(font);
+    qe_free(&font);
     return NULL;
 }
 
@@ -781,7 +781,7 @@
      * close_font.
      */
     memset(font, 0, sizeof(*font));
-    free(font);
+    qe_free(&font);
 }
 
 /* get a char struct associated to a char. Return NULL if no glyph
@@ -1152,7 +1152,7 @@
         b = qs->yank_buffers[qs->yank_current];
         if (!b) 
             return;
-        buf = malloc(b->total_size);
+        buf = qe_malloc_array(unsigned char, b->total_size);
         if (!buf)
             return;
         eb_read(b, 0, buf, b->total_size);
@@ -1160,7 +1160,7 @@
         XChangeProperty(display, rq->requestor, rq->property,
                         XA_STRING, 8, PropModeReplace,
                         buf, b->total_size);
-        free(buf);
+        qe_free(&buf);
     }
     ev.xselection.property = rq->property;
     XSendEvent(display, rq->requestor, False, 0, &ev);
@@ -1410,7 +1410,7 @@
 {
     X11Bitmap *xb;
 
-    xb = malloc(sizeof(X11Bitmap));
+    xb = qe_malloc(X11Bitmap);
     if (!xb)
         return -1;
     b->priv_data = xb;
@@ -1452,7 +1452,7 @@
             XImage *ximage;
             ximage = XCreateImage(display, None, attr.depth, ZPixmap, 0, 
                                   NULL, b->width, b->height, 8, 0);
-            ximage->data = malloc(b->height * ximage->bytes_per_line);
+            ximage->data = qe_malloc_array(char, b->height * 
ximage->bytes_per_line);
             xb->u.ximage = ximage;
         }
         break;
@@ -1462,7 +1462,7 @@
             XShmSegmentInfo *shm_info;
             
             /* XXX: error testing */
-            shm_info = malloc(sizeof(XShmSegmentInfo));
+            shm_info = qe_malloc(XShmSegmentInfo);
             ximage = XShmCreateImage(display, None, attr.depth, ZPixmap, NULL,
                                      shm_info, b->width, b->height);
             shm_info->shmid = shmget(IPC_PRIVATE,
@@ -1487,7 +1487,7 @@
             XvImage *xvimage;
             xvimage = XvCreateImage(display, xv_port, xv_format, 0, 
                                     b->width, b->height);
-            xvimage->data = malloc(xvimage->data_size);
+            xvimage->data = qe_malloc_array(char, xvimage->data_size);
             xb->u.xvimage = xvimage;
         }
         break;
@@ -1496,7 +1496,7 @@
             XvImage *xvimage;
             XShmSegmentInfo *shm_info;
 
-            shm_info = malloc(sizeof(XShmSegmentInfo));
+            shm_info = qe_malloc(XShmSegmentInfo);
             xvimage = XvShmCreateImage(display, xv_port, xv_format, 0, 
                                        b->width, b->height, shm_info);
             shm_info->shmid = shmget(IPC_PRIVATE,
@@ -1519,7 +1519,7 @@
     }
     return 0;
  fail:
-    free(xb);
+    qe_free(&xb);
     return -1;
 }
 
@@ -1539,11 +1539,11 @@
         XShmDetach(display, xb->shm_info);
         XDestroyImage(xb->u.ximage);
         shmdt(xb->shm_info->shmaddr);
-        free(xb->shm_info);
+        qe_free(&xb->shm_info);
         break;
 #ifdef CONFIG_XV
     case BMP_XVIMAGE:
-        free(xb->u.xvimage->data);
+        qe_free(&xb->u.xvimage->data);
         XFree(xb->u.xvimage);
         xv_open_count--;
         break;
@@ -1551,12 +1551,12 @@
         XShmDetach(display, xb->shm_info);
         XFree(xb->u.xvimage);
         shmdt(xb->shm_info->shmaddr);
-        free(xb->shm_info);
+        qe_free(&xb->shm_info);
         xv_open_count--;
         break;
 #endif
     }
-    free(xb);
+    qe_free(&b->priv_data);
 }
 
 static void x11_bmp_draw(__unused__ QEditScreen *s, QEBitmap *b, 
@@ -1615,7 +1615,7 @@
             XImage *ximage;
             ximage = XCreateImage(display, None, attr.depth, ZPixmap, 0, 
                                   NULL, w1, h1, 8, 0);
-            ximage->data = malloc(h1 * ximage->bytes_per_line);
+            ximage->data = qe_malloc_array(char, h1 * ximage->bytes_per_line);
             pict->data[0] = (unsigned char *)ximage->data;
             pict->linesize[0] = ximage->bytes_per_line;
             xb->ximage_lock = ximage;

Index: libqhtml/css.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/libqhtml/css.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- libqhtml/css.c      10 Dec 2007 13:28:08 -0000      1.10
+++ libqhtml/css.c      12 Dec 2007 11:49:03 -0000      1.11
@@ -310,7 +310,7 @@
         }
         p = p->hash_next;
     }
-    p = malloc(sizeof(CSSIdentEntry) + strlen(str));
+    p = qe_malloc_hack(CSSIdentEntry, strlen(str));
     if (!p)
         return CSS_ID_NIL;
     p->id = table_ident_nb;
@@ -321,12 +321,10 @@
     /* put ident in table */
     if (table_ident_nb == table_ident_allocated) {
         n = table_ident_allocated + CSS_IDENT_INCR;
-        pp = realloc(table_ident, n * sizeof(CSSIdentEntry *));
-        if (!pp) {
-            free(p);
+        if (!qe_realloc(&table_ident, n * sizeof(CSSIdentEntry *))) {
+            qe_free(&p);
             return CSS_ID_NIL;
         }
-        table_ident = pp;
         table_ident_allocated = n;
     }
     table_ident[table_ident_nb++] = p;
@@ -364,7 +362,7 @@
             p->value = value;
         }
     }
-    p = malloc(sizeof(CSSCounterValue));
+    p = qe_malloc(CSSCounterValue);
     if (!p)
         return;
     p->counter_id = counter_id;
@@ -389,7 +387,7 @@
 
     for (p = s->counter_stack_ptr; p != s->counter_stack_base; p = p1) {
         p1 = p->prev;
-        free(p);
+        qe_free(&p);
         p = p1;
     }
     s->counter_stack_ptr = s->counter_stack_base;
@@ -659,7 +657,7 @@
         value++;
     }
     if (buf[0] != '\0')
-        return strdup(buf);
+        return qe_strdup(buf);
     else
         return NULL;
 }
@@ -840,11 +838,10 @@
         pp = &p->hash_next;
     }
     /* add new props */
-    p = malloc(sizeof(CSSState));
+    p = qe_malloc_dup(props, sizeof(CSSState));
     if (!p)
         return NULL;
     s->nb_props++;
-    memcpy(p, props, sizeof(CSSState));
     *pp = p;
     p->hash_next = NULL;
     return p;
@@ -853,7 +850,7 @@
 /* free one CSSState */
 static void free_props(CSSState *props)
 {
-    free(props);
+    qe_free(&props);
 }
 
 static int css_compute_block(CSSContext *s, CSSBox *box, 
@@ -878,7 +875,7 @@
     css_compute_block(s, box1, pelement_props);
     
     css_set_text_string(box1, content);
-    free(content);
+    qe_free(&content);
     /* XXX: make child box */
     return box1;
 }
@@ -1150,10 +1147,9 @@
     }
 #endif    
 
-    box2 = malloc(sizeof(CSSBox));
+    box2 = qe_mallocz(CSSBox);
     if (!box2) 
         return;
-    memset(box2, 0, sizeof(CSSBox));
     box2->split = 1;
     box2->props = box1->props; /* same properties */
     box2->content_type = box1->content_type;
@@ -1648,7 +1644,7 @@
 #endif
     /* layout the interior */
     if (css_layout_block(s->ctx, &layout, box)) {
-        free(b);
+        qe_free(&b);
         return -1;
     }
     /* add the float in the float list */
@@ -1856,10 +1852,9 @@
 
     if (level_max > 0) {
         /* needed to do bidir reordering */
-        box_table = malloc(sizeof(InlineBox) * nb_boxes);
+        box_table = qe_malloc_dup(line_boxes, sizeof(InlineBox) * nb_boxes);
         if (box_table) {
             /* record the logical order of the boxes */
-            memcpy(box_table, line_boxes, nb_boxes * sizeof(InlineBox));
             /* rearrange them to match visual order */
             embed_boxes(box_table, nb_boxes, level_max);
         }
@@ -1939,8 +1934,7 @@
     y += line_height;
     s->y = y;
 
-    if (box_table)
-        free(box_table);
+    qe_free(&box_table);
     
     /* prepare for next line */
  the_end:
@@ -2445,7 +2439,7 @@
     s->nb_cols++;
     if (s->nb_cols > s->nb_cols_allocated) {
         s->nb_cols_allocated = s->nb_cols_allocated + COL_INCR;
-        s->cols = realloc(s->cols, s->nb_cols_allocated * sizeof(ColStruct));
+        qe_realloc(&s->cols, s->nb_cols_allocated * sizeof(ColStruct));
         memset(s->cols + s->nb_cols_allocated - COL_INCR, 0, 
                COL_INCR * sizeof(ColStruct));
     }
@@ -3084,14 +3078,14 @@
     tl->caption_box = NULL;
     if (layout_table_render(tl, table_box)) {
     fail:
-        free(tl->cols);
+        qe_free(&tl->cols);
         return -1;
     }
     tl->y += tl->border_v;
     /* compute total table height */
     table_box->height = max(tl->y, table_box->height);
 
-    free(tl->cols);
+    qe_free(&tl->cols);
 
     /* handle table caption */
     caption_box = tl->caption_box;
@@ -3152,7 +3146,7 @@
 {
     FloatBlock *b, **pb;
 
-    b = malloc(sizeof(FloatBlock));
+    b = qe_malloc(FloatBlock);
     if (!b)
         return 0;
     b->box = box;
@@ -3173,7 +3167,7 @@
 
     while (b != NULL) {
         b1 = b->next;
-        free(b);
+        qe_free(&b);
         b = b1;
     }
 }
@@ -4360,10 +4354,9 @@
 {
     CSSBox *box;
 
-    box = malloc(sizeof(CSSBox));
+    box = qe_mallocz(CSSBox);
     if (!box)
         return NULL;
-    memset(box, 0, sizeof(CSSBox));
     box->tag = tag;
     box->attrs = attrs;
     return box;
@@ -4400,26 +4393,26 @@
         case CSS_CONTENT_TYPE_STRING:
             /* split boxes never own their content */
             if (!box->split)
-                free((void *)box->u.buffer.start);
+                qe_free((void **)(void *)&box->u.buffer.start);
             break;
         case CSS_CONTENT_TYPE_IMAGE:
-            free(box->u.image.content_alt);
+            qe_free(&box->u.image.content_alt);
             break;
         }
         box1 = box->next;
         a = box->attrs;
         while (a != NULL) {
             a1 = a->next;
-            free(a);
+            qe_free(&a);
             a = a1;
         }
         p = box->properties;
         while (p != NULL) {
             p1 = p->next;
-            free(p);
+            qe_free(&p);
             p = p1;
         }
-        free(box);
+        qe_free(&box);
         box = box1;
     }
 }
@@ -4440,7 +4433,7 @@
     char *str;
 
     box->content_type = CSS_CONTENT_TYPE_STRING;
-    str = strdup(string);
+    str = qe_strdup(string);
     len = strlen(string);
     box->u.buffer.start = (unsigned long)str;
     box->u.buffer.end = (unsigned long)str + len;
@@ -4478,10 +4471,9 @@
 {
     CSSContext *s;
 
-    s = malloc(sizeof(CSSContext));
+    s = qe_mallocz(CSSContext);
     if (!s)
         return NULL;
-    memset(s, 0, sizeof(CSSContext));
     s->style_sheet = NULL;
     s->screen = screen;
     s->b = b;
@@ -4511,7 +4503,7 @@
     if (s->style_sheet) {
         css_free_style_sheet(s->style_sheet);
     }
-    free(s);
+    qe_free(&s);
 }
 
 /* must be called before using any css functions */

Index: libqhtml/cssparse.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/libqhtml/cssparse.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- libqhtml/cssparse.c 3 Dec 2007 16:12:40 -0000       1.8
+++ libqhtml/cssparse.c 12 Dec 2007 11:49:03 -0000      1.9
@@ -181,7 +181,7 @@
     *q = '\0';
     //    printf("string='%s'\n", buf);
     *pp = p;
-    return strdup(buf);
+    return qe_strdup(buf);
 }
 
 /* add a given number of values */
@@ -191,7 +191,7 @@
 {
     CSSProperty *prop;
 
-    prop = malloc(sizeof(CSSProperty) + 
+    prop = qe_malloc_hack(CSSProperty,
                   (nb_values - 1) * sizeof(CSSPropertyValue));
     if (!prop)
         return;
@@ -618,10 +618,9 @@
 {
     CSSStyleSheet *s;
 
-    s = malloc(sizeof(CSSStyleSheet));
+    s = qe_mallocz(CSSStyleSheet);
     if (!s)
         return NULL;
-    memset(s, 0, sizeof(*s));
     s->plast_entry = &s->first_entry;
     return s;
 }
@@ -632,7 +631,7 @@
 
     for (attr = ss->attrs; attr != NULL; attr = attr1) {
         attr1 = attr->next;
-        free(attr);
+        qe_free(&attr);
     }
 }
 
@@ -649,18 +648,18 @@
         for (ss = e->sel.next; ss != NULL; ss = ss1) {
             ss1 = ss->next;
             free_selector(ss);
-            free(ss);
+            qe_free(&ss);
         }
         free_selector(&e->sel);
 
         for (p = e->props; p != NULL; p = p1) {
             p1 = p->next;
-            free(p);
+            qe_free(&p);
         }
-        free(e);
+        qe_free(&e);
     }
     
-    free(s);
+    qe_free(&s);
 }
 
 static int bgetc1(CSSParseState *b)
@@ -758,7 +757,7 @@
 {
     CSSStyleSheetAttributeEntry *ae;
     
-    ae = malloc(sizeof(CSSStyleSheetAttributeEntry) + strlen(value));
+    ae = qe_malloc_hack(CSSStyleSheetAttributeEntry, strlen(value));
     ae->attr = attr;
     ae->op = op;
     strcpy(ae->value, value);
@@ -776,10 +775,9 @@
     CSSStyleSheetEntry *e, **pp;
     
     /* add the style sheet entry */
-    e = malloc(sizeof(CSSStyleSheetEntry));
+    e = qe_mallocz(CSSStyleSheetEntry);
     if (!e)
         return NULL;
-    memset(e, 0, sizeof(*e));
     e->sel = *ss;
     e->media = media;
     
@@ -837,7 +835,7 @@
         /* add selector operations */
         pss = &e1->sel.next;
         for (ss = e->sel.next; ss != NULL; ss = ss->next) {
-            ss1 = malloc(sizeof(CSSSimpleSelector));
+            ss1 = qe_malloc(CSSSimpleSelector);
             dup_selector(ss1, ss);
             *pss = ss1;
             pss = &ss1->next;
@@ -1063,9 +1061,8 @@
                 } else if (isalpha(ch)) {
                     tree_op = CSS_TREE_OP_DESCENDANT;
                 add_tree:
-                    ss1 = malloc(sizeof(CSSSimpleSelector));
+                    ss1 = qe_malloc_dup(ss, sizeof(CSSSimpleSelector));
                     if (ss1) {
-                        memcpy(ss1, ss, sizeof(CSSSimpleSelector));
                         last_ss = ss1;
                     }
                     last_tree_op = tree_op;

Index: libqhtml/xmlparse.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/libqhtml/xmlparse.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -b -r1.11 -r1.12
--- libqhtml/xmlparse.c 6 Dec 2007 17:59:23 -0000       1.11
+++ libqhtml/xmlparse.c 12 Dec 2007 11:49:03 -0000      1.12
@@ -155,7 +155,7 @@
 static inline void strbuf_reset(StringBuffer *b)
 {
     if (b->buf != b->buf1)
-        free(b->buf);
+        qe_free(&b->buf);
     strbuf_init(b);
 }
 
@@ -168,8 +168,7 @@
     ptr = b->buf;
     if (b->buf == b->buf1)
         ptr = NULL;
-    ptr = realloc(ptr, size1);
-    if (ptr) {
+    if (qe_realloc(&ptr, size1)) {
         if (b->buf == b->buf1)
             memcpy(ptr, b->buf1, STRING_BUF_SIZE);
         b->buf = ptr;
@@ -211,7 +210,7 @@
 
 static void offsetbuf_reset(OffsetBuffer *b)
 {
-    free(b->offsets);
+    qe_free(&b->offsets);
     offsetbuf_init(b);
 }
 
@@ -229,8 +228,7 @@
             if (!n)
                 n = 1;
             n = n * 2;
-            b->offsets = realloc(b->offsets, n * sizeof(unsigned int));
-            if (!b->offsets)
+            if (!qe_realloc(&b->offsets, n * sizeof(unsigned int)))
                 return;
             b->nb_allocated_offsets = n;
         }
@@ -274,10 +272,9 @@
 {
     XMLState *s;
     
-    s = malloc(sizeof(XMLState));
+    s = qe_mallocz(XMLState);
     if (!s)
         return NULL;
-    memset(s, 0, sizeof(*s));
     s->flags = flags;
     s->is_html = flags & XML_HTML;
     s->html_syntax = flags & XML_HTML_SYNTAX;
@@ -302,7 +299,7 @@
 {
     CSSAttribute *attr;
 
-    attr = malloc(sizeof(CSSAttribute) + strlen(value));
+    attr = qe_malloc_hack(CSSAttribute, strlen(value));
     if (!attr)
         return NULL;
     attr->attr = attr_id;
@@ -433,7 +430,7 @@
         }
         if (value && value[0] != '\0') {
             arg.type = CSS_VALUE_STRING;
-            arg.u.str = strdup(value);
+            arg.u.str = qe_strdup(value);
             css_add_prop(&last_prop, CSS_content_alt, &arg);
         }
         
@@ -1283,7 +1280,7 @@
     strbuf_reset(&s->str);
     root_box = s->root_box;
     
-    free(s);
+    qe_free(&s);
     return root_box;
 }
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]