<div dir="ltr">Hi,<div><br></div><div>I just realized that the only reason of not being able to pass native data types to C world was my mistake so I believe you can ignore this patch :)</div><div><br></div><div>Best,</div>

</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Oct 15, 2013 at 12:32 AM, S.Çağlar Onur <span dir="ltr"><<a href="mailto:caglar@10ur.org" target="_blank">caglar@10ur.org</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">All the other (similar) API calls behaves this way, aka passing NULL returns the count.<br>
This patch both provides consistency to calling conventions and<br>
also may make binding's life little easier cause we need to call those function two times.<br>
First to get the length and then get the data itself. This is required because we cannot<br>
pass our native data types directly to C world, at least Go can't for now.<br>
<br>
Right now a function that returns size only has to do something like that to free returned array;<br>
<br>
    int i;<br>
    struct lxc_snapshot *s;<br>
<br>
    int n = c->snapshot_list(c, &s);<br>
    if (n < 1)<br>
        return 0;<br>
<br>
    for (i = 0; i < n; i++) {<br>
        s[i].free(&s[i]);<br>
    }<br>
    free(s);<br>
    return n;<br>
<br>
whereas following should be enough.<br>
<br>
    return c->snapshot_list(c, NULL);<br>
---<br>
 src/lxc/lxccontainer.c | 37 ++++++++++++++++++++-----------------<br>
 src/lxc/lxccontainer.h |  4 ++--<br>
 2 files changed, 22 insertions(+), 19 deletions(-)<br>
<br>
diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c<br>
index 6f97879..391e4cd 100644<br>
--- a/src/lxc/lxccontainer.c<br>
+++ b/src/lxc/lxccontainer.c<br>
@@ -2516,23 +2516,25 @@ static int lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **r<br>
                }<br>
                if (!file_exists(path2))<br>
                        continue;<br>
-               nsnaps = realloc(snaps, (count + 1)*sizeof(*snaps));<br>
-               if (!nsnaps) {<br>
-                       SYSERROR("Out of memory");<br>
-                       goto out_free;<br>
-               }<br>
-               snaps = nsnaps;<br>
-               snaps[count].free = lxcsnap_free;<br>
-               snaps[count].name = strdup(direntp->d_name);<br>
-               if (!snaps[count].name)<br>
-                       goto out_free;<br>
-               snaps[count].lxcpath = strdup(snappath);<br>
-               if (!snaps[count].lxcpath) {<br>
-                       free(snaps[count].name);<br>
-                       goto out_free;<br>
+               if(ret_snaps != NULL) {<br>
+                       nsnaps = realloc(snaps, (count + 1)*sizeof(*snaps));<br>
+                       if (!nsnaps) {<br>
+                               SYSERROR("Out of memory");<br>
+                               goto out_free;<br>
+                       }<br>
+                       snaps = nsnaps;<br>
+                       snaps[count].free = lxcsnap_free;<br>
+                       snaps[count].name = strdup(direntp->d_name);<br>
+                       if (!snaps[count].name)<br>
+                               goto out_free;<br>
+                       snaps[count].lxcpath = strdup(snappath);<br>
+                       if (!snaps[count].lxcpath) {<br>
+                               free(snaps[count].name);<br>
+                               goto out_free;<br>
+                       }<br>
+                       snaps[count].comment_pathname = get_snapcomment_path(snappath, direntp->d_name);<br>
+                       snaps[count].timestamp = get_timestamp(snappath, direntp->d_name);<br>
                }<br>
-               snaps[count].comment_pathname = get_snapcomment_path(snappath, direntp->d_name);<br>
-               snaps[count].timestamp = get_timestamp(snappath, direntp->d_name);<br>
                count++;<br>
        }<br>
<br>
@@ -2541,7 +2543,8 @@ static int lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **r<br>
                WARN("failed to close directory");<br>
        process_unlock();<br>
<br>
-       *ret_snaps = snaps;<br>
+       if (ret_snaps != NULL)<br>
+               *ret_snaps = snaps;<br>
        return count;<br>
<br>
 out_free:<br>
diff --git a/src/lxc/lxccontainer.h b/src/lxc/lxccontainer.h<br>
index 5901066..bb77126 100644<br>
--- a/src/lxc/lxccontainer.h<br>
+++ b/src/lxc/lxccontainer.h<br>
@@ -197,12 +197,12 @@ struct lxc_container {<br>
<br>
        /*<br>
         * snapshot_list() will return a description of all snapshots of c in<br>
-        * a simple array.  See src/tests/snapshot.c for the proper way to<br>
+        * a simple array, if ret_snaps is not null.  See src/tests/snapshot.c for the proper way to<br>
         * free the allocated results.<br>
         *<br>
         * Returns the number of snapshots.<br>
         */<br>
-       int (*snapshot_list)(struct lxc_container *, struct lxc_snapshot **);<br>
+       int (*snapshot_list)(struct lxc_container *c, struct lxc_snapshot **ret_snaps);<br>
<br>
        /*<br>
         * snapshot_restore() will create a new container based on a snapshot.<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.8.1.2<br>
<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br>S.Çağlar Onur <<a href="mailto:caglar@10ur.org">caglar@10ur.org</a>>
</div>