check alloc
authorJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 13 Dec 2021 06:47:37 +0000 (08:47 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 13 Dec 2021 06:47:37 +0000 (08:47 +0200)
Makefile
imago/Makefile
imago/src/filejpeg.c
imago/src/filepng.c
imago/src/filergbe.c
imago/src/ftmodule.c
imago/src/imago2.c
src/main.c
src/util.h
src/video.c
src/watdpmi.c

index 802614e..c70be70 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,10 +1,9 @@
-obj = main.obj video.obj vga.obj vbe.obj watdpmi.obj mouse.obj
+obj = main.obj video.obj vga.obj vbe.obj watdpmi.obj mouse.obj chkalloc.obj
 bin = imgv.exe
 
 #opt = -otexan
 warn = -w=3
 dbg = -d3
-def = -DNO_STDINT_H
 !ifdef __UNIX__
 inc = -Iimago/src
 libimago = imago/imago.lib
@@ -14,6 +13,8 @@ libimago = imago\imago.lib
 !endif
 libdir = libpath imago
 
+#def = -DCHECK_ALLOC
+
 CC = wcc386
 LD = wlink
 CFLAGS = $(warn) $(dbg) $(opt) $(def) -zq -bt=dos $(inc)
index c710306..67db23f 100644 (file)
@@ -22,6 +22,7 @@ obj = src/conv.obj src/filejpeg.obj src/filepng.obj src/fileppm.obj src/filergbe
        src/filetga.obj src/filelbm.obj src/ftmodule.obj src/imago2.obj src/imago_gl.obj &
        src/byteord.obj src/modules.obj $(libpng) $(zlib) $(jpeglib)
 
+inc = -I../src
 !else
 
 libpng = libpng\png.obj libpng\pngerror.obj libpng\pngget.obj libpng\pngmem.obj &
@@ -46,6 +47,8 @@ jpeglib = jpeglib\jcapimin.obj jpeglib\jcapistd.obj jpeglib\jccoefct.obj &
 obj = src\conv.obj src\filejpeg.obj src\filepng.obj src\fileppm.obj src\filergbe.obj &
        src\filetga.obj src\filelbm.obj src\ftmodule.obj src\imago2.obj src\imago_gl.obj &
        src\byteord.obj src\modules.obj $(libpng) $(zlib) $(jpeglib)
+
+inc = -I..\src
 !endif
 
 alib = imago.lib
@@ -56,21 +59,25 @@ dbg = -d1
 def = -DPNG_NO_SNPRINTF
 
 CC = wcc386
-CFLAGS = $(dbg) $(opt) $(def) -zq -bt=dos -Ilibpng -Izlib -Ijpeglib
+CFLAGS = $(dbg) $(opt) $(def) -zq -bt=dos -Ilibpng -Izlib -Ijpeglib $(inc)
 
-$(alib): $(obj)
+$(alib): cflags.occ $(obj)
        %write objects.lbc $(obj)
        wlib -b -n $@ @objects
 
 .c: src;libpng;jpeglib;zlib
 
+cflags.occ: Makefile
+       %write $@ $(CFLAGS)
+
 .c.obj: .autodepend
-       $(CC) -fo=$@ $(CFLAGS) $[*
+       $(CC) -fo=$@ @cflags.occ $[*
 
 !ifdef __UNIX__
 clean: .symbolic
        rm -f $(obj)
        rm -f objects.lbc
+       rm -f cflags.occ
        rm -f $(alib)
 !else
 clean: .symbolic
@@ -79,5 +86,6 @@ clean: .symbolic
        del libpng\*.obj
        del jpeglib\*.obj
        del objects.lbc
+       del cflags.occ
        del $(alib)
 !endif
index f7139c6..d9cf26f 100644 (file)
@@ -31,6 +31,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <jpeglib.h>
 #include "imago2.h"
 #include "ftmodule.h"
+#include "chkalloc.h"
 
 #define INPUT_BUF_SIZE 512
 #define OUTPUT_BUF_SIZE        512
@@ -125,7 +126,7 @@ static int read(struct img_pixmap *img, struct img_io *io)
                return -1;
        }
 
-       if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
+       if(!(scanlines = chk_malloc(img->height * sizeof *scanlines))) {
                jpeg_destroy_decompress(&cinfo);
                return -1;
        }
@@ -142,7 +143,7 @@ static int read(struct img_pixmap *img, struct img_io *io)
        jpeg_finish_decompress(&cinfo);
        jpeg_destroy_decompress(&cinfo);
 
-       free(scanlines);
+       chk_free(scanlines);
        return 0;
 }
 
@@ -168,7 +169,7 @@ static int write(struct img_pixmap *img, struct img_io *io)
                img = &tmpimg;
        }
 
-       if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
+       if(!(scanlines = chk_malloc(img->height * sizeof *scanlines))) {
                img_destroy(&tmpimg);
                return -1;
        }
@@ -202,7 +203,7 @@ static int write(struct img_pixmap *img, struct img_io *io)
        jpeg_finish_compress(&cinfo);
        jpeg_destroy_compress(&cinfo);
 
-       free(scanlines);
+       chk_free(scanlines);
        img_destroy(&tmpimg);
        return 0;
 }
index 077b2ba..dcba12d 100644 (file)
@@ -24,6 +24,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <png.h>
 #include "imago2.h"
 #include "ftmodule.h"
+#include "chkalloc.h"
 
 static int check_file(struct img_io *io);
 static int read_file(struct img_pixmap *img, struct img_io *io);
@@ -175,7 +176,7 @@ static int write_file(struct img_pixmap *img, struct img_io *io)
                        PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
        png_set_text(png, info, &txt, 1);
 
-       if(!(rows = malloc(img->height * sizeof *rows))) {
+       if(!(rows = chk_malloc(img->height * sizeof *rows))) {
                png_destroy_write_struct(&png, &info);
                img_destroy(&tmpimg);
                return -1;
@@ -192,7 +193,7 @@ static int write_file(struct img_pixmap *img, struct img_io *io)
        png_write_end(png, info);
        png_destroy_write_struct(&png, &info);
 
-       free(rows);
+       chk_free(rows);
 
        img_destroy(&tmpimg);
        return 0;
index 488f1ad..7b24287 100644 (file)
@@ -232,24 +232,24 @@ static int rgbe_write_header(struct img_io *io, int width, int height, rgbe_head
        }
        buf = malloc(ptypelen > 120 ? ptypelen + 8 : 128);
        sprintf(buf, "#?%s\n", programtype);
-       if(io->write(buf, strlen(buf), io->uptr) < 0)
+       if(io->write(buf, strlen(buf), io->uptr) <= 0)
                goto err;
        /* The #? is to identify file type, the programtype is optional. */
        if(info && (info->valid & RGBE_VALID_GAMMA)) {
                sprintf(buf, "GAMMA=%g\n", info->gamma);
-               if(io->write(buf, strlen(buf), io->uptr) < 0)
+               if(io->write(buf, strlen(buf), io->uptr) <= 0)
                        goto err;
        }
        if(info && (info->valid & RGBE_VALID_EXPOSURE)) {
                sprintf(buf, "EXPOSURE=%g\n", info->exposure);
-               if(io->write(buf, strlen(buf), io->uptr) < 0)
+               if(io->write(buf, strlen(buf), io->uptr) <= 0)
                        goto err;
        }
        strcpy(buf, "FORMAT=32-bit_rle_rgbe\n\n");
-       if(io->write(buf, strlen(buf), io->uptr) < 0)
+       if(io->write(buf, strlen(buf), io->uptr) <= 0)
                goto err;
        sprintf(buf, "-Y %d +X %d\n", height, width);
-       if(io->write(buf, strlen(buf), io->uptr) < 0)
+       if(io->write(buf, strlen(buf), io->uptr) <= 0)
                goto err;
 
        free(buf);
index b52d010..065d6e8 100644 (file)
@@ -19,6 +19,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <stdlib.h>
 #include <string.h>
 #include "ftmodule.h"
+#include "chkalloc.h"
 
 static struct list_node {
        struct ftype_module *module;
@@ -34,7 +35,7 @@ int img_register_module(struct ftype_module *mod)
 {
        struct list_node *node;
 
-       if(!(node = malloc(sizeof *node))) {
+       if(!(node = chk_malloc(sizeof *node))) {
                return -1;
        }
 
index 77bf7f0..b4ec465 100644 (file)
@@ -21,6 +21,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <string.h>
 #include "imago2.h"
 #include "ftmodule.h"
+#include "chkalloc.h"
 
 static int pixel_size(enum img_fmt fmt);
 static size_t def_read(void *buf, size_t bytes, void *uptr);
@@ -40,17 +41,17 @@ void img_init(struct img_pixmap *img)
 
 void img_destroy(struct img_pixmap *img)
 {
-       free(img->pixels);
+       chk_free(img->pixels);
        img->pixels = 0;        /* just in case... */
        img->width = img->height = 0xbadbeef;
-       free(img->name);
+       chk_free(img->name);
 }
 
 struct img_pixmap *img_create(void)
 {
        struct img_pixmap *p;
 
-       if(!(p = malloc(sizeof *p))) {
+       if(!(p = chk_malloc(sizeof *p))) {
                return 0;
        }
        img_init(p);
@@ -60,14 +61,14 @@ struct img_pixmap *img_create(void)
 void img_free(struct img_pixmap *img)
 {
        img_destroy(img);
-       free(img);
+       chk_free(img);
 }
 
 int img_set_name(struct img_pixmap *img, const char *name)
 {
        char *tmp;
 
-       if(!(tmp = malloc(strlen(name) + 1))) {
+       if(!(tmp = chk_malloc(strlen(name) + 1))) {
                return -1;
        }
        strcpy(tmp, name);
@@ -94,7 +95,7 @@ int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void
        void *newpix;
        int pixsz = pixel_size(fmt);
 
-       if(!(newpix = malloc(w * h * pixsz))) {
+       if(!(newpix = chk_malloc(w * h * pixsz))) {
                return -1;
        }
 
@@ -104,7 +105,7 @@ int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void
                memset(newpix, 0, w * h * pixsz);
        }
 
-       free(img->pixels);
+       chk_free(img->pixels);
        img->pixels = newpix;
        img->width = w;
        img->height = h;
@@ -154,7 +155,7 @@ int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, enum img_fmt
 
 void img_free_pixels(void *pix)
 {
-       free(pix);
+       chk_free(pix);
 }
 
 int img_load(struct img_pixmap *img, const char *fname)
index 9367eca..31e8a71 100644 (file)
@@ -5,6 +5,7 @@
 #include "video.h"
 #include "mouse.h"
 #include "imago2.h"
+#include "chkalloc.h"
 
 int switch_mode(int m);
 int find_best_mode(int minx, int miny);
@@ -62,7 +63,7 @@ int main(int argc, char **argv)
        num = num_video_modes();
        vmodes = video_modes();
 
-       if(!(vmlist = malloc(num * sizeof *vmlist))) {
+       if(!(vmlist = chk_malloc(num * sizeof *vmlist))) {
                fprintf(stderr, "failed to allocate video mode list\n");
                return 1;
        }
@@ -155,12 +156,13 @@ int main(int argc, char **argv)
 end:
 
        img_destroy(&img);
-       free(backbuf);
-       free(vmlist);
+       chk_free(backbuf);
+       chk_free(vmlist);
        if(cur_vm >= 0) {
                set_text_mode();
        }
        cleanup_video();
+       chk_check();
        return 0;
 }
 
@@ -169,8 +171,8 @@ int switch_mode(int m)
        vmode = vmlist + m;
        cur_vm = m;
 
-       free(backbuf);
-       if(!(backbuf = malloc(vmode->ysz * vmode->pitch))) {
+       chk_free(backbuf);
+       if(!(backbuf = chk_malloc(vmode->ysz * vmode->pitch))) {
                fprintf(stderr, "failed to allocate back buffer\n");
                return -1;
        }
index 6d1e57e..37195e8 100644 (file)
@@ -1,12 +1,11 @@
 #ifndef UTIL_H_
 #define UTIL_H_
 
+#if defined(__WATCOMC__) && __WATCOMC__ < 1200
+#define NO_STDINT_H
+#endif
 
 #ifdef NO_STDINT_H
-
-#if __WATCOMC__ >= 1200
-#include <sys/types.h>
-#else
 typedef char int8_t;
 typedef unsigned char uint8_t;
 typedef short int16_t;
@@ -14,8 +13,6 @@ typedef unsigned short uint16_t;
 typedef int int32_t;
 typedef unsigned int uint32_t;
 typedef unsigned long uintptr_t;
-#endif
-
 #else
 #include <stdint.h>
 #endif
index 9c77bba..91f09f3 100644 (file)
@@ -6,6 +6,7 @@
 #include "vbe.h"
 #include "vga.h"
 #include "util.h"
+#include "chkalloc.h"
 
 #ifdef __WATCOMC__
 #include <i86.h>
@@ -61,7 +62,7 @@ int init_video(void)
 
        num_vmodes = 0;
        max_modes = 256;
-       if(!(vmodes = malloc(max_modes * sizeof *vmodes))) {
+       if(!(vmodes = chk_malloc(max_modes * sizeof *vmodes))) {
                fprintf(stderr, "failed to allocate video modes list\n");
                return -1;
        }
@@ -76,9 +77,9 @@ int init_video(void)
 
                if(num_vmodes >= max_modes) {
                        int newmax = max_modes ? (max_modes << 1) : 16;
-                       if(!(vmptr = realloc(vmodes, newmax * sizeof *vmodes))) {
+                       if(!(vmptr = chk_realloc(vmodes, newmax * sizeof *vmodes))) {
                                fprintf(stderr, "failed to grow video mode list (%d)\n", newmax);
-                               free(vmodes);
+                               chk_free(vmodes);
                                return -1;
                        }
                        vmodes = vmptr;
@@ -123,7 +124,7 @@ int init_video(void)
 
 void cleanup_video(void)
 {
-       free(vmodes);
+       chk_free(vmodes);
 }
 
 struct video_mode *video_modes(void)
@@ -394,7 +395,7 @@ void set_msr(uint32_t msr, uint32_t low, uint32_t high);
        parm [ecx][eax][edx]
 
 #endif /* __WATCOMC__ */
-       
+
 
 #define MSR_MTRRCAP                    0xfe
 #define MSR_MTRRDEFTYPE                0x2ff
index 18f61a1..e21d3af 100644 (file)
@@ -27,7 +27,7 @@ void dpmi_free(uint16_t sel)
        int386(0x31, &regs, &regs);
 }
 
-void dpmi_int(int inum, struct dpmi_real_regs *dregs)
+void dpmi_int(int inum, struct dpmi_regs *dregs)
 {
        union REGS regs = {0};