Skip to content

Commit

Permalink
fix my dumb mistake
Browse files Browse the repository at this point in the history
  • Loading branch information
planetis-m committed Jun 21, 2024
1 parent f888f70 commit addd681
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions src/rmem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ runnableExamples:
op.free(objects[1])
op.free(objects[3])
let newObj = cast[ptr MyObject](op.alloc()) # Reuses a slot
echo "Allocated a new object, x = ", newObj.x # Always zero
echo "Allocated a new object, x = ", newObj.x # Memory is cleared

# Example 3: BiStack
var bs = createBiStack(len = 1000) # Create a BiStack with 1000 bytes
Expand Down Expand Up @@ -114,12 +114,12 @@ const
# Types and Structures Definition

type
MemNode = ptr object # Memory pool node
MemNode = object # Memory pool node
size: int
next, prev: MemNode
next, prev: ptr MemNode

AllocList = object # Freelist implementation
head, tail: MemNode
head, tail: ptr MemNode
len: int

Arena = object # Arena allocator
Expand Down Expand Up @@ -173,13 +173,13 @@ proc `=copy`*(dest: var BiStack; source: BiStack) {.error.}
proc alignSize*(size, align: int): int {.inline.} =
result = (size + (align - 1)) and not (align - 1)

proc splitMemNode(node: MemNode, bytes: int): MemNode =
proc splitMemNode(node: ptr MemNode, bytes: int): ptr MemNode =
let n = cast[uint](node)
result = cast[MemNode](n + uint(node.size - bytes))
result = cast[ptr MemNode](n + uint(node.size - bytes))
node.size -= bytes
result.size = bytes

proc insertMemNodeBefore(list: var AllocList, insert, curr: MemNode) =
proc insertMemNodeBefore(list: var AllocList, insert, curr: ptr MemNode) =
insert.next = curr
if curr.prev == nil:
list.head = insert
Expand All @@ -188,13 +188,13 @@ proc insertMemNodeBefore(list: var AllocList, insert, curr: MemNode) =
curr.prev.next = insert
curr.prev = insert

proc replaceMemNode(old, replace: MemNode) =
proc replaceMemNode(old, replace: ptr MemNode) =
replace.prev = old.prev
replace.next = old.next
if old.prev != nil: old.prev.next = replace
if old.next != nil: old.next.prev = replace

proc removeMemNode(list: var AllocList, node: MemNode): MemNode =
proc removeMemNode(list: var AllocList, node: ptr MemNode): ptr MemNode =
if node.prev != nil:
node.prev.next = node.next
else:
Expand All @@ -214,7 +214,7 @@ proc removeMemNode(list: var AllocList, node: MemNode): MemNode =
dec list.len
result = node

proc findMemNode(list: var AllocList, bytes: int): MemNode =
proc findMemNode(list: var AllocList, bytes: int): ptr MemNode =
var node = list.head
while node != nil:
if node.size < bytes:
Expand All @@ -227,7 +227,7 @@ proc findMemNode(list: var AllocList, bytes: int): MemNode =
return splitMemNode(node, bytes)
return nil

proc insertMemNode(mempool: var MemPool, list: var AllocList, node: MemNode, isBucket: bool) =
proc insertMemNode(mempool: var MemPool, list: var AllocList, node: ptr MemNode, isBucket: bool) =
if list.head == nil:
list.head = node
inc(list.len)
Expand Down Expand Up @@ -298,7 +298,7 @@ proc createMemPool*(size: Natural): MemPool =
if size == 0:
return
# Align the mempool size to at least the size of an alloc node.
let buf {.noalias.} = cast[ptr UncheckedArray[uint8]](c_malloc(size.csize_t))
let buf {.noalias.} = c_malloc(size.csize_t)
if buf == nil:
return
result.arena.size = size
Expand All @@ -316,7 +316,7 @@ proc createMemPool*(buf {.noalias.}: pointer, size: Natural): MemPool =
proc alloc*(mempool: var MemPool, size: Natural): pointer =
if size == 0 or size > mempool.arena.size:
return nil
var newMem: MemNode = nil
var newMem: ptr MemNode = nil
let allocSize = alignSize(size + sizeof(MemNode), sizeof(int))
let bucketSlot = (allocSize shr MEMPOOL_BUCKET_BITS) - 1
# If the size is small enough, let's check if our buckets has a fitting memory block.
Expand All @@ -332,7 +332,7 @@ proc alloc*(mempool: var MemPool, size: Natural): pointer =
# Subtract allocation size from the mempool.
mempool.arena.offs -= uint(allocSize)
# Use the available mempool space as the new node.
newMem = cast[MemNode](mempool.arena.offs)
newMem = cast[ptr MemNode](mempool.arena.offs)
newMem.size = allocSize
# Visual of the allocation block.
# --------------
Expand All @@ -346,7 +346,7 @@ proc alloc*(mempool: var MemPool, size: Natural): pointer =
# --------------
newMem.next = nil
newMem.prev = nil
let finalMem {.noalias.} = cast[ptr UncheckedArray[uint8]](cast[uint](newMem) + uint(sizeof(MemNode)))
let finalMem {.noalias.} = cast[pointer](cast[uint](newMem) + uint(sizeof(MemNode)))
result = finalMem
zeroMem(result, newMem.size - sizeof(MemNode))

Expand All @@ -358,7 +358,7 @@ proc free*(mempool: var MemPool, `ptr`: pointer) =
return
# Behind the actual pointer data is the allocation info.
let `block` = p - uint(sizeof(MemNode))
let memNode = cast[MemNode](`block`)
let memNode = cast[ptr MemNode](`block`)
let bucketSlot = (memNode.size shr MEMPOOL_BUCKET_BITS) - 1
# Make sure the pointer data is valid.
if `block` < mempool.arena.offs or
Expand All @@ -385,14 +385,13 @@ proc realloc*(mempool: var MemPool, `ptr`: pointer, size: Natural): pointer =
elif cast[uint](`ptr`) - uint(sizeof(MemNode)) < mempool.arena.mem:
return nil
else:
let node = cast[MemNode](cast[uint](`ptr`) - uint(sizeof(MemNode)))
const NODE_SIZE = sizeof(MemNode)
let resizedBlock = cast[ptr UncheckedArray[uint8]](alloc(mempool, size))
let node = cast[ptr MemNode](cast[uint](`ptr`) - uint(sizeof(MemNode)))
let resizedBlock = alloc(mempool, size)
if resizedBlock == nil:
return nil
else:
let resized = cast[MemNode](cast[uint](resizedBlock) - uint(sizeof(MemNode)))
let copySize = min(node.size, resized.size) - NODE_SIZE
let resized = cast[ptr MemNode](cast[uint](resizedBlock) - uint(sizeof(MemNode)))
let copySize = min(node.size, resized.size) - sizeof(MemNode)
copyMem(resizedBlock, `ptr`, copySize)
free(mempool, `ptr`)
return resizedBlock
Expand Down Expand Up @@ -426,7 +425,7 @@ proc createObjPool*(objsize, len: Natural): ObjPool =
if len == 0 or objsize == 0:
return
let alignedSize = alignSize(objsize, sizeof(int))
let buf {.noalias.} = cast[ptr UncheckedArray[uint8]](c_calloc(csize_t(len), csize_t(alignedSize)))
let buf {.noalias.} = c_calloc(csize_t(len), csize_t(alignedSize))
if buf == nil:
return
result.objSize = alignedSize
Expand Down Expand Up @@ -487,7 +486,7 @@ proc createBiStack*(len: Natural): BiStack =
result = BiStack()
if len == 0:
return
let buf = cast[ptr UncheckedArray[uint8]](c_malloc(csize_t(len)))
let buf = c_malloc(csize_t(len))
if buf == nil:
return
result.size = len
Expand Down

0 comments on commit addd681

Please sign in to comment.