change permissions
[clsql/s11.git] / db-db2 / foreign-resources.lisp
blobea17376d68cefbcc48df7cb3686286d46f429652
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; $Id$
6 ;;;;
7 ;;;; This file is part of CLSQL.
8 ;;;;
9 ;;;; CLSQL users are granted the rights to distribute and use this software
10 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
11 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
12 ;;;; *************************************************************************
14 (in-package #:clsql-db2)
16 (defparameter *foreign-resource-hash* (make-hash-table :test #'equal))
18 (defstruct (foreign-resource)
19 (type (error "Missing TYPE.")
20 :read-only t)
21 (sizeof (error "Missing SIZEOF.")
22 :read-only t)
23 (buffer (error "Missing BUFFER.")
24 :read-only t)
25 (in-use nil :type boolean))
28 (defun %get-resource (type sizeof)
29 (let ((resources (gethash type *foreign-resource-hash*)))
30 (car (member-if
31 #'(lambda (res)
32 (and (= (foreign-resource-sizeof res) sizeof)
33 (not (foreign-resource-in-use res))))
34 resources))))
36 (defun %insert-foreign-resource (type res)
37 (let ((resource (gethash type *foreign-resource-hash*)))
38 (setf (gethash type *foreign-resource-hash*)
39 (cons res resource))))
41 (defmacro acquire-foreign-resource (type &optional size)
42 `(let ((res (%get-resource ,type ,size)))
43 (unless res
44 (setf res (make-foreign-resource
45 :type ,type :sizeof ,size
46 :buffer (uffi:allocate-foreign-object ,type ,size)))
47 (%insert-foreign-resource ',type res))
48 (claim-foreign-resource res)))
50 (defun free-foreign-resource (ares)
51 (setf (foreign-resource-in-use ares) nil)
52 ares)
54 (defun claim-foreign-resource (ares)
55 (setf (foreign-resource-in-use ares) t)
56 ares)