Source

spica / microcode / malloc.scm

Full commit
  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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
;; -*- coding: utf-8; mode: scheme -*-
;;
;; malloc.scm - malloc/free
;;
;;   Copyright (c) 2011 KOGURO, Naoki (naoki@koguro.net)
;;   All rights reserved.
;;
;;   Redistribution and use in source and binary forms, with or without
;;   modification, are permitted provided that the following conditions
;;   are met:
;;
;;   1. Redistributions of source code must retain the above copyright
;;      notice, this list of conditions and the following disclaimer.
;;   2. Redistributions in binary form must reproduce the above copyright
;;      notice, this list of conditions and the following disclaimer in the
;;      documentation and/or other materials provided with the distribution.
;;   3. Neither the name of the authors nor the names of its contributors
;;      may be used to endorse or promote products derived from this
;;      software without specific prior written permission.
;;
;;   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;;   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;;   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;;   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;;   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;;   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
;;   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
;;   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
;;   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;

(use us-test)

;; macros for debug
(defmacro show-free-small-blocks ()
  '(check-point
    (print "*** small free blocks ***")
    (let ((blocks (let loop ((result '())
                             (free-small-block
                              (ram-read-word (label :free-small-block))))
                    (if (= free-small-block 0)
                        (sort result)
                        (loop (cons free-small-block result)
                              (sdram-read-word (+ free-small-block 1)))))))
      (format #t " ~d free blocks" (length blocks))
      (fold (lambda (x i)
              (when (= (modulo i 6) 0)
                (display "\n   "))
              (format #t "#x~8,'0x, " x)
              (+ i 1))
            0
            blocks)
      (newline))))

(defmacro show-free-blocks ()
  '(check-point
    (let ((start-free-block (ram-read-word (label :start-free-block))))
      (print "*** free blocks ***")
      (let loop ((addr start-free-block))
        (let ((size (sdram-read-word addr))
              (next-addr (sdram-read-word (+ addr 1))))
          (format #t "#x~8,'0x:  size = ~d\n" addr size)
          (format #t "             next = #x~8,'0x\n" next-addr)
          (unless (= next-addr start-free-block)
            (loop next-addr)))))))

(defmacro mov-imm (reg v)
  (let ((n (cond
             ((fixnum? v)
              (logand v #x3fffffff))
             ((char? v)
              (logior (char->ucs v) #x40000000))
             ((equal? v #f) #x50000000)
             ((equal? v #t) #x58000000)
             ((equal? v '()) #x54000000)
             ((equal? v 'eof) #x5c000000)
             ((equal? v 'undefined) #x52000000)
             ((equal? v 'unbound) #x5a000000)
             (else
              (errorf "can't convert ~s to immediate value." v)))))
    `(mov ,reg ,n)))

(defmacro header->size (rt header)
  `(begin
     (sll ,rt ,header 8)
     (srl ,rt ,rt 8)))

(defmacro header->mark (rt header)
  `(srl ,rt ,header 30))

;; header must be unmarked.
(defmacro header-set-mark (rt header)
  `(adh ,rt ,header #xff40))

;; header must be marked.
(defmacro header-set-unmark (rt header)
  `(adh ,rt ,header #xffc0))

(defmacro header-clear-mark&counter (rt header)
  `(begin
     (sll ,rt ,header 8)
     (srl ,rt ,rt 8)))

(defmacro retain (data)
  `(begin
     (when (obj? ,data)
       (let-temp (%header)
         (mlw %header ,data 0
              ()
              (adh %header %header #x3f01))
         (check-point
          (reportf 1 "  retain <#x~8,'0x ~a>: #x~8,'0x\n" (reg ',data) (bin->str (reg ',data)) (reg '%header)))
         (msw %header ,data 0)))))

(defmacro release (obj)
  `(:release ,obj))

(lib-code
 (defun :construct (type size) (allocator header)
   (if (<= size 4)
       (mov allocator :malloc4)
       (mov allocator :malloc))
   (call allocator size)

   (when (= $v0 0)
     (unless (= size 4)
       (:light-gc size)
       (branch-if (<= size $v0) :retry)
       (nop))
     (:full-gc)
     :retry
     (call allocator size)
     (when (= $v0 0)
       ;; TODO: exception?
       (mov-imm $v0 #f)
       (ret)))

   (check-point
    (reportf 1 "  allocate <#x~8,'0x>\n" (reg '$v0)))
   (mlw header $v0 0
        ()
        (adh header header #x3f01))
   (check-point
    (reportf 1 "  retain(in allocate) <#x~8,'0x ~a>: #x~8,'0x\n" (reg '$v0) (bin->str (reg '$v0)) (reg 'header)))
   (msw header $v0 0
        (begin
          (or type type #xff80)
          (adh $v0 $v0 type))))

 (defun :destruct (obj) (size i)
   (check-point
    (reportf 1 "  destruct <#x~8,'0x ~a>\n" (reg 'obj) (bin->str (reg 'obj))))
   (if (uvector? obj)
       (:clear-obj obj)
       (begin
         (mlw size obj 0
              (mov i 1))
         (while (!= i size)
           (mlw $v1 obj i)
           (when (obj? $v1)
             (mlw $v0 $v1 0
                  ()
                  (adh $v0 $v0 #x3fff))
             (msw $v0 $v1 0
                  (srl $v0 $v0 24))
             (msw $zero obj i)
             (when (= $v0 0)
               (:destruct $v1)))
           (inc i))))
   (:free obj 0))

 (defun :clear-obj (obj) (i size)
   (mlw size obj 0
        (mov i 1))
   (while (!= i size)
     (msw $zero obj i
          (add i i 1))))

 (defun :release (obj) ()
   (when (obj? obj)
     (mlw $v0 obj 0
          ()
          (adh $v0 $v0 #x3fff))
     (check-point
      (reportf 1 "  release <#x~8,'0x: ~a>: #x~8,'0x\n" (reg 'obj) (bin->str (reg 'obj)) (reg '$v0)))
     (msw $v0 obj 0
          (srl $v0 $v0 24))
     (when (= $v0 0)
       (tail-call* :destruct obj))))

 (defun :light-gc (size) (obj next)
   (lw obj $zero :free-small-block)
   (nop)
   (while (!= obj 0)
     (mlw next obj 1
          ()
          (sw next $zero :free-small-block))
     (:free obj 1)
     (ret-if (<= size $v0))
     (mov obj next)))

 (defun :mark (obj) (size i)
   (let-temp (%header %mark)
     (mlw %header obj 0
          (mov i 1)
          (header->mark %mark %header))
     (ret-unless (= %mark 0))
     (header-set-mark %header %header)
     (msw %header obj 0)
     (header->size size %header))
   (unless (uvector? obj)
     (while (!= i size)
       (let-temp (child)
         (mlw child obj i
              (add i i 1))
         (when (obj? child)
           (:mark child))))))

 (defun :full-gc () (obj i end size root root-size)
   (let-temp (%hook)
     (lw %hook $zero :prepare-full-gc)
     (lw end $zero :end-heap)
     (call %hook)
     (mov root $v0)
     (mov root-size $v1))
     
   (lw end $zero :end-heap)
   ;; traverse all objects from roots and mark.
   (mov i 0)
   (while (< i root-size)
     (lw obj root i)
     (add i i 1)
     (when (obj? obj)
       (:mark obj)))

   ;; sweep unused objects.
   (mlw obj $zero 0)  ; skip first free block
   (while (!= obj end)
     (let-temp (%header %mark)
       (mlw %header obj 0
            ()
            (begin
              (header->size size %header)
              (header->mark %mark %header)))
       (if (= %mark 0)
           (let-temp (%counter)
             (srl %counter %header 24)
             ;; If %counter isn't 0, the object was alive.
             (unless (= %counter 0)
               ;; clear counter
               (header-clear-mark&counter %header %header)
               (msw %header obj 0)
               (:clear-obj obj)
               ;; %header and %mark will be destroyed.
               (:free obj 1)))
           (begin
             ;; clear mark bit, because it is expired.
             (header-set-unmark %header %header)
             (msw %header obj 0))))
     (add obj obj size)))


  ;;
  ;; top block      000000h: 00 00 00 04
  ;;                000001h: 00 00 00 04
  ;;                000002h: 00 00 00 00
  ;;                000003h: 00 00 00 00
  ;; 1st free block 000004h: 00 3f ff fc
  ;;                000005h: 00 00 00 00
  ;; NOTE: top block is not allocated.
  (defun :minit () ()
    (let-temp (%data)
      (mov %data #x00000002)
      (msw %data $zero #x0)
      (msw %data $zero #x1)

      (mov %data #x3ffffe)
      (msw %data $zero #x2)
      (msw $zero $zero #x3)

      (sw $zero $zero :start-free-block)
      (sw $zero $zero :free-small-block)
      ))

  ;; return 4 word allocated memory. If no free space, return 0.
  :malloc4
  (let-temp (%free-block)
    (lw $v0 $zero :free-small-block)
    (nop)
    (if (= $v0 0)
        (begin
          (j :malloc)
          (add $a0 $zero 3))
        (mlw %free-block $v0 1
             ()
             (sw %free-block $zero :free-small-block))
        (msw $zero $v0 1)))
  (j $ra)
  (nop)

  ;; size: allocate memory size (word)
  ;; return an address. If no free space, return 0.
  :malloc
  (with-reg-alias ((size $a0))
    (let-temp (%free-block %free-size %prev-block %start-free-block)
      (lw %start-free-block $zero :start-free-block)
      (nop)
      ;; We have to round size up by a multiple of 2.
      (mlw %free-block %start-free-block 1
           (begin
             (sub size size 1)
             (or size size #b1)
             (add size size 1)
             (mov %prev-block %start-free-block)))

      (repeat (!= %prev-block %start-free-block)
        (mlw %free-size %free-block $zero) ;; header is equal to free block size.
        (cond
          ((= %free-block 0)
           ;; next free block
           (mlw %free-block %free-block 1
                (mov %prev-block %free-block)))
          ((< size %free-size)
           ;; allocate memory (allocate tail) and truncate free block
           (add $v0 %free-block %free-size)
           (sub $v0 $v0 size)
           (msw size $v0 0
                (sub %free-size %free-size size))
           (msw $zero $v0 1)

           (msw %free-size %free-block 0)
           (j $ra)
           (sw %prev-block $zero :start-free-block))
          ((= size %free-size)
           ;; Rewrite previous free block's next block address.
           (mlw %free-block %free-block 1
                ;; allocate memory
                (mov $v0 %free-block))
           (msw $zero $v0 1)
           (msw %free-block %prev-block 1)

           (j $ra)
           (sw %prev-block $zero :start-free-block))
          (else
           ;; next free block
           (mlw %free-block %free-block 1
                (mov %prev-block %free-block)))))
      (j $ra)
      (mov $v0 0)))

  ;; addr: allocated memory.
  ;; force-free-block: 0 or 1. If flag is 1, this block is returned to :free-block
  ;; inspite of its size.
  ;; return: free block's size
  (defun :free (addr force-free-block) (%next-free %prev-free %t0 %t1)
    (with-reg-alias ((size $v0))
      ;; Get size of addr block.
      (mlw size addr 0
           (begin
             ;; clear type
             (sll addr addr 8)
             (srl addr addr 8))
           (add size size force-free-block))

      ;; If the block is small (4 words), it is returned to :free-small-block.
      (when (= size 4)
        (lw %next-free $zero :free-small-block)
        (sw addr $zero :free-small-block)
        (msw %next-free addr 1)
        (ret))

      ;; Add free blocks.
      (lw %prev-free $zero :start-free-block)
      (sub size size force-free-block)
      (mlw %next-free %prev-free 1)

      (while (and (or (< addr %prev-free)
                      (< %next-free addr))
                  (or (!= %next-free 0)
                      (< addr %prev-free))
                  (or (!= %prev-free 0)
                      (< %next-free addr)))
        (mlw %next-free %next-free 1
             (mov %prev-free %next-free)))

      ;; join if addr is the next block of %prev-free
      (mlw %t0 %prev-free 0
           ()
           (add %t1 %prev-free %t0))
      (if (and (= %t1 addr)
               (!= %prev-free 0))
          (begin
            (add size %t0 size)
            (msw size %prev-free 0
                 (mov addr %prev-free)))
          (begin
            (mlw %t1 %prev-free 1)
            (msw addr %prev-free 1)
            (msw %t1 addr 1)))
      (sw addr $zero :start-free-block)

      ;; join if %next-free is the next block of addr
      (add %t1 addr size)
      (when (and (= %t1 %next-free)
                 (!= %next-free 0))
        (mlw %t0 %next-free 0)
        (mlw %t1 %next-free 1
             (add size %t0 size))
        (msw size addr 0)
        (msw %t1 addr 1))))

  :start-free-block
  (@data 0)

  :free-small-block
  (@data 0)

  :end-heap
  (@data #x400000)

  :prepare-full-gc
  (@data #x0)

  (import "malloc-test.scm")
  )
;; end of file