blob: c141bcd891e29e9ae64c8b470ec35f336a7ee687 (
plain)
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
|
;;;; -*- 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)
|