[Bf-blender-cvs] [762d93a26b9] master: DNA: use better type for SDNA->structs

Jacques Lucke noreply at git.blender.org
Tue Sep 29 12:15:11 CEST 2020


Commit: 762d93a26b973b61ef52e7326aa8f272ccbe7007
Author: Jacques Lucke
Date:   Tue Sep 29 12:12:09 2020 +0200
Branches: master
https://developer.blender.org/rB762d93a26b973b61ef52e7326aa8f272ccbe7007

DNA: use better type for SDNA->structs

The data layout remains exactly the same..
This change just gives all the elements in `SDNA->structs` names,
making it more comfortable to work with the data.

Reviewers: campbellbarton

Differential Revision: https://developer.blender.org/D8926

===================================================================

M	source/blender/blenloader/intern/readblenentry.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/blenloader/intern/writefile.c
M	source/blender/makesdna/DNA_sdna_types.h
M	source/blender/makesdna/intern/dna_genfile.c
M	source/blender/makesrna/intern/rna_define.c

===================================================================

diff --git a/source/blender/blenloader/intern/readblenentry.c b/source/blender/blenloader/intern/readblenentry.c
index 888863dda06..7a527b82e9f 100644
--- a/source/blender/blenloader/intern/readblenentry.c
+++ b/source/blender/blenloader/intern/readblenentry.c
@@ -103,8 +103,8 @@ void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp)
       break;
     }
 
-    const short *sp = fd->filesdna->structs[bhead->SDNAnr];
-    const char *name = fd->filesdna->types[sp[0]];
+    const SDNA_Struct *struct_info = fd->filesdna->structs[bhead->SDNAnr];
+    const char *name = fd->filesdna->types[struct_info->type];
     char buf[4];
 
     buf[0] = (bhead->code >> 24) & 0xFF;
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 2b68c5ea203..19fab73b259 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -2133,7 +2133,7 @@ static void switch_endian_structs(const struct SDNA *filesdna, BHead *bhead)
   char *data;
 
   data = (char *)(bhead + 1);
-  blocksize = filesdna->types_size[filesdna->structs[bhead->SDNAnr][0]];
+  blocksize = filesdna->types_size[filesdna->structs[bhead->SDNAnr]->type];
 
   nblocks = bhead->nr;
   while (nblocks--) {
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index 336276bdd40..74a4ddfd8f3 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -539,7 +539,6 @@ static void writestruct_at_address_nr(
     WriteData *wd, int filecode, const int struct_nr, int nr, const void *adr, const void *data)
 {
   BHead bh;
-  const short *sp;
 
   BLI_assert(struct_nr > 0 && struct_nr < SDNA_TYPE_MAX);
 
@@ -553,9 +552,9 @@ static void writestruct_at_address_nr(
   bh.nr = nr;
 
   bh.SDNAnr = struct_nr;
-  sp = wd->sdna->structs[bh.SDNAnr];
+  const SDNA_Struct *struct_info = wd->sdna->structs[bh.SDNAnr];
 
-  bh.len = nr * wd->sdna->types_size[sp[0]];
+  bh.len = nr * wd->sdna->types_size[struct_info->type];
 
   if (bh.len == 0) {
     return;
diff --git a/source/blender/makesdna/DNA_sdna_types.h b/source/blender/makesdna/DNA_sdna_types.h
index cfb65860b8e..3af12ae791f 100644
--- a/source/blender/makesdna/DNA_sdna_types.h
+++ b/source/blender/makesdna/DNA_sdna_types.h
@@ -24,6 +24,30 @@
 
 struct MemArena;
 
+#
+#
+typedef struct SDNA_StructMember {
+  /** This struct must not change, it's only a convenience view for raw data stored in SDNA. */
+
+  /** An index into SDNA->types. */
+  short type;
+  /** An index into SDNA->names. */
+  short name;
+} SDNA_StructMember;
+
+#
+#
+typedef struct SDNA_Struct {
+  /** This struct must not change, it's only a convenience view for raw data stored in SDNA. */
+
+  /** An index into SDNA->types. */
+  short type;
+  /** The amount of members in this struct. */
+  short members_len;
+  /** "Flexible array member" that contains information about all members of this struct. */
+  SDNA_StructMember members[];
+} SDNA_Struct;
+
 #
 #
 typedef struct SDNA {
@@ -51,14 +75,8 @@ typedef struct SDNA {
   /** Type lengths. */
   short *types_size;
 
-  /**
-   * sp = structs[a] is the address of a struct definition
-   * sp[0] is struct type number, sp[1] amount of members
-   *
-   * (sp[2], sp[3]), (sp[4], sp[5]), .. are the member
-   * type and name numbers respectively.
-   */
-  short **structs;
+  /** Information about structs and their members. */
+  SDNA_Struct **structs;
   /** Number of struct types. */
   int structs_len;
 
diff --git a/source/blender/makesdna/intern/dna_genfile.c b/source/blender/makesdna/intern/dna_genfile.c
index 77f27223722..b140a847188 100644
--- a/source/blender/makesdna/intern/dna_genfile.c
+++ b/source/blender/makesdna/intern/dna_genfile.c
@@ -206,17 +206,15 @@ int DNA_elem_size_nr(const SDNA *sdna, short type, short name)
 static void printstruct(SDNA *sdna, short strnr)
 {
   /* is for debug */
-  int b, nr;
-  short *sp;
-
-  sp = sdna->structs[strnr];
 
-  printf("struct %s\n", sdna->types[sp[0]]);
-  nr = sp[1];
-  sp += 2;
+  SDNA_Struct *struct_info = sdna->structs[strnr];
+  printf("struct %s\n", sdna->types[struct_info->type]);
 
-  for (b = 0; b < nr; b++, sp += 2) {
-    printf("   %s %s\n", sdna->types[sp[0]], sdna->names[sp[1]]);
+  for (int b = 0; b < struct_info->members_len; b++) {
+    SDNA_StructMember *struct_member = &struct_info->members[b];
+    printf("   %s %s\n",
+           sdna->types[struct_member->type],
+           sdna->names[struct_member->name]);
   }
 }
 #endif
@@ -228,7 +226,7 @@ static int dna_struct_find_nr_ex_impl(
     /* From SDNA struct. */
     const char **types,
     const int UNUSED(types_len),
-    short **const structs,
+    SDNA_Struct **const structs,
     const int structs_len,
 #ifdef WITH_DNA_GHASH
     GHash *structs_map,
@@ -238,8 +236,8 @@ static int dna_struct_find_nr_ex_impl(
     unsigned int *index_last)
 {
   if (*index_last < structs_len) {
-    const short *sp = structs[*index_last];
-    if (STREQ(types[sp[0]], str)) {
+    const SDNA_Struct *struct_info = structs[*index_last];
+    if (STREQ(types[struct_info->type], str)) {
       return *index_last;
     }
   }
@@ -256,8 +254,8 @@ static int dna_struct_find_nr_ex_impl(
 #else
   {
     for (int index = 0; index < structs_len; index++) {
-      const short *sp = structs[index];
-      if (STREQ(types[sp[0]], str)) {
+      const SDNA_Struct *struct_info = structs[index];
+      if (STREQ(types[struct_info->type], str)) {
         *index_last = index;
         return index;
       }
@@ -461,7 +459,7 @@ static bool init_structDNA(SDNA *sdna, bool do_endian_swap, const char **r_error
     }
 
     data++;
-    sdna->structs = MEM_callocN(sizeof(void *) * sdna->structs_len, "sdnastrcs");
+    sdna->structs = MEM_callocN(sizeof(SDNA_Struct *) * sdna->structs_len, "sdnastrcs");
   }
   else {
     *r_error_message = "STRC error in SDNA file";
@@ -470,32 +468,27 @@ static bool init_structDNA(SDNA *sdna, bool do_endian_swap, const char **r_error
 
   sp = (short *)data;
   for (int nr = 0; nr < sdna->structs_len; nr++) {
-    sdna->structs[nr] = sp;
+    SDNA_Struct *struct_info = (SDNA_Struct *)sp;
+    sdna->structs[nr] = struct_info;
 
     if (do_endian_swap) {
-      short a;
+      BLI_endian_switch_int16(&struct_info->type);
+      BLI_endian_switch_int16(&struct_info->members_len);
 
-      BLI_endian_switch_int16(&sp[0]);
-      BLI_endian_switch_int16(&sp[1]);
-
-      a = sp[1];
-      sp += 2;
-      while (a--) {
-        BLI_endian_switch_int16(&sp[0]);
-        BLI_endian_switch_int16(&sp[1]);
-        sp += 2;
+      for (short a = 0; a < struct_info->members_len; a++) {
+        SDNA_StructMember *member = &struct_info->members[a];
+        BLI_endian_switch_int16(&member->type);
+        BLI_endian_switch_int16(&member->name);
       }
     }
-    else {
-      sp += 2 * sp[1] + 2;
-    }
+    sp += 2 + (sizeof(SDNA_StructMember) / sizeof(short)) * struct_info->members_len;
   }
 
   {
     /* second part of gravity problem, setting "gravity" type to void */
     if (gravity_fix > -1) {
       for (int nr = 0; nr < sdna->structs_len; nr++) {
-        sp = sdna->structs[nr];
+        sp = (short *)sdna->structs[nr];
         if (STREQ(sdna->types[sp[0]], "ClothSimSettings")) {
           sp[10] = SDNA_TYPE_VOID;
         }
@@ -509,8 +502,9 @@ static bool init_structDNA(SDNA *sdna, bool do_endian_swap, const char **r_error
     sdna->structs_map = BLI_ghash_str_new_ex("init_structDNA gh", sdna->structs_len);
 
     for (intptr_t nr = 0; nr < sdna->structs_len; nr++) {
-      sp = sdna->structs[nr];
-      BLI_ghash_insert(sdna->structs_map, (void *)sdna->types[sp[0]], POINTER_FROM_INT(nr));
+      SDNA_Struct *struct_info = sdna->structs[nr];
+      BLI_ghash_insert(
+          sdna->structs_map, (void *)sdna->types[struct_info->type], POINTER_FROM_INT(nr));
     }
   }
 #endif
@@ -526,12 +520,12 @@ static bool init_structDNA(SDNA *sdna, bool do_endian_swap, const char **r_error
     }
 
     /* finally pointer_size: use struct ListBase to test it, never change the size of it! */
-    sp = sdna->structs[nr];
+    SDNA_Struct *struct_info = sdna->structs[nr];
     /* weird; i have no memory of that... I think I used sizeof(void *) before... (ton) */
 
-    sdna->pointer_size = sdna->types_size[sp[0]] / 2;
+    sdna->pointer_size = sdna->types_size[struct_info->type] / 2;
 
-    if (sp[1] != 2 || (sdna->pointer_size != 4 && sdna->pointer_size != 8)) {
+    if (struct_info->members_len != 2 || (sdna->pointer_size != 4 && sdna->pointer_size != 8)) {
       *r_error_message = "ListBase struct error! Needs it to calculate pointerize.";
       /* well, at least sizeof(ListBase) is error proof! (ton) */
       return false;
@@ -623,18 +617,16 @@ void DNA_sdna_current_free(void)
 static void recurs_test_compflags(const SDNA *sdna, char *compflags, int structnr)
 {
   /* check all structs, test if it's inside another struct */
-  const short *sp = sdna->structs[structnr];
-  const int typenr = sp[0];
+  const int typenr = sdna->structs[structnr]->type;
 
   for (int a = 0; a < sdna->structs_len; a++) {
-    if ((a != structnr) && (compflags[a] == SDNA_CMP_EQUAL)) {
-      sp = sdna->structs[a];
-      const int elems = sp[1];
-      sp += 2;
-      for (int b = 0; b < elems; b++, sp += 2) {
-        if (sp[0] == typenr) {
-          const char *cp = sdna->names[sp[1]];
-          if (!ispointer(cp)) {
+    if (a != structnr && compflags[a] == SDNA_CMP_EQUAL) {
+      SDNA_Struct *struct_info = sdna->structs[a];
+      for (int b = 0; b < struct_info->members_len; b++) {
+        SDNA_StructMember *member = &struct_info->members[b];
+        if (member->type == typenr) {
+          const char *member_name = sdna->names[member->name];
+          if (!ispointer(member_name)) {
             compflags[a] = SDNA_CMP_NOT_EQUAL;
             recurs_test_compflags(sdna, compflags, a);
           }
@@ -663,53 +655,53 @@ const char *DNA_struct_get_compareflags(const SDNA *oldsdna, const SDNA *newsdna
   unsigned int newsdna_index_last = 0;
 
   for (int a = 0; a < oldsdna->structs_len; a++) {
-    const short *sp_old = oldsdna->structs[a];

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list