vx32

Local 9vx git repository for patches.
git clone git://r-36.net/vx32
Log | Files | Refs

proc.c (941B)


      1 #include <stdlib.h>
      2 #include <errno.h>
      3 #include <assert.h>
      4 
      5 #include "vx32.h"
      6 #include "vx32impl.h"
      7 
      8 static vxproc *procs[VXPROCSMAX];
      9 
     10 vxproc *vxproc_alloc(void)
     11 {
     12 	// Find an available process number
     13 	int pno;
     14 	for (pno = 0; ; pno++) {
     15 		if (pno == VXPROCSMAX) {
     16 			errno = EAGAIN;
     17 			return NULL;
     18 		}
     19 		if (procs[pno] == 0)
     20 			break;
     21 	}
     22 
     23 	// Allocate the vxproc structure
     24 	vxproc *pr = calloc(1, sizeof(vxproc));
     25 	if (pr == NULL) {
     26 		errno = ENOMEM;
     27 		return NULL;
     28 	}
     29 	pr->vxpno = pno;
     30 
     31 	// Create the process's emulation state
     32 	if (vxemu_init(pr) < 0)
     33 		return NULL;
     34 
     35 	procs[pno] = pr;
     36 	return pr;
     37 }
     38 
     39 void vxproc_free(vxproc *proc)
     40 {
     41 	assert(procs[proc->vxpno] == proc);
     42 	procs[proc->vxpno] = NULL;
     43 	
     44 	// Free the emulation state
     45 	if (proc->emu != NULL)
     46 		vxemu_free(proc->emu);
     47 	
     48 	// Free the process memory.
     49 	if (proc->mem != NULL)
     50 		vxmem_free(proc->mem);
     51 
     52 	free(proc);
     53 }
     54 
     55 void vxproc_flush(vxproc *proc)
     56 {
     57 	vxemu_flush(proc->emu);
     58 }