library/src/datastructures/BP-HashTable.cc
// This links arbitrary data types into the table.  These data types
// can be allocated elsewhere
This links arbitrary data types into the table.  These data types
can be allocated elsewhere
This links arbitrary data types into the table . These data types can be allocated elsewhere
BP_ERROR_T BP_HashRegAddDataToTable
(
		P_BP_HASH_TABLE_REGISTRY       hreg,
		P_BP_HASH_TABLE_KEYED_ENTRY    table,
		char *                         key,
		BP_HASH_TABLE_KEYED_ENTRY_TYPE data_type,
		void *                         data,
		size_t                         data_size
)
{
	BP_GLOBAL_HASH_TABLE_OVERRIDE(ERR_FAILURE);
	if(!table)
		return ERR_FAILURE;
	if(!key)
		return ERR_FAILURE;
	if(BP_StringIsReasonablePrintableString(key, bpstrlen(key), BP_FALSE, BP_FALSE) == ERR_FAILURE)
		return ERR_FAILURE;
	P_BP_HASH_TABLE_KEYED_ENTRY search_table = (P_BP_HASH_TABLE_KEYED_ENTRY) table->data;
	P_BP_HASH_TABLE_KEYED_ENTRY found_item   = NULL;
	HASH_FIND
	(
		hh,
		search_table,
		key,
		bpstrlen(key),
		found_item
	);
	if(found_item)
		return ERR_FAILURE;
	P_BP_HASH_TABLE_KEYED_ENTRY add_to_table = (P_BP_HASH_TABLE_KEYED_ENTRY) table->data;
	P_BP_HASH_TABLE_KEYED_ENTRY new_entry = (P_BP_HASH_TABLE_KEYED_ENTRY) BP_HashRegAddMemoryEntry
	(
		hreg,
		BP_HASH_REG_MEM_ENTRY_TYPE_KEYED_ENTRY_IN_HIVE,
		sizeof(BP_HASH_TABLE_KEYED_ENTRY),
		BPLN
	);
	if(!new_entry)
		return ERR_FAILURE;
	BP_HASH_TABLE_INIT_SEM(new_entry->lock);
	new_entry->parent = (void *) table;
	new_entry->key = ht_strdup(hreg, key);
	switch(data_type)
	{
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_POINTER_EXTERNALLY_MANAGED: 
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_C_DATASTRUCTURE_EXTERNALLY_MANAGED:
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_CPP_CLASS_POINTER_EXTERNALLY_MANAGED:
			new_entry->data = (void *) data;
			new_entry->type = data_type;
			break;
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_NAMED_HASH_TABLE:
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_BINARY_BLOB:
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING:
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_UNICODE16_STRING:
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_UNICODE32_STRING:
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_SIZE_T:
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_SIGNED_INT:
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_JSON:
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_MESSAGE_TO_WORKER:
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_MESSAGE_FROM_WORKER:
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_EVENT_UNPROCESSED:
		case BP_HASH_TABLE_KEYED_ENTRY_TYPE_EVENT_PROCESSED:
			new_entry->data = (void *)BP_HashRegAddMemoryEntry
			(
					hreg,
					BP_HASH_REG_MEM_ENTRY_TYPE_GENERIC_ALLOCATION,
					data_size+1,
					BPLN
			);
			if(!new_entry->data)
			{
				printf("\n\n");
				printf("\n [!!] Critical error in BP_HashRegAddDataToTable() cannot calloc hash data.  Likely out of memory, exiting application immediately before any possible corruptions occur.");
				printf("\n\n");
				exit(0);
				return ERR_FAILURE;
			}
			if(data)
				memcpy(new_entry->data,  data, data_size);
			new_entry->type = data_type;
			new_entry->data_size = data_size;
			break;
		default:
			printf("\n [+] Bad datatype passed to BP_HashRegAddDataToTable.  This is critical.");
			return ERR_FAILURE;
			break;
	}
	%%%%%%%%%%%%%%%%%
	%%%%%%%%%%%%%%%%%
	HASH_ADD_KEYPTR
	(
		hh,
		add_to_table,
		new_entry->key,
		bpstrlen(new_entry->key),
		new_entry
	);
	table->data = add_to_table;
	hreg->total_data_entries_in_hive++;
	return ERR_SUCCESS;
}
BP_ERROR_T BP_HashRegAddDataToTable ( P_BP_HASH_TABLE_REGISTRY hreg , P_BP_HASH_TABLE_KEYED_ENTRY table , char * key , BP_HASH_TABLE_KEYED_ENTRY_TYPE data_type , void * data , size_t data_size ) { BP_GLOBAL_HASH_TABLE_OVERRIDE ( ERR_FAILURE ) ; if ( ! table ) return ERR_FAILURE ; if ( ! key ) return ERR_FAILURE ; if ( BP_StringIsReasonablePrintableString ( key , bpstrlen ( key ) , BP_FALSE , BP_FALSE ) == ERR_FAILURE ) return ERR_FAILURE ; P_BP_HASH_TABLE_KEYED_ENTRY search_table = ( P_BP_HASH_TABLE_KEYED_ENTRY ) table -> data ; P_BP_HASH_TABLE_KEYED_ENTRY found_item = NULL ; HASH_FIND ( hh , search_table , key , bpstrlen ( key ) , found_item ) ; if ( found_item ) return ERR_FAILURE ; P_BP_HASH_TABLE_KEYED_ENTRY add_to_table = ( P_BP_HASH_TABLE_KEYED_ENTRY ) table -> data ; P_BP_HASH_TABLE_KEYED_ENTRY new_entry = ( P_BP_HASH_TABLE_KEYED_ENTRY ) BP_HashRegAddMemoryEntry ( hreg , BP_HASH_REG_MEM_ENTRY_TYPE_KEYED_ENTRY_IN_HIVE , sizeof ( BP_HASH_TABLE_KEYED_ENTRY ) , BPLN ) ; if ( ! new_entry ) return ERR_FAILURE ; BP_HASH_TABLE_INIT_SEM ( new_entry -> lock ) ; new_entry -> parent = ( void * ) table ; new_entry -> key = ht_strdup ( hreg , key ) ; switch ( data_type ) { case BP_HASH_TABLE_KEYED_ENTRY_TYPE_POINTER_EXTERNALLY_MANAGED : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_C_DATASTRUCTURE_EXTERNALLY_MANAGED : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_CPP_CLASS_POINTER_EXTERNALLY_MANAGED : new_entry -> data = ( void * ) data ; new_entry -> type = data_type ; break ; case BP_HASH_TABLE_KEYED_ENTRY_TYPE_NAMED_HASH_TABLE : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_BINARY_BLOB : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_UNICODE16_STRING : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_UNICODE32_STRING : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_SIZE_T : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_SIGNED_INT : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_JSON : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_MESSAGE_TO_WORKER : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_MESSAGE_FROM_WORKER : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_EVENT_UNPROCESSED : case BP_HASH_TABLE_KEYED_ENTRY_TYPE_EVENT_PROCESSED : new_entry -> data = ( void * ) BP_HashRegAddMemoryEntry ( hreg , BP_HASH_REG_MEM_ENTRY_TYPE_GENERIC_ALLOCATION , data_size + 1 , BPLN ) ; if ( ! new_entry -> data ) { printf ( " \n \n " ) ; printf ( " \n " ) ; printf ( " \n \n " ) ; exit ( 0 ) ; return ERR_FAILURE ; } if ( data ) memcpy ( new_entry -> data , data , data_size ) ; new_entry -> type = data_type ; new_entry -> data_size = data_size ; break ; default : printf ( " \n " ) ; return ERR_FAILURE ; break ; } HASH_ADD_KEYPTR ( hh , add_to_table , new_entry -> key , bpstrlen ( new_entry -> key ) , new_entry ) ; table -> data = add_to_table ; hreg -> total_data_entries_in_hive ++ ; return ERR_SUCCESS ; }
This links arbitrary data types into the table.
This links arbitrary data types into the table .
// macro to override hreg on null // esnure we have a table // ensure string is reasonable printable // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Determine Key Uniqueness %%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set the search table // check to see that hash doesn't exist // if an item was found with this key, exit // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Add Data To Table if Unique %%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // add to the table // allocate new entry // ensure entry was created before continuing // initialize the hash table semaphore // set the parent pointer to the current table // set key as dup key // switch on the passed in data type, behavior differs based on unmanaged or // managed types. // Externally managed data /* this type is unmanaged, meaning no free occurs to the data involved. */ // set data (null is ok) // Managed types are allocated and copied to the memory hash table // of the hash registry, and are deleted when the hash registry // is destroyed. // allocate space for data (in memory entries) // exit application immediately if we can't allocate any memory // only copy in data if the data pointer exists // set the entry type based on the passed in value (must be a valid type) // set datasize member // exit the case // if the datatype is unmanaged, exit immediately // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Add Data to Table and Exit %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // add to the table // rewrite the pointer location after lookup // increase the total number of tables in the hive // return indicating success