"Fossies" - the Fresh Open Source Software Archive

Member "rpm2html-1.11.2/stringbuf.c" (18 Dec 2000, 3615 Bytes) of package /linux/privat/rpm2html-1.11.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stringbuf.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * stringbuf.h: interface for the String buffering module.
    3  *
    4  * daniel@veillard.com
    5  */
    6 
    7 #include <stdio.h>
    8 #include <stdlib.h>
    9 #include <string.h>
   10 #ifdef HAVE_UNISTD_H
   11 #include <unistd.h> 
   12 #endif
   13 #include "stringbuf.h"
   14 
   15 #include "memory.h"
   16 
   17 /*
   18  * Not the default, too heavy on CPU ...
   19  */
   20 
   21 #ifdef WITH_STRING_BUF
   22 
   23 #define CLEANUP_FREQUENCY 2000
   24 
   25 /*
   26  * An hash table for the Strings
   27  */
   28 
   29 #define HASH_SIZE 65536
   30 
   31 typedef struct stringHash {
   32     struct stringHash *next;    /* next clash in the hash table */
   33     int use;            /* number of references */
   34     const char *val;        /* the string value */
   35 } stringHash, *stringHashPtr;
   36 
   37 stringHashPtr stringHashTable[HASH_SIZE];
   38 
   39 /*
   40  * Hash table initialization.
   41  */
   42 
   43 static int stringHashInitialized = 0;
   44 
   45 static void stringHashInitialize(void) {
   46     int i;
   47 
   48     for (i = 0; i < HASH_SIZE; i++) {
   49         stringHashTable[i] = NULL;
   50     }
   51     stringHashInitialized = 1;
   52 }
   53 
   54 /*
   55  * Compute an hash key
   56  */
   57 
   58 static int stringGetHash(const char *str) {
   59     unsigned short res = 0;
   60 
   61     while (*str != 0) res += *(str++);
   62 
   63     return((int) (res % HASH_SIZE));
   64 }
   65 
   66 /*
   67  * Cleanup the hash table for unused entries
   68  */
   69 
   70 void stringTableCleanup(void) {
   71     int key;
   72     int used = 0, entries = 0;
   73     stringHashPtr cur;
   74     stringHashPtr prev;
   75 
   76     for (key =0;key < HASH_SIZE;key++) {
   77         if (stringHashTable[key] != NULL) used++;
   78         while (stringHashTable[key] != NULL) {
   79         if (stringHashTable[key]->use == 0) {
   80             cur = stringHashTable[key]->next;
   81         stringHashTable[key]->val = NULL;
   82         stringHashTable[key]->use = 0;
   83         stringHashTable[key]->next = NULL;
   84         xmlFree(stringHashTable[key]);
   85         stringHashTable[key] = cur;
   86         } else {
   87             entries++;
   88             prev = stringHashTable[key];
   89         cur = stringHashTable[key]->next;
   90         while (cur != NULL) {
   91             if (cur->use == 0) {
   92                 xmlFree(cur->val);
   93             prev->next = cur->next;
   94             cur->next = NULL;
   95             xmlFree(cur);
   96             cur = prev->next;
   97             } else {
   98             entries++;
   99                 prev = cur;
  100             cur = cur->next;
  101             }
  102         }
  103         goto next_key;
  104         }
  105     }
  106 next_key:
  107     }
  108     printf("String Buffer: %d entries, %d hash\n", entries, used);
  109 }
  110 
  111 /*
  112  * Duplicate an "external" string. Search the string in the
  113  * list if not found, add it, otherwise increment the counter.
  114  */
  115 const char *stringAdd(const char *str) {
  116     int key;
  117     stringHashPtr cur;
  118     static int toCleanup = CLEANUP_FREQUENCY;
  119 
  120     if (stringHashInitialized == 0) stringHashInitialize();
  121     toCleanup--;
  122     if (toCleanup <= 0) {
  123         toCleanup = CLEANUP_FREQUENCY;
  124         stringTableCleanup();
  125     }
  126 
  127     key = stringGetHash(str);
  128     cur = stringHashTable[key];
  129     while (cur != NULL) {
  130         if (!strcmp(str, cur->val)) {
  131         cur->use++;
  132         return(cur->val);
  133     }
  134     cur = cur->next;
  135     }
  136     cur = xmlMalloc(sizeof(stringHash));
  137     cur->val = xmlStrdup(str);
  138     cur->use = 1;
  139     cur->next = stringHashTable[key];
  140     stringHashTable[key] = cur;
  141 
  142     return(cur->val);
  143 }
  144 
  145 /*
  146  * Free a duplicate of the string. Decrement the counter, if zero,
  147  * free the string and remove it from the list.
  148  */
  149 void  stringFree(const char *str) {
  150     int key;
  151     stringHashPtr cur;
  152 
  153     key = stringGetHash(str);
  154     cur = stringHashTable[key];
  155     while (cur != NULL) {
  156         if (!strcmp(str, cur->val)) {
  157         cur->use--;
  158         /* TODO : garbage collect ... */
  159         return;
  160     }
  161     cur = cur->next;
  162     }
  163     printf("stringFree : %s not found !\n", str);
  164 }
  165 
  166 #else /* ! WITH_STRING_BUF */
  167 const char *stringAdd(const char *str) {
  168     return(xmlStrdup(str));
  169 }
  170 void  stringFree(const char *str) {
  171     xmlFree((char *) str);
  172 }
  173 #endif /* !WITH_STRING_BUF */
  174