X-Git-Url: http://git.kpe.io/?p=clsql.git;a=blobdiff_plain;f=db-oracle%2Fforeign-resources.lisp;fp=db-oracle%2Fforeign-resources.lisp;h=5f66948c8805054c9771968cac12a2f2e8079cd1;hp=0000000000000000000000000000000000000000;hb=39bc32836bdf5bdab576ba1e4ef3762f46000b98;hpb=5dea5b54ed275947c1d78426483acb1a7fb92f21 diff --git a/db-oracle/foreign-resources.lisp b/db-oracle/foreign-resources.lisp new file mode 100644 index 0000000..5f66948 --- /dev/null +++ b/db-oracle/foreign-resources.lisp @@ -0,0 +1,61 @@ +;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*- +;;;; ************************************************************************* +;;;; FILE IDENTIFICATION +;;;; +;;;; Name: alien-resources.lisp +;;;; +;;;; $Id$ +;;;; +;;;; This file is part of CLSQL. +;;;; +;;;; CLSQL 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. +;;;; ************************************************************************* + +(in-package #:clsql-oracle) + +(defparameter *foreign-resource-hash* (make-hash-table :test #'equal)) + +(defstruct (foreign-resource) + (type (error "Missing TYPE.") + :read-only t) + (sizeof (error "Missing SIZEOF.") + :read-only t) + (buffer (error "Missing BUFFER.") + :read-only t) + (in-use nil :type boolean)) + + +(defun %get-resource (type sizeof) + (let ((resources (gethash type *foreign-resource-hash*))) + (car (member-if + #'(lambda (res) + (and (= (foreign-resource-sizeof res) sizeof) + (not (foreign-resource-in-use res)))) + resources)))) + +(defun %insert-foreign-resource (type res) + (let ((resource (gethash type *foreign-resource-hash*))) + (setf (gethash type *foreign-resource-hash*) + (cons res (gethash type *foreign-resource-hash*))))) + +(defmacro acquire-foreign-resource (type &optional size) + `(let ((res (%get-resource ',type ,size))) + (unless res + (setf res (make-foreign-resource + :type ',type :sizeof ,size + :buffer (uffi:allocate-foreign-object ,type ,size))) + (%insert-foreign-resource ',type res)) + (claim-foreign-resource res))) + +(defun free-foreign-resource (ares) + (setf (foreign-resource-in-use ares) nil) + ares) + +(defun claim-foreign-resource (ares) + (setf (foreign-resource-in-use ares) t) + ares) + + +