diff mbox

[1/4,libnftnl] rule: Implement internal expression iterator

Message ID 20160808111758.4062-1-carlosfg@riseup.net
State Changes Requested
Delegated to: Pablo Neira
Headers show

Commit Message

Carlos Falgueras García Aug. 8, 2016, 11:17 a.m. UTC
With 'nftnl_expr_iter_init' we can create an expression iterator without
dynamic memory allocation.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
---
 include/internal.h |  1 +
 include/rule.h     | 15 +++++++++++++++
 src/rule.c         | 23 ++++++++++++-----------
 3 files changed, 28 insertions(+), 11 deletions(-)
 create mode 100644 include/rule.h

Comments

Pablo Neira Ayuso Aug. 8, 2016, 11:25 a.m. UTC | #1
On Mon, Aug 08, 2016 at 01:17:55PM +0200, Carlos Falgueras García wrote:
> With 'nftnl_expr_iter_init' we can create an expression iterator without
> dynamic memory allocation.

I'd suggest this description:

Introduce nftnl_expr_iter_init() to allow stack allocated iterators
for internal use.

Another comment below.

> Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
> ---
>  include/internal.h |  1 +
>  include/rule.h     | 15 +++++++++++++++
>  src/rule.c         | 23 ++++++++++++-----------
>  3 files changed, 28 insertions(+), 11 deletions(-)
>  create mode 100644 include/rule.h
> 
> diff --git a/include/internal.h b/include/internal.h
> index c74e2bf..f1b6511 100644
> --- a/include/internal.h
> +++ b/include/internal.h
> @@ -12,6 +12,7 @@
>  #include "set.h"
>  #include "set_elem.h"
>  #include "expr.h"
> +#include "rule.h"
>  #include "expr_ops.h"
>  #include "buffer.h"
>  
> diff --git a/include/rule.h b/include/rule.h
> new file mode 100644
> index 0000000..e2ea578
> --- /dev/null
> +++ b/include/rule.h
> @@ -0,0 +1,15 @@
> +#ifndef _LIBNFTNL_RULE_INTERNAL_H_
> +#define _LIBNFTNL_RULE_INTERNAL_H_
> +
> +#include <libnftnl/rule.h>
> +#include <libnftnl/expr.h>
> +
> +struct nftnl_expr_iter {
> +	const struct nftnl_rule	*r;
> +	struct nftnl_expr	*cur;
> +};
> +
> +void nftnl_expr_iter_init(const struct nftnl_rule *r,
> +			  struct nftnl_expr_iter *iter);

If nftnl_expr_iter_init() is only used from src/rule.c, then there is
no need to expose this rule include/rule.h

> +
> +#endif
> diff --git a/src/rule.c b/src/rule.c
> index a0edca7..0cfddf2 100644
> --- a/src/rule.c
> +++ b/src/rule.c
> @@ -1025,10 +1025,17 @@ int nftnl_expr_foreach(struct nftnl_rule *r,
>  }
>  EXPORT_SYMBOL_ALIAS(nftnl_expr_foreach, nft_rule_expr_foreach);
>  
> -struct nftnl_expr_iter {
> -	struct nftnl_rule		*r;
> -	struct nftnl_expr	*cur;
> -};
> +void nftnl_expr_iter_init(const struct nftnl_rule *r,
> +			  struct nftnl_expr_iter *iter)
> +{
> +	iter->r = r;
> +	if (list_empty(&r->expr_list))
> +		iter->cur = NULL;
> +	else
> +		iter->cur = list_entry(r->expr_list.next, struct nftnl_expr,
> +				       head);
> +}
> +EXPORT_SYMBOL(nftnl_expr_iter_init);
>  
>  struct nftnl_expr_iter *nftnl_expr_iter_create(struct nftnl_rule *r)
>  {
> @@ -1037,13 +1044,7 @@ struct nftnl_expr_iter *nftnl_expr_iter_create(struct nftnl_rule *r)
>  	iter = calloc(1, sizeof(struct nftnl_expr_iter));
>  	if (iter == NULL)
>  		return NULL;
> -
> -	iter->r = r;
> -	if (list_empty(&r->expr_list))
> -		iter->cur = NULL;
> -	else
> -		iter->cur = list_entry(r->expr_list.next, struct nftnl_expr,
> -				       head);
> +	nftnl_expr_iter_init(r, iter);
>  
>  	return iter;
>  }
> -- 
> 2.8.3
> 
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Carlos Falgueras García Aug. 8, 2016, 11:49 a.m. UTC | #2
On 08/08/2016 01:25 PM, Pablo Neira Ayuso wrote:
> On Mon, Aug 08, 2016 at 01:17:55PM +0200, Carlos Falgueras García wrote:
>> With 'nftnl_expr_iter_init' we can create an expression iterator without
>> dynamic memory allocation.
>
> I'd suggest this description:
>
> Introduce nftnl_expr_iter_init() to allow stack allocated iterators
> for internal use.
>
> Another comment below.

Ok, I'll change it at v2, thanks.

>> Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
>> ---
>>  include/internal.h |  1 +
>>  include/rule.h     | 15 +++++++++++++++
>>  src/rule.c         | 23 ++++++++++++-----------
>>  3 files changed, 28 insertions(+), 11 deletions(-)
>>  create mode 100644 include/rule.h
>>
>> diff --git a/include/internal.h b/include/internal.h
>> index c74e2bf..f1b6511 100644
>> --- a/include/internal.h
>> +++ b/include/internal.h
>> @@ -12,6 +12,7 @@
>>  #include "set.h"
>>  #include "set_elem.h"
>>  #include "expr.h"
>> +#include "rule.h"
>>  #include "expr_ops.h"
>>  #include "buffer.h"
>>
>> diff --git a/include/rule.h b/include/rule.h
>> new file mode 100644
>> index 0000000..e2ea578
>> --- /dev/null
>> +++ b/include/rule.h
>> @@ -0,0 +1,15 @@
>> +#ifndef _LIBNFTNL_RULE_INTERNAL_H_
>> +#define _LIBNFTNL_RULE_INTERNAL_H_
>> +
>> +#include <libnftnl/rule.h>
>> +#include <libnftnl/expr.h>
>> +
>> +struct nftnl_expr_iter {
>> +	const struct nftnl_rule	*r;
>> +	struct nftnl_expr	*cur;
>> +};
>> +
>> +void nftnl_expr_iter_init(const struct nftnl_rule *r,
>> +			  struct nftnl_expr_iter *iter);
>
> If nftnl_expr_iter_init() is only used from src/rule.c, then there is
> no need to expose this rule include/rule.h
>

Ok.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/include/internal.h b/include/internal.h
index c74e2bf..f1b6511 100644
--- a/include/internal.h
+++ b/include/internal.h
@@ -12,6 +12,7 @@ 
 #include "set.h"
 #include "set_elem.h"
 #include "expr.h"
+#include "rule.h"
 #include "expr_ops.h"
 #include "buffer.h"
 
diff --git a/include/rule.h b/include/rule.h
new file mode 100644
index 0000000..e2ea578
--- /dev/null
+++ b/include/rule.h
@@ -0,0 +1,15 @@ 
+#ifndef _LIBNFTNL_RULE_INTERNAL_H_
+#define _LIBNFTNL_RULE_INTERNAL_H_
+
+#include <libnftnl/rule.h>
+#include <libnftnl/expr.h>
+
+struct nftnl_expr_iter {
+	const struct nftnl_rule	*r;
+	struct nftnl_expr	*cur;
+};
+
+void nftnl_expr_iter_init(const struct nftnl_rule *r,
+			  struct nftnl_expr_iter *iter);
+
+#endif
diff --git a/src/rule.c b/src/rule.c
index a0edca7..0cfddf2 100644
--- a/src/rule.c
+++ b/src/rule.c
@@ -1025,10 +1025,17 @@  int nftnl_expr_foreach(struct nftnl_rule *r,
 }
 EXPORT_SYMBOL_ALIAS(nftnl_expr_foreach, nft_rule_expr_foreach);
 
-struct nftnl_expr_iter {
-	struct nftnl_rule		*r;
-	struct nftnl_expr	*cur;
-};
+void nftnl_expr_iter_init(const struct nftnl_rule *r,
+			  struct nftnl_expr_iter *iter)
+{
+	iter->r = r;
+	if (list_empty(&r->expr_list))
+		iter->cur = NULL;
+	else
+		iter->cur = list_entry(r->expr_list.next, struct nftnl_expr,
+				       head);
+}
+EXPORT_SYMBOL(nftnl_expr_iter_init);
 
 struct nftnl_expr_iter *nftnl_expr_iter_create(struct nftnl_rule *r)
 {
@@ -1037,13 +1044,7 @@  struct nftnl_expr_iter *nftnl_expr_iter_create(struct nftnl_rule *r)
 	iter = calloc(1, sizeof(struct nftnl_expr_iter));
 	if (iter == NULL)
 		return NULL;
-
-	iter->r = r;
-	if (list_empty(&r->expr_list))
-		iter->cur = NULL;
-	else
-		iter->cur = list_entry(r->expr_list.next, struct nftnl_expr,
-				       head);
+	nftnl_expr_iter_init(r, iter);
 
 	return iter;
 }