Files
Viper_VRTE/vector.vp
T
TermiNexus db3be6968f fix(memhub,gzip): 内存池 OOM 归属画像与 free 拒绝现场;gzip 注解 *= 词法粘连
memhub: alloc 失败打印归属 site/ctx 与存活块 order 分布(新增 dump_alive_orders);free 被 _is_valid_ptr 拒绝时首次打印现场;接通 big_alloc_count

gzip: ub: uchar*= buf 补空格(*= 被词法合并为单 OP,注解止于 uchar)

新增 traceback 模块;删除 dbgstack.vp;其余模块随新语法与尺寸口径同步
2026-09-26 19:27:24 +08:00

66 lines
2.0 KiB
Plaintext

import t, c
from stdint import *
import memhub
class Vector[T]():
data: bytes
length: sizet
capacity: sizet
elem_size: sizet
pool: memhub.MemManager*
def __init__(self, pool: memhub.MemManager*, capacity: sizet, _hint: T):
self.elem_size = T.__sizeof__()
self.capacity = capacity
self.length = 0
self.pool = pool
self.data = pool.alloc(capacity * self.elem_size)
def push(self, value: T):
if self.data == None: return
if self.length >= self.capacity:
self._grow()
if self.length >= self.capacity: return
offset: uint64 = uint64(self.data) + self.length * self.elem_size
dst: T* = (T*)offset # typed pointer: store whole T, not its first byte
*dst = value
self.length += 1
def _grow(self):
if self.pool == None: return
new_capacity: sizet = self.capacity * 2
if new_capacity == 0: new_capacity = 4
new_size: sizet = new_capacity * self.elem_size
new_data: rawptr = self.pool.realloc(self.data, new_size)
if new_data == None: return
self.data = new_data
self.capacity = new_capacity
def get(self, index: sizet) -> T:
if self.data == None: return T(0)
offset: uint64 = uint64(self.data) + index * self.elem_size
typed_ptr = (T*)offset
return *typed_ptr
def set(self, index: sizet, value: T):
if index >= self.length: return
if self.data == None: return
offset: uint64 = uint64(self.data) + index * self.elem_size
dst: T* = (T*)offset # typed pointer: store whole T, not its first byte
*dst = value
def len(self) -> sizet:
return self.length
def clear(self):
self.length = 0
def free(self):
if self.data != None:
if self.pool != None:
self.pool.free(self.data)
self.data = None
self.length = 0
self.capacity = 0