Write the Derived Creation Routine - Untitled
Description: Untitled

Next: Write Other Derived Routines, Previous: Define the Derived Structures, Up: Deriving a New Hash Table Type

##### 2.18.4.2 Write the derived creation routine

You must write a routine which will create and initialize an entry in the hash table. This routine is passed as the function argument to `bfd_hash_table_init`.

In order to permit other hash tables to be derived from the hash table you are creating, this routine must be written in a standard way.

The first argument to the creation routine is a pointer to a hash table entry. This may be `NULL`, in which case the routine should allocate the right amount of space. Otherwise the space has already been allocated by a hash table type derived from this one.

After allocating space, the creation routine must call the creation routine of the hash table type it is derived from, passing in a pointer to the space it just allocated. This will initialize any fields used by the base hash table.

Finally the creation routine must initialize any local fields for the new hash table type.

Here is a boilerplate example of a creation routine. function_name is the name of the routine. entry_type is the type of an entry in the hash table you are creating. base_newfunc is the name of the creation routine of the hash table type your hash table is derived from.

         struct bfd_hash_entry *
         function_name (struct bfd_hash_entry *entry,
                              struct bfd_hash_table *table,
                              const char *string)
         {
           struct entry_type *ret = (entry_type *) entry;

          /* Allocate the structure if it has not already been allocated by a
             derived class.  */
           if (ret == NULL)
             {
               ret = bfd_hash_allocate (table, sizeof (* ret));
               if (ret == NULL)
                 return NULL;
             }

          /* Call the allocation method of the base class.  */
           ret = ((entry_type *)
                 base_newfunc ((struct bfd_hash_entry *) ret, table, string));

          /* Initialize the local fields here.  */

           return (struct bfd_hash_entry *) ret;
         }

Description
The creation routine for the linker hash table, which is in `linker.c`, looks just like this example. function_name is `_bfd_link_hash_newfunc`. entry_type is `struct bfd_link_hash_entry`. base_newfunc is `bfd_hash_newfunc`, the creation routine for a basic hash table.

`_bfd_link_hash_newfunc` also initializes the local fields in a linker hash table entry: `type`, `written` and `next`.