* add hint for future debugging season, so we remember that if it crashes while freei...
[citadel.git] / libcitadel / lib / hash.c
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <string.h>
4 //dbg
5 #include <stdio.h>
6 #include "libcitadel.h"
7 #include "lookup3.h"
8
9 typedef struct Payload Payload;
10
11 struct Payload {
12         /**
13          * \brief Hash Payload storage Structure; filled in linear.
14          */
15         void *Data; /**< the Data belonging to this storage */
16         DeleteHashDataFunc Destructor; /**< if we want to destroy Data, do it with this function. */
17 };
18
19 struct HashKey {
20         /**
21          * \brief Hash key element; sorted by key
22          */
23         long Key;         /**< Numeric Hashkey comperator for hash sorting */
24         long Position;    /**< Pointer to a Payload struct in the Payload Aray */
25         char *HashKey;    /**< the Plaintext Hashkey */
26         long HKLen;       /**< length of the Plaintext Hashkey */
27         Payload *PL;      /**< pointer to our payload for sorting */
28 };
29
30 struct HashList {
31         /**
32          * \brief Hash structure; holds arrays of Hashkey and Payload. 
33          */
34         Payload **Members;     /**< Our Payload members. This fills up linear */
35         HashKey **LookupTable; /**< Hash Lookup table. Elements point to members, and are sorted by their hashvalue */
36         char **MyKeys;         /**< this keeps the members for a call of GetHashKeys */
37         HashFunc Algorithm;    /**< should we use an alternating algorithm to calc the hash values? */
38         long nMembersUsed;     /**< how many pointers inside of the array are used? */
39         long MemberSize;       /**< how big is Members and LookupTable? */
40         long tainted;          /**< if 0, we're hashed, else s.b. else sorted us in his own way. */
41         long uniq;             /**< are the keys going to be uniq? */
42 };
43
44 struct HashPos {
45         /**
46          * \brief Anonymous Hash Iterator Object. used for traversing the whole array from outside 
47          */
48         long Position;
49 };
50
51
52 /**
53  * \brief Iterate over the hash and call PrintEntry. 
54  * \param Hash your Hashlist structure
55  * \param Trans is called so you could for example print 'A:' if the next entries are like that.
56  *        Must be aware to receive NULL in both pointers.
57  * \param PrintEntry print entry one by one
58  * \returns the number of items printed
59  */
60 int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry)
61 {
62         int i;
63         void *Previous;
64         void *Next;
65         const char* KeyStr;
66
67         if (Hash == NULL)
68                 return 0;
69
70         for (i=0; i < Hash->nMembersUsed; i++) {
71                 if (i==0) {
72                         Previous = NULL;
73                 }
74                 else {
75                         if (Hash->LookupTable[i - 1] == NULL)
76                                 Previous = NULL;
77                         else
78                                 Previous = Hash->Members[Hash->LookupTable[i-1]->Position]->Data;
79                 }
80                 if (Hash->LookupTable[i] == NULL) {
81                         KeyStr = "";
82                         Next = NULL;
83                 }
84                 else {
85                         Next = Hash->Members[Hash->LookupTable[i]->Position]->Data;
86                         KeyStr = Hash->LookupTable[i]->HashKey;
87                 }
88
89                 Trans(Previous, Next, i % 2);
90                 PrintEntry(KeyStr, Next, i % 2);
91         }
92         return i;
93 }
94
95
96 /**
97  * \brief verify the contents of a hash list; here for debugging purposes.
98  * \param Hash your Hashlist structure
99  * \param First Functionpointer to allow you to print your payload
100  * \param Second Functionpointer to allow you to print your payload
101  * \returns 0
102  */
103 int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second)
104 {
105         const char *foo;
106         const char *bar;
107         const char *bla = "";
108         long key;
109         long i;
110
111         if (Hash == NULL)
112                 return 0;
113
114         if (Hash->MyKeys != NULL)
115                 free (Hash->MyKeys);
116
117         Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nMembersUsed);
118 #ifdef DEBUG
119         printf("----------------------------------\n");
120 #endif
121         for (i=0; i < Hash->nMembersUsed; i++) {
122                 
123                 if (Hash->LookupTable[i] == NULL)
124                 {
125                         foo = "";
126                         bar = "";
127                         key = 0;
128                 }
129                 else 
130                 {
131                         key = Hash->LookupTable[i]->Key;
132                         foo = Hash->LookupTable[i]->HashKey;
133                         if (First != NULL)
134                                 bar = First(Hash->Members[Hash->LookupTable[i]->Position]->Data);
135                         else 
136                                 bar = "";
137                         if (Second != NULL)
138                                 bla = Second(Hash->Members[Hash->LookupTable[i]->Position]->Data);
139                         else
140                                 bla = "";
141                 }
142 #ifdef DEBUG
143                 printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
144 #endif
145         }
146 #ifdef DEBUG
147         printf("----------------------------------\n");
148 #endif
149         return 0;
150 }
151
152
153 /**
154  * \brief instanciate a new hashlist
155  * \returns the newly allocated list. 
156  */
157 HashList *NewHash(int Uniq, HashFunc F)
158 {
159         HashList *NewList;
160         NewList = malloc (sizeof(HashList));
161         memset(NewList, 0, sizeof(HashList));
162
163         NewList->Members = malloc(sizeof(Payload*) * 100);
164         memset(NewList->Members, 0, sizeof(Payload*) * 100);
165
166         NewList->LookupTable = malloc(sizeof(HashKey*) * 100);
167         memset(NewList->LookupTable, 0, sizeof(HashKey*) * 100);
168
169         NewList->MemberSize = 100;
170         NewList->tainted = 0;
171         NewList->uniq = Uniq;
172         NewList->Algorithm = F;
173
174         return NewList;
175 }
176
177 int GetCount(HashList *Hash)
178 {
179         if(Hash==NULL) return 0;
180         return Hash->nMembersUsed;
181 }
182
183
184 /**
185  * \brief private destructor for one hash element.
186  * Crashing? go one frame up and do 'print *FreeMe->LookupTable[i]'
187  * \param Data an element to free using the user provided destructor, or just plain free
188  */
189 static void DeleteHashPayload (Payload *Data)
190 {
191         /** do we have a destructor for our payload? */
192         if (Data->Destructor)
193                 Data->Destructor(Data->Data);
194         else
195                 free(Data->Data);
196 }
197
198 /**
199  * \brief destroy a hashlist and all of its members
200  * Crashing? do 'print *FreeMe->LookupTable[i]'
201  * \param Hash Hash to destroy. Is NULL'ed so you are shure its done.
202  */
203 void DeleteHash(HashList **Hash)
204 {
205         int i;
206         HashList *FreeMe;
207
208         FreeMe = *Hash;
209         if (FreeMe == NULL)
210                 return;
211         for (i=0; i < FreeMe->nMembersUsed; i++)
212         {
213                 /** get rid of our payload */
214                 if (FreeMe->Members[i] != NULL)
215                 {
216                         DeleteHashPayload(FreeMe->Members[i]);
217                         free(FreeMe->Members[i]);
218                 }
219                 /** delete our hashing data */
220                 if (FreeMe->LookupTable[i] != NULL)
221                 {
222                         free(FreeMe->LookupTable[i]->HashKey);
223                         free(FreeMe->LookupTable[i]);
224                 }
225         }
226         /** now, free our arrays... */
227         free(FreeMe->LookupTable);
228         free(FreeMe->Members);
229         /** did s.b. want an array of our keys? free them. */
230         if (FreeMe->MyKeys != NULL)
231                 free(FreeMe->MyKeys);
232         /** buye bye cruel world. */    
233         free (FreeMe);
234         *Hash = NULL;
235 }
236
237 /**
238  * \brief Private function to increase the hash size.
239  * \param Hash the Hasharray to increase
240  */
241 static void IncreaseHashSize(HashList *Hash)
242 {
243         /* Ok, Our space is used up. Double the available space. */
244         Payload **NewPayloadArea;
245         HashKey **NewTable;
246         
247         if (Hash == NULL)
248                 return ;
249
250         /** double our payload area */
251         NewPayloadArea = (Payload**) malloc(sizeof(Payload*) * Hash->MemberSize * 2);
252         memset(&NewPayloadArea[Hash->MemberSize], 0, sizeof(Payload*) * Hash->MemberSize);
253         memcpy(NewPayloadArea, Hash->Members, sizeof(Payload*) * Hash->MemberSize);
254         free(Hash->Members);
255         Hash->Members = NewPayloadArea;
256         
257         /** double our hashtable area */
258         NewTable = malloc(sizeof(HashKey*) * Hash->MemberSize * 2);
259         memset(&NewTable[Hash->MemberSize], 0, sizeof(HashKey*) * Hash->MemberSize);
260         memcpy(NewTable, Hash->LookupTable, sizeof(HashKey*) * Hash->MemberSize);
261         free(Hash->LookupTable);
262         Hash->LookupTable = NewTable;
263         
264         Hash->MemberSize *= 2;
265 }
266
267
268 /**
269  * \brief private function to add a new item to / replace an existing in -  the hashlist
270  * if the hash list is full, its re-alloced with double size.
271  * \parame Hash our hashlist to manipulate
272  * \param HashPos where should we insert / replace?
273  * \param HashKeyStr the Hash-String
274  * \param HKLen length of HashKeyStr
275  * \param Data your Payload to add
276  * \param Destructor Functionpointer to free Data. if NULL, default free() is used.
277  */
278 static void InsertHashItem(HashList *Hash, 
279                            long HashPos, 
280                            long HashBinKey, 
281                            const char *HashKeyStr, 
282                            long HKLen, 
283                            void *Data,
284                            DeleteHashDataFunc Destructor)
285 {
286         Payload *NewPayloadItem;
287         HashKey *NewHashKey;
288
289         if (Hash == NULL)
290                 return;
291
292         if (Hash->nMembersUsed >= Hash->MemberSize)
293                 IncreaseHashSize (Hash);
294
295         /** Arrange the payload */
296         NewPayloadItem = (Payload*) malloc (sizeof(Payload));
297         NewPayloadItem->Data = Data;
298         NewPayloadItem->Destructor = Destructor;
299         /** Arrange the hashkey */
300         NewHashKey = (HashKey*) malloc (sizeof(HashKey));
301         NewHashKey->HashKey = (char *) malloc (HKLen + 1);
302         NewHashKey->HKLen = HKLen;
303         memcpy (NewHashKey->HashKey, HashKeyStr, HKLen + 1);
304         NewHashKey->Key = HashBinKey;
305         NewHashKey->PL = NewPayloadItem;
306         /** our payload is queued at the end... */
307         NewHashKey->Position = Hash->nMembersUsed;
308         /** but if we should be sorted into a specific place... */
309         if ((Hash->nMembersUsed != 0) && 
310             (HashPos != Hash->nMembersUsed) ) {
311                 long ItemsAfter;
312
313                 ItemsAfter = Hash->nMembersUsed - HashPos;
314                 /** make space were we can fill us in */
315                 if (ItemsAfter > 0)
316                 {
317                         memmove(&Hash->LookupTable[HashPos + 1],
318                                 &Hash->LookupTable[HashPos],
319                                 ItemsAfter * sizeof(HashKey*));
320                 } 
321         }
322         
323         Hash->Members[Hash->nMembersUsed] = NewPayloadItem;
324         Hash->LookupTable[HashPos] = NewHashKey;
325         Hash->nMembersUsed++;
326 }
327
328 /**
329  * \brief if the user has tainted the hash, but wants to insert / search items by their key
330  *  we need to search linear through the array. You have been warned that this will take more time!
331  * \param Hash Our Hash to manipulate
332  * \param HashBinKey the Hash-Number to lookup. 
333  * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! )
334  */
335 static long FindInTaintedHash(HashList *Hash, long HashBinKey)
336 {
337         long SearchPos;
338
339         if (Hash == NULL)
340                 return 0;
341
342         for (SearchPos = 0; SearchPos < Hash->nMembersUsed; SearchPos ++) {
343                 if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
344                         return SearchPos;
345                 }
346         }
347         return SearchPos;
348 }
349
350 /**
351  * \brief Private function to lookup the Item / the closest position to put it in
352  * \param Hash Our Hash to manipulate
353  * \param HashBinKey the Hash-Number to lookup. 
354  * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! )
355  */
356 static long FindInHash(HashList *Hash, long HashBinKey)
357 {
358         long SearchPos;
359         long StepWidth;
360
361         if (Hash == NULL)
362                 return 0;
363
364         if (Hash->tainted)
365                 return FindInTaintedHash(Hash, HashBinKey);
366
367         SearchPos = Hash->nMembersUsed / 2;
368         StepWidth = SearchPos / 2;
369         while ((SearchPos > 0) && 
370                (SearchPos < Hash->nMembersUsed)) 
371         {
372                 /** Did we find it? */
373                 if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
374                         return SearchPos;
375                 }
376                 /** are we Aproximating in big steps? */
377                 if (StepWidth > 1){
378                         if (Hash->LookupTable[SearchPos]->Key > HashBinKey)
379                                 SearchPos -= StepWidth;
380                         else
381                                 SearchPos += StepWidth;
382                         StepWidth /= 2;                 
383                 }
384                 else { /** We are right next to our target, within 4 positions */
385                         if (Hash->LookupTable[SearchPos]->Key > HashBinKey) {
386                                 if ((SearchPos > 0) && 
387                                     (Hash->LookupTable[SearchPos - 1]->Key < HashBinKey))
388                                         return SearchPos;
389                                 SearchPos --;
390                         }
391                         else {
392                                 if ((SearchPos + 1 < Hash->nMembersUsed) && 
393                                     (Hash->LookupTable[SearchPos + 1]->Key > HashBinKey))
394                                         return SearchPos;
395                                 SearchPos ++;
396                         }
397                         StepWidth--;
398                 }
399         }
400         return SearchPos;
401 }
402
403 /**
404  * \brief private abstract wrapper around the hashing algorithm
405  * \param HKey the hash string
406  * \param HKLen length of HKey
407  * \returns the calculated hash value
408  */
409 inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
410 {
411         if (Hash == NULL)
412                 return 0;
413
414         if (Hash->Algorithm == NULL)
415                 return hashlittle(HKey, HKLen, 9283457);
416         else
417                 return Hash->Algorithm(HKey, HKLen);
418 }
419
420
421 /**
422  * \brief Add a new / Replace an existing item in the Hash
423  * \param HashList the list to manipulate
424  * \param HKey the hash-string to store Data under
425  * \param HKeyLen Length of HKey
426  * \param Data the payload you want to associate with HKey
427  * \param DeleteIt if not free() should be used to delete Data set to NULL, else DeleteIt is used.
428  */
429 void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDataFunc DeleteIt)
430 {
431         long HashBinKey;
432         long HashAt;
433
434         if (Hash == NULL)
435                 return;
436
437         /** first, find out were we could fit in... */
438         HashBinKey = CalcHashKey(Hash, HKey, HKLen);
439         HashAt = FindInHash(Hash, HashBinKey);
440
441         if (HashAt >= Hash->MemberSize)
442                 IncreaseHashSize (Hash);
443
444         /** oh, we're brand new... */
445         if (Hash->LookupTable[HashAt] == NULL) {
446                 InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
447         }/** Insert After? */
448         else if (Hash->LookupTable[HashAt]->Key > HashBinKey) {
449                 InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
450         }/** Insert before? */
451         else if (Hash->LookupTable[HashAt]->Key < HashBinKey) {
452                 InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
453         }
454         else { /** Ok, we have a colision. replace it. */
455                 if (Hash->uniq) {
456                         long PayloadPos;
457                         
458                         PayloadPos = Hash->LookupTable[HashAt]->Position;
459                         DeleteHashPayload(Hash->Members[PayloadPos]);
460                         Hash->Members[PayloadPos]->Data = Data;
461                         Hash->Members[PayloadPos]->Destructor = DeleteIt;
462                 }
463                 else {
464                         InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
465                 }
466         }
467 }
468
469 /**
470  * \brief Lookup the Data associated with HKey
471  * \param Hash the Hashlist to search in
472  * \param HKey the hashkey to look up
473  * \param HKLen length of HKey
474  * \param Data returns the Data associated with HKey
475  * \returns 0 if not found, 1 if.
476  */
477 int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data)
478 {
479         long HashBinKey;
480         long HashAt;
481
482         if (Hash == NULL)
483                 return 0;
484
485         if (HKLen <= 0) {
486                 *Data = NULL;
487                 return  0;
488         }
489         /** first, find out were we could be... */
490         HashBinKey = CalcHashKey(Hash, HKey, HKLen);
491         HashAt = FindInHash(Hash, HashBinKey);
492         if ((HashAt < 0) || /**< Not found at the lower edge? */
493             (HashAt >= Hash->nMembersUsed) || /**< Not found at the upper edge? */
494             (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */
495                 *Data = NULL;
496                 return 0;
497         }
498         else { /** GOTCHA! */
499                 long MemberPosition;
500
501                 MemberPosition = Hash->LookupTable[HashAt]->Position;
502                 *Data = Hash->Members[MemberPosition]->Data;
503                 return 1;
504         }
505 }
506
507 /* TODO? */
508 int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload)
509 {
510         return 0;
511 }
512
513 /**
514  * \brief get the Keys present in this hash, simila to array_keys() in PHP
515  *  Attention: List remains to Hash! don't modify or free it!
516  * \param Hash Your Hashlist to extract the keys from
517  * \param List returns the list of hashkeys stored in Hash
518  */
519 int GetHashKeys(HashList *Hash, char ***List)
520 {
521         long i;
522         if (Hash == NULL)
523                 return 0;
524         if (Hash->MyKeys != NULL)
525                 free (Hash->MyKeys);
526
527         Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nMembersUsed);
528         for (i=0; i < Hash->nMembersUsed; i++) {
529         
530                 Hash->MyKeys[i] = Hash->LookupTable[i]->HashKey;
531         }
532         *List = (char**)Hash->MyKeys;
533         return Hash->nMembersUsed;
534 }
535
536 /**
537  * \brief creates a hash-linear iterator object
538  * \returns the hash iterator
539  */
540 HashPos *GetNewHashPos(void)
541 {
542         HashPos *Ret;
543         
544         Ret = (HashPos*)malloc(sizeof(HashPos));
545         Ret->Position = 0;
546         return Ret;
547 }
548
549 /**
550  * \brief frees a linear hash iterator
551  */
552 void DeleteHashPos(HashPos **DelMe)
553 {
554         if (*DelMe != NULL)
555         {
556                 free(*DelMe);
557                 *DelMe = NULL;
558         }
559 }
560
561
562 /**
563  * \brief Get the data located where HashPos Iterator points at, and Move HashPos one forward
564  * \param Hash your Hashlist to follow
565  * \param HKLen returns Length of Hashkey Returned
566  * \param HashKey returns the Hashkey corrosponding to HashPos
567  * \param Data returns the Data found at HashPos
568  * \returns whether the item was found or not.
569  */
570 int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
571 {
572         long PayloadPos;
573
574         if ((Hash == NULL) || (Hash->nMembersUsed <= At->Position))
575                 return 0;
576         *HKLen = Hash->LookupTable[At->Position]->HKLen;
577         *HashKey = Hash->LookupTable[At->Position]->HashKey;
578         PayloadPos = Hash->LookupTable[At->Position]->Position;
579         *Data = Hash->Members[PayloadPos]->Data;
580         At->Position++;
581         return 1;
582 }
583
584 /**
585  * \brief sorting function for sorting the Hash alphabeticaly by their strings
586  * \param Key1 first item
587  * \param Key2 second item
588  */
589 static int SortByKeys(const void *Key1, const void* Key2)
590 {
591         HashKey *HKey1, *HKey2;
592         HKey1 = *(HashKey**) Key1;
593         HKey2 = *(HashKey**) Key2;
594
595         return strcasecmp(HKey1->HashKey, HKey2->HashKey);
596 }
597
598 /**
599  * \brief sorting function for sorting the Hash alphabeticaly reverse by their strings
600  * \param Key1 first item
601  * \param Key2 second item
602  */
603 static int SortByKeysRev(const void *Key1, const void* Key2)
604 {
605         HashKey *HKey1, *HKey2;
606         HKey1 = *(HashKey**) Key1;
607         HKey2 = *(HashKey**) Key2;
608
609         return strcasecmp(HKey2->HashKey, HKey1->HashKey);
610 }
611
612 /**
613  * \brief sorting function to regain hash-sequence and revert tainted status
614  * \param Key1 first item
615  * \param Key2 second item
616  */
617 static int SortByHashKeys(const void *Key1, const void* Key2)
618 {
619         HashKey *HKey1, *HKey2;
620         HKey1 = *(HashKey**) Key1;
621         HKey2 = *(HashKey**) Key2;
622
623         return HKey1->Key > HKey2->Key;
624 }
625
626
627 /**
628  * \brief sort the hash alphabeticaly by their keys.
629  * Caution: This taints the hashlist, so accessing it later 
630  * will be significantly slower! You can un-taint it by SortByHashKeyStr
631  * \param Hash the list to sort
632  * \param Order 0/1 Forward/Backward
633  */
634 void SortByHashKey(HashList *Hash, int Order)
635 {
636         if (Hash->nMembersUsed < 2)
637                 return;
638         qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), 
639               (Order)?SortByKeys:SortByKeysRev);
640         Hash->tainted = 1;
641 }
642
643 /**
644  * \brief sort the hash by their keys (so it regains untainted state).
645  * this will result in the sequence the hashing allgorithm produces it by default.
646  * \param Hash the list to sort
647  */
648 void SortByHashKeyStr(HashList *Hash)
649 {
650         Hash->tainted = 0;
651         if (Hash->nMembersUsed < 2)
652                 return;
653         qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), SortByHashKeys);
654 }
655
656
657 /**
658  * \brief gives user sort routines access to the hash payload
659  * \param Searchentry to retrieve Data to
660  * \returns Data belonging to HashVoid
661  */
662 const void *GetSearchPayload(const void *HashVoid)
663 {
664         return (*(HashKey**)HashVoid)->PL->Data;
665 }
666
667 /**
668  * \brief sort the hash by your sort function. see the following sample.
669  * this will result in the sequence the hashing allgorithm produces it by default.
670  * \param Hash the list to sort
671  * \param SortBy Sortfunction; see below how to implement this
672  */
673 void SortByPayload(HashList *Hash, CompareFunc SortBy)
674 {
675         if (Hash->nMembersUsed < 2)
676                 return;
677         qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), SortBy);
678         Hash->tainted = 1;
679 }
680
681
682
683
684 /**
685  * given you've put char * into your hash as a payload, a sort function might
686  * look like this:
687  * int SortByChar(const void* First, const void* Second)
688  * {
689  *      char *a, *b;
690  *      a = (char*) GetSearchPayload(First);
691  *      b = (char*) GetSearchPayload(Second);
692  *      return strcmp (a, b);
693  * }
694  */
695
696
697 /*
698  * Generic function to free a pointer.  This can be used as a callback with the
699  * hash table, even on systems where free() is defined as a macro or has had other
700  * horrible things done to it.
701  */
702 void generic_free_handler(void *ptr) {
703         free(ptr);
704 }
705
706
707