summaryrefslogtreecommitdiff
path: root/db-db2/foreign-resources.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'db-db2/foreign-resources.lisp')
-rw-r--r--db-db2/foreign-resources.lisp57
1 files changed, 57 insertions, 0 deletions
diff --git a/db-db2/foreign-resources.lisp b/db-db2/foreign-resources.lisp
new file mode 100644
index 0000000..c141bcd
--- /dev/null
+++ b/db-db2/foreign-resources.lisp
@@ -0,0 +1,57 @@
+;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
+;;;; *************************************************************************
+;;;; FILE IDENTIFICATION
+;;;;
+;;;; 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-db2)
+
+(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 resource))))
+
+(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)
+
+
+