]> git.sur5r.net Git - cc65/commitdiff
Rewrote tgi_load_driver in assembler and fixed a possible memory leak when
authoruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 22 Jul 2012 11:07:46 +0000 (11:07 +0000)
committeruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 22 Jul 2012 11:07:46 +0000 (11:07 +0000)
doing so. Changed the behaviour in case a driver is already loaded: Since the
library cannot know if this driver was linked statically or loaded
dynamically, an already installed driver is considered an error. It must be
removed before calling tgi_load_driver.

git-svn-id: svn://svn.cc65.org/cc65/trunk@5790 b7a2c559-68d2-44c3-8de9-860c34a00d81

asminc/tgi-error.inc
asminc/tgi-kernel.inc
include/tgi/tgi-error.h
libsrc/tgi/Makefile
libsrc/tgi/tgi-kernel.s
libsrc/tgi/tgi_geterrormsg.s
libsrc/tgi/tgi_load.s [new file with mode: 0644]
libsrc/tgi/tgi_load_driver.c [deleted file]

index b375918a694847a8d8b87137c49c700b81304237..0e026ee1b3debd2e0aaf6502984442d7aa208404 100644 (file)
@@ -6,7 +6,7 @@
 ;*                                                                           */
 ;*                                                                           */
 ;*                                                                           */
-;* (C) 2002-2009, Ullrich von Bassewitz                                      */
+;* (C) 2002-2012, Ullrich von Bassewitz                                      */
 ;*                Roemerstrasse 52                                           */
 ;*                D-70794 Filderstadt                                        */
 ;* EMail:         uz@cc65.org                                                */
@@ -45,6 +45,7 @@
         TGI_ERR_INV_FONT        ; Font file is invalid
         TGI_ERR_NO_RES          ; Out of resources (memory, handles, ...)
         TGI_ERR_UNKNOWN         ; Unknown error
+        TGI_ERR_INSTALLED       ; A driver is already installed
 
         TGI_ERR_COUNT           ; Special: Number of error messages
 .endenum
index 6eabe9a3b0eb0f5ed8a63b652a5830192e5d31dc..9061fe9af861b72c11fb662cb7156d478794ed21 100644 (file)
@@ -6,7 +6,7 @@
 ;*                                                                           */
 ;*                                                                           */
 ;*                                                                           */
-;* (C) 2002-2009, Ullrich von Bassewitz                                      */
+;* (C) 2002-2012, Ullrich von Bassewitz                                      */
 ;*                Roemerstrasse 52                                           */
 ;*                D-70794 Filderstadt                                        */
 ;* EMail:         uz@cc65.org                                                */
@@ -170,6 +170,7 @@ TGI_CLIP_TOP            = $08
 ;------------------------------------------------------------------------------
 ; ASM functions
 
+        .global tgi_clear_ptr
         .global tgi_clippedline
         .global tgi_curtoxy
         .global tgi_getset
index 11c97e337d935984a4306a8893c90fb842cd6b7a..e8dd9c236f0b088e6f8d353aace23f7938d28b6a 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2002-2009, Ullrich von Bassewitz                                      */
+/* (C) 2002-2012, Ullrich von Bassewitz                                      */
 /*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
@@ -53,6 +53,7 @@
 #define TGI_ERR_INV_FUNC       6       /* Function not supported */
 #define TGI_ERR_INV_FONT        7       /* Font file is invalid */
 #define TGI_ERR_NO_RES          8       /* Out of resources */
+#define TGI_ERR_INSTALLED       9       /* A driver is already installed */
 
 
 
index c1a8b41878d7b5644e3447e4c00db72ab64c45b8..086f98c562fc16d8bc168a58a124c68983839647 100644 (file)
@@ -30,7 +30,6 @@ CFLAGS        = -Osir -g -T -t $(SYS) --forget-inc-paths -I . -I ../../include
 # Object files
 
 C_OBJS =        tgi_arc.o               \
-                tgi_load_driver.o       \
                 tgi_load_vectorfont.o   \
                 tgi_pieslice.o
 
@@ -68,6 +67,7 @@ S_OBJS =              tgi-kernel.o            \
                 tgi_line.o              \
                 tgi_linepop.o           \
                 tgi_lineto.o            \
+                tgi_load.o              \
                 tgi_outcode.o           \
                 tgi_outtext.o           \
                 tgi_outtextxy.o         \
index 74995412a3d4731131e0518814b78a5a955d1ebb..cfdd375b2e323932cfb6d96fd4d96823225613c7 100644 (file)
@@ -201,6 +201,7 @@ _tgi_uninstall:
 
 ; Clear driver pointer and error code
 
+tgi_clear_ptr:
         lda     #$00
         sta     _tgi_drv
         sta     _tgi_drv+1
index b7cfd87c1e4da04a8636301218c11e944291929a..d683a1ad5607d5fea1162ad931ccdf6bcba2c1ca 100644 (file)
@@ -41,6 +41,7 @@ offs:   .byte   <(msg0-msgtab)
         .byte   <(msg7-msgtab)
         .byte   <(msg8-msgtab)
         .byte   <(msg9-msgtab)
+        .byte   <(msg10-msgtab)
 
 msgtab:
 msg0:   .asciiz         "No error"
@@ -53,4 +54,6 @@ msg6:   .asciiz         "Function not supported"
 msg7:   .asciiz         "Invalid font file"
 msg8:   .asciiz         "Out of resources"
 msg9:   .asciiz         "Unknown error"
+msg10:  .asciiz         "A driver is already installed"
 
+.assert (*-msgtab) < 256, error, "Message table too large"
diff --git a/libsrc/tgi/tgi_load.s b/libsrc/tgi/tgi_load.s
new file mode 100644 (file)
index 0000000..9ce4380
--- /dev/null
@@ -0,0 +1,118 @@
+;
+; Ullrich von Bassewitz, 2012-07-22
+;
+; void __fastcall__ tgi_load_driver (const char* name);
+; /* Load and install the given driver. */
+
+
+        .include        "tgi-kernel.inc"
+        .include        "tgi-error.inc"
+        .include        "modload.inc"
+        .include        "fcntl.inc"
+
+        .import         pushax
+        .import         pusha0
+        .import         incsp2
+        .import         _open
+        .import         _read
+        .import         _close
+
+
+
+;----------------------------------------------------------------------------
+; Variables
+
+.data
+
+ctrl:   .addr   _read
+        .res    2                       ; CALLERDATA
+        .res    2                       ; MODULE
+        .res    2                       ; MODULE_SIZE
+        .res    2                       ; MODULE_ID
+
+;----------------------------------------------------------------------------
+; Code
+
+.code
+
+.proc   _tgi_load_driver
+
+; Check if we do already have a driver loaded. This is an error. Do not
+; touch A/X because they contain the file name.
+
+        ldy     _tgi_drv
+        bne     @L0
+        ldy     _tgi_drv+1
+        beq     @L1
+@L0:    lda     #TGI_ERR_INSTALLED
+        bne     @L3
+
+; Push the name onto the C stack and open the file. The parameter will get
+; removed by open().
+; ctrl.callerdata = open (name, O_RDONLY);
+
+@L1:    jsr     pushax
+        lda     #<O_RDONLY
+        jsr     pusha0
+        ldy     #4                      ; Argument size
+        jsr     _open
+        sta     ctrl + MOD_CTRL::CALLERDATA
+        stx     ctrl + MOD_CTRL::CALLERDATA+1
+
+; if (ctrl.callerdata >= 0) {
+
+        txa
+        bmi     @L2
+
+; /* Load the module */
+; Res = mod_load (&ctrl);
+
+        lda     #<ctrl
+        ldx     #>ctrl
+        jsr     _mod_load
+        pha
+
+; /* Close the input file */
+; close (ctrl.callerdata);
+
+        lda     ctrl + MOD_CTRL::CALLERDATA
+        ldx     ctrl + MOD_CTRL::CALLERDATA+1
+        jsr     _close
+
+; /* Check the return code */
+; if (Res == MLOAD_OK) {
+
+        pla
+        beq     @L5
+@L2:    lda     #TGI_ERR_CANNOT_LOAD
+
+; Set an error and exit
+
+@L3:    sta     _tgi_error
+@L4:    rts
+
+; Check the driver signature, install the driver. c is already on stack and
+; will get removed by ser_install().
+; Res = ser_install (ctrl.module);
+
+@L5:    lda     ctrl + MOD_CTRL::MODULE
+        ldx     ctrl + MOD_CTRL::MODULE+1
+        jsr     _tgi_install
+
+; If tgi_install was successful, we're done
+
+        lda     _tgi_error
+        beq     @L4
+
+; The driver didn't install correctly. Remove it from memory. The error code
+; will be retained.
+
+        lda     _tgi_drv
+        ldx     _tgi_drv+1
+        jsr     _mod_free               ; Free the driver memory
+        jmp     tgi_clear_ptr           ; Clear tgi_drv and return
+
+.endproc
+
+
+   
diff --git a/libsrc/tgi/tgi_load_driver.c b/libsrc/tgi/tgi_load_driver.c
deleted file mode 100644 (file)
index e4319e0..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-/*****************************************************************************/
-/*                                                                           */
-/*                             tgi_load_driver.c                             */
-/*                                                                           */
-/*                       Loader module for TGI drivers                       */
-/*                                                                           */
-/*                                                                           */
-/*                                                                           */
-/* (C) 2002-2009, Ullrich von Bassewitz                                      */
-/*                Roemerstrasse 52                                           */
-/*                D-70794 Filderstadt                                        */
-/* EMail:         uz@cc65.org                                                */
-/*                                                                           */
-/*                                                                           */
-/* This software is provided 'as-is', without any expressed or implied       */
-/* warranty.  In no event will the authors be held liable for any damages    */
-/* arising from the use of this software.                                    */
-/*                                                                           */
-/* Permission is granted to anyone to use this software for any purpose,     */
-/* including commercial applications, and to alter it and redistribute it    */
-/* freely, subject to the following restrictions:                            */
-/*                                                                           */
-/* 1. The origin of this software must not be misrepresented; you must not   */
-/*    claim that you wrote the original software. If you use this software   */
-/*    in a product, an acknowledgment in the product documentation would be  */
-/*    appreciated but is not required.                                       */
-/* 2. Altered source versions must be plainly marked as such, and must not   */
-/*    be misrepresented as being the original software.                      */
-/* 3. This notice may not be removed or altered from any source              */
-/*    distribution.                                                          */
-/*                                                                           */
-/*****************************************************************************/
-
-
-
-#include <unistd.h>
-#include <fcntl.h>
-#include <modload.h>
-#include <tgi.h>
-#include <tgi/tgi-kernel.h>
-
-
-
-/*****************************************************************************/
-/*                                   Code                                    */
-/*****************************************************************************/
-
-
-
-void __fastcall__ tgi_load_driver (const char* name)
-/* Install the given driver. This function is identical to tgi_load with the
- * only difference that the name of the driver is specified explicitly. You
- * should NOT use this function in most cases, use tgi_load() instead.
- */
-{
-    static struct mod_ctrl ctrl = {
-        read            /* Read from disk */
-    };
-    unsigned Res;
-
-    /* Check if we do already have a driver loaded. If so, remove it. */
-    if (tgi_drv != 0) {
-        tgi_unload ();
-    }
-
-    /* Now open the file */
-    ctrl.callerdata = open (name, O_RDONLY);
-    if (ctrl.callerdata >= 0) {
-
-        /* Load the module */
-        Res = mod_load (&ctrl);
-
-        /* Close the input file */
-        close (ctrl.callerdata);
-
-        /* Check the return code */
-        if (Res == MLOAD_OK) {
-
-            /* Check the driver signature, install the driver */
-            tgi_install (ctrl.module);
-            return;
-
-        }
-    }
-
-    /* Error loading the driver */
-    tgi_error = TGI_ERR_CANNOT_LOAD;
-}