/* allocation functions */ VMState * vm_newstate(void *program, size_t program_size, VMInterruptPolicy interrupt_policy) { VMState *newstate = NULL; err_malloc(newstate = calloc(1, sizeof(VMState))); // ramsize is in bytes err_malloc(newstate->ram = calloc(ramsize, 1)); #ifdef VM_WITH_THREADS err(pthread_mutex_init(&newstate->lock, NULL) == 0, "pthread_mutex_init", VM_OSERROR); #endif // We make all registers ints err_malloc(newstate->registers = calloc(nregisters, sizeof(int))); newstate->interrupt_policy = interrupt_policy; newstate->interrupts = NULL; newstate->break_async = false; newstate->breakpoints = NULL; if (!_read_elf(newstate, program, program_size)) goto error; return newstate; error: vm_closestate(newstate); return NULL; }
VMState * vm_newstate_no_code(VMInterruptPolicy interrupt_policy) { VMState *newstate = NULL; err_malloc(newstate = calloc(1, sizeof(VMState))); // ramsize is in bytes err_malloc(newstate->ram = calloc(ramsize, 1)); #ifdef VM_WITH_THREADS err(pthread_mutex_init(&newstate->lock, NULL) == 0, "pthread_mutex_init", VM_OSERROR); #endif // We make all registers ints err_malloc(newstate->registers = calloc(nregisters, sizeof(OPCODE_TYPE))); err_malloc(newstate->pins = calloc(npins, sizeof(OPCODE_TYPE))); newstate->interrupt_policy = interrupt_policy; newstate->break_async = false; return newstate; error: vm_closestate(newstate); return NULL; }
/* allocation functions */ VMState * vm_newstate(void *program, size_t program_size, VMInterruptPolicy interrupt_policy) { VMState *newstate; if (!(newstate = vm_newstate_no_code(interrupt_policy))) goto error; if (!_read_elf(newstate, program, program_size)) goto error; return newstate; error: vm_closestate(newstate); return NULL; }
VMState * vm_newstate_no_code(VMInterruptPolicy interrupt_policy) { VMState *newstate = NULL; err_malloc(newstate = calloc(1, sizeof(VMState))); err_malloc(newstate->chunk = calloc(CHUNK_END, 1)); #ifdef VM_WITH_THREADS err(pthread_mutex_init(&newstate->lock, NULL) == 0, "pthread_mutex_init", VM_OSERROR); #endif newstate->interrupt_policy = interrupt_policy; newstate->break_async = false; return newstate; error: vm_closestate(newstate); return NULL; }