Пример #1
0
bool gui::create_context(gui::context_t *ui)
{
    if (ui)
    {
        ui->HotItem     = LLGUI_INVALID_ID;
        ui->ActiveItem  = LLGUI_INVALID_ID;
        ui->PointerX    = 0.0f;
        ui->PointerY    = 0.0f;
        ui->InteractX   = 0.0f;
        ui->InteractY   = 0.0f;
        ui->Interaction = gui::INTERACTION_OFF;
        ui->KeyCount    = 0;
        ui->CapsLockOn  = false;
        ui->ShiftDown   = false;
        ui->UpdateTime  = 0.0f;
        ui->DeltaTime   = 0.0f;
        ui->RepeatRate  = 10.0f; // 10 characters per-second
        ui->BlinkRate   = 2.0f;  // blink 2 times per-second
        ui->CaretAlpha  = 1.0f;  // fully opaque
        gui::init_key_buffer(&ui->KeyHistory);
        clist_init(&ui->Buttons, 32);
        clist_init(&ui->Toggles, 32);
        // more clist_init()
        // ...
        return true;
    }
    else return false;
}
Пример #2
0
void test_clist_remove(void)
{
  struct key k1;
  struct key k2;
  struct key k3;
  k1.id = 1;
  k2.id = 2;
  k3.id = 3;
  struct clist list;
  clist_init(&list, match_function, empty_destroy_function, CLIST_UNSORTED);

  int ret = clist_append(&list, (void *)&k1);
  CU_ASSERT(ret == CLIST_TRUE);
  ret = clist_append(&list, (void *)&k2);
  CU_ASSERT(ret == CLIST_TRUE);
  ret = clist_append(&list, (void *)&k3);
  CU_ASSERT(ret == CLIST_TRUE);

  struct key *data;

  ret = clist_get_by_id(&list, (void **)&data, 1);
  CU_ASSERT(ret == CLIST_TRUE);
  if (ret == CLIST_TRUE)
    CU_ASSERT(data->id == 2);

  ret = clist_remove(&list, (void **)&data, 1);
  CU_ASSERT(ret == CLIST_TRUE);

  ret = clist_get_by_id(&list, (void **)&data, 1);
  CU_ASSERT(ret == CLIST_TRUE);
  if (ret == CLIST_TRUE)
    CU_ASSERT(data->id == 3);
  
  clist_destroy(&list);

  /* test corner case (one element list) */

  k1.id = 1;
  clist_init(&list, match_function, empty_destroy_function, CLIST_UNSORTED);

  ret = clist_append(&list, (void *)&k1);
  CU_ASSERT(ret == CLIST_TRUE);

  ret = clist_get_by_id(&list, (void **)&data, 0);
  CU_ASSERT(ret == CLIST_TRUE);
  if (ret == CLIST_TRUE)
    CU_ASSERT(data->id == 1);

  ret = clist_remove(&list, (void **)&data, 0);
  CU_ASSERT(ret == CLIST_TRUE);
  
  clist_destroy(&list);

}
Пример #3
0
void
andna_caches_init(int family)
{
	net_family = family;

	setzero(&lcl_keyring, sizeof(lcl_keyring));

	andna_lcl = (lcl_cache *) clist_init(&lcl_counter);
	andna_c = (andna_cache *) clist_init(&andna_c_counter);
	andna_counter_c = (counter_c *) clist_init(&cc_counter);
	andna_rhc = (rh_cache *) clist_init(&rhc_counter);
}
Пример #4
0
void
reset_rnode_allowed(struct allowed_rnode **alr, int *alr_counter)
{
    if (*alr)
        list_destroy((*alr));
    *alr = (struct allowed_rnode *) clist_init(alr_counter);
}
Пример #5
0
void init_named_flags()
{
	int r;
	
	for (r=0; r<FLAGS_NAME_HASH_ENTRIES; r++)
		clist_init(&name2flags[r], next, prev);
}
Пример #6
0
int main()
{
    CList *list = (CList*)malloc(sizeof(CList));
	clist_init(list,destroy);
	Stu *stu = (Stu*)malloc(sizeof(Stu));
	stu->age = 11;
	stu->name ="HeXinyuan";
	clist_ins_next(list,NULL,(void *)stu);
	Stu *stu2 = (Stu*)malloc(sizeof(Stu));
	stu2->age = 22;
	stu2->name ="ZhangJingli";
    clist_ins_next(list,clist_head(list),(void *)stu2);
    Stu *newStu = createStu(33,"HeXiaoxiao");
    clist_ins_next(list,clist_head(list),(void *)newStu);
    printf("clist_size()= %d\n",clist_size(list));
    Stu *result = (Stu*)(clist_head(list)->data);
    CListElmt *head = clist_head(list);
    while(head!=NULL){
    	Stu* res = (Stu*)(head->data);
    	printf("res->age = %d,res->name=%s\n",res->age,res->name);
    	head = head->next;
    	if(clist_head(list) == head){
    		break;
    	}
    }
    void *dataStu;
    clist_rem_next(list,clist_head(list),&dataStu);
    printf("%d,%s\n",((Stu*)(dataStu))->age,((Stu*)(dataStu))->name);
    clist_destroy(list);
	return 0;
}
/* PUBLIC */
Pair_index init_pair_index(int n)
{
  Pair_index p;
  int i, j;

  p = get_pair_index();

  p->finished = 1;
  p->n = n;
  p->i = 0;
  p->j = 0;
  p->min = INT_LARGE;
  p->new_min = INT_LARGE;

  p->lists = malloc(n * sizeof(Clist));
  p->top   = malloc(n * n * sizeof(Clist_pos));
  p->curr  = malloc(n * n * sizeof(Clist_pos));

  /* top and curr will be indexed as top[i*n+j]. */

  for (i = 0; i < n; i++)
    p->lists[i] = clist_init("");
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++) {
      p->top[i*p->n+j] = NULL;
      p->curr[i*p->n+j] = NULL;
    }

  return p;
}  /* init_pair_index */
Пример #8
0
EC_BOOL cproc_init_by_rank(CPROC *cproc, const UINT32 src_rank)
{
    UINT32 des_rank;

    for(des_rank = 0; des_rank < CPROC_SIZE(cproc); des_rank ++)
    {
        CPROC_ITEM *cproc_item;

        /***********************************************************************
            src_rank is the owner of (src_rank, des_rank) TO_SEND/SENDING QUEUE
            src_rank is the owner of (des_rank, src_rank) RECVING/IS_RECV QUEUE
        thus,
            any rank can reach send/recv data by (src_rank, des_rank)

        ***********************************************************************/

        cproc_item = CPROC_ITEM(cproc, src_rank, des_rank);
        CPROC_ITEM_ROW_RANK(cproc_item) = src_rank;
        CPROC_ITEM_COL_RANK(cproc_item) = des_rank;

        csbuff_init(CPROC_ITEM_CSBUFF(cproc_item), CMUTEX_PROCESS_SHARED/*CMUTEX_PROCESS_PRIVATE*/);
        csbuff_set_max_len(CPROC_ITEM_CSBUFF(cproc_item), CPROC_DATA_CACHE_MAX_SIZE);
        clist_init(CPROC_ITEM_SENDING_QUEUE(cproc_item), MM_IGNORE, LOC_CPROC_0001);

        cproc_item = CPROC_ITEM(cproc, des_rank, src_rank);
        CPROC_ITEM_INCOMING_TASK_NODE(cproc_item) = NULL_PTR;
    }

    return (EC_TRUE);
}
Пример #9
0
void test_clist_insert(void)
{
  struct key k1;
  struct key k2;
  struct key k3;
  k1.id = 1;
  k2.id = 2;
  k3.id = 3;
  struct clist list;
  clist_init(&list, match_function, empty_destroy_function, CLIST_UNSORTED);
  int ret = clist_append(&list, (void *)&k1);

  CU_ASSERT(ret == CLIST_TRUE);
  ret = clist_append(&list, (void *)&k2);
  CU_ASSERT(ret == CLIST_TRUE);
  ret = clist_insert(&list, (void *)&k3, 1);
  CU_ASSERT(ret == CLIST_TRUE);

  struct key *data;
  ret = clist_get_by_id(&list, (void **)&data, 1);
  CU_ASSERT(ret == CLIST_TRUE);
  if (ret == CLIST_TRUE)
    CU_ASSERT(data->id == 3);

  ret = clist_destroy(&list);
  CU_ASSERT(ret == CLIST_TRUE);
}
Пример #10
0
void taskc_mgr_init(TASKC_MGR *taskc_mgr)
{
    CLIST *taskc_node_list;

    taskc_node_list = (CLIST *)TASKC_MGR_NODE_LIST(taskc_mgr);
    clist_init(taskc_node_list, MM_IGNORE, LOC_TCNODE_0006);
    return;
}
Пример #11
0
/*
 * rnl_reset
 *
 * reset the whole rnode_list
 */
void
rnl_reset(struct rnode_list **rnlist, int *rnlist_counter)
{
    struct rnode_list *rnl = *rnlist, *next;

    list_safe_for(rnl, next)
    rnl_del(rnlist, rnlist_counter, rnl, 1);
    *rnlist = (struct rnode_list *) clist_init(rnlist_counter);
}
Пример #12
0
void test_clist_append_insert_append(void)
{
#define NUM_ELEMENTS 10
  struct clist list;
  int ret = clist_init(&list, match_function, free_destroy_function, CLIST_UNSORTED);
  struct key *k = NULL;
  int ctr = NUM_ELEMENTS;
  for (; ctr>0; ctr--)
  {
    k = (struct key*)malloc(sizeof(struct key));
    k->id = ctr;
    printf("adding id: %i\n", k->id);
    ret = clist_append(&list, (void *)k);
    CU_ASSERT(ret==CLIST_TRUE);
  }
  for (ctr=NUM_ELEMENTS; ctr>0; ctr--)
  {
    k = (struct key*)malloc(sizeof(struct key));
    k->id = ctr;
    printf("adding id: %i\n", k->id);
    ret = clist_insert(&list, (void *)k, ctr);
    CU_ASSERT(ret==CLIST_TRUE);
  }
  for (ctr=NUM_ELEMENTS; ctr>0; ctr--)
  {
    k = (struct key*)malloc(sizeof(struct key));
    k->id = ctr;
    printf("adding id: %i\n", k->id);
    ret = clist_insert(&list, (void *)k, 0);
    CU_ASSERT(ret==CLIST_TRUE);
  }
  for (ctr=NUM_ELEMENTS; ctr>0; ctr--)
  {
    k = (struct key*)malloc(sizeof(struct key));
    k->id = ctr;
    printf("adding id: %i\n", k->id);
    ret = clist_append(&list, (void *)k);
    CU_ASSERT(ret==CLIST_TRUE);
  }

  struct clist_iterator *it = malloc(sizeof(struct clist_iterator));
  ret = clist_get_iterator(&list, it, 0);

  ret = clist_iterate(&list, it, (void **)&k, 0);
  CU_ASSERT(ret==CLIST_TRUE);
  ctr = 0;
  do
  {
    printf("k->id: %i\n", k->id);
    ctr ++;
  } while (clist_iterate(&list, it, (void **)&k, 1)==CLIST_TRUE);
  CU_ASSERT(ctr==NUM_ELEMENTS*4);
  clist_destroy(&list);

}
Пример #13
0
void test_clist_sort(void)
{
  int items[] = {5, 10, 23, 12, 1,
                 91, 32, 12, 3, 2};
#define NUM_ELEMENTS 10
  struct clist list;
  int ret = clist_init(&list, match_function, free_destroy_function, CLIST_UNSORTED);
  struct key *k = NULL;
  int ctr = 0;
  for (; ctr<NUM_ELEMENTS; ctr++)
  {
    k = (struct key*)malloc(sizeof(struct key));
    k->id = items[ctr];
    printf("adding id: %i\n", k->id);
    ret = clist_append(&list, (void *)k);
    CU_ASSERT(ret==CLIST_TRUE);
  }

/*traverse list before sort*/
  struct clist_iterator *it = malloc(sizeof(struct clist_iterator));
  ret = clist_get_iterator(&list, it, 0);
  CU_ASSERT(ret==CLIST_TRUE);

  ret = clist_iterate(&list, it, (void **)&k, 0);
  CU_ASSERT(ret==CLIST_TRUE);
  ctr = 0;
  do
  {
    printf("k->id: %i\n", k->id);
    ctr ++;
  } while (clist_iterate(&list, it, (void **)&k, 1)==CLIST_TRUE);
  CU_ASSERT(ctr==NUM_ELEMENTS);
  
  clist_qsort(&list, ASCENDING);

/*traverse list after sort*/
  ret = clist_get_iterator(&list, it, 0);
  CU_ASSERT(ret==CLIST_TRUE);
  ret = clist_iterate(&list, it, (void **)&k, 0);
  CU_ASSERT(ret==CLIST_TRUE);
  ctr = 0;
  int prev = k->id;;
  do
  {
    printf("k->id: %i, prev: %i\n", k->id, prev);
    CU_ASSERT(prev<=k->id);
    prev = k->id;
    ctr ++;
  } while (clist_iterate(&list, it, (void **)&k, 1)==CLIST_TRUE);
  CU_ASSERT(ctr==NUM_ELEMENTS);
  free(it);
  
  clist_destroy(&list);
}
Пример #14
0
void
first_init_radar(void)
{
    max_radar_wait = MAX_RADAR_WAIT;

    pthread_attr_init(&radar_qspn_send_t_attr);
    pthread_attr_setdetachstate(&radar_qspn_send_t_attr,
                                PTHREAD_CREATE_DETACHED);

    /* register the radar's ops in the pkt_op_table */
    add_pkt_op(ECHO_ME, SKT_BCAST, ntk_udp_radar_port, radard);
    add_pkt_op(ECHO_REPLY, SKT_UDP, ntk_udp_radar_port, radar_recv_reply);

    rlist = (struct rnode_list *) clist_init(&rlist_counter);
    alwd_rnodes =
        (struct allowed_rnode *) clist_init(&alwd_rnodes_counter);

    radar_daemon_ctl = 0;
    init_radar();
}
Пример #15
0
CLIST *clist_new(const UINT32 mm_type, const UINT32 location)
{
    CLIST *clist;

    SAFE_CLIST_MALLOC(clist, location);

    if(clist)
    {
        clist_init(clist, mm_type, location);
    }
    return (clist);
}
Пример #16
0
/*!
 * \brief add/save a detected constraint to the list in memory
 * \param pg_constraint_t constraint
 */
static void db_postgres_constraint_add(pg_constraint_t *c)
{
	if (!pg_constraint) {
		pg_constraint = c;
		LM_DBG("adding init constraint [%s][%s][%s]\n", c->database.s,
				c->table.s, c->unique.s);
		clist_init(pg_constraint, next, prev);
	} else {
		LM_DBG("adding append constraint [%s][%s][%s]\n", c->database.s,
				c->table.s, c->unique.s);
		clist_append(pg_constraint, c, next, prev);
	}
}
Пример #17
0
void
init_radar(void)
{
    hook_retry = 0;
    my_echo_id = 0;
    total_radar_scans = 0;
    setzero(radar_scans, sizeof(radar_scans));
    radar_scan_mutex = 0;

    radar_q = (struct radar_queue *) clist_init(&radar_q_counter);

    setzero(send_qspn_now, sizeof(u_char) * MAX_LEVELS);
}
Пример #18
0
/* PUBLIC */
void init_hints(Uniftype utype,
		int bsub_wt_attr,
		BOOL collect_labels,
		BOOL back_demod_hints,
		void (*demod_proc) (Topform, int, int, BOOL, BOOL))
{
  Bsub_wt_attr = bsub_wt_attr;
  Collect_labels = collect_labels;
  Back_demod_hints = back_demod_hints;
  Demod_proc = demod_proc;
  Hints_idx = lindex_init(FPA, utype, 10, FPA, utype, 10);
  if (Back_demod_hints)
    Back_demod_idx = mindex_init(FPA, utype, 10);
  Redundant_hints = clist_init("redundant_hints");
}  /* init_hints */
Пример #19
0
/* PUBLIC */
Clist read_clause_clist(FILE *fin, FILE *fout, char *name, BOOL assign_id)
{
  Clist lst = clist_init(name);
  Topform c;

  c = read_clause(fin, fout);
  while (c != NULL && !end_of_list_clause(c)) {
    if (assign_id)
      assign_clause_id(c);
    c->justification = input_just();
    clist_append(c, lst);
    c = read_clause(fin, fout);
  }
  if (c != NULL)
    zap_topform(c);  /* end_of_list_clause */
  return lst;
}  /* read_clause_clist */
Пример #20
0
static _bool get_messages(str_t *input, dwg_hbp_t *hbp)
{
	str_t *current_offset	= malloc(sizeof(str_t));
	int total_bytes			= 0;
	dwg_hbp_t *hbp_item		= NULL;

	clist_init(hbp, next, prev);

	do
	{
		current_offset->s	= input->s + total_bytes;
		current_offset->len	= input->len - total_bytes;

/*		printf("len now: %d\n", current_offset->len);
		printf("============\n");
		hexdump(current_offset->s, current_offset->len);
*/
		hbp_item			= malloc(sizeof(dwg_hbp_t));
		if (!hbp_item)
		{
			LOG(L_ERROR, "%s: No more memory trying to allocate %d bytes\n", __FUNCTION__, sizeof(dwg_hbp_t));
			return FALSE;
		}

		hbp_item->hdr		= dwg_deserialize_message(current_offset, &hbp_item->body);

		if (!hbp_item->body.s)
			return FALSE;

//		printf("hdr len: %d | type: %d\n", hbp_item->hdr.length, hbp_item->hdr.type);
//		hexdump(current_offset->s, current_offset->len);

//		hexdump(current_offset->s, DWG_MSG_HEADER_SIZE + hbp_item->hdr.length);

		clist_append(hbp, hbp_item, next, prev);

		total_bytes += DWG_MSG_HEADER_SIZE + hbp_item->hdr.length;

//		printf("bytes %d/%d\n", total_bytes, input->len);
	}
	while (total_bytes < input->len);

	free(current_offset);

	return TRUE;
}
Пример #21
0
clist_t *clist_alloc_dm(const char *file, const int line)
#endif
{
  clist_t *list;
  size_t len = sizeof(clist_t);

#ifndef DMALLOC
  list = malloc(len);
#else
  list = dmalloc_malloc(file, line, len, DMALLOC_FUNC_MALLOC, 0, 0);
#endif

  if(list != NULL)
    {
      clist_init(list);
    }

  return list;
}
Пример #22
0
int main() {
    CList list;
    CListElmt *element;
    clist_init(&list, free);

    int elements_left;
    int data1 = 4;
    int data2 = 9;
    clist_ins_next(&list, NULL, &data1);
    clist_ins_next(&list, clist_head(&list), &data2);
    elements_left = clist_size(&list);

    for (element = clist_head(&list); element != NULL && elements_left;
         element = clist_next(element), elements_left--) {
        printf("element->data = %d\n", *((int *) element->data));
    }

    return 0;
}
Пример #23
0
static struct rpc_struct_l* new_rpc_struct()
{
	struct rpc_struct_l* rs;
	
	/* alloc everything in one chunk */
	rs=ctl_malloc(sizeof(struct rpc_struct_l)+STRUCT_MAX_BODY);
	if (rs==0)
		goto error;
	memset(rs, 0, sizeof(struct rpc_struct_l));
	clist_init(&rs->substructs, next, prev);
	if (binrpc_init_pkt(&rs->pkt,
				(unsigned char*)rs+sizeof(struct rpc_struct_l),
				STRUCT_MAX_BODY)<0){
		ctl_free(rs);
		goto error;
	}
	return rs;
error:
	return 0;
}
Пример #24
0
static
Stree stree_insert(Stree s, Topform c)
{
  if (s == NULL) {
    s = get_stree();
    s->weight = c->weight;
    s->clauses = clist_init("clauses_by_weight");
    clist_append(c, s->clauses);
  }
  else if (c->weight == s->weight) {
    clist_append(c, s->clauses);
  }
  else if (c->weight < s->weight)
    s->left = stree_insert(s->left, c);
  else
    s->right = stree_insert(s->right, c);
  s->greatest_id = c->id;  /* clauses always inserted with increasing IDs */
  s->n++;
  return s;
}  /* stree_insert */
Пример #25
0
EC_BOOL cconnp_init(CCONNP *cconnp)
{
    if(NULL_PTR != cconnp)
    {
        CCONNP_SRV_IPADDR(cconnp)              = CMPI_ERROR_IPADDR;
        CCONNP_SRV_PORT(cconnp)                = CMPI_ERROR_SRVPORT;

        CCONNP_SRV_TCID(cconnp)                = CMPI_ERROR_TCID;
        CCONNP_SRV_COMM(cconnp)                = CMPI_ERROR_COMM;
        CCONNP_SRV_SIZE(cconnp)                = 0;

        CCONNP_TIMEOUT_MSEC(cconnp)            = 0;
        CCONNP_EXPIRED_MSEC(cconnp)            = 0;

        CCONNP_DATA_NEW_HANDLER(cconnp)        = NULL_PTR;
        CCONNP_DATA_FREE_HANDLER(cconnp)       = NULL_PTR;

        CCONNP_CONNECT_HANDLER(cconnp)         = NULL_PTR;
        CCONNP_SEND_HANDLER(cconnp)            = NULL_PTR;
        CCONNP_RECV_HANDLER(cconnp)            = NULL_PTR;
        CCONNP_CLOSE_HANDLER(cconnp)           = NULL_PTR;
        CCONNP_TIMEOUT_HANDLER(cconnp)         = NULL_PTR;

        CCONNP_SHAKEHAND_SEND_HANDLER(cconnp)  = NULL_PTR;
        CCONNP_SHAKEHAND_RECV_HANDLER(cconnp)  = NULL_PTR;
     
        CCONNP_BEFORE_SEND_HANDLER(cconnp)     = NULL_PTR;
        CCONNP_AFTER_SEND_HANDLER(cconnp)      = NULL_PTR;
     
        CCONNP_HEALTH_CHECKER_HANDLER(cconnp)  = NULL_PTR;

        clist_init(CCONNP_SEND_DATA_LIST(cconnp), MM_IGNORE, LOC_CCONNP_0002);

        cqueue_init(CCONNP_IDLE_CONN_QUEUE(cconnp), MM_CSOCKET_CNODE, LOC_CCONNP_0003);
    }

    return (EC_TRUE);
}
Пример #26
0
int main(void)
{
  struct eltpool *ep = ep_new(sizeof(struct argh), 64);
  clist l;
  clist_init(&l);
  for (uns i=0; i<65536; i++)
    {
      struct argh *a = ep_alloc(ep);
      if (i % 3)
	clist_add_tail(&l, &a->n);
      else
	clist_add_head(&l, &a->n);
      if (!(i % 5))
	{
	  a = clist_head(&l);
	  clist_remove(&a->n);
	  ep_free(ep, a);
	}
    }
  ep_delete(ep);
  puts("OK");
  return 0;
}
Пример #27
0
int main() {

    //linked list
    CList *l;

    int *data = 0;
    int *data1 = 1;
    int *data2 = 2;

    clist_init(l, (void *) &data);
    printf("Circular list has been initalized\n");

    clist_ins_next(l, NULL, data);
    clist_ins_next(l, clist_head(l), data1);
    clist_ins_next(l, clist_next(clist_head(l)), data2);

    printf("Current linked list size %d\n", clist_size(l));
    printf("Head elem %d\n", clist_head(l)->data);

    printf("Next of head %d\n", clist_head(l)->next->data);
    printf("Next next of head %d\n", clist_head(l)->next->next->data);

    clist_rem_next(l, clist_head(l)->next, (void *) &data2);
    clist_rem_next(l, clist_head(l), (void *) &data1);
    clist_rem_next(l, clist_head(l), (void *) &data);

    printf("Linked list has been cleared\n");

    if (clist_size(l) == 0) {
        clist_destroy(l);
        printf("Circular list has been destroyed\n");
    } else {
        printf("Can't delete list. It's not empty %d\n", clist_size(l));

    };
    return 0;
}
Пример #28
0
int main(int argc, char *argv[])
{
	struct clist comp_units;
	clist_init(&comp_units);
	char *outfile = "out.rpx";
	_Bool final_link = 0;

	// usage: rpl [-x] [-o <outfile>] <infiles...>
	int opt;
	while((opt = getopt(argc, argv, "o:x")) != -1) {
		switch(opt) {
		case 'o': 
			outfile = optarg;
			break;
		case 'x':
			// this option enables the assumption that this will be the final
			// link operation and absolute jump targets can now be resolved.
			final_link = 1;
			break;
		default:
			{
				printf("usage: %s -o <outfile> <infile> ...\n", argv[0]);
				return -1;
			}
			break;
		}
	}


	struct comp_unit out_u;
	comp_unit_init(&out_u);
	size_t out_text_cap = 0;

	struct clist abs_patch_addrs;
	clist_init(&abs_patch_addrs);

	struct clist rel_patch_addrs;
	clist_init(&rel_patch_addrs);


	for(int i = optind; i < argc; i++) {
		struct comp_unit *u = comp_unit_read(argv[i]);
		if(u == NULL)
			return -1;

		// for each infile:

		// emit its code
		size_t text_start_off = out_u.text_len;

		array_append_bytes(
			&out_u.text, &out_u.text_len, &out_text_cap,
			u->text, u->text_len);

		// collate export locations
		for(struct clnode *i = clist_first(&u->exported);
		    i != clist_end(&u->exported);
		    i = clnode_next(i)) {
			struct name_addr_node *uex = (struct name_addr_node *)i;

			struct name_addr_node *ex =
				name_addr_node_init(
					malloc(sizeof(struct name_addr_node)),
					uex->name, uex->addr + text_start_off);

			clist_queue(&out_u.exported, &ex->hdr);
		}

		// collate abs-dep locations
		for(struct clnode *i = clist_first(&u->abs_deps);
		    i != clist_end(&u->abs_deps);
		    i = clnode_next(i)) {
			struct name_addr_node *udep = (struct name_addr_node *)i;

			struct name_addr_node *dep =
				name_addr_node_init(
					malloc(sizeof(struct name_addr_node)),
					udep->name, udep->addr + text_start_off);

			clist_queue(&abs_patch_addrs, &dep->hdr);
		}

		// collate rel-dep locations
		for(struct clnode *i = clist_first(&u->rel_deps);
		    i != clist_end(&u->rel_deps);
		    i = clnode_next(i)) {
			struct name_addr_node *udep = (struct name_addr_node *)i;

			struct name_addr_node *dep =
				name_addr_node_init(
					malloc(sizeof(struct name_addr_node)),
					udep->name, udep->addr + text_start_off);

			clist_queue(&rel_patch_addrs, &dep->hdr);
		}

		// collate foreign locations
		for(struct clnode *i = clist_first(&u->foreign_deps);
		    i != clist_end(&u->foreign_deps);
		    i = clnode_next(i)) {
			struct name_addr_node *ufdep = (struct name_addr_node *)i;

			struct name_addr_node *fdep =
				name_addr_node_init(
					malloc(sizeof(struct name_addr_node)),
					ufdep->name, ufdep->addr + text_start_off);

			clist_queue(&out_u.foreign_deps, &fdep->hdr);
		}
	}

	//
	// fixup deps if they can be resolved
	//

	while(! clist_is_empty(&rel_patch_addrs)) {
		struct clnode *i = clnode_remove(clist_first(&rel_patch_addrs));
		struct name_addr_node *patch_dst = (struct name_addr_node *)i;

		struct name_addr_node *patch_src =
			name_addr_node_find(&out_u.exported, patch_dst->name);

		if(! patch_src) { // external dep
			clist_queue(&out_u.rel_deps, &patch_dst->hdr);
		} else {
			jump_t jump_target = patch_src->addr;
			jump_t jump_source = patch_dst->addr;
			jump_t jump = jump_target - jump_source;
			jump = htobe32(jump);
			memcpy(out_u.text + patch_dst->addr, &jump, sizeof(jump));

			free(i);
		}
	}

	if(final_link) {
		while(! clist_is_empty(&abs_patch_addrs)) {
			struct clnode *i = clnode_remove(clist_first(&abs_patch_addrs));
			struct name_addr_node *patch_dst = (struct name_addr_node *)i;
	
			struct name_addr_node *patch_src =
				name_addr_node_find(&out_u.exported, patch_dst->name);
	
			if(! patch_src) { // external dep
				fprintf(stderr, "error: unresolved absolute link target: %s\n", 
				        patch_dst->name);
			} else {
				jump_t jump = patch_src->addr;
				jump = htobe32(jump);
				memcpy(out_u.text + patch_dst->addr, &jump, sizeof(jump));
	
				free(i);
			}
		}
	} else {
		while(! clist_is_empty(&abs_patch_addrs)) {
			struct clnode *i = clnode_remove(clist_first(&abs_patch_addrs));
			struct name_addr_node *patch_dst = (struct name_addr_node *)i;

			clist_queue(&out_u.abs_deps, &patch_dst->hdr);
		}
	}

	//
	// emit object file
	//

	comp_unit_write(&out_u, outfile);

	return 0;
}
Пример #29
0
int main(int argc, char *argv[])
{
	if(argc != 3) {
		printf("usage: %s <infile> <outfile>\n", argv[0]);
		return -1;
	}
	char *infile = argv[1];
	char *outfile = argv[2];

	FILE *instream;
	if(strcmp(infile, "--") == 0)
		instream = stdin;
	else
		instream = fopen(infile, "r");
	if(! instream) {
		perror("fopen()");
		fprintf(stderr, "file %s\n", infile);
		return -1;
	}

	FILE *outstream;
	if(strcmp(outfile, "--") == 0)
		outstream = stdout;
	else
		outstream = fopen(outfile, "w");
	if(! outstream) {
		perror("fopen()");
		fprintf(stderr, "file %s\n", outfile);
		return -1;
	}

	// tokenize
	struct clist words;
	clist_init(&words);
	read_words_from_FILE(instream, &words, ';');


	struct comp_unit cu;
	size_t out_text_cap = 0;
	comp_unit_init(&cu);

	// pass 1:
	// identify and store all labels and addressess
	// identify exported labels
	// identify patch locations
	// generate code and immediates

	struct clist label_addrs;
	clist_init(&label_addrs);

	struct clist abs_patch_addrs;
	clist_init(&abs_patch_addrs);

	struct clist rel_patch_addrs;
	clist_init(&rel_patch_addrs);

	for(struct clnode *i = clist_first(&words);
	    i != clist_end(&words);
	    i = clnode_next(i)) {

		struct word_node *wn = (struct word_node *)i;
		char *directive = wn->word;
		if(*directive != '.') {
			printf("expected directive: %s\n", directive);
			return -1;
		}
		directive++;

		i = clnode_next(i);
		if(i == clist_end(&words)) {
			printf("all directives require an argument\n");
			return -1;
		}
		struct word_node *wn_arg = (struct word_node *)i;
		char *arg = wn_arg->word;
		if(*arg == '.') {
			printf("expected arg: %s\n", arg);
			return -1;
		}

		struct name_addr_node *n_a_node;
		char *endp;
		unsigned long long arg_uval;
		long long arg_sval;

		if(strcmp(directive, "label") == 0) {

			n_a_node =
				name_addr_node_init(
					malloc(sizeof(struct name_addr_node)), arg, cu.text_len);

			clist_queue(&label_addrs, &n_a_node->hdr);

		} else if(strcmp(directive, "export") == 0) {

			n_a_node=
				name_addr_node_init(
					malloc(sizeof(struct name_addr_node)), arg, cu.text_len);

			clist_queue(&label_addrs, &n_a_node->hdr);

			n_a_node =
				name_addr_node_init(
					malloc(sizeof(struct name_addr_node)), arg, cu.text_len);

			clist_queue(&cu.exported, &n_a_node->hdr);

		} else if(strcmp(directive, "data_u1") == 0) {

			arg_uval = strtoull(arg, &endp, 0);
			if(*endp) {
				printf("parse u1 immedate error for %s\n", arg);
				return 0;
			}
			if(arg_uval > 0xFFULL) {
				printf("parse u1 immedate too large %s\n", arg);
				return 0;
			}

			uint8_t v = arg_uval;

			array_append_bytes(
				&cu.text, &cu.text_len, &out_text_cap, &v, sizeof(v));

		} else if(strcmp(directive, "data_u2") == 0) {

			arg_uval = strtoull(arg, &endp, 0);
			if(*endp) {
				printf("parse u2 immedate error for %s\n", arg);
				return 0;
			}
			if(arg_uval > 0xFFFFULL) {
				printf("parse u2 immedate too large %s\n", arg);
				return 0;
			}

			uint16_t v = arg_uval;
			v = htobe16(v);

			array_append_bytes(
				&cu.text, &cu.text_len, &out_text_cap, (unsigned char*)&v, sizeof(v));

		} else if(strcmp(directive, "data_u4") == 0) {

			arg_uval = strtoull(arg, &endp, 0);
			if(*endp) {
				printf("parse u4 immedate error for %s\n", arg);
				return 0;
			}
			if(arg_uval > 0xFFFFFFFFULL) {
				printf("parse u4 immedate too large %s\n", arg);
				return 0;
			}

			uint32_t v = arg_uval;
			v = htobe32(v);

			array_append_bytes(
				&cu.text, &cu.text_len, &out_text_cap, (unsigned char*)&v, sizeof(v));

		} else if(strcmp(directive, "data_u8") == 0) {

			arg_uval = strtoull(arg, &endp, 0);
			if(*endp) {
				printf("parse u8 immedate error for %s\n", arg);
				return 0;
			}

			uint64_t v = arg_uval;
			v = htobe64(v);

			array_append_bytes(
				&cu.text, &cu.text_len, &out_text_cap, (unsigned char*)&v, sizeof(v));

		} else if(strcmp(directive, "data_s1") == 0) {

			arg_sval = strtoll(arg, &endp, 0);
			if(*endp) {
				printf("parse s1 immedate error for %s\n", arg);
				return 0;
			}
			if(arg_sval > 0x7FLL || arg_sval < -0x80LL) {
				printf("parse s1 immedate out of range %s\n", arg);
				return 0;
			}

			int8_t v = arg_sval;

			array_append_bytes(
				&cu.text, &cu.text_len, &out_text_cap, (unsigned char*)&v, sizeof(v));

		} else if(strcmp(directive, "data_s2") == 0) {

			arg_sval = strtoll(arg, &endp, 0);
			if(*endp) {
				printf("parse s2 immedate error for %s\n", arg);
				return 0;
			}
			if(arg_sval > 0x7FFFLL || arg_sval < -0x8000LL) {
				printf("parse s2 immedate out of range %s\n", arg);
				return 0;
			}

			int16_t v = arg_sval;
			v = htobe16(v);

			array_append_bytes(
				&cu.text, &cu.text_len, &out_text_cap, (unsigned char*)&v, sizeof(v));

		} else if(strcmp(directive, "data_s4") == 0) {

			arg_sval = strtoll(arg, &endp, 0);
			if(*endp) {
				printf("parse s4 immedate error for %s\n", arg);
				return 0;
			}
			if(arg_sval > 0x7FFFFFFFLL || arg_sval < -0x80000000LL) {
				printf("parse s4 immedate out of range %s\n", arg);
				return 0;
			}

			int32_t v = arg_sval;
			v = htobe32(v);

			array_append_bytes(
				&cu.text, &cu.text_len, &out_text_cap, (unsigned char*)&v, sizeof(v));

		} else if(strcmp(directive, "data_s8") == 0) {

			arg_sval = strtoll(arg, &endp, 0);
			if(*endp) {
				printf("parse s8 immedate error for %s\n", arg);
				return 0;
			}

			int64_t v = arg_sval;
			v = htobe64(v);

			array_append_bytes(
				&cu.text, &cu.text_len, &out_text_cap, (unsigned char*)&v, sizeof(v));

		} else if(strcmp(directive, "addr") == 0) {

			n_a_node =
				name_addr_node_init(
					malloc(sizeof(struct name_addr_node)), arg, cu.text_len);
			clist_queue(&abs_patch_addrs, &n_a_node->hdr);

			uint32_t v = 0;
			array_append_bytes(
				&cu.text, &cu.text_len, &out_text_cap, (unsigned char*)&v, sizeof(v));

		} else if(strcmp(directive, "reladdr") == 0) {

			n_a_node =
				name_addr_node_init(
					malloc(sizeof(struct name_addr_node)), arg, cu.text_len);
			clist_queue(&rel_patch_addrs, &n_a_node->hdr);

			int32_t v = 0;
			array_append_bytes(
				&cu.text, &cu.text_len, &out_text_cap, (unsigned char*)&v, sizeof(v));

		} else if(strcmp(directive, "foreign_id") == 0) {

			n_a_node =
				name_addr_node_init(
					malloc(sizeof(struct name_addr_node)), arg, cu.text_len);
			clist_queue(&cu.foreign_deps, &n_a_node->hdr);

			// patched up later
			uint32_t v = 0;
			array_append_bytes(
				&cu.text, &cu.text_len, &out_text_cap, (unsigned char*)&v, sizeof(v));

		} else if(strcmp(directive, "mnemonic") == 0) {

			uint8_t op;
			for(op = 0; op < ARRLEN(mnemonics); op++)
				if(strcasecmp(mnemonics[op], arg) == 0)
					break;

			if(op == ARRLEN(mnemonics)) {
				printf("unknown mnemonic: %s\n", arg);
				return -1;
			}

			array_append_bytes(
				&cu.text, &cu.text_len, &out_text_cap, &op, sizeof(op));

		} else {
			printf("unknown assembler directive: %s\n", directive);
			return -1;
		}

	}

	// pass 2:
	// fixup patch addresses

	while(! clist_is_empty(&rel_patch_addrs)) {
		struct clnode *i = clnode_remove(clist_first(&rel_patch_addrs));
		struct name_addr_node *patch_dst = (struct name_addr_node *)i;

		struct name_addr_node *patch_src =
			name_addr_node_find(&label_addrs, patch_dst->name);

		if(! patch_src) { // external dep
			clist_queue(&cu.rel_deps, &patch_dst->hdr);
		} else {
			jump_t jump_target = patch_src->addr;
			jump_t jump_source = patch_dst->addr;
			jump_t jump = jump_target - jump_source;
			jump = htobe32(jump);
			memcpy(cu.text + patch_dst->addr, &jump, sizeof(jump));

			free(i);
		}
	}


	while(! clist_is_empty(&abs_patch_addrs)) {
		struct clnode *i = clnode_remove(clist_first(&abs_patch_addrs));
		struct name_addr_node *patch_dst = (struct name_addr_node *)i;

		struct name_addr_node *patch_src =
			name_addr_node_find(&label_addrs, patch_dst->name);

		if(! patch_src) { // external dep
			clist_queue(&cu.abs_deps, &patch_dst->hdr);
		} else {
			jump_t jump = patch_src->addr;
			jump = htobe32(jump);
			memcpy(cu.text + patch_dst->addr, &jump, sizeof(jump));

			free(i);
		}
	}
	
	//
	// write object file
	//

	comp_unit_write(&cu, argv[2]);

	return 0;
}
Пример #30
0
/* e_rnode_init: Initialize an ext_rnode_cache list and zeros the `counter' */
ext_rnode_cache *
e_rnode_init(u_int * counter)
{
	return (ext_rnode_cache *) clist_init(counter);
}