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