libsmack: shrink few rule and label struct fields
authorRafal Krypa <r.krypa@samsung.com>
Thu, 13 Feb 2014 13:16:40 +0000 (14:16 +0100)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 13 Feb 2014 16:19:07 +0000 (17:19 +0100)
Use smaller struct fields to optimize memory usage and speed.
On a 32-bit machine this saves 4 bytes per rule and 4 bytes per label.
Limit label length to 8 bits. It's max value is already limited to 255.
Limit label id to 16 bits. While policy with more than 2^16 labels is
theoretically possible, it would be handled very badly by kernel.

Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
libsmack/libsmack.c

index 79ae822..7217313 100644 (file)
@@ -59,6 +59,7 @@
 #define ACCESS_TYPE_L 0x20
 
 #define DICT_HASH_SIZE 4096
+#define MAX_LABELS_CNT (UINT16_MAX + 1)
 
 extern char *smackfs_mnt;
 extern int smackfs_mnt_dirfd;
@@ -68,14 +69,14 @@ extern int init_smackfs_mnt(void);
 struct smack_rule {
        int8_t allow_code;
        int8_t deny_code;
-       int subject_id;
-       int object_id;
+       uint16_t subject_id;
+       uint16_t object_id;
        struct smack_rule *next;
 };
 
 struct smack_label {
-       int len;
-       int id;
+       uint8_t len;
+       uint16_t id;
        char *label;
        struct smack_label *next;
 };
@@ -877,6 +878,9 @@ static struct smack_label *label_add(struct smack_accesses *handle, const char *
        struct smack_label *new_label;
        int len;
 
+       if (handle->labels_cnt == MAX_LABELS_CNT)
+               return NULL;
+
        len = get_label(NULL, label, &hash_value);
        if (len == -1)
                return NULL;