File: foreign-resources.lisp

package info (click to toggle)
cl-sql 3.1.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,820 kB
  • ctags: 2,954
  • sloc: lisp: 19,242; xml: 17,449; makefile: 346; ansic: 190; sh: 135; cpp: 9
file content (59 lines) | stat: -rw-r--r-- 1,832 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
;;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
;;;; $Id: foreign-resources.lisp 9517 2004-05-29 15:31:36Z kevin $
;;;;
;;;; 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)