X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=uffi%2Fmcl%2Fobjects.cl;fp=uffi%2Fmcl%2Fobjects.cl;h=f959dc14400e481b3b20acccb1bc0dcb1e705d6d;hb=1ab81b8bacd3df5ac0558eb0deb6fee8decd6b27;hp=0000000000000000000000000000000000000000;hpb=4464cb9bed614c141d39ffaf72328cfc4d5a1452;p=uffi.git diff --git a/uffi/mcl/objects.cl b/uffi/mcl/objects.cl new file mode 100644 index 0000000..f959dc1 --- /dev/null +++ b/uffi/mcl/objects.cl @@ -0,0 +1,113 @@ +;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*- +;;;; ************************************************************************* +;;;; FILE IDENTIFICATION +;;;; +;;;; Name: objects.cl +;;;; Purpose: UFFI source to handle objects and pointers +;;;; Programmers: Kevin M. Rosenberg and John DeSoi +;;;; Date Started: Feb 2002 +;;;; +;;;; $Id: objects.cl,v 1.1 2002/08/02 14:39:12 kevin Exp $ +;;;; +;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg +;;;; and John DeSoi +;;;; +;;;; UFFI users are granted the rights to distribute and use this software +;;;; as governed by the terms of the Lisp Lesser GNU Public License +;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL. +;;;; ************************************************************************* + +(declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0))) +(in-package :uffi) + + +;;; +;;; Some MCL specific utilities +;;; +(defun foreign-object-size (type) + "Returns the size for the specified mcl type or record type" + (let ((mcl-type (ccl:find-mactype type nil t))) + (if mcl-type + (ccl::mactype-record-size mcl-type) + (ccl::record-descriptor-length (ccl:find-record-descriptor type t t)) ) ) ) ;error if not a record + + +; trap macros don't work right directly in the macros +(eval-when (:compile-toplevel :load-toplevel :execute) + +(defun new-ptr (size) + (#_NewPtr size)) + +(defun dispose-ptr (ptr) + (#_DisposePtr ptr)) + +) + +;;; +;;; Start of standard UFFI +;;; +(defmacro allocate-foreign-object (type &optional (size :unspecified)) + "Allocates an instance of TYPE. If size is specified, then allocate +an array of TYPE with size SIZE." + (if (eq size :unspecified) + `(new-ptr ,(foreign-object-size (convert-from-uffi-type type :allocation))) + `(new-ptr ,(* size (foreign-object-size (convert-from-uffi-type type :allocation)))))) + + + +(defmacro free-foreign-object (obj) + `(dispose-ptr ,obj)) + +(defmacro null-pointer-p (obj) + `(ccl:%null-ptr-p ,obj)) + + +(defmacro make-null-pointer (type) + (declare (ignore type)) + `(ccl:%null-ptr)) + + +;! need to check uffi update and see if :routine is the right context + +(defun accessor-symbol (type get-or-set) + "Returns the symbol used to access the foreign type." + (let* ((mcl-type (convert-from-uffi-type (eval type) :routine)) + (mac-type (ccl:find-mactype mcl-type)) + name) + (ecase get-or-set + (:get (setf name (ccl::mactype-get-function mac-type))) + (:set (setf name (ccl::mactype-set-function mac-type)))) + (find-symbol (symbol-name name) :ccl))) + +(defmacro deref-pointer (ptr type) + `(,(accessor-symbol type :get) ,ptr)) + + +(defmacro deref-pointer-set (ptr type value) + `(,(accessor-symbol type :set) ,ptr ,value)) + + +(defsetf deref-pointer deref-pointer-set) + + +(defmacro pointer-address (obj) + `(ccl:%ptr-to-int ,obj)) + + +(defmacro with-foreign-objects (bindings &rest body) + (let ((simple nil) (recs nil) type) + (dolist (spec (reverse bindings)) ;keep order - macroexpands to let* + (setf type (convert-from-uffi-type (eval (nth 1 spec)) :allocate)) + (if (ccl:mactype-p type) + (push (list (first spec) (foreign-object-size type)) simple) + (push spec recs))) + (cond ((and simple recs) + `(ccl:%stack-block ,simple + (ccl:rlet ,recs + ,@body))) + (simple `(ccl:%stack-block ,simple ,@body)) + (recs `(ccl:rlet ,recs ,@body))))) + + +(defmacro with-foreign-object ((var type) &rest body) + `(with-foreign-objects ((,var ,type)) ,@body))