int test_vm_alloc() { int i; int *baz, *buf = vm_zalloc(tvm, sizeof(int) * 16); ASSERT_NOTNULL(buf); for (i = 0; i < 16; ++i) ASSERT_EQ(int, buf[i], 0); vm_free(tvm, buf); buf = vm_zalloc(tvm, sizeof(int) * 4096); ASSERT_NOTNULL(buf); for (i = 0; i < 4096; ++i) ASSERT_EQ(int, buf[i], 0); vm_free(tvm, buf); buf = vm_zalloc(tvm, sizeof(int) * 32); for (i = 0; i < 32; ++i) ASSERT_EQ(int, buf[i], 0); baz = vm_realloc(tvm, buf, sizeof(int) * 4096); ASSERT_TRUE(buf == baz); for (i = 0; i < 32; ++i) ASSERT_EQ(int, baz[i], 0); vm_free(tvm, vm_zalloc(tvm, 16)); buf = vm_realloc(tvm, baz, sizeof(int) * 1024 * 4096); ASSERT_FALSE(buf == baz); return 0; }
int test_memory_managment() { #define MM_PARAMS n, 16, sizeof(int) int i, n; int *baz, *buf = vm_zalloc(tvm, sizeof(int) * 16); n = 14; for (i = 0; i < n; ++i) buf[i] = i; baz = mm_need(tvm, buf, MM_PARAMS); ASSERT_TRUE(buf == baz); for (i = 0; i < n; ++i) ASSERT_EQ(int, baz[i], i); n = 16; buf = mm_need(tvm, baz, MM_PARAMS); ASSERT_TRUE(buf == baz); for (i = 0; i < 14; ++i) ASSERT_EQ(int, buf[i], i); ASSERT_EQ(int, buf[14], 0); ASSERT_EQ(int, buf[15], 0); baz = mm_shrink(tvm, buf, MM_PARAMS); ASSERT_TRUE(buf == baz); n = 4; buf = mm_shrink(tvm, baz, MM_PARAMS); ASSERT_FALSE(buf == baz); for (i = 0; i < n; ++i) ASSERT_EQ(int, buf[i], i); #undef MM_PARAMS mm_free(tvm, buf, n, sizeof(int)); return 0; }
struct kstr *vm_strcat(struct ymd_mach *vm, const struct kstr *lhs, const struct kstr *rhs) { char *tmp = vm_zalloc(vm, lhs->len + rhs->len); struct kstr *x; memcpy(tmp, lhs->land, lhs->len); memcpy(tmp + lhs->len, rhs->land, rhs->len); x = kstr_fetch(vm, tmp, lhs->len + rhs->len); vm_free(vm, tmp); return x; }
static void kpool_resize(struct ymd_mach *vm, int shift) { struct kpool *kt = &vm->kpool; struct gc_node **bak = kt->slot; struct gc_node **i, **k = bak + (1 << kt->shift); kt->slot = vm_zalloc(vm, (1 << shift) * sizeof(struct gc_node *)); for (i = bak; i != k; ++i) { if (*i) { struct gc_node *x = *i; while (x) { struct gc_node *next = x->next; kpool_copy2(kt->slot, x, shift); x = next; } } } if (bak) vm_free(vm, bak); kt->shift = shift; }
void kpool_init(struct ymd_mach *vm) { struct kpool *kt = &vm->kpool; kt->used = 0; kt->shift = 8; kt->slot = vm_zalloc(vm, (1 << kt->shift) * sizeof(struct gc_node *)); }
static int vm_init_context(struct ymd_mach *vm) { vm->curr = vm_zalloc(vm, sizeof(*vm->curr)); vm->curr->vm = vm; vm->curr->top = vm->curr->stk; return 0; }