My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
;;;;-*- Mode: Lisp; Package: LUI -*-
;*********************************************************************
;* *
;* M E M O R Y *
;* *
;*********************************************************************
;* Author : Alexander Repenning (alexander@agentsheets.com) *
;* http://www.agentsheets.com *
;* Copyright : (c) 1996-2009, AgentSheets Inc. *
;* Filename : memory.lisp *
;* Updated : 04/28/09 *
;* Version : *
;* 1.0 : based on AGL.lisp v 1.2.6 *
;* 1.1 : 01/10/09 using package instead of hash-table *
;* SW/HW : CCL 1.2 OS X, Mac PPC, Intel *
;* Abstract : Tools to create and access memory *
;* *
;******************************************************************

(in-package :lui)

(export '(sizeof make-vector copy-vector with-vector with-vector-of-size dispose-vector
make-byte-vector get-byte set-byte make-vector-of-size get-long SET-BYTE-VECTOR))

;;______________________________________________________________
;; Universally Unique Identifier (UUID) |
;; http://en.wikipedia.org/wiki/Universally_Unique_Identifier |
;;_____________________________________________________________/

(defun UNIVERSALLY-UNIQUE-IDENTIFIER () "
Return an Universally Unique Identifier (UUID). An UUID is an identifier standard
used in software construction, standardized by the Open Software Foundation (OSF) as
part of the Distributed Computing Environment (DCE)"
#-windows-target (ccl::lisp-string-from-nsstring (#_CFUUIDCreateString (%null-ptr) (#_CFUUIDCreate (%null-ptr))))
#+windows-target (rlet ((uuid :<UUID>))
(#_UuidCreate uuid)
(format nil "~8,'0X-~4,'0X-~4,'0X-~4,'0X-~12,'0X"
(pref uuid :<UUID>.<D>ata1)
(pref uuid :<UUID>.<D>ata2)
(pref uuid :<UUID>.<D>ata3)
(dpb (%get-unsigned-byte (pref uuid :<UUID>.<D>ata4) 0)
(byte 8 8)
(%get-unsigned-byte (pref uuid :<UUID>.<D>ata4) 1))
(dpb (dpb (%get-unsigned-word (pref uuid :<UUID>.<D>ata4) 1)
(byte 16 16)
(%get-unsigned-word (pref uuid :<UUID>.<D>ata4) 2))
(byte 32 32)
(%get-unsigned-word (pref uuid :<UUID>.<D>ata4) 3)))))

;;_____________________
;; Sizeof |
;;____________________/

(defgeneric SIZEOF (Object)
(:documentation "the size of object in memory. Object can be instance or type name"))


(defmethod SIZEOF ((Self float)) 4)
(defmethod SIZEOF ((Self double-float)) 8)
(defmethod SIZEOF ((Self fixnum)) 4)

(defmethod SIZEOF ((Type (eql 'long))) 4)
(defmethod SIZEOF ((Type (eql 'float))) 4)
(defmethod SIZEOF ((Type (eql 'double-float))) 8)
(defmethod SIZEOF ((Type (eql 'fixnum))) 4)

(defmethod SIZEOF ((Self macptr))
#-windows-target (#_GetPtrSize Self)
#+windows-target (#_HeapSize (#_GetProcessHeap) 0 Self))

;;_____________________
;; Memory Vectors |
;;____________________/

(defun MAKE-VECTOR (&rest Values) "
in: &rest Values
out: Vector.
Create a vector initialized with <Values>.
Vector is not automatically deallocated."
(let* ((Index 0)
(Size (reduce #'+ Values :key #'sizeof))
(&Vector #-windows-target (#_NewPtr Size)
#+windows-target (#_HeapAlloc (#_GetProcessHeap) 0 Size)))
(when (%null-ptr-p &Vector)
(error "Memory allocation of ~A bytes failed" Size))
(dolist (Value Values &Vector)
(etypecase Value
(fixnum (setf (%get-long &Vector Index) Value))
(single-float (setf (%get-single-float &Vector Index) Value))
(double-float (setf (%get-double-float &Vector Index) Value)))
(incf Index (sizeof Value)))))


(defun COPY-VECTOR (Vector &optional Size) "
in: Vector, &optional Size.
out: Vector-Copy.
Create a copy of <Vector>."
(unless Size (setq Size (sizeof Vector)))
(let ((Vector-Copy (make-vector-of-size Size)))
;; this is a reall slow way to copy a vector byte by byte
;; consider using #_memmove but worry about Mac/PC
(dotimes (i Size Vector-Copy)
(setf (%get-byte Vector-Copy i) (%get-byte Vector i)))))


(defun MAKE-VECTOR-OF-SIZE (Size) "
in: Size int.
out: Vector.
Make vector of byte size"
(let ((&Vector
#-windows-target (#_NewPtr Size)
#+windows-target (#_HeapAlloc (#_GetProcessHeap) 0 Size)))
(when (%null-ptr-p &Vector)
(error "Memory allocation of ~A bytes failed" Size))
&Vector))


(defun MAKE-BYTE-VECTOR (&rest Values)"
in: &rest Values
out: Vector.
Create a vector initialized with <Values>.
Vector is not automatically deallocated."
(let* ((Index 0)
(&Vector #-windows-target (#_NewPtr (length Values))
#+windows-target (#_HeapAlloc (#_GetProcessHeap) 0 (length Values))))
(when (%null-ptr-p &Vector)
(error "Memory allocation of ~A bytes failed" (length Values)))
(dolist (Value Values &Vector)
(setf (%get-byte &Vector Index) Value)
(incf Index))))


(defun SET-BYTE-VECTOR (Vector &rest Values) "
in: Vector *byte, &rest Values list of byte.
out: Vector *byte.
Set the bytes of vector to new values."
(declare (dynamic-extent Values))
(when (> (length Values) (sizeof Vector))
(error "out of range"))
(let ((Offset 0))
(declare (fixnum Offset)
(optimize (speed 3) (safety 0)))
(dolist (Value Values Vector)
(setf (%get-byte Vector Offset) Value)
(incf Offset))))


(defun DISPOSE-VECTOR (Vector) "
in: Vector.
Dispose of vector."
#-windows-target (#_DisposePtr Vector)
#+windows-target (#_HeapFree (#_GetProcessHeap) 0 Vector))


(eval-when (:compile-toplevel :load-toplevel :execute)
(defmacro WITH-VECTOR ((Vector &rest Values) &body Forms)
`(let ((,Vector (make-vector ,@Values)))
(unwind-protect
(progn
,@Forms)
(dispose-vector ,Vector)))))


(eval-when (:compile-toplevel :load-toplevel :execute)
(defmacro WITH-VECTOR-OF-SIZE ((Vector Size) &body Forms)
`(let ((,Vector (make-vector-of-size ,Size)))
(unwind-protect
(progn
,@Forms)
(dispose-vector ,Vector)))))

;;_____________________
;; Vectors Access |
;;____________________/

(proclaim '(inline get-single-float get-byte get-long))


(defun GET-BYTE (Vector &optional (Offset 0)) "
in: Vector *bye, &optional Offset int default 0.
out: Byte byte.
Return byte at offset."
(%get-byte Vector Offset))


(defun SET-BYTE (Vector Value &optional (Offset 0)) "
in: Vector *byte, Value byte; &optional Offset int default 0.
Set vector at byte offset to value."
(setf (%get-byte Vector Offset) Value))


(defun GET-LONG (Vector &optional (Offset 0)) "
in: Vector *bye, &optional Offset int default 0.
out: Float
Return Float at byte offset."
(%get-long Vector Offset))


(defun GET-SINGLE-FLOAT (Vector &optional (Offset 0)) "
in: Vector *bye, &optional Offset int default 0.
out: Float
Return Float at byte offset."
(%get-single-float Vector Offset))


(defun GET-DOUBLE-FLOAT (Vector &optional (Offset 0)) "
in: Vector *bye, &optional Offset int default 0.
out: Float
Return Float at byte offset."
(%get-double-float Vector Offset))

;;_________________________________
;; Vector Constants with Reader |
;; e.g. {0.5 0.6 0.7} |
;;________________________________/


(defpackage STATIC-VECTORS)


(proclaim '(inline GET-CACHED-VECTOR))

(defun GET-CACHED-VECTOR (Key &rest Values) "
in: Key symbol; &rest Values.
out: Vector
Returns vector. Use key to cache and implement constant pointers"
(or
(and (boundp Key) (symbol-value Key))
(set Key (apply #'make-vector Values))))


(defun READ-NUMBER (Stream)
(read-from-string
(with-output-to-string (Out)
(loop
(let ((Char (read-char Stream nil nil)))
(case Char
((#\} #\Space)
(unread-char Char Stream)
(return))
(t (write-char Char Out))))))))


(defun VECTOR-READER (Stream Char)
(declare (ignore Char))
(let ((Numbers nil))
(loop
(let ((Char (read-char Stream nil #\])))
(case Char
(#\} (return `(get-cached-vector
',(intern (universally-unique-identifier) :STATIC-VECTORS)
,@(reverse Numbers))))
((#\Space #\Tab #\Newline))
(t (unread-char Char Stream)
(push (read-number Stream) Numbers)))))))


(set-macro-character #\{ #'vector-reader)


#| Examples:

(sizeof pi)
(sizeof 3.14)


(make-vector 3.1415 7.5 6.7)

(with-vector (V 1.0 2.0 3.0)
(dotimes (i 3)
(print (get-single-float v (* i #.(sizeof 0.0))))))


;; eval a couple of times: notice, pointer address is different every time
{3.14 5.0 3.0}

;; but

(defun TEST ()
{3.14 5.0 3.0})

;; this function will return the SAME vector, i.e., pointer at same address
;; with same content every time, until you eval the function definition again
;; consider this to be a constant: do not dispose
(test)


(get-single-float (test))
(get-single-float (test) #.(* 1 (sizeof 0.0)))
(get-single-float (test) #.(* 2 (sizeof 0.0)))

(defun TEST2 ()
(make-vector 3.14 5.0 3.0))

(test2)

(sizeof {0.5d0 0.6d0 0.7d0})
(sizeof {0.5 0.6 0.7})
(sizeof {1 2 3})

;; compare speed

(defun VALUE-1 ()
(with-vector (v 1.0 2.0 3.0)
(get-single-float v)))

(value-1)

(defun VALUE-2 ()
(get-single-float {1.0 2.0 3.0}))

(value-2)


(sizeof (make-byte-vector 3 4 5))

(with-vector-of-size (Selection 2048)
(sizeof Selection))


(defparameter *v1* (make-vector-of-size 1000000))

(sizeof *v1*)

(sizeof (copy-vector *v1*)) ;; about 10ms on Mac Book Pro 2.6Ghz


(make-vector-of-size 10000000000000) ;; should raise errorc




|#

Change log

r1584 by alex.repenning on Dec 11, 2011   Diff
add checks to ALL vector memory allocation
function to make sure the vector could be
allocated.
Go to: 
Project members, sign in to write a code review

Older revisions

r1251 by alex.repenning on Aug 17, 2011   Diff
new vector function: COPY-VECTOR
r726 by alex.repenning on Nov 22, 2010   Diff
add long as 4 byte type to sizeof
r299 by alex.repenning on Mar 17, 2010   Diff
memory: SET-BYTE-VECTOR
All revisions of this file

File info

Size: 10145 bytes, 333 lines
Powered by Google Project Hosting