]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
backtrace: add a clone() method
authorMartin Willi <martin@revosec.ch>
Wed, 10 Jul 2013 15:14:20 +0000 (17:14 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 10 Jul 2013 15:28:18 +0000 (17:28 +0200)
src/libstrongswan/utils/backtrace.c
src/libstrongswan/utils/backtrace.h

index 9477d4352ab329997e58827ff49333ec8dd59ccb..93031908a83a3a98c47b32dd1a560e31adef6f25 100644 (file)
@@ -52,6 +52,11 @@ struct private_backtrace_t {
        void *frames[];
 };
 
+/**
+ * Forward declaration of method getter
+ */
+static backtrace_t get_methods();
+
 /**
  * Write a format string with arguments to a FILE line, if it is NULL to DBG
  */
@@ -531,6 +536,21 @@ METHOD(backtrace_t, create_frame_enumerator, enumerator_t*,
        return &enumerator->public;
 }
 
+METHOD(backtrace_t, clone, backtrace_t*,
+       private_backtrace_t *this)
+{
+       private_backtrace_t *clone;
+
+       clone = malloc(sizeof(private_backtrace_t) +
+                                  this->frame_count * sizeof(void*));
+       memcpy(clone->frames, this->frames, this->frame_count * sizeof(void*));
+       clone->frame_count = this->frame_count;
+
+       clone->public = get_methods();
+
+       return &clone->public;
+}
+
 METHOD(backtrace_t, destroy, void,
        private_backtrace_t *this)
 {
@@ -564,6 +584,21 @@ static inline int backtrace_unwind(void **frames, int count)
 }
 #endif /* HAVE_UNWIND */
 
+/**
+ * Get implementation methods of backtrace_t
+ */
+static backtrace_t get_methods()
+{
+       return (backtrace_t) {
+               .log = _log_,
+               .contains_function = _contains_function,
+               .equals = _equals,
+               .clone = _clone,
+               .create_frame_enumerator = _create_frame_enumerator,
+               .destroy = _destroy,
+       };
+}
+
 /**
  * See header
  */
@@ -583,13 +618,7 @@ backtrace_t *backtrace_create(int skip)
        memcpy(this->frames, frames + skip, frame_count * sizeof(void*));
        this->frame_count = frame_count;
 
-       this->public = (backtrace_t) {
-               .log = _log_,
-               .contains_function = _contains_function,
-               .equals = _equals,
-               .create_frame_enumerator = _create_frame_enumerator,
-               .destroy = _destroy,
-       };
+       this->public = get_methods();
 
        return &this->public;
 }
index 62104238dc1621026872e9cd14789571eb5eb391..416f588988de4d4212db6b11029c95ba506bbd7a 100644 (file)
@@ -59,6 +59,14 @@ struct backtrace_t {
         * @return              TRUE if backtraces are equal
         */
        bool (*equals)(backtrace_t *this, backtrace_t *other);
+
+       /**
+        * Create a copy of this backtrace.
+        *
+        * @return              cloned copy
+        */
+       backtrace_t* (*clone)(backtrace_t *this);
+
        /**
         * Create an enumerator over the stack frame addresses.
         *