Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
astobj2_hash.c File Reference

Hash table functions implementing astobj2 containers. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/astobj2.h"
#include "astobj2_private.h"
#include "astobj2_container_private.h"
#include "asterisk/dlinkedlists.h"
#include "asterisk/utils.h"
Include dependency graph for astobj2_hash.c:

Go to the source code of this file.

Data Structures

struct  ao2_container_hash
 
struct  hash_bucket
 
struct  hash_bucket_node
 
struct  hash_traversal_state
 
struct  hash_traversal_state_check
 

Functions

struct ao2_container__ao2_container_alloc_hash (unsigned int ao2_options, unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn, const char *tag, const char *file, int line, const char *func)
 
struct ao2_container__ao2_container_alloc_list (unsigned int ao2_options, unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn, const char *tag, const char *file, int line, const char *func)
 
static struct ao2_containerhash_ao2_alloc_empty_clone (struct ao2_container_hash *self, const char *tag, const char *file, int line, const char *func)
 
static struct ao2_containerhash_ao2_container_init (struct ao2_container_hash *self, unsigned int options, unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn)
 Initialize a hash container with the desired number of buckets. More...
 
static void hash_ao2_destroy (struct ao2_container_hash *self)
 
static struct hash_bucket_nodehash_ao2_find_first (struct ao2_container_hash *self, enum search_flags flags, void *arg, struct hash_traversal_state *state)
 
static struct hash_bucket_nodehash_ao2_find_next (struct ao2_container_hash *self, struct hash_traversal_state *state, struct hash_bucket_node *prev)
 
static enum ao2_container_insert hash_ao2_insert_node (struct ao2_container_hash *self, struct hash_bucket_node *node)
 
static struct hash_bucket_nodehash_ao2_iterator_next (struct ao2_container_hash *self, struct hash_bucket_node *node, enum ao2_iterator_flags flags)
 
static struct hash_bucket_nodehash_ao2_new_node (struct ao2_container_hash *self, void *obj_new, const char *tag, const char *file, int line, const char *func)
 
static void hash_ao2_node_destructor (void *v_doomed)
 
static int hash_zero (const void *user_obj, const int flags)
 always zero hash function More...
 

Variables

static const struct ao2_container_methods v_table_hash
 

Detailed Description

Hash table functions implementing astobj2 containers.

Author
Richard Mudgett rmudg[email protected][email protected][email protected]digiu[email protected]m.co[email protected]m

Definition in file astobj2_hash.c.

Function Documentation

◆ __ao2_container_alloc_hash()

struct ao2_container* __ao2_container_alloc_hash ( unsigned int  ao2_options,
unsigned int  container_options,
unsigned int  n_buckets,
ao2_hash_fn hash_fn,
ao2_sort_fn sort_fn,
ao2_callback_fn cmp_fn,
const char *  tag,
const char *  file,
int  line,
const char *  func 
)

Definition at line 1079 of file astobj2_hash.c.

References __ao2_alloc(), container_destruct(), hash_ao2_container_init(), and ao2_container_hash::n_buckets.

Referenced by __ao2_container_alloc_list(), and hash_ao2_alloc_empty_clone().

1083 {
1084  unsigned int num_buckets;
1085  size_t container_size;
1086  struct ao2_container_hash *self;
1087 
1088  num_buckets = hash_fn ? n_buckets : 1;
1089  container_size = sizeof(struct ao2_container_hash) + num_buckets * sizeof(struct hash_bucket);
1090 
1091  self = __ao2_alloc(container_size, container_destruct, ao2_options,
1092  tag ?: __PRETTY_FUNCTION__, file, line, func);
1093  return hash_ao2_container_init(self, container_options, num_buckets, hash_fn,
1094  sort_fn, cmp_fn);
1095 }
void container_destruct(void *_c)
void * __ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result
Definition: astobj2.c:765
static struct ao2_container * hash_ao2_container_init(struct ao2_container_hash *self, unsigned int options, unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn)
Initialize a hash container with the desired number of buckets.
ao2_hash_fn * hash_fn
Definition: astobj2_hash.c:71

◆ __ao2_container_alloc_list()

struct ao2_container* __ao2_container_alloc_list ( unsigned int  ao2_options,
unsigned int  container_options,
ao2_sort_fn sort_fn,
ao2_callback_fn cmp_fn,
const char *  tag,
const char *  file,
int  line,
const char *  func 
)

Definition at line 1097 of file astobj2_hash.c.

References __ao2_container_alloc_hash(), and NULL.

1100 {
1101  return __ao2_container_alloc_hash(ao2_options, container_options, 1, NULL,
1102  sort_fn, cmp_fn, tag, file, line, func);
1103 }
struct ao2_container * __ao2_container_alloc_hash(unsigned int ao2_options, unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn, const char *tag, const char *file, int line, const char *func)
#define NULL
Definition: resample.c:96

◆ hash_ao2_alloc_empty_clone()

static struct ao2_container* hash_ao2_alloc_empty_clone ( struct ao2_container_hash self,
const char *  tag,
const char *  file,
int  line,
const char *  func 
)
static

Definition at line 116 of file astobj2_hash.c.

References __ao2_container_alloc_hash(), __is_ao2_object, ao2_options_get(), and NULL.

118 {
119  if (!__is_ao2_object(self, file, line, func)) {
120  return NULL;
121  }
122 
124  self->n_buckets, self->hash_fn, self->common.sort_fn, self->common.cmp_fn,
125  tag, file, line, func);
126 }
struct ao2_container common
Items common to all containers.
Definition: astobj2_hash.c:70
#define __is_ao2_object(user_data, file, line, func)
struct ao2_container * __ao2_container_alloc_hash(unsigned int ao2_options, unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn, const char *tag, const char *file, int line, const char *func)
#define NULL
Definition: resample.c:96
ao2_hash_fn * hash_fn
Definition: astobj2_hash.c:71
unsigned int ao2_options_get(void *obj)
Retrieve the ao2 options used to create the object.
Definition: astobj2.c:778
ao2_callback_fn * cmp_fn

◆ hash_ao2_container_init()

static struct ao2_container* hash_ao2_container_init ( struct ao2_container_hash self,
unsigned int  options,
unsigned int  n_buckets,
ao2_hash_fn hash_fn,
ao2_sort_fn sort_fn,
ao2_callback_fn cmp_fn 
)
static

Initialize a hash container with the desired number of buckets.

Parameters
selfContainer to initialize.
optionsContainer behaviour options (See enum ao2_container_opts)
n_bucketsNumber of buckets for hash
hash_fnPointer to a function computing a hash value.
sort_fnPointer to a sort function.
cmp_fnPointer to a compare function used by ao2_find.
Returns
A pointer to a struct container.

Definition at line 1057 of file astobj2_hash.c.

References ast_atomic_fetchadd_int(), ao2_container::cmp_fn, hash_zero(), NULL, options, ao2_container::sort_fn, and v_table_hash.

Referenced by __ao2_container_alloc_hash().

1060 {
1061  if (!self) {
1062  return NULL;
1063  }
1064 
1065  self->common.v_table = &v_table_hash;
1066  self->common.sort_fn = sort_fn;
1067  self->common.cmp_fn = cmp_fn;
1068  self->common.options = options;
1069  self->hash_fn = hash_fn ? hash_fn : hash_zero;
1070  self->n_buckets = n_buckets;
1071 
1072 #ifdef AO2_DEBUG
1073  ast_atomic_fetchadd_int(&ao2.total_containers, 1);
1074 #endif /* defined(AO2_DEBUG) */
1075 
1076  return (struct ao2_container *) self;
1077 }
#define NULL
Definition: resample.c:96
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:755
static int hash_zero(const void *user_obj, const int flags)
always zero hash function
static const struct ao2_container_methods v_table_hash
Generic container type.
static struct test_options options

◆ hash_ao2_destroy()

static void hash_ao2_destroy ( struct ao2_container_hash self)
static

Definition at line 739 of file astobj2_hash.c.

References abs, ao2_container_count(), ast_assert, AST_DLLIST_EMPTY, AST_DLLIST_FIRST, AST_DLLIST_LAST, AST_DLLIST_NEXT, AST_DLLIST_PREV, ast_log, hash_bucket_node::common, FORMAT, FORMAT2, hash_bucket_node::links, LOG_ERROR, hash_bucket_node::my_bucket, NULL, ao2_container_node::obj, and OBJ_SEARCH_OBJECT.

740 {
741  int idx;
742 
743  /* Check that the container no longer has any nodes */
744  for (idx = self->n_buckets; idx--;) {
745  if (!AST_DLLIST_EMPTY(&self->buckets[idx].list)) {
746  ast_log(LOG_ERROR, "Node ref leak. Hash container still has nodes!\n");
747  ast_assert(0);
748  break;
749  }
750  }
751 }
#define ast_assert(a)
Definition: utils.h:695
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
#define AST_DLLIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: dlinkedlists.h:468
struct hash_bucket::@341 list
struct hash_bucket buckets[0]
Definition: astobj2_hash.c:75

◆ hash_ao2_find_first()

static struct hash_bucket_node* hash_ao2_find_first ( struct ao2_container_hash self,
enum search_flags  flags,
void *  arg,
struct hash_traversal_state state 
)
static

Definition at line 335 of file astobj2_hash.c.

References abs, ao2_ref, hash_traversal_state::arg, AST_DLLIST_FIRST, AST_DLLIST_LAST, AST_DLLIST_NEXT, AST_DLLIST_PREV, hash_traversal_state::bucket_last, hash_traversal_state::bucket_start, hash_bucket_node::common, hash_traversal_state::descending, hash_traversal_state::flags, hash_bucket_node::links, NULL, ao2_container_node::obj, OBJ_ORDER_ASCENDING, OBJ_ORDER_DESCENDING, OBJ_ORDER_MASK, OBJ_ORDER_POST, OBJ_ORDER_PRE, OBJ_SEARCH_KEY, OBJ_SEARCH_MASK, OBJ_SEARCH_OBJECT, OBJ_SEARCH_PARTIAL_KEY, and hash_traversal_state::sort_fn.

336 {
337  struct hash_bucket_node *node;
338  int bucket_cur;
339  int cmp;
340 
341  memset(state, 0, sizeof(*state));
342  state->arg = arg;
343  state->flags = flags;
344 
345  /* Determine traversal order. */
346  switch (flags & OBJ_ORDER_MASK) {
347  case OBJ_ORDER_POST:
349  state->descending = 1;
350  break;
351  case OBJ_ORDER_PRE:
352  case OBJ_ORDER_ASCENDING:
353  default:
354  break;
355  }
356 
357  /*
358  * If lookup by pointer or search key, run the hash and optional
359  * sort functions. Otherwise, traverse the whole container.
360  */
361  switch (flags & OBJ_SEARCH_MASK) {
362  case OBJ_SEARCH_OBJECT:
363  case OBJ_SEARCH_KEY:
364  /* we know hash can handle this case */
365  bucket_cur = abs(self->hash_fn(arg, flags & OBJ_SEARCH_MASK)
366  % self->n_buckets);
367  state->sort_fn = self->common.sort_fn;
368  break;
370  /* scan all buckets for partial key matches */
371  bucket_cur = -1;
372  state->sort_fn = self->common.sort_fn;
373  break;
374  default:
375  /* don't know, let's scan all buckets */
376  bucket_cur = -1;
377  state->sort_fn = NULL;
378  break;
379  }
380 
381  if (state->descending) {
382  /*
383  * Determine the search boundaries of a descending traversal.
384  *
385  * bucket_cur downto state->bucket_last
386  */
387  if (bucket_cur < 0) {
388  bucket_cur = self->n_buckets - 1;
389  state->bucket_last = 0;
390  } else {
391  state->bucket_last = bucket_cur;
392  }
393  state->bucket_start = bucket_cur;
394 
395  /* For each bucket */
396  for (; state->bucket_last <= bucket_cur; --bucket_cur) {
397  /* For each node in the bucket. */
398  for (node = AST_DLLIST_LAST(&self->buckets[bucket_cur].list);
399  node;
400  node = AST_DLLIST_PREV(node, links)) {
401  if (!node->common.obj) {
402  /* Node is empty */
403  continue;
404  }
405 
406  if (state->sort_fn) {
407  /* Filter node through the sort_fn */
408  cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK);
409  if (cmp > 0) {
410  continue;
411  }
412  if (cmp < 0) {
413  /* No more nodes in this bucket are possible to match. */
414  break;
415  }
416  }
417 
418  /* We have the first traversal node */
419  ao2_ref(node, +1);
420  return node;
421  }
422  }
423  } else {
424  /*
425  * Determine the search boundaries of an ascending traversal.
426  *
427  * bucket_cur to state->bucket_last-1
428  */
429  if (bucket_cur < 0) {
430  bucket_cur = 0;
431  state->bucket_last = self->n_buckets;
432  } else {
433  state->bucket_last = bucket_cur + 1;
434  }
435  state->bucket_start = bucket_cur;
436 
437  /* For each bucket */
438  for (; bucket_cur < state->bucket_last; ++bucket_cur) {
439  /* For each node in the bucket. */
440  for (node = AST_DLLIST_FIRST(&self->buckets[bucket_cur].list);
441  node;
442  node = AST_DLLIST_NEXT(node, links)) {
443  if (!node->common.obj) {
444  /* Node is empty */
445  continue;
446  }
447 
448  if (state->sort_fn) {
449  /* Filter node through the sort_fn */
450  cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK);
451  if (cmp < 0) {
452  continue;
453  }
454  if (cmp > 0) {
455  /* No more nodes in this bucket are possible to match. */
456  break;
457  }
458  }
459 
460  /* We have the first traversal node */
461  ao2_ref(node, +1);
462  return node;
463  }
464  }
465  }
466 
467  return NULL;
468 }
Definition: test_heap.c:38
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
#define NULL
Definition: resample.c:96
Traverse in ascending order (First to last container object)
Definition: astobj2.h:1125
The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
Definition: astobj2.h:1120
#define AST_DLLIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: dlinkedlists.h:445
#define ao2_ref(o, delta)
Definition: astobj2.h:464
Traverse in descending order (Last to first container object)
Definition: astobj2.h:1127
ao2_sort_fn * sort_fn
Definition: astobj2_hash.c:81
unsigned int descending
Definition: astobj2_hash.c:91
struct ao2_container_node common
Items common to all container nodes.
Definition: astobj2_hash.c:42
struct hash_bucket_node::@340 links
Traverse in pre-order (Node then children, for tree container)
Definition: astobj2.h:1136
enum search_flags flags
Definition: astobj2_hash.c:89
ao2_hash_fn * hash_fn
Definition: astobj2_hash.c:71
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
struct hash_bucket::@341 list
#define abs(x)
Definition: f2c.h:195
Traverse in post-order (Children then node, for tree container)
Definition: astobj2.h:1145
#define AST_DLLIST_PREV(elm, field)
Returns the previous entry in the list before the given entry.
Definition: dlinkedlists.h:456
Search option field mask.
Definition: astobj2.h:1076
struct hash_bucket buckets[0]
Definition: astobj2_hash.c:75
#define AST_DLLIST_FIRST(head)
Returns the first entry contained in a list.
Definition: dlinkedlists.h:421
Traverse order option field mask.
Definition: astobj2.h:1123
#define AST_DLLIST_LAST(head)
Returns the last entry contained in a list.
Definition: dlinkedlists.h:430

◆ hash_ao2_find_next()

static struct hash_bucket_node* hash_ao2_find_next ( struct ao2_container_hash self,
struct hash_traversal_state state,
struct hash_bucket_node prev 
)
static

Definition at line 483 of file astobj2_hash.c.

References ao2_ref, hash_traversal_state::arg, AST_DLLIST_FIRST, AST_DLLIST_LAST, AST_DLLIST_NEXT, AST_DLLIST_PREV, hash_traversal_state::bucket_last, hash_bucket_node::common, hash_traversal_state::descending, hash_traversal_state::flags, hash_bucket_node::links, hash_bucket_node::my_bucket, NULL, ao2_container_node::obj, OBJ_SEARCH_MASK, and hash_traversal_state::sort_fn.

484 {
485  struct hash_bucket_node *node;
486  void *arg;
487  enum search_flags flags;
488  int bucket_cur;
489  int cmp;
490 
491  arg = state->arg;
492  flags = state->flags;
493  bucket_cur = prev->my_bucket;
494  node = prev;
495 
496  /*
497  * This function is structured the same as hash_ao2_find_first()
498  * intentionally. We are resuming the search loops from
499  * hash_ao2_find_first() in order to find the next node. The
500  * search loops must be resumed where hash_ao2_find_first()
501  * returned with the first node.
502  */
503  if (state->descending) {
504  goto hash_descending_resume;
505 
506  /* For each bucket */
507  for (; state->bucket_last <= bucket_cur; --bucket_cur) {
508  /* For each node in the bucket. */
509  for (node = AST_DLLIST_LAST(&self->buckets[bucket_cur].list);
510  node;
511  node = AST_DLLIST_PREV(node, links)) {
512  if (!node->common.obj) {
513  /* Node is empty */
514  continue;
515  }
516 
517  if (state->sort_fn) {
518  /* Filter node through the sort_fn */
519  cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK);
520  if (cmp > 0) {
521  continue;
522  }
523  if (cmp < 0) {
524  /* No more nodes in this bucket are possible to match. */
525  break;
526  }
527  }
528 
529  /* We have the next traversal node */
530  ao2_ref(node, +1);
531 
532  /*
533  * Dereferencing the prev node may result in our next node
534  * object being removed by another thread. This could happen if
535  * the container uses RW locks and the container was read
536  * locked.
537  */
538  ao2_ref(prev, -1);
539  if (node->common.obj) {
540  return node;
541  }
542  prev = node;
543 
544 hash_descending_resume:;
545  }
546  }
547  } else {
548  goto hash_ascending_resume;
549 
550  /* For each bucket */
551  for (; bucket_cur < state->bucket_last; ++bucket_cur) {
552  /* For each node in the bucket. */
553  for (node = AST_DLLIST_FIRST(&self->buckets[bucket_cur].list);
554  node;
555  node = AST_DLLIST_NEXT(node, links)) {
556  if (!node->common.obj) {
557  /* Node is empty */
558  continue;
559  }
560 
561  if (state->sort_fn) {
562  /* Filter node through the sort_fn */
563  cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK);
564  if (cmp < 0) {
565  continue;
566  }
567  if (cmp > 0) {
568  /* No more nodes in this bucket are possible to match. */
569  break;
570  }
571  }
572 
573  /* We have the next traversal node */
574  ao2_ref(node, +1);
575 
576  /*
577  * Dereferencing the prev node may result in our next node
578  * object being removed by another thread. This could happen if
579  * the container uses RW locks and the container was read
580  * locked.
581  */
582  ao2_ref(prev, -1);
583  if (node->common.obj) {
584  return node;
585  }
586  prev = node;
587 
588 hash_ascending_resume:;
589  }
590  }
591  }
592 
593  /* No more nodes in the container left to traverse. */
594  ao2_ref(prev, -1);
595  return NULL;
596 }
Definition: test_heap.c:38
search_flags
Flags passed to ao2_callback_fn(), ao2_hash_fn(), and ao2_sort_fn() to modify behaviour.
Definition: astobj2.h:1038
#define NULL
Definition: resample.c:96
#define AST_DLLIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: dlinkedlists.h:445
#define ao2_ref(o, delta)
Definition: astobj2.h:464
ao2_sort_fn * sort_fn
Definition: astobj2_hash.c:81
unsigned int descending
Definition: astobj2_hash.c:91
struct ao2_container_node common
Items common to all container nodes.
Definition: astobj2_hash.c:42
struct hash_bucket_node::@340 links
enum search_flags flags
Definition: astobj2_hash.c:89
struct hash_bucket::@341 list
#define AST_DLLIST_PREV(elm, field)
Returns the previous entry in the list before the given entry.
Definition: dlinkedlists.h:456
Search option field mask.
Definition: astobj2.h:1076
struct hash_bucket buckets[0]
Definition: astobj2_hash.c:75
#define AST_DLLIST_FIRST(head)
Returns the first entry contained in a list.
Definition: dlinkedlists.h:421
#define AST_DLLIST_LAST(head)
Returns the last entry contained in a list.
Definition: dlinkedlists.h:430

◆ hash_ao2_insert_node()

static enum ao2_container_insert hash_ao2_insert_node ( struct ao2_container_hash self,
struct hash_bucket_node node 
)
static

Definition at line 239 of file astobj2_hash.c.

References AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW, AO2_CONTAINER_ALLOC_OPT_DUPS_MASK, AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT, AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN, AO2_CONTAINER_INSERT_NODE_INSERTED, AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED, AO2_CONTAINER_INSERT_NODE_REJECTED, ao2_ref, AST_DLLIST_INSERT_AFTER_CURRENT, AST_DLLIST_INSERT_BEFORE_CURRENT, AST_DLLIST_INSERT_HEAD, AST_DLLIST_INSERT_TAIL, AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN, AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END, AST_DLLIST_TRAVERSE_SAFE_BEGIN, AST_DLLIST_TRAVERSE_SAFE_END, hash_bucket_node::common, hash_bucket_node::links, hash_bucket::list, hash_bucket_node::my_bucket, ao2_container_node::obj, OBJ_SEARCH_OBJECT, options, and SWAP.

241 {
242  int cmp;
243  struct hash_bucket *bucket;
244  struct hash_bucket_node *cur;
245  ao2_sort_fn *sort_fn;
246  uint32_t options;
247 
248  bucket = &self->buckets[node->my_bucket];
249  sort_fn = self->common.sort_fn;
250  options = self->common.options;
251 
252  if (options & AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN) {
253  if (sort_fn) {
255  cmp = sort_fn(cur->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
256  if (cmp > 0) {
257  continue;
258  }
259  if (cmp < 0) {
262  }
263  switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
264  default:
266  break;
268  /* Reject all objects with the same key. */
271  if (cur->common.obj == node->common.obj) {
272  /* Reject inserting the same object */
274  }
275  break;
277  SWAP(cur->common.obj, node->common.obj);
278  ao2_ref(node, -1);
280  }
281  }
283  }
284  AST_DLLIST_INSERT_HEAD(&bucket->list, node, links);
285  } else {
286  if (sort_fn) {
287  AST_DLLIST_TRAVERSE_SAFE_BEGIN(&bucket->list, cur, links) {
288  cmp = sort_fn(cur->common.obj, node->common.obj, OBJ_SEARCH_OBJECT);
289  if (cmp < 0) {
290  continue;
291  }
292  if (cmp > 0) {
295  }
296  switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
297  default:
299  break;
301  /* Reject all objects with the same key. */
304  if (cur->common.obj == node->common.obj) {
305  /* Reject inserting the same object */
307  }
308  break;
310  SWAP(cur->common.obj, node->common.obj);
311  ao2_ref(node, -1);
313  }
314  }
316  }
317  AST_DLLIST_INSERT_TAIL(&bucket->list, node, links);
318  }
320 }
#define AST_DLLIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: dlinkedlists.h:848
#define AST_DLLIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Allow objects with duplicate keys in container.
Definition: astobj2.h:1185
int() ao2_sort_fn(const void *obj_left, const void *obj_right, int flags)
Type of generic container sort function.
Definition: astobj2.h:1280
#define AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: dlinkedlists.h:887
#define SWAP(a, b)
Definition: utils.h:230
#define AST_DLLIST_INSERT_AFTER_CURRENT(elm, field)
Inserts a list node after the current node during a traversal.
Definition: dlinkedlists.h:722
Insert objects at the beginning of the container. (Otherwise it is the opposite; insert at the end...
Definition: astobj2.h:1176
#define AST_DLLIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
The ao2 container objects with duplicate keys option field mask.
Definition: astobj2.h:1181
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define AST_DLLIST_INSERT_BEFORE_CURRENT(elm, field)
Inserts a list node before the current node during a traversal.
Definition: dlinkedlists.h:695
struct ao2_container_node common
Items common to all container nodes.
Definition: astobj2_hash.c:42
struct hash_bucket_node::@340 links
#define AST_DLLIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: dlinkedlists.h:912
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
Replace objects with duplicate keys in container.
Definition: astobj2.h:1215
#define AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END
Closes a safe loop traversal block.
Definition: dlinkedlists.h:920
struct hash_bucket::@341 list
Reject objects with duplicate keys in container.
Definition: astobj2.h:1192
static struct test_options options
Reject duplicate objects in container.
Definition: astobj2.h:1205

◆ hash_ao2_iterator_next()

static struct hash_bucket_node* hash_ao2_iterator_next ( struct ao2_container_hash self,
struct hash_bucket_node node,
enum ao2_iterator_flags  flags 
)
static

Definition at line 613 of file astobj2_hash.c.

References AO2_ITERATOR_DESCENDING, AST_DLLIST_FIRST, AST_DLLIST_LAST, AST_DLLIST_NEXT, AST_DLLIST_PREV, hash_bucket_node::common, hash_bucket_node::links, hash_bucket_node::my_bucket, NULL, and ao2_container_node::obj.

614 {
615  int cur_bucket;
616 
617  if (flags & AO2_ITERATOR_DESCENDING) {
618  if (node) {
619  cur_bucket = node->my_bucket;
620 
621  /* Find next non-empty node. */
622  for (;;) {
623  node = AST_DLLIST_PREV(node, links);
624  if (!node) {
625  break;
626  }
627  if (node->common.obj) {
628  /* Found a non-empty node. */
629  return node;
630  }
631  }
632  } else {
633  /* Find first non-empty node. */
634  cur_bucket = self->n_buckets;
635  }
636 
637  /* Find a non-empty node in the remaining buckets */
638  while (0 <= --cur_bucket) {
639  node = AST_DLLIST_LAST(&self->buckets[cur_bucket].list);
640  while (node) {
641  if (node->common.obj) {
642  /* Found a non-empty node. */
643  return node;
644  }
645  node = AST_DLLIST_PREV(node, links);
646  }
647  }
648  } else {
649  if (node) {
650  cur_bucket = node->my_bucket;
651 
652  /* Find next non-empty node. */
653  for (;;) {
654  node = AST_DLLIST_NEXT(node, links);
655  if (!node) {
656  break;
657  }
658  if (node->common.obj) {
659  /* Found a non-empty node. */
660  return node;
661  }
662  }
663  } else {
664  /* Find first non-empty node. */
665  cur_bucket = -1;
666  }
667 
668  /* Find a non-empty node in the remaining buckets */
669  while (++cur_bucket < self->n_buckets) {
670  node = AST_DLLIST_FIRST(&self->buckets[cur_bucket].list);
671  while (node) {
672  if (node->common.obj) {
673  /* Found a non-empty node. */
674  return node;
675  }
676  node = AST_DLLIST_NEXT(node, links);
677  }
678  }
679  }
680 
681  /* No more nodes to visit in the container. */
682  return NULL;
683 }
#define NULL
Definition: resample.c:96
#define AST_DLLIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: dlinkedlists.h:445
struct ao2_container_node common
Items common to all container nodes.
Definition: astobj2_hash.c:42
struct hash_bucket_node::@340 links
struct hash_bucket::@341 list
#define AST_DLLIST_PREV(elm, field)
Returns the previous entry in the list before the given entry.
Definition: dlinkedlists.h:456
struct hash_bucket buckets[0]
Definition: astobj2_hash.c:75
#define AST_DLLIST_FIRST(head)
Returns the first entry contained in a list.
Definition: dlinkedlists.h:421
#define AST_DLLIST_LAST(head)
Returns the last entry contained in a list.
Definition: dlinkedlists.h:430

◆ hash_ao2_new_node()

static struct hash_bucket_node* hash_ao2_new_node ( struct ao2_container_hash self,
void *  obj_new,
const char *  tag,
const char *  file,
int  line,
const char *  func 
)
static

Definition at line 208 of file astobj2_hash.c.

References __ao2_ref(), abs, AO2_ALLOC_OPT_LOCK_NOLOCK, AO2_ALLOC_OPT_NO_REF_DEBUG, ao2_alloc_options, hash_bucket_node::common, hash_ao2_node_destructor(), hash_bucket_node::my_bucket, ao2_container_node::my_container, NULL, ao2_container_node::obj, and OBJ_SEARCH_OBJECT.

209 {
210  struct hash_bucket_node *node;
211  int i;
212 
213  node = ao2_alloc_options(sizeof(*node), hash_ao2_node_destructor,
215  if (!node) {
216  return NULL;
217  }
218 
219  i = abs(self->hash_fn(obj_new, OBJ_SEARCH_OBJECT) % self->n_buckets);
220 
221  __ao2_ref(obj_new, +1, tag ?: "Container node creation", file, line, func);
222  node->common.obj = obj_new;
223  node->common.my_container = (struct ao2_container *) self;
224  node->my_bucket = i;
225 
226  return node;
227 }
Definition: test_heap.c:38
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
static void hash_ao2_node_destructor(void *v_doomed)
Definition: astobj2_hash.c:145
#define NULL
Definition: resample.c:96
int __ao2_ref(void *o, int delta, const char *tag, const char *file, int line, const char *func)
Definition: astobj2.c:498
struct ao2_container_node common
Items common to all container nodes.
Definition: astobj2_hash.c:42
ao2_hash_fn * hash_fn
Definition: astobj2_hash.c:71
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
#define abs(x)
Definition: f2c.h:195
Generic container type.
struct ao2_container * my_container

◆ hash_ao2_node_destructor()

static void hash_ao2_node_destructor ( void *  v_doomed)
static

Definition at line 145 of file astobj2_hash.c.

References __adjust_lock(), __container_unlink_node, ao2_container_check(), AO2_DEVMODE_STAT, AO2_LOCK_REQ_WRLOCK, AO2_UNLINK_NODE_UNLINK_OBJECT, AST_DEVMODE, AST_DLLIST_REMOVE, ast_log, ao2_container_hash::buckets, ao2_container_hash::common, hash_bucket_node::common, ao2_container::destroying, is_ao2_object, ao2_container_node::is_linked, hash_bucket_node::links, hash_bucket::list, LOG_ERROR, hash_bucket_node::my_bucket, ao2_container_node::my_container, ao2_container_node::obj, and OBJ_NOLOCK.

Referenced by hash_ao2_new_node().

146 {
147  struct hash_bucket_node *doomed = v_doomed;
148 
149  if (doomed->common.is_linked) {
150  struct ao2_container_hash *my_container;
151  struct hash_bucket *bucket;
152 
153  /*
154  * Promote to write lock if not already there. Since
155  * adjust_lock() can potentially release and block waiting for a
156  * write lock, care must be taken to ensure that node references
157  * are released before releasing the container references.
158  *
159  * Node references held by an iterator can only be held while
160  * the iterator also holds a reference to the container. These
161  * node references must be unreferenced before the container can
162  * be unreferenced to ensure that the node will not get a
163  * negative reference and the destructor called twice for the
164  * same node.
165  */
166  my_container = (struct ao2_container_hash *) doomed->common.my_container;
167 #ifdef AST_DEVMODE
168  is_ao2_object(my_container);
169 #endif
170 
171  __adjust_lock(my_container, AO2_LOCK_REQ_WRLOCK, 1);
172 
173 #if defined(AO2_DEBUG)
174  if (!my_container->common.destroying
176  ast_log(LOG_ERROR, "Container integrity failed before node deletion.\n");
177  }
178 #endif /* defined(AO2_DEBUG) */
179  bucket = &my_container->buckets[doomed->my_bucket];
180  AST_DLLIST_REMOVE(&bucket->list, doomed, links);
181  AO2_DEVMODE_STAT(--my_container->common.nodes);
182  }
183 
184  /*
185  * We could have an object in the node if the container is being
186  * destroyed or the node had not been linked in yet.
187  */
188  if (doomed->common.obj) {
190  }
191 }
struct ao2_container common
Items common to all containers.
Definition: astobj2_hash.c:70
#define __container_unlink_node(node, flags)
#define AST_DEVMODE
Definition: buildopts.h:6
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
#define is_ao2_object(user_data)
int ao2_container_check(struct ao2_container *self, enum search_flags flags)
Perform an integrity check on the specified container.
#define ast_log
Definition: astobj2.c:42
#define AO2_DEVMODE_STAT(stat)
struct ao2_container_node common
Items common to all container nodes.
Definition: astobj2_hash.c:42
#define LOG_ERROR
Definition: logger.h:285
unsigned int destroying
TRUE if the container is being destroyed.
struct hash_bucket::@341 list
enum ao2_lock_req __adjust_lock(void *user_data, enum ao2_lock_req lock_how, int keep_stronger)
Definition: astobj2.c:425
#define AST_DLLIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
struct ao2_container * my_container
struct hash_bucket buckets[0]
Definition: astobj2_hash.c:75

◆ hash_zero()

static int hash_zero ( const void *  user_obj,
const int  flags 
)
static

always zero hash function

it is convenient to have a hash function that always returns 0. This is basically used when we want to have a container that is a simple linked list.

Returns
0

Definition at line 1040 of file astobj2_hash.c.

Referenced by hash_ao2_container_init().

1041 {
1042  return 0;
1043 }

Variable Documentation

◆ v_table_hash

const struct ao2_container_methods v_table_hash
static

Hash container virtual method table.

Definition at line 1014 of file astobj2_hash.c.

Referenced by hash_ao2_container_init().