dymokomi/luce-server
Source of release 0.5.1
0.5.1 / src / internal / storage.lucb
## Heap ownership shared by network and application threads. Every allocation
## names memory.heap explicitly; no result depends on a caller's scoped arena.
import io
import memory
pub let invalid: ErrorCode = ErrorCode.package(1)
pub let limit: ErrorCode = ErrorCode.package(2)
pub let closed: ErrorCode = ErrorCode.package(3)
pub let conflict: ErrorCode = ErrorCode.package(4)
pub func copy(text: str) -> str!:
if text.length == 0:
return ""
let room = try new u8[text.length + 1] --- in memory.heap
memory.copy(room, text.bytes, text.length)
room[text.length] = 0
return (str)room[..<text.length]
pub func release(text: str):
if text.length == 0:
return
free(u8[]((u8*)text.bytes.data, text.length + 1)) in memory.heap
## A bounded growable byte buffer. A view expires on append, clear or destroy.
## Empty buffers own no allocation. Copies must not be independently destroyed.
pub struct Buffer: io.Writer:
pub var data: u8[]
pub var used: usize
pub var maximum: usize = 1048576
pub func view() -> const u8[]:
if self.used == 0:
var empty: const u8[]
return empty
return self.data[..<self.used]
pub func append(input: const u8[]) -> !:
if input.length == 0: return
if input.length > self.maximum - self.used:
error(limit, "the buffer limit was exceeded")
let needed = self.used + input.length
if needed > self.data.length:
var capacity: usize = 256 if self.data.length == 0 else self.data.length
while capacity < needed:
capacity = self.maximum if capacity > self.maximum // 2 else capacity * 2
if capacity > self.maximum:
capacity = self.maximum
let grown = try new u8[capacity] --- in memory.heap
memory.copy(grown, self.view(), self.used)
if self.data.length > 0:
free(self.data) in memory.heap
self.data = grown
memory.copy(self.data[self.used..], input, input.length)
self.used = needed
pub func write(input: const u8[]) -> usize!:
try self.append(input)
return input.length
pub func clear():
self.used = 0
pub func destroy():
if self.data.length > 0:
free(self.data) in memory.heap
var empty: u8[]
self.data = empty
self.used = 0