📄 ssl_util_table.c
字号:
} } if (key_size_p != NULL) *key_size_p = entry_p->te_key_size; if (data_buf_p != NULL) { if (entry_p->te_data_size == 0) *data_buf_p = NULL; else { /* * if we were storing it compacted, we now need to malloc some * space if the user wants the value after the delete. */ *data_buf_p = table_p->ta_malloc(entry_p->te_data_size); if (*data_buf_p == NULL) return TABLE_ERROR_ALLOC; if (table_p->ta_data_align == 0) data_copy_p = ENTRY_DATA_BUF(table_p, entry_p); else data_copy_p = entry_data_buf(table_p, entry_p); memcpy(*data_buf_p, data_copy_p, entry_p->te_data_size); } } if (data_size_p != NULL) *data_size_p = entry_p->te_data_size; table_p->ta_free(entry_p); table_p->ta_entry_n--; /* do we need auto-adjust down? */ if ((table_p->ta_flags & TABLE_FLAG_AUTO_ADJUST) && (table_p->ta_flags & TABLE_FLAG_ADJUST_DOWN) && SHOULD_TABLE_SHRINK(table_p)) return table_adjust(table_p, table_p->ta_entry_n); return TABLE_ERROR_NONE;}/* * int table_info * * DESCRIPTION: * * Get some information about a table_p structure. * * RETURNS: * * Success - TABLE_ERROR_NONE * * Failure - Table error code. * * ARGUMENTS: * * table_p - Table structure pointer from which we are getting * information. * * num_buckets_p - Pointer to an integer which, if not NULL, will * contain the number of buckets in the table. * * num_entries_p - Pointer to an integer which, if not NULL, will * contain the number of entries stored in the table. */int table_info(table_t * table_p, int *num_buckets_p, int *num_entries_p){ if (table_p == NULL) return TABLE_ERROR_ARG_NULL; if (table_p->ta_magic != TABLE_MAGIC) return TABLE_ERROR_PNT; if (num_buckets_p != NULL) *num_buckets_p = table_p->ta_bucket_n; if (num_entries_p != NULL) *num_entries_p = table_p->ta_entry_n; return TABLE_ERROR_NONE;}/* * int table_adjust * * DESCRIPTION: * * Set the number of buckets in a table to a certain value. * * RETURNS: * * Success - TABLE_ERROR_NONE * * Failure - Table error code. * * ARGUMENTS: * * table_p - Table structure pointer of which we are adjusting. * * bucket_n - Number buckets to adjust the table to. Set to 0 to * adjust the table to its number of entries. */int table_adjust(table_t * table_p, const int bucket_n){ table_entry_t *entry_p, *next_p; table_entry_t **buckets, **bucket_p, **bounds_p; int bucket; unsigned int buck_n; if (table_p == NULL) return TABLE_ERROR_ARG_NULL; if (table_p->ta_magic != TABLE_MAGIC) return TABLE_ERROR_PNT; /* * NOTE: we walk through the entries and rehash them. If we stored * the hash value as a full int in the table-entry, all we would * have to do is remod it. */ /* normalize to the number of entries */ if (bucket_n == 0) buck_n = table_p->ta_entry_n; else buck_n = bucket_n; /* we must have at least 1 bucket */ if (buck_n == 0) buck_n = 1; /* make sure we have somethign to do */ if (buck_n == table_p->ta_bucket_n) return TABLE_ERROR_NONE; /* allocate a new bucket list */ if ((buckets = (table_entry_t **) table_p->ta_calloc(buck_n, sizeof(table_entry_t *))) == NULL) return TABLE_ERROR_ALLOC; if (table_p->ta_buckets == NULL) return TABLE_ERROR_ALLOC; /* * run through each of the items in the current table and rehash * them into the newest bucket sizes */ bounds_p = table_p->ta_buckets + table_p->ta_bucket_n; for (bucket_p = table_p->ta_buckets; bucket_p < bounds_p; bucket_p++) { for (entry_p = *bucket_p; entry_p != NULL; entry_p = next_p) { /* hash the old data into the new table size */ bucket = hash(ENTRY_KEY_BUF(entry_p), entry_p->te_key_size, 0) % buck_n; /* record the next one now since we overwrite next below */ next_p = entry_p->te_next_p; /* insert into new list, no need to append */ entry_p->te_next_p = buckets[bucket]; buckets[bucket] = entry_p; /* * NOTE: we may want to adjust the bucket_c linear entry here to * keep it current */ } /* remove the old table pointers as we go by */ *bucket_p = NULL; } /* replace the table buckets with the new ones */ table_p->ta_free(table_p->ta_buckets); table_p->ta_buckets = buckets; table_p->ta_bucket_n = buck_n; return TABLE_ERROR_NONE;}/* * const char *table_strerror * * DESCRIPTION: * * Return the corresponding string for the error number. * * RETURNS: * * Success - String equivalient of the error. * * Failure - String "invalid error code" * * ARGUMENTS: * * error - Error number that we are converting. */const char *table_strerror(const int error){ error_str_t *err_p; for (err_p = errors; err_p->es_error != 0; err_p++) { if (err_p->es_error == error) return err_p->es_string; } return INVALID_ERROR;}/* * int table_type_size * * DESCRIPTION: * * Return the size of the internal table type. * * RETURNS: * * The size of the table_t type. * * ARGUMENTS: * * None. */int table_type_size(void){ return sizeof(table_t);}/************************* linear access routines ****************************//* * int table_first * * DESCRIPTION: * * Find first element in a table and pass back information about the * key/data pair. If any of the key/data pointers are NULL then they * are ignored. * * NOTE: This function is not reentrant. More than one thread cannot * be doing a first and next on the same table at the same time. Use * the table_first_r version below for this. * * RETURNS: * * Success - TABLE_ERROR_NONE * * Failure - Table error code. * * ARGUMENTS: * * table_p - Table structure pointer from which we are getting the * first element. * * key_buf_p - Pointer which, if not NULL, will be set to the address * of the storage of the first key that is allocated in the table. If * an (int) is stored as the first key (for example) then key_buf_p * should be (int **) i.e. the address of a (int *). * * key_size_p - Pointer to an integer which, if not NULL, will be set * to the size of the key that is stored in the table and that is * associated with the first key. * * data_buf_p - Pointer which, if not NULL, will be set to the address * of the data storage that is allocated in the table and that is * associated with the first key. If a (long) is stored as the data * (for example) then data_buf_p should be (long **) i.e. the address * of a (long *). * * data_size_p - Pointer to an integer which, if not NULL, will be set * to the size of the data that is stored in the table and that is * associated with the first key. */int table_first(table_t * table_p, void **key_buf_p, int *key_size_p, void **data_buf_p, int *data_size_p){ table_entry_t *entry_p; if (table_p == NULL) return TABLE_ERROR_ARG_NULL; if (table_p->ta_magic != TABLE_MAGIC) return TABLE_ERROR_PNT; /* initialize our linear magic number */ table_p->ta_linear.tl_magic = LINEAR_MAGIC; entry_p = first_entry(table_p, &table_p->ta_linear); if (entry_p == NULL) return TABLE_ERROR_NOT_FOUND; if (key_buf_p != NULL) *key_buf_p = ENTRY_KEY_BUF(entry_p); if (key_size_p != NULL) *key_size_p = entry_p->te_key_size; if (data_buf_p != NULL) { if (entry_p->te_data_size == 0) *data_buf_p = NULL; else { if (table_p->ta_data_align == 0) *data_buf_p = ENTRY_DATA_BUF(table_p, entry_p); else *data_buf_p = entry_data_buf(table_p, entry_p); } } if (data_size_p != NULL) *data_size_p = entry_p->te_data_size; return TABLE_ERROR_NONE;}/* * int table_next * * DESCRIPTION: * * Find the next element in a table and pass back information about * the key/data pair. If any of the key/data pointers are NULL then * they are ignored. * * NOTE: This function is not reentrant. More than one thread cannot * be doing a first and next on the same table at the same time. Use * the table_next_r version below for this. * * RETURNS: * * Success - TABLE_ERROR_NONE * * Failure - Table error code. * * ARGUMENTS: * * table_p - Table structure pointer from which we are getting the * next element. * * key_buf_p - Pointer which, if not NULL, will be set to the address * of the storage of the next key that is allocated in the table. If * an (int) is stored as the next key (for example) then key_buf_p * should be (int **) i.e. the address of a (int *). * * key_size_p - Pointer to an integer which, if not NULL, will be set * to the size of the key that is stored in the table and that is * associated with the next key. * * data_buf_p - Pointer which, if not NULL, will be set to the address * of the data storage that is allocated in the table and that is * associated with the next key. If a (long) is stored as the data * (for example) then data_buf_p should be (long **) i.e. the address * of a (long *). * * data_size_p - Pointer to an integer which, if not NULL, will be set * to the size of the data that is stored in the table and that is * associated with the next key. */int table_next(table_t * table_p, void **key_buf_p, int *key_size_p, void **data_buf_p, int *data_size_p){ table_entry_t *entry_p; int error; if (table_p == NULL) return TABLE_ERROR_ARG_NULL; if (table_p->ta_magic != TABLE_MAGIC) return TABLE_ERROR_PNT; if (table_p->ta_linear.tl_magic != LINEAR_MAGIC) return TABLE_ERROR_LINEAR; /* move to the next entry */ entry_p = next_entry(table_p, &table_p->ta_linear, &error); if (entry_p == NULL) return error; if (key_buf_p != NULL) *key_buf_p = ENTRY_KEY_BUF(entry_p); if (key_size_p != NULL) *key_size_p = entry_p->te_key_size; if (data_buf_p != NULL) { if (entry_p->te_data_size == 0) *data_buf_p = NULL; else { if (table_p->ta_data_align == 0) *data_buf_p = ENTRY_DATA_BUF(table_p, entry_p); else *data_buf_p = entry_data_buf(table_p, entry_p); } } if (data_size_p != NULL) *data_size_p = entry_p->te_data_size; return TABLE_ERROR_NONE;}/* * int table_this * * DESCRIPTION: * * Find the current element in a table and pass back information about * the key/data pair. If any of the key/data pointers are NULL then * they are ignored. * * NOTE: This function is not reentrant. Use the table_current_r * version below. * * RETURNS: * * Success - TABLE_ERROR_NONE * * Failure - Table error code. * * ARGUMENTS: * * table_p - Table structure pointer from which we are getting the * current element. * * key_buf_p - Pointer which, if not NULL, will be set to the address * of the storage of the current key that is allocated in the table. * If an (int) is stored as the current key (for example) then * key_buf_p should be (int **) i.e. the address of a (int *). * * key_size_p - Pointer to an integer which, if not NULL, will be set * to the size of the key that is stored in the table and that is * associated with the current key. * * data_buf_p - Pointer which, if not NULL, will be set to the address * of the data storage that is allocated in the table and that is * associated with the current key. If a (long) is stored as the data * (for example) then data_buf_p should be (long **) i.e. the address * of a (long *). * * data_size_p - Pointer to an
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -