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 4 (Latest)

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 356796 New Change
[20] 773 lines
[+20] [+] struct ao2_container *__ao2_container_alloc_debug(unsigned int n_buckets,
774
/*! \brief
774
/*! \brief
775
 * Returns the number of elements in a container.
775
 * Returns the number of elements in a container.
776
 */
776
 */
777
int ao2_container_count(struct ao2_container *c);
777
int ao2_container_count(struct ao2_container *c);
778

    
   
778

   

    
   
779
/*!

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

    
   
781
 * \since 11.0

    
   
782
 *

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

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

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

    
   
786
 *    Otherwise, the src container is locked first.

    
   
787
 *

    
   
788
 * \pre The dest container must be empty.  If the duplication fails, the

    
   
789
 * dest container will be emptied.

    
   
790
 *

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

    
   
792
 * needed for every object in the src container.

    
   
793
 *

    
   
794
 * \retval 0 on success.

    
   
795
 * \retval -1 on error.

    
   
796
 */

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

    
   
798

   

    
   
799
/*!

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

    
   
801
 * \since 11.0

    
   
802
 *

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

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

    
   
805
 *

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

    
   
807
 * needed for every object in the orig container.

    
   
808
 *

    
   
809
 * \retval Clone container on success.

    
   
810
 * \retval NULL on error.

    
   
811
 */

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

    
   
813
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);

    
   
814
#if defined(REF_DEBUG)

    
   
815

   

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

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

    
   
818

   

    
   
819
#elif defined(__AST_DEBUG_MALLOC)

    
   
820

   

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

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

    
   
823

   

    
   
824
#else

    
   
825

   

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

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

    
   
828

   

    
   
829
#endif

    
   
830

   
779
/*@} */
831
/*@} */
780

    
   
832

   
781
/*! \name Object Management
833
/*! \name Object Management
782
 * Here we have functions to manage objects.
834
 * Here we have functions to manage objects.
783
 *
835
 *
[+20] [20] 408 lines
/trunk/main/astobj2.c
Revision 356796 New Change
 
/trunk/tests/test_astobj2.c
Revision 356796 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.