Review Board 1.7.16


Add ability to clone ao2 containers.

Review Request #1746 - Created Feb. 15, 2012 and submitted

rmudgett
trunk
Reviewers
asterisk-dev
Asterisk
Occasionally there is a need to put all objects in one container also into another container.

Some reasons you might need to do this:
1) You need to reconfigure a container.   You would do this by creating a new container with the new configuration and ao2_container_dup the old container into it.  Then replace the old container with the new.  Then destroy the old container.

2) You need the contents of a container to remain stable while operating on all of the objects.  You would do this by creating a cloned container of the original with ao2_container_clone.  The cloned container is a snapshot of the objects at the time of the cloning.  When done, just destroy the cloned container.
Added unit testing code to test the new ao2_container_clone and ao2_container_dup functions.

Tests still pass.

Diff revision 2

This is not the most recent revision of the diff. The latest diff is revision 4. See what's changed.

1 2 3 4
1 2 3 4

  1. /trunk/include/asterisk/astobj2.h: Loading...
  2. /trunk/main/astobj2.c: Loading...
  3. /trunk/tests/test_astobj2.c: Loading...
/trunk/include/asterisk/astobj2.h
Revision 355447 New Change
[20] 748 lines
[+20] [+] struct ao2_container *__ao2_container_alloc_debug(const unsigned int n_buckets,
749
/*! \brief
749
/*! \brief
750
 * Returns the number of elements in a container.
750
 * Returns the number of elements in a container.
751
 */
751
 */
752
int ao2_container_count(struct ao2_container *c);
752
int ao2_container_count(struct ao2_container *c);
753

    
   
753

   

    
   
754
/*!

    
   
755
 * \brief Copy all object references in the src container into the dest container.

    
   
756
 * \since 11.0

    
   
757
 *

    
   
758
 * \param dest Container to copy src object references into.

    
   
759
 * \param src Container to copy all object references from.

    
   
760
 * \param flags OBJ_NOLOCK if a lock is already held on both containers.

    
   
761
 *    Otherwise, the src container is locked first.

    
   
762
 *

    
   
763
 * \note This can potentially be expensive because a malloc is

    
   
764
 * needed for every object in the src container.

    
   
765
 *

    
   
766
 * \retval 0 on success.

    
   
767
 * \retval -1 on error.

    
   
768
 */

    
   
769
int ao2_container_dup(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags);

    
   
770

   

    
   
771
/*!

    
   
772
 * \brief Create a clone/copy of the given container.

    
   
773
 * \since 11.0

    
   
774
 *

    
   
775
 * \param orig Container to copy all object references from.

    
   
776
 * \param flags OBJ_NOLOCK if a lock is already held on the container.

    
   
777
 *

    
   
778
 * \note This can potentially be expensive because a malloc is

    
   
779
 * needed for every object in the orig container.

    
   
780
 *

    
   
781
 * \retval Clone container on success.

    
   
782
 * \retval NULL on error.

    
   
783
 */

    
   
784
struct ao2_container *__ao2_container_clone(struct ao2_container *orig, enum search_flags flags);

    
   
785
struct ao2_container *__ao2_container_clone_debug(struct ao2_container *orig, enum search_flags flags, const char *tag, char *file, int line, const char *funcname, int ref_debug);

    
   
786
#if defined(REF_DEBUG)

    
   
787

   

    
   
788
#define ao2_t_container_clone(orig, flags, tag)	__ao2_container_clone_debug(orig, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)

    
   
789
#define ao2_container_clone(orig, flags)		__ao2_container_clone_debug(orig, flags, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)

    
   
790

   

    
   
791
#elif defined(__AST_DEBUG_MALLOC)

    
   
792

   

    
   
793
#define ao2_t_container_clone(orig, flags, tag)	__ao2_container_clone_debug(orig, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)

    
   
794
#define ao2_container_clone(orig, flags)		__ao2_container_clone_debug(orig, flags, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)

    
   
795

   

    
   
796
#else

    
   
797

   

    
   
798
#define ao2_t_container_clone(orig, flags, tag)	__ao2_container_clone(orig, flags)

    
   
799
#define ao2_container_clone(orig, flags)		__ao2_container_clone(orig, flags)

    
   
800

   

    
   
801
#endif

    
   
802

   
754
/*@} */
803
/*@} */
755

    
   
804

   
756
/*! \name Object Management
805
/*! \name Object Management
757
 * Here we have functions to manage objects.
806
 * Here we have functions to manage objects.
758
 *
807
 *
[+20] [20] 390 lines
/trunk/main/astobj2.c
Revision 355447 New Change
 
/trunk/tests/test_astobj2.c
Revision 355447 New Change
 
  1. /trunk/include/asterisk/astobj2.h: Loading...
  2. /trunk/main/astobj2.c: Loading...
  3. /trunk/tests/test_astobj2.c: Loading...

https://reviewboard.asterisk.org/ runs on a server provided by Digium, Inc. and uses bandwidth donated to the open source Asterisk community by API Digital Communications in Huntsville, AL USA.
Please report problems with this site to asteriskteam@digium.com.