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

Shared Library and MCL.



Hi, folks. 

I'm still trying to make the Apple Shared Library Manager to work with
MCL 2.0.1.  Here's some idea if MCL is not going to be supported on
PowerMacs.  We can at least to use the Shared Libraries in native mode
for some time critical tasks and stay in MCL for its development
environment and ease of prototyping user interfaces.  (Maybe some one
can just tell me I'm out of my mind at this moment.)

Anyway, here is the problem.  I try to use the foreign function
interface to hook the ASLM to MCL.  (See code at the end.) I've gotten
some suggestions to use libraries and object files that are compiled
using the near pointer model.  So I did that.  Now the problem is that
after I called the "InitLibraryManager" (returned 0, no error) and
"LoadFunctionSet" (loaded the function set define in "Sample.exp", and
it returned 0, too), but when I called the "NewTrafficLight", it
crashes.  Bus error! (See detailed information in the code comments.) I
tried to dig out something from the documentation, but it says draft on
it and I get confused a few times what I should do about it. 

Test environment: MCL 2.0.1, ASLM 1.1, MPW 3.3.1, System 7.1, Mac IIfx.

Could anyone please give me some hints? Thanks in advance.

Hai Wang
RSTAR, Inc.
Cambridge.

Code follows:

;;;=====================================================================

;;;
;;; Hacking of the ASLM (Shared Library Manager)
;;;

;;;
;;; Error codes copied from the "LibraryManager.h"
;;;
(defconstant kNoError 0)
(defconstant kNotFound -3120)
(defconstant kNoParent -3121)
(defconstant kParentNotFound -3122)
(defconstant kNotRelated -3123)
(defconstant kInvalidObject -3124)
(defconstant kPoolCorrupted -3125)
(defconstant kOutOfMemory -3126)
(defconstant kCodeNotLoaded -3127)
(defconstant kCouldNotLoadCode -3128)
(defconstant kFilePreflighted -3129)
(defconstant kFileNotPreflighted -3130)
(defconstant kFileNotFound -3131)
(defconstant kLibraryManagerNotLoaded -3132)
(defconstant kDuplicateFound -3134)
(defconstant kSeedChanged -3135)
(defconstant kUnconstructedObject -3136)
(defconstant kInternalError -3137)
(defconstant kVersionError -3138)
(defconstant kFolderNotFound -3139)
(defconstant kFolderInUse -3140)
(defconstant kResourceNotFound -3141)
(defconstant kNotAllowedNow -3155)
(defconstant kNotSupported -3167)


(defconstant true 1)
(defconstant false 0)

(defconstant kCurrentZone 4)  ; See file "LibraryManager.h"
(defconstant kNormalMemory 1) ; See file "LibraryManager.h"

;;;
;;;==============================================================
;;;

(defparameter *c-libraries*
  '("KSQ402:MPW Folder:Libraries:Libraries:Interface.o"
    "KSQ402:MPW Folder:Libraries:CLibraries:StdCLib.o"
    ))

(defparameter *aslm-libraries* 
  '(
    "KSQ402:MPW Folder:Libraries:Libraries:LibraryManager.n.o"
    ))

;;;
;;; Load near code, don't load far.
;;;
(ff-load 
 '("KSQ402:MPW Folder:Libraries:Libraries:LibraryManager.n.o")
 :ffenv-name 'aslm
 :entry-names '("InitLibraryManager" 
                "LoadFunctionSet"
                "UnloadFunctionSet"
                "CleanupLibraryManager"))

(deffcfun (%ASLM-InitLibraryManager "InitLibraryManager")
  ((fixnum :long) (fixnum :long) (fixnum :long)) :word)

(deffcfun (%ASLM-CleanupLibraryManager "CleanupLibraryManager")
  () :novalue)

(deffcfun (%ASLM-LoadFunctionSet "LoadFunctionSet")
  ((string :cstring) (fixnum :long)) :word)

(deffcfun (%ASLM-UnloadFunctionSet "UnloadFunctionSet")
  ((string :cstring)) :word)

#|
;;;
;;;===============================================================
;;;

;;;
;;; This is the library id. See SampleLibrary.h
;;;
(defconstant kTrafficLightFunctionSet 
  "slm:samp$CTrafficLightFunctionSet,1.1")

;;;
;;; Load near code, don't load far.
;;;
(ff-load 
 '("KSQ402:Assorted:aslm:CSample:Objects:SampleLibrary6.cln.o")
 :ffenv-name 'sample-lib
 :libraries *aslm-libraries*
 :entry-names '("NewTrafficLight"
                "FreeTrafficLight"
                "GetLightState"
                "SetLightState"
                "DrawLight"
                "AdjustTrafficLightMenus"
                "DoTrafficLightMenuCommand"))

(deffcfun (%NewTrafficLight "NewTrafficLight")
  () :word)

(deffcfun (%DrawLight "DrawLight")
  () :novalue)

(deffcfun (%GetLightState "GetLightState")
  () :char)

(deffcfun (%FreeTrafficLight "FreeTrafficLight")
  () :novalue)

;;;
;;; The real test.
;;; %ASLM-InitLibraryManager returned 0, which means kNoErr. Good.
(%ASLM-InitLibraryManager 0 kCurrentZone kNormalMemory)

;;; %ASLM-LoadFunctionSet returned 0, not bad.
(%ASLM-LoadFunctionSet kTrafficLightFunctionSet true)

;;; Crash!!!
;;; I trace through the entry point to the point where it crashed 
;;; in Macsbug. The reason why it crashed is it tried to access 
;;; invalid memory. From the entry point stub_NewTrafficLight to 
;;; the first "jmp" to "movea.l ExpandMem,a1", it looks all right. 
;;; But a few instructions after the "movea.l ExpandMem,a1", there was
;;; an instruction that access -$98(a5),d0. Here I suspect that 
;;; something wrong with the global world. Am I doing something wrong 
;;; or missing some other calls to setup the global A5 right?
(%NewTrafficLight)

;;; Could not continue any more.
(%DrawLight)
(%GetLightState)
(%ASLM-UnloadFunctionSet kTrafficLightFunctionSet)
(%ASLM-CleanupLibraryManager)
|#