static Node install( char *word, char *def ) { Node node; unsigned code = hashcode( word ); for ( node = hashtable[ code ]; node != NULL; node = node->next ) { if ( strcmp( node->name, word ) == 0 ) { /* already defined, we supersede it */ strcpy( node->def, def ); return node; } } if ( (node = (Node) malloc( sizeof( *node ) )) == NULL ) { return NULL; } strcpy( node->name, word ); strcpy( node->def, def ); /* * you cannot just write node->next = NULL; * because there is no pointer pointing to node * after looking up, some pointer and node point to the same thing NULL * but if you got something like node->next = null; * then you can alloc space for node->next, and fill the structure. */ node->next = hashtable[ code ]; hashtable[ code ] = node; return node; }
static unsigned long aout_addsym(char *name,taddr value,int bind, int info,int type,int desc,int be) /* add a new symbol, return its symbol table index */ { struct SymbolNode **chain = &aoutsymlist.hashtab[hashcode(name)%SYMHTABSIZE]; struct SymbolNode *sym; while (sym = *chain) chain = &sym->hashchain; /* new symbol table entry */ *chain = sym = mycalloc(sizeof(struct SymbolNode)); if (!name) name = emptystr; sym->name = name; sym->index = aoutsymlist.nextindex++; setval(be,sym->s.n_strx,4,aout_addstr(name)); sym->s.n_type = type; /* GNU binutils don't use BIND_LOCAL/GLOBAL in a.out files! We do! */ sym->s.n_other = ((bind&0xf)<<4) | (info&0xf); setval(be,sym->s.n_desc,2,desc); setval(be,sym->s.n_value,4,value); addtail(&aoutsymlist.l,&sym->n); return sym->index; }
static unsigned long aout_addstr(char *s) /* add a new symbol name to the string table and return its offset */ { struct StrTabNode **chain = &aoutstrlist.hashtab[hashcode(s)%STRHTABSIZE]; struct StrTabNode *sn; if (*s == '\0') return 0; /* search string in hash table */ while (sn = *chain) { if (!strcmp(s,sn->str)) return (sn->offset); /* it's already in, return offset */ chain = &sn->hashchain; } /* new string table entry */ *chain = sn = mymalloc(sizeof(struct StrTabNode)); sn->hashchain = NULL; sn->str = s; sn->offset = aoutstrlist.nextoffset; addtail(&aoutstrlist.l,&sn->n); aoutstrlist.nextoffset += strlen(s) + 1; return sn->offset; }
static Node lookup( char *word ) { Node ret; for ( ret = hashtable[ hashcode( word ) ]; ret != NULL; ret = ret->next ) { if ( strcmp( ret->name, word ) == 0 ) { return ret; } } return NULL; /* no match found */ }
static int aout_findsym(char *name,int be) /* find a symbol by its name, return symbol table index or -1 */ { struct SymbolNode **chain = &aoutsymlist.hashtab[hashcode(name)%SYMHTABSIZE]; struct SymbolNode *sym; while (sym = *chain) { if (!strcmp(name,sym->name)) return ((int)sym->index); chain = &sym->hashchain; } return (-1); }
static void drawTextNormal(int x, int y, int size, int align, SDL_Color c, char *text) { SDL_Surface *surface; SDL_Texture *t; int w, h; long hash; if (size >= MAX_FONTS) { printf("ERROR: %d exceeds max font size index of %d\n", size, MAX_FONTS); exit(1); } if (!font[size]) { loadFont(size); } hash = hashcode(text, size); t = getCachedText(hash); if (!t) { surface = TTF_RenderText_Blended(font[size], text, c); t = SDL_CreateTextureFromSurface(app.renderer, surface); SDL_FreeSurface(surface); cacheText(hash, t); } SDL_QueryTexture(t, NULL, NULL, &w, &h); if (align == TA_CENTER) { x -= (w / 2); } else if (align == TA_RIGHT) { x -= w; } SDL_SetTextureColorMod(t, c.r, c.g, c.b); blit(t, x, y, 0); }
/** * i is an index into the array of temporary states */ int save_state (int i) { int p; // p is the int k; // k gets the custom codepoint of state 'i's last transition int h,hc; hc=hashcode(i,(k=codes[owf[i]])); h=abs(hc)%HASHS; while ((p = states_hash_table[h]) != 0 && !state_cmp(p,i)) h=(h+HASHC2)%HASHS; if (p==0) { // we did not find an equivalent state, but an empty slot if (HASHS - states_count < HASHS / 10 ) { hashresize(); h=abs(hc)%HASHS; // re-compute the hash slot to start with while (states_hash_table[h] != 0) h=(h+HASHC2)%HASHS; // re-find an open slot } states_hash_table[h] = new_state(i,k); hash_codes[h] = hc; states_count++; } return states_hash_table[h]; }
static void undef( char *word, Node macro ) { Node node, prev; unsigned code = hashcode( word ); prev = node = hashtable[ code ]; if ( node == NULL ) { strcpy( macro->name, "####" ); return; } if ( strcmp( node->name, word ) == 0 ) { strcpy( macro->name, node->name ); strcpy( macro->def, node->def ); macro->next = NULL; if ( node->next == NULL ) { free( node ); hashtable[ code ] = NULL; } return; } /* attention here: you have to assign node to prev, before iterate */ for ( node = node->next; node != NULL; prev = node, node = node->next ) { if ( strcmp( node->name, word ) == 0 ) { strcpy( macro->name, node->name ); strcpy( macro->def, node->def ); macro->next = NULL; /* you cannot write like this: next = node; next = next->next; free( node ); * it does not work we have to make something like prev->next = node->next */ /* attention: prev = node->next, does not work */ prev->next = node->next; free( node ); return; } } /* no match found */ strcpy( macro->name, "####" ); }
static int calculatePoints(consistentImpl_t* pC) { char key[MAX_SERVER_NAME_SIZE]; int length = 0, returnValue = 0; int requiredSize = pC->spread * pC->serverCount; if (pC->pointsCount != requiredSize) { if (pC->points) { free(pC->points); pC->points = 0; pC->pointsCount = 0; } pC->points = calloc(requiredSize, sizeof(point_t)); IfTrue(pC->points, ERR, "Error allocating memory for points"); pC->pointsCount = requiredSize; } for (int i = 0; i < pC->serverCount; i++) { for (int j = 0; j < pC->spread; j++) { memset(key, 0 , MAX_SERVER_NAME_SIZE); length = snprintf(key, MAX_SERVER_NAME_SIZE, "%s-%d", pC->servers[i].serverName, j); IfTrue(length < MAX_SERVER_NAME_SIZE, ERR, "server names to big"); pC->points[(i*pC->spread)+j].hashPoint = hashcode(pC, key, length); pC->points[(i*pC->spread)+j].serverIndex = i; } } qsort(pC->points, pC->pointsCount, sizeof(point_t), pointsCompare); goto OnSuccess; OnError: if (pC->points) { free(pC->points); pC->points = 0; pC->pointsCount = 0; } returnValue = -1; OnSuccess: return returnValue; }
const char* consistentFindServer(consistent_t consistent, char* key) { consistentImpl_t* pC = CONSISTENT(consistent); u_int32_t hashValue = hashcode(pC, key, strlen(key)); int highp = pC->pointsCount, lowp = 0, midp = 0; u_int32_t midval, midval1; char* server = 0; int finalIndex = 0; // divide and conquer array search to find server with next biggest // point after what this key hashes to while (1) { midp = (int)( ( lowp+highp ) / 2 ); if (midp == pC->pointsCount) { // if at the end, roll back to zeroth server = pC->servers[pC->points[0].serverIndex].serverName; finalIndex = 0; break; } midval = pC->points[midp].hashPoint; midval1 = midp == 0 ? 0 : pC->points[midp-1].hashPoint; if (hashValue <= midval && hashValue > midval1) { server = pC->servers[pC->points[midp].serverIndex].serverName; finalIndex = midp; break; } if (midval < hashValue) { lowp = midp + 1; }else { highp = midp - 1; } if (lowp > highp) { server = pC->servers[pC->points[0].serverIndex].serverName; finalIndex = 0; break; } } //we found the server check if it is available if (pC->servers[finalIndex].available) { return server; } // the current server is not available..find the next available // and return that one int next = finalIndex + 1; while (next != finalIndex) { if (next < pC->pointsCount) { if (pC->servers[pC->points[next].serverIndex].available) { return pC->servers[pC->points[next].serverIndex].serverName; } next++; }else { next = 0; } } return 0; }
UT_uint32 hashcode(const UT_String& string) { // from glib return hashcode(string.c_str()); }