void sl_insert(struct sl *l, struct generic_data data) { key_t qKey = data.key; uchar_t i = 0, cl = l->height-1; struct sl_node *n = malloc(sizeof(*n)); sl_cursor cur = sl_zigzag(l, qKey); if(KEY_EQUAL(qKey, cur->data.key)) { mds_error(E_ILLEGAL_ARGUMENT, "Got a similar key"); return; } /* dll_insert((struct dl_list*)l->lists[0], cur->prev, n) */ n->prev = cur->prev; cur->prev->next = n; n->next = cur; cur->prev = n; n->below = n->above = NULL; n->data = data; cur = n; while(rand()%2) { if(i>cl) { l->lists[l->height] = make_list(l, l->height); sl_insertAbove(cur); break; } cur = sl_insertAbove(cur); i++; } l->size++; }
/* Finds the node with key qKey * This is the essence of binary search trees */ bt_cursor bst_search(bt_cursor cur, key_t qKey) { if(KEY_EQUAL(cur->data.key, qKey) || !bt_isInternal(*cur)) return cur; if(KEY_GREATER(cur->data.key, qKey)) return bst_search(cur->left, qKey); return bst_search(cur->right, qKey); }
int sl_get(struct sl *l, key_t qKey) { sl_cursor cur = sl_zigzag(l, qKey); if(!KEY_EQUAL(cur->data.key, qKey)) { mds_error(E_ILLEGAL_ARGUMENT, "Could not find key"); return; } return cur->data.value; }
void bst_put(key_t nKey, int nValue) { struct generic_data *n_entry = malloc(sizeof(*n_entry)); n_entry->key = nKey; n_entry->value = nValue; bt_cursor n_pos = bst_search(t->root, nKey); if(KEY_EQUAL(n_pos.data.key, nKey)) n_pos->data.value = nValue; else if(KEY_GREATER(n_pos.data.key, nKey)) bt_set_left(*n_pos, *n_entry); else bt_set_right(*n_pos, *n_entry); }
int sl_set(struct sl *l, key_t qKey, int value) { int old_value; sl_cursor cur = sl_zigzag(l, qKey); if(!KEY_EQUAL(cur->data.key, qKey)) { mds_error(E_ILLEGAL_ARGUMENT, "Could not find key"); return; } old_value = cur->data.value; do { cur->data.value = value; } while(cur=cur->above); return old_value; }
int sl_remove(struct sl *l, key_t qKey) { sl_cursor tempCur, cur = sl_zigzag(l, qKey); if(!KEY_EQUAL(cur->data.key, qKey)) { mds_error(E_ILLEGAL_ARGUMENT, "Could not find key"); return; } do { tempCur = cur; cur->prev->next = cur->next; tempCur->next->prev = cur->prev; free(tempCur); cur = cur->above; } while(cur=cur->above); l->size--; }
/* * Respond to a Cache Update Indication from SCSP * * * Arguments: * aip pointer to interface control block * smp pointer to message from SCSP * * Returns: * 0 Message processed OK * errno Reason for failure * */ int atmarp_scsp_update_in(Atmarp_intf *aip, Scsp_if_msg *smp) { int accept, rc; Atmarp *aap; /* * Look up the entry */ ATMARP_LOOKUP(aip, smp->si_atmarp.sa_cpa.s_addr, aap); /* * Whether we accept the request depends on whether we * already have an entry for it */ if (!aap) { /* * We don't have this entry--accept it */ accept = 1; } else { /* * We do have an entry for this host--check the * origin ID */ if (bcmp(&aip->ai_ip_addr.s_addr, smp->si_atmarp.sa_oid.id, SCSP_ATMARP_ID_LEN) == 0) { /* * The received entry originated with us-- * reject it */ accept = 0; } else if (bcmp(&aip->ai_ip_addr.s_addr, aap->aa_oid.id, SCSP_ATMARP_ID_LEN) == 0) { /* * We originated the entry we currently have-- * only accept the new one if SCSP has higher * priority than the existing entry */ accept = aap->aa_origin < UAO_SCSP; } else { /* * Accept the entry if it is more up-to-date * than the existing entry */ accept = KEY_EQUAL(&aap->aa_key, &smp->si_atmarp.sa_key) && OID_EQUAL(&aap->aa_oid, &smp->si_atmarp.sa_oid) && (aap->aa_seq < smp->si_atmarp.sa_seq); } } /* * Add the entry to the cache, if appropriate */ if (accept) { if (!aap) { /* * Copy info from SCSP to a new cache entry */ aap = (Atmarp *)UM_ALLOC(sizeof(Atmarp)); if (!aap) atmarp_mem_err("atmarp_scsp_update_in: sizeof(Atmarp)"); UM_ZERO(aap, sizeof(Atmarp)); aap->aa_dstip = smp->si_atmarp.sa_cpa; aap->aa_dstatm = smp->si_atmarp.sa_cha; aap->aa_dstatmsub = smp->si_atmarp.sa_csa; aap->aa_key = smp->si_atmarp.sa_key; aap->aa_oid = smp->si_atmarp.sa_oid; aap->aa_seq = smp->si_atmarp.sa_seq; aap->aa_intf = aip; aap->aa_origin = UAO_SCSP; /* * Add the new entry to our cache */ ATMARP_ADD(aip, aap); } else { /* * Update the existing entry */ aap->aa_dstip = smp->si_atmarp.sa_cpa; aap->aa_dstatm = smp->si_atmarp.sa_cha; aap->aa_dstatmsub = smp->si_atmarp.sa_csa; aap->aa_key = smp->si_atmarp.sa_key; aap->aa_oid = smp->si_atmarp.sa_oid; aap->aa_seq = smp->si_atmarp.sa_seq; aap->aa_origin = UAO_SCSP; } /* * Send the updated entry to the kernel */ if (atmarp_update_kernel(aap) == 0) rc = SCSP_RSP_OK; else rc = SCSP_RSP_REJ; } else { rc = SCSP_RSP_REJ; } /* * Turn the received message into a response */ smp->si_type = SCSP_UPDATE_RSP; smp->si_rc = rc; /* * Send the message to SCSP */ rc = atmarp_scsp_out(aip, (char *)smp, smp->si_len); return(rc); }
/* * Answer a reqeust for information about a cache entry * * Arguments: * aap pointer to entry * state entry's new state * * Returns: * 0 success * errno reason for failure * */ int atmarp_scsp_solicit(Atmarp_intf *aip, Scsp_if_msg *smp) { int i, rc = 0; Atmarp *aap; Scsp_if_msg *rsp = NULL; /* * Search the interface's ATMARP cache for an entry with * the specified cache key and origin ID */ for (i = 0; i < ATMARP_HASHSIZ; i++) { for (aap = aip->ai_arptbl[i]; aap; aap = aap->aa_next) { if (KEY_EQUAL(&aap->aa_key, &smp->si_sum.ss_key) && OID_EQUAL(&aap->aa_oid, &smp->si_sum.ss_oid)) break; } if (aap) break; } /* * Get storage for a Solicit Response */ rsp = (Scsp_if_msg *)UM_ALLOC(sizeof(Scsp_if_msg)); if (!rsp) { atmarp_mem_err("atmarp_scsp_solicit: sizeof(Scsp_if_msg)"); } UM_ZERO(rsp, sizeof(Scsp_if_msg)); /* * Fill out the Solicit Rsp */ rsp->si_type = SCSP_SOLICIT_RSP; rsp->si_proto = smp->si_proto; rsp->si_tok = smp->si_tok; if (aap) { /* * Copy fields from the ATMARP entry to the SCSP * Update Request message */ rsp->si_rc = SCSP_RSP_OK; rsp->si_len = sizeof(Scsp_if_msg_hdr) + sizeof(Scsp_atmarp_msg); rsp->si_atmarp.sa_state = SCSP_ASTATE_UPD; rsp->si_atmarp.sa_cpa = aap->aa_dstip; ATM_ADDR_COPY(&aap->aa_dstatm, &rsp->si_atmarp.sa_cha); ATM_ADDR_COPY(&aap->aa_dstatmsub, &rsp->si_atmarp.sa_csa); rsp->si_atmarp.sa_key = aap->aa_key; rsp->si_atmarp.sa_oid = aap->aa_oid; rsp->si_atmarp.sa_seq = aap->aa_seq; } else { /* * Entry not found--set return code */ rsp->si_rc = SCSP_RSP_NOT_FOUND; rsp->si_len = smp->si_len; rsp->si_sum = smp->si_sum; } /* * Send the message to SCSP */ rc = atmarp_scsp_out(aip, (char *)rsp, rsp->si_len); UM_FREE(rsp); return(rc); }
void bst_remove(key_t qKey) { bt_cursor n_pos = bst_search(t->root, qKey); if(KEY_EQUAL(n_pos.data.key, qKey)) bst_node_remove(n_pos); }