コード例 #1
0
int main(){
	int n=50;
	int q;

	// 1.
	//test addend and removeend
	linkedlist* list=linkedlist_init(sizeof(int));
	for(int i=0;i<n;i++){
		q=i+n;
		linkedlist_addend(list,&q);
	}
	for(int i=0;i<n;i++){
		int* ptr=linkedlist_rmend(list);
		printf("%d ",*ptr);
		free(ptr);
	}
	printf("\n");
	linkedlist_free(list);

	// 2.
	//test the iterators
	list=linkedlist_init(sizeof(int));
	for(int i=0;i<n;i++){
		q=i+n;
		linkedlist_addend(list,&q);
	}
	iterator* iter=linkedlist_iterator(list); //iterate from the start of the list
	while(linkedlist_iteratorhasnext(iter)){
		printf("%d ",*(int*)linkedlist_iteratornext(iter));
	}
	printf("\n");
	linkedlist_freeiter(iter);
	linkedlist_free(list);

	list=linkedlist_init(sizeof(int));
	for(int i=0;i<n;i++){
		q=i+n;
		linkedlist_addend(list,&q);
	}
	iterator* iterend=linkedlist_iteratorend(list); //iterate from the back of the list
	while(linkedlist_iteratorhasprev(iterend)){
		printf("%d ", *(int*)linkedlist_iteratorprev(iterend));
	}
	printf("\n");
	linkedlist_freeiter(iterend);
	linkedlist_free(list);

	//test the getfront and getend
	list=linkedlist_init(sizeof(char*));
	char* string="This is a string";
	char* string2="meow";
	linkedlist_addend(list,&string);
	linkedlist_addend(list,&string2);
	printf("%s %s\n",*(char**)linkedlist_getfront(list),*(char**)linkedlist_getend(list));
	linkedlist_free(list);
}
コード例 #2
0
ファイル: kmem.c プロジェクト: amrsekilly/quafios
void kmem_init() {

    /* initialize free lists:  */
    /* ----------------------- */
    uint32_t i;
    for (i = 0; i < 32; i++)
        linkedlist_init(&freelist[i]);
    linkedlist_init(&usedlist);

    arch_vmpage_map(NULL, KERNEL_MEMORY_BASE, 0);
    linkedlist_add(&freelist[U], (linknode *)((uint32_t) KERNEL_MEMORY_BASE));

    kmem_initialized = 1;

}
コード例 #3
0
ファイル: normalizer.c プロジェクト: OviDanielB/WebServer
normalizer_t* normalizer_init() {

	normalizer_t* normalizer = malloc(sizeof(normalizer_t));
	if(!normalizer) {
		error(1, errno, "error allocating normalizer");
	}

	normalizer->vdfnsn_regex = malloc(sizeof(regex_t));
	if(!normalizer->vdfnsn_regex) {
		error(1, errno, "error allocating vodafone sn regex");
	}

	if(regcomp(normalizer->vdfnsn_regex, "/SN([0-9]+)[ ]*", REG_EXTENDED)!=0) {
		assert(false);
	}

	normalizer->language_regex = malloc(sizeof(regex_t));
	if(!normalizer->language_regex) {
		error(1, errno, "error allocating language regex");
	}

	if(regcomp(normalizer->language_regex, "([a-zA-Z]{2}-[a-zA-Z]{2})", REG_EXTENDED)!=0) {
		assert(false);
	}

	normalizer->handlers = linkedlist_init(&ref_eq);
	linkedlist_add(normalizer->handlers, &normalize_yeswap);
	linkedlist_add(normalizer->handlers, &normalize_babelfish);
	linkedlist_add(normalizer->handlers, &normalize_uplink);
	linkedlist_add(normalizer->handlers, &normalize_vodafonesn);
	linkedlist_add(normalizer->handlers, &normalize_language);

	return normalizer;
}
コード例 #4
0
static char *test_linkedlist_insert_two() {
	linkedlist_t list;
	linkedlist_init(&list);

	int x = 1;
	linkedlist_insert(&list, &x);

	mu_assert_equals_int("Error: Invalid head value", 
		x, 
		*(int *)linkedlist_gethead(&list));

	int y = 2;
	linkedlist_insert(&list, &y);

	mu_assert_equals_int("Error: Invalid head value", 
		x, 
		*(int *)linkedlist_gethead(&list));

	mu_assert_equals_int("Error: Invalid tail value", 
		y, 
		*(int *)linkedlist_gettail(&list));

	mu_assert_equals_int("Error: incorrect size.", 2, linkedlist_getsize(&list));

	linkedlist_destroy(&list);
	return 0;
}
コード例 #5
0
ファイル: encode.c プロジェクト: shiyingzheng/huffman_encode
/*
 * Constructs a linkedlist of bits of encoded file.
 * Takes in a pointer to a huffmantree and a FILE pointer.
 * Returns a pointer to the linkedlist.
 */
linkedlist* treebits(huffmantree* tree,char** t){
	linkedlist* list=linkedlist_init(sizeof(char));

	//Add the tree representation to the list.
	char* treestring=huffmantree_tostring(tree);
	char b;
	int i=0;
	while(treestring[i]){
		b=treestring[i];
		linkedlist_addend(list,&b);
		i++;
	}
	int c;
	char* charstr;
	charstr=t[1<<CHAR_BIT]; //This is the last entry of the table. Follow to get to the EOF node.

	//Adds the path to EOF to the list.
	i=0;
	while(charstr[i]){
		b=charstr[i];
		linkedlist_addend(list,&b);
		i++;
	}
	free(treestring);
	return list;
}
コード例 #6
0
ファイル: list_proto.c プロジェクト: akiradeveloper/jedi
int main(void){
  xlist* l = xlist_delegate( linkedlist_init(), &linkedlist_op );
  int n = 100;
  printf("%p\n", &n);
  l->add(l, &n);
  l->get(l, 0);
  printf("%p\n", l->get(l, 0));
  return 0;
}
コード例 #7
0
/*
 * Initialize a sortedlist.
 * Takes in an integer which is the size of each element of the sortedlist,
 *	and a comparator which takes in two void pointers, compare them, and 
 * 	returns an int.
 * Returns a pointer to a sorted list.
 */
sortedlist* sortedlist_init(int datasize, int (*cmp)(void* a, void* b)){
	sortedlist* this=malloc(sizeof(sortedlist));
	if(!this){
		exit(EXIT_FAILURE);
	}
	this->list=linkedlist_init(datasize);
	this->cmp=cmp;
	this->iter=linkedlist_iterator(this->list);
	return this;
}
コード例 #8
0
ファイル: grt.c プロジェクト: bocchino/ClusterSTM
void grt_init(int argc, char **argv) {

  pthread_t thread;

  /* Set up handler table */
  if (!entry_table) {
    entry_table = grt_entry_table;
    table_size = GRT_TABLE_SIZE;
  }

  /* call startup */
  GASNET_Safe(gasnet_init(&argc, &argv));		

  /* get SPMD info */
  grt_id = gasnet_mynode();
  grt_num_procs = gasnet_nodes();
  gethostname(grt_proc_name, MAX_PROCESSOR_NAME);

  /* Attach to network */
  GASNET_Safe(gasnet_attach(entry_table, table_size,
			    GASNET_HEAP_SIZE, MINHEAPOFFSET));
  if (grt_id == 0) {
    printf("%s\n", argv[0]);
#ifdef GRT_WORD_32
      printf("We are on a 32-bit machine.\n");
#else
      printf("We are on a 64-bit machine.\n");
#endif
      printf("gasnet_AMMaxMedium()=%lu\n", gasnet_AMMaxMedium());
      printf("gasnet_AMMaxLongRequest()=%lu\n", gasnet_AMMaxLongRequest());
      printf("gasnet_AMMaxLongReply()=%lu\n", gasnet_AMMaxLongReply());
  }
  fflush(stdout);
  BARRIER();
  fflush(stdout);
  BARRIER();

  /* Get segment info */
  grt_seginfo = (gasnet_seginfo_t*) malloc(sizeof(gasnet_seginfo_t) *
					   grt_num_procs);
  GASNET_Safe(gasnet_getSegmentInfo(grt_seginfo, grt_num_procs));

  /* Initialize the heap for memory allocation */
  grt_heap_base = grt_addr(grt_id, 0);
  grt_heap = umalloc_makeheap(grt_heap_base,
			      grt_heap_size, UMALLOC_HEAP_GROWS_UP);  

  /* Spawn off a thread to handle remote handler requests */
  pthread_create(&thread, NULL, poll, NULL);

  /* Set up thread list */
  linkedlist_init(&thread_list, 0, 0);

  BARRIER();
}
コード例 #9
0
ファイル: cvcl-incsolve.c プロジェクト: MohsinN/jpf
void initialize()
{
  //hashmap_init(&id2Constraint, 0, NULL, NULL, NULL, NULL);
  hashmap_init(&vars, 0, hash_str, cmp_str, NULL, NULL);
  linkedlist_init(&exprPool, 0, NULL);
  stack_init(&pcids, 0, NULL);

  vc = vc_createValidityChecker(NULL);
  intType = vc_intType(vc);
  realType = vc_realType(vc);
}
コード例 #10
0
ファイル: sym.c プロジェクト: OpenSharp/NDceRpc
int
syminit(struct sym *sym, struct allocator *al)
{
	memset(sym, 0, sizeof *sym);
	if (hashmap_init(&sym->attrs, 0, hash_str, cmp_str, NULL, al) == -1 ||
				linkedlist_init(&sym->mems, 0, al) == -1) {
		AMSG("");
		return -1;
	}

	return 0;
}
コード例 #11
0
static char *test_linkedlist_insert_one() {
	linkedlist_t list;
	linkedlist_init(&list);

	int x = 5;
	linkedlist_insert(&list, &x);

	mu_assert_equals_int("Error: insert error.", x, *(int *)linkedlist_gethead(&list));
	mu_assert_equals_int("Error: incorrect size.", 1, linkedlist_getsize(&list));

	linkedlist_destroy(&list);
	return 0;
}
コード例 #12
0
ファイル: cvcl.c プロジェクト: MohsinN/jpf
JNIEXPORT void JNICALL Java_gov_nasa_jpf_symbolic_dp_NativeInterface_initializeCVCL
  (JNIEnv *env, jclass cls)
{
  // initialize stuff
  hashmap_init(&vars, 0, hash_str, cmp_str, NULL, NULL);
  linkedlist_init(&exprPool, 0, NULL);
  
  //flags = vc_createFlags();
  //vc_setStringFlag(flags, "dump-log", "test1.cvc");
  //vc = vc_createValidityChecker(flags);
  vc = vc_createValidityChecker(NULL);
  intType = vc_intType(vc);
  realType = vc_realType(vc);
}
コード例 #13
0
ファイル: main.c プロジェクト: lovelock/DataStructure
int main() {
    p_linkedlist_t head = linkedlist_init();

    /*
     * linkedlist_insert(head, 2, 7);
     * linkedlist_append(head, 9);
     * linkedlist_prepend(head, 809);
     */
    int val;
    /* linkedlist_delete(head, 1, &val); */
    /* linkedlist_pop(head, &val); */
    linkedlist_shift(head, &val);
    printf("deleted value is: %d\n", val);

    linkedlist_show(head);
}
コード例 #14
0
ファイル: AllTests.c プロジェクト: Lothy/FreeRSC
static void
testing_init()
{
#ifdef STATIC_ALLOC_LINKEDLIST_NODES
    printf("Only static memory to be used for linkedlist nodes.\n");
    linkedlist_init();
    printf("\n");
#else
    printf("Linkedlist nodes will be created with heap memory.\n");
#endif

#ifdef STATIC_ALLOC_CLIENTS
    printf("Only static memory to be used for client structures.\n");
    client_init();
    printf("\n");
#else
    printf("Client structures will be created with heap memory.\n");
#endif

    world_init();
}
コード例 #15
0
ファイル: AdtInit.c プロジェクト: OpenSharp/NDceRpc
int
AdtInit(int verbose, struct cfg *cfg, char *args[])
{
	char buf[0xFFFF];
	struct allocator *suba;
	struct stack s;
	struct linkedlist l;
	struct varray va;

	if ((suba = suba_init(buf, 0xFFFF, 1, 0)) == NULL ||
			stack_init(&s, 0, suba) == -1 ||
			linkedlist_init(&l, 0, suba) == -1 ||
			varray_init(&va, sizeof(int), suba) == -1) {
		AMSG("");
		return -1;
	}

	linkedlist_add(&l, "two");
	stack_push(&s, "two");
	linkedlist_add(&l, "three");
	stack_push(&s, "one");
	varray_get(&va, 444);
	stack_push(&s, "three");
	varray_get(&va, 4);
	varray_get(&va, 44);
	linkedlist_add(&l, "one");

	if (varray_deinit(&va) != 0 ||
			linkedlist_deinit(&l, NULL, NULL) != 0 ||
			stack_deinit(&s, NULL, NULL) != 0) {
		AMSG("");
		return -1;
	}

	tcase_printf(verbose, "done ");

	cfg = NULL;
	args[0] = NULL;
    return 0;
}
コード例 #16
0
static char *test_linkedlist_remove() {
	linkedlist_t list;
	linkedlist_init(&list);

	int x = 1, y = 2, z = 3;
	linkedlist_insert(&list, &x); 
	linkedlist_insert(&list, &y);
	linkedlist_insert(&list, &z);

	mu_assert_equals_int("Error: head value", x, *(int *)linkedlist_gethead(&list));
	mu_assert_equals_int("Error: tail value", z, *(int *)linkedlist_gettail(&list));
	mu_assert_equals_int("Error: size value before remove", 3, linkedlist_getsize(&list));

	linkedlist_remove(&list, &y);

	mu_assert_equals_int("Error: head value", x, *(int *)linkedlist_gethead(&list));
	mu_assert_equals_int("Error: tail value", z, *(int *)linkedlist_gettail(&list));
	mu_assert_equals_int("Error: size value after remove", 2, linkedlist_getsize(&list));

	linkedlist_destroy(&list);
	return 0;
}
コード例 #17
0
int main (int argc, char *argv[])
{
    int rv;
    int result;
    int i, j, count;
    linkedlist_t sll;
    linkedlist_node_t *node;
    void *value;
    void *found;

    rv = linkedlist_init(&sll, 1, compare_pointers, NULL);
    if (rv) {
        fprintf(stderr, "linkedlist_init failed: %d\n", rv);
        return rv;
    } else {
        printf("list initialized\n");
    }
    flush();

    for (i = START_INT; i <= END_INT; i++) {
        value = integer2pointer(i);
        rv = linkedlist_add_once(&sll, value, &found);
        if (rv) {
            fprintf(stderr, "adding %d failed\n", i);
        }
        hashmap[i] = 1;
        rv = linkedlist_add_once(&sll, value, &found);
        if (rv) {
            fprintf(stderr, "adding %d failed in 2nd attempt\n", i);
        }
        rv = linkedlist_add_once(&sll, value, &found);
        if (rv) {
            fprintf(stderr, "adding %d failed in 3rd attempt\n", i);
        }
    }
    printf("all %d items added to list\n", sll.n);
    assert(sll.n == (END_INT - START_INT + 1));
    flush();

    printf("checking that the list is ordered\n");
    node = sll.head;
    result = -1000;
    while (not_endof_linkedlist(node)) {
        printf("%lld ", pointer2integer(node->user_data));
        if (pointer2integer(node->user_data) < result) {
            fprintf(stderr, "list order incorrect, current: %lld expected: >= %d\n",
                pointer2integer(node->user_data), result);
            result = pointer2integer(node->user_data);
        }
        node = node->next;
    }
    printf("\n");
    fflush(stdout);
    fflush(stdout);

    /* now delete one at a time and make sure everything is consistent */
    printf("now deleting and verifying one at a time\n");
    flush();

    count = sll.n;
    int *s, *d;
    for (i = END_INT; i >= START_INT; i--) {

        rv = linkedlist_delete(&sll, integer2pointer(i), (void**) &d);
        if (rv == 0) {
            count--;
            assert(count == sll.n);
            assert(integer2pointer(i) == d);
            hashmap[i] = 0;

            /* 
             * make sure that the deleted entry is NOT in the list 
             * but ALL others are
             */
            for (j = START_INT; j <= END_INT; j++) {
                if (hashmap[j]) {
                    assert(linkedlist_search(&sll, integer2pointer(j), (void**)&s) == 0);
                    assert(j == pointer2integer(s));
                } else {
                    assert(linkedlist_search(&sll, integer2pointer(j), (void**)&s) == ENODATA);
                }
            }
        } else {
            fprintf(stderr, "linkedlist_delete for %d failed\n", i);
        }
    }
    assert(sll.n == 0);
    printf("all data verified, list is sane\n");

    return 0;
}
コード例 #18
0
ファイル: init.c プロジェクト: amrsekilly/quafios
void proc_init() {

    /* Process Manager Initialization */
    int32_t i, err = 0;
    char *initpath = "/bin/init";

    /* (I) Initialize linked lists:  */
    /* ----------------------------- */
    linkedlist_init((linkedlist *) &proclist);
    linkedlist_init((linkedlist *) &q_ready);
    linkedlist_init((linkedlist *) &q_blocked);

    /* (II) Create "init" process:  */
    /* ---------------------------- */
    /* Allocate memory for process structures: */
    initproc = (proc_t *) kmalloc(sizeof(proc_t));

    /* set parent */
    initproc->parent = NULL;

    /* initialize descriptors: */
    initproc->plist.proc = initproc;
    initproc->sched.proc = initproc;
    initproc->irqd.proc  = initproc;

    /* Set "init" pid <1>: */
    initproc->pid = 1;

    /* Add it to process list: */
    linkedlist_addlast((linkedlist *) &proclist,
                       (linknode   *) &(initproc->plist));

    /* Kernel-mode stack: */
    initproc->kstack = kernel_stack;

    /* User memory: */
    umem_init(&(initproc->umem));

    /* initialize file descriptors: */
    for(i = 0; i < FD_MAX; i++)
        initproc->file[i] = NULL;

    /* current working directory: */
    file_open("/", 0, &(initproc->cwd));

    /* not forked: */
    initproc->after_fork = 0;

    /* initialize inbox */
    initproc->inbox_lock = 0;
    initproc->blocked_for_msg = 0;
    linkedlist_init(&(initproc->inbox));

    /* children */
    initproc->blocked_for_child = 0;

    /* not blocked */
    initproc->blocked = 0;

    /* exit status: */
    initproc->terminated = 0;
    initproc->status = 0;

    /* (III) Run the "init" process:  */
    /* ------------------------------ */
    curproc = initproc; /* init process is running! */
    arch_vmswitch(&(initproc->umem));

    /* (IV) Enable multitasking:  */
    /* -------------------------- */
    scheduler_enabled = 1; /* start multitasking! */

    /* (V) Open console streams:  */
    /* -------------------------- */
    /* create device file for console: */
    mknod("/dev/console", FT_SPECIAL, system_console->devid);

    /* open console file: */
    open("/dev/console", 0); /* this will open the file at fd "0". */
    dup(0); /* duplicate at fd "1". */
    dup(0); /* duplicate at fd "2". */

    /* (VI) Execute "init" program to initialize the operating system:  */
    /* ---------------------------------------------------------------- */
    printk("Executing \"%s\" ...\n\n", initpath);
    printk("%a", 0x0E);
    execve(initpath, 0, 0);
    printk("FATAL: Error happened during execution.\n");
    idle();

}
コード例 #19
0
ファイル: tmpfs.c プロジェクト: amrsekilly/quafios
int32_t tmpfs_mknod(inode_t *dir, char *name, int32_t mode, int32_t devid) {

    /* local variables */
    int32_t err;
    tmpfs_ino_t ino;
    inode_t *inode;
    tmpfs_inode_t *dir_tmpfs_inode = (tmpfs_inode_t *) dir->ino;
    tmpfs_inode_t *nod_tmpfs_inode;
    char *fsname;
    tmpfs_dentry_t *p;

    /* parent must be a directory. */
    if ((dir->mode & FT_MASK) != FT_DIR)
        return ENOTDIR;

    /* parent is already deleted? */
    if (!(dir->ref))
        return ENOENT;

    /* name shouldn't exist. */
    err = tmpfs_lookup(dir, name, NULL);
    if (!err) {
        return EEXIST;
    } else if (err != ENOENT) {
        return err;
    }

    /* allocate a new inode: */
    ino = (tmpfs_ino_t) kmalloc(sizeof(tmpfs_inode_t));
    if (!ino)
        return ENOSPC;
    nod_tmpfs_inode = (tmpfs_inode_t *) ino;
    linkedlist_init(&(nod_tmpfs_inode->u.blocks));

    /* get the inode: */
    inode = (inode_t *) iget(dir->sb, ino);

    /* initialize the inode: */
    inode->ref   = 1;
    inode->mode  = mode;
    inode->size  = 0;
    inode->devid = devid;
    tmpfs_update_inode(inode);

    /* put the inode: */
    iput(inode);

    /* allocate space for name: */
    fsname = kmalloc(strlen(name)+1);
    if (!fsname)
        return ENOMEM;
    strcpy(fsname, name);

    /* look up for an empty entry */
    p = dir_tmpfs_inode->u.dentries.first;
    while(p && (p->inode > 1))
        p = p->next;

    /* no empty entry? allocate a new one! */
    if (!p) {
        if (!(p = kmalloc(sizeof(tmpfs_dentry_t)))) {
            kfree(fsname);
            return ENOMEM;
        }
        linkedlist_addlast(&(dir_tmpfs_inode->u.dentries), p);
        tmpfs_update_inode(dir);
    }

    /* fill in the entry: */
    p->inode = ino;
    p->name  = fsname;

    /* done */
    return ESUCCESS;

}
コード例 #20
0
ファイル: tmpfs.c プロジェクト: amrsekilly/quafios
super_block_t *tmpfs_read_super(device_t *dev) {

    /* local variables */
    super_block_t *sb;
    tmpfs_inode_t *tmpfs_inode;
    tmpfs_dentry_t *dot;
    tmpfs_dentry_t *dotdot;

    /* allocate memory for superblock: */
    sb = kmalloc(sizeof(super_block_t));
    if (!sb)
        return NULL;

    /* initialize superblock: */
    sb->fsdriver = &tmpfs_t;
    sb->dev = dev;
    sb->icount = 0;
    sb->mounts = 1;

    /* allocate root inode: */
    tmpfs_inode = kmalloc(sizeof(tmpfs_inode_t));
    if (!tmpfs_inode) {
        kfree(sb);
        return NULL;
    }

    /* initialize the inode: */
    tmpfs_inode->ref   = 1;
    tmpfs_inode->mode  = FT_DIR;
    tmpfs_inode->size  = 0;
    tmpfs_inode->devid = 0;
    linkedlist_init(&(tmpfs_inode->u.blocks));

    /* create <.> directory entry: */
    dot = kmalloc(sizeof(tmpfs_dentry_t));
    if (!dot) {
        kfree(tmpfs_inode);
        kfree(sb);
        return NULL;
    }
    dot->inode = (ino_t) tmpfs_inode;
    dot->name  = kmalloc(2);
    if (!(dot->name)) {
        kfree(dot);
        kfree(tmpfs_inode);
        kfree(sb);
        return NULL;
    }
    dot->name[0] = '.';
    dot->name[1] = 0;
    linkedlist_addlast(&(tmpfs_inode->u.dentries), dot);

    /* create <..> entry: */
    dotdot = kmalloc(sizeof(tmpfs_dentry_t));
    if (!dotdot) {
        kfree(dot->name);
        kfree(dot);
        kfree(tmpfs_inode);
        kfree(sb);
        return NULL;
    }
    dotdot->inode = (ino_t) tmpfs_inode;
    dotdot->name  = kmalloc(3);
    if (!(dotdot->name)) {
        kfree(dotdot);
        kfree(dot->name);
        kfree(dot);
        kfree(tmpfs_inode);
        kfree(sb);
        return NULL;
    }
    dotdot->name[0] = '.';
    dotdot->name[1] = '.';
    dotdot->name[2] = 0;
    linkedlist_addlast(&(tmpfs_inode->u.dentries), dotdot);

    /* Update super block: */
    sb->root_ino = (ino_t) tmpfs_inode;

    /* done: */
    return sb;

}
コード例 #21
0
ファイル: fork.c プロジェクト: iocoder/graduation
int32_t fork() {
    /* Time to... fork!
     * I had spent a long time preparing for this!
     * now I am done, it's time to start working on fork().
     */
    int32_t i;
    proc_t *newproc;

    /* create a new process structure: */
    newproc = kmalloc(sizeof(proc_t));
    if (newproc == NULL)
        return -1;

    /* set parent */
    newproc->parent = curproc;

    /* initialize descriptors: */
    newproc->plist.proc = newproc;
    newproc->sched.proc = newproc;
    newproc->irqd.proc  = newproc;
    newproc->semad.proc = newproc;

    /* create memory: */
    if (umem_init(&(newproc->umem))) {
        kfree(newproc);
        return -1; /* error. */
    }

    /* inherit parent's memory: */
    if (umem_copy(&(curproc->umem), &(newproc->umem))) {
        umem_free(&(newproc->umem));
        kfree(newproc);
        return -1; /* error. */
    }

    /* create a new kernel stack. */
    newproc->kstack = (unsigned char *) kmalloc(KERNEL_STACK_SIZE);
    if (newproc->kstack == NULL) {
        umem_free(&(newproc->umem));
        kfree(newproc);
        return -1; /* error. */
    }
    initproc->phy_stack_bot = arch_vmpage_getAddr(NULL, newproc->kstack);

    /* initialize kernel stack...
     * this invokes page faults to allocate memory for
     * the stack early.
     */
    for (i = 0; i < KERNEL_STACK_SIZE; i++)
        newproc->kstack[i] = 0;

    /* copy context from parent's stack to child's stack: */
    copy_context(newproc);

    /* copy the set of file descriptors: */
    for (i = 0; i < FD_MAX; i++) {
        if (curproc->file[i] == NULL) {
            newproc->file[i] = NULL;
        } else {
            curproc->file[i]->fcount++;
            newproc->file[i] = curproc->file[i];
        }
    }

    /* inherit the current working directory: */
    curproc->cwd->fcount++;
    newproc->cwd = curproc->cwd;

    /* set pid: */
    newproc->pid = ++last_pid;

    /* inform the scheduler that this is a just-forked process: */
    newproc->after_fork = 1;

    /* initialize inbox */
    newproc->inbox_lock = 0;
    newproc->blocked_for_msg = 0;
    linkedlist_init(&(newproc->inbox));

    /* children */
    newproc->blocked_for_child = 0;

    /* not blocked */
    newproc->blocked = 0;
    newproc->lock_to_unlock = NULL;

    /* exit status: */
    newproc->terminated = 0;
    newproc->status = 0;

    /* add the new process to the list of processes: */
    linkedlist_addlast((linkedlist*)&proclist, (linknode*)&(newproc->plist));

    /* add to scheduler's queue: */
    linkedlist_addlast((linkedlist*)&q_ready, (linknode*)&(newproc->sched));

    /* call the scheduler */
    scheduler();

    /* return */
    if (curproc == newproc) {
        return 0;
    } else {
        return newproc->pid;
    }

}
コード例 #22
0
ファイル: cvcl.c プロジェクト: MohsinN/jpf
int main(int argc, char** argv)
{
  if(argc < 2)
    throwRuntimeException( "must specify the file name that contains the query" );

  printf( "argc: %d\n", argc);

  printf("%s\n",argv[1]);


  // initialize stuff
  hashmap_init(&vars, 0, hash_str, cmp_str, NULL, NULL);
  linkedlist_init(&exprPool, 0, NULL);
  
  //flags = vc_createFlags();
  //vc_setStringFlag(flags, "dump-log", "test1.cvc");
  //vc = vc_createValidityChecker(flags);
  vc = vc_createValidityChecker(NULL);
  intType = vc_intType(vc);
  realType = vc_realType(vc);

  constraint = (char*) malloc(sizeof(char)*10000);

  int k = 0;
  FILE* fpt = fopen(argv[1],"r");

  while(1){
    char c = fgetc(fpt);
    if (c == EOF) break;
    constraint[k++]=c;
  }
  constraint[k]='\0';


  if (constraint[0] == '\0')
    return TRUE;

  vc_push(vc);
  marker = 0;
  int constraintCount = 1;
  char c;
  int i = 0;
  do{
    c = constraint[i++];
    if (c == ',')
      constraintCount++;
  }while(c != '\0');
  
  Expr* constraintArray = (Expr*) malloc(sizeof(Expr)*constraintCount);
  i = 0;
  char token[2]; // it must be just a comma and '\0'
  do{
    constraintArray[i++] = parse();
  }while(readToken(token));

  Expr andExpr = vc_andExprN(vc, constraintArray, constraintCount);
  linkedlist_add(&exprPool, andExpr);

  jboolean result = check(vc, andExpr);

  //clean up
  free(constraintArray);
  freeStuff();
  vc_pop(vc);

  if(result)
    printf("satisfiable");
  else
    printf("unsatisfiable");

  return EXIT_SUCCESS;
}