Browse Source

Expose allocators

master
Macoy Madson 8 months ago
parent
commit
e14525d64a
  1. 23
      src/Allocator.cake

23
src/Allocator.cake

@ -4,20 +4,19 @@
(import "CHelpers.cake")
(c-import "<stdlib.h>"
"<assert.h>")
"<assert.h>"
&with-decls "<stddef.h>")
(comptime-cond
('auto-test
(c-import "<stdio.h>")))
(forward-declare (struct block-allocation))
(defstruct-local block-allocation
(defstruct block-allocation
size size_t
capacity size_t
data (* void))
(defun-local block-allocation-create (size size_t &return (* block-allocation))
(defun block-allocation-create (size size_t &return (* block-allocation))
;; Create the header alongside the block so it can all go away in one free
(var-cast-to new-block (* block-allocation) (malloc (+ size (sizeof (type block-allocation)))))
(unless new-block
@ -28,14 +27,14 @@
(set (path new-block > data) (type-cast (+ new-block 1) (* void)))
(return new-block))
(defun-local block-allocation-free (block (* block-allocation))
(defun block-allocation-free (block (* block-allocation))
(free block))
;;
;; Simple linear allocator based on a single block
;;
(defun-local linear-allocate (block (* block-allocation) size size_t &return (* void))
(defun linear-allocate (block (* block-allocation) size size_t &return (* void))
(unless (and block (path block > data))
(allocate-failure "block allocation was not initialized")
(return null))
@ -48,7 +47,7 @@
(set (path block > size) (+ (path block > size) size))
(return start))
(defun-local linear-allocate-clear (block (* block-allocation))
(defun linear-allocate-clear (block (* block-allocation))
(when block
(set (path block > size) 0)))
@ -84,12 +83,12 @@
;; ;; All done, "pop" from our stack and reset the capacity
;; (chain-allocation-free my-stack-allocator-root)
;; (set (deref last-chain-link) stack-before-my-operation)
(defstruct-local chain-allocation
(defstruct chain-allocation
next-link (* chain-allocation)
new-block-size size_t
block block-allocation)
(defun-local chain-allocation-create (new-block-size size_t &return (* chain-allocation))
(defun chain-allocation-create (new-block-size size_t &return (* chain-allocation))
;; Create the header alongside the block so it can all go away in one free
(var-cast-to new-chain (* chain-allocation)
(malloc (+ new-block-size (sizeof (type chain-allocation)))))
@ -115,12 +114,12 @@
(return true))
;; Must pass in the first block, not the final chain pointer!
(defun-local chain-allocation-free (root (* chain-allocation))
(defun chain-allocation-free (root (* chain-allocation))
(each-link-in-chain-allocator root current-link next-link
(free current-link)))
;; You must pass in the address of chain because the chain may be extended
(defun-local chain-allocate (last-link-in-chain-in-out (* (* chain-allocation)) size size_t
(defun chain-allocate (last-link-in-chain-in-out (* (* chain-allocation)) size size_t
&return (* void))
(var requested-size size_t size)
(var current-link (* chain-allocation) (deref last-link-in-chain-in-out))

Loading…
Cancel
Save