From 55546b4e135283f2ec4b5715536e59722c55f11c Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 28 Jun 2016 20:05:42 +1000 Subject: writefile: replace most struct lookups /w constants Removes many hash lookups per file-save and undo-step. --- source/blender/makesdna/intern/CMakeLists.txt | 16 ++++++-- source/blender/makesdna/intern/makesdna.c | 53 +++++++++++++++++++-------- 2 files changed, 51 insertions(+), 18 deletions(-) (limited to 'source/blender/makesdna/intern') diff --git a/source/blender/makesdna/intern/CMakeLists.txt b/source/blender/makesdna/intern/CMakeLists.txt index b4ada7ffbf1..0bb6e866bf4 100644 --- a/source/blender/makesdna/intern/CMakeLists.txt +++ b/source/blender/makesdna/intern/CMakeLists.txt @@ -56,8 +56,14 @@ add_executable(makesdna ${SRC} ${SRC_DNA_INC}) # Output dna.c add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/dna.c - COMMAND "$" ${CMAKE_CURRENT_BINARY_DIR}/dna.c ${CMAKE_SOURCE_DIR}/source/blender/makesdna/ + OUTPUT + ${CMAKE_CURRENT_BINARY_DIR}/dna.c + ${CMAKE_CURRENT_BINARY_DIR}/dna_type_offsets.h + COMMAND + "$" + ${CMAKE_CURRENT_BINARY_DIR}/dna.c + ${CMAKE_CURRENT_BINARY_DIR}/dna_type_offsets.h + ${CMAKE_SOURCE_DIR}/source/blender/makesdna/ DEPENDS makesdna ) @@ -78,7 +84,11 @@ set(SRC ${SRC_DNA_INC} ) -set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/dna.c PROPERTIES GENERATED TRUE) +set_source_files_properties( + ${CMAKE_CURRENT_BINARY_DIR}/dna.c + ${CMAKE_CURRENT_BINARY_DIR}/dna_type_offsets.h + PROPERTIES GENERATED TRUE +) blender_add_lib(bf_dna "${SRC}" "${INC}" "${INC_SYS}") diff --git a/source/blender/makesdna/intern/makesdna.c b/source/blender/makesdna/intern/makesdna.c index fc94a8d9ff4..b78299316e1 100644 --- a/source/blender/makesdna/intern/makesdna.c +++ b/source/blender/makesdna/intern/makesdna.c @@ -968,7 +968,7 @@ void printStructLengths(void) } -static int make_structDNA(const char *baseDirectory, FILE *file) +static int make_structDNA(const char *baseDirectory, FILE *file, FILE *file_offsets) { int len, i; const short *sp; @@ -1159,6 +1159,19 @@ static int make_structDNA(const char *baseDirectory, FILE *file) /* end end padding test */ } + /* write a simple enum with all structs offsets, + * should only be accessed via SDNA_TYPE_FROM_STRUCT macro */ + { + fprintf(file_offsets, "#define SDNA_TYPE_FROM_STRUCT(id) _SDNA_TYPE_##id\n"); + fprintf(file_offsets, "enum {\n"); + for (i = 0; i < nr_structs; i++) { + const short *structpoin = structs[i]; + const int structtype = structpoin[0]; + fprintf(file_offsets, "\t_SDNA_TYPE_%s = %d,\n", types[structtype], i); + } + fprintf(file_offsets, "\tSDNA_TYPE_MAX = %d,\n", nr_structs); + fprintf(file_offsets, "};\n"); + } MEM_freeN(namedata); MEM_freeN(typedata); @@ -1190,43 +1203,53 @@ static void make_bad_file(const char *file, int line) int main(int argc, char **argv) { - FILE *file; int return_status = 0; - if (argc != 2 && argc != 3) { - printf("Usage: %s outfile.c [base directory]\n", argv[0]); + if (argc != 3 && argc != 4) { + printf("Usage: %s dna.c dna_struct_offsets.h [base directory]\n", argv[0]); return_status = 1; } else { - file = fopen(argv[1], "w"); - if (!file) { + FILE *file_dna = fopen(argv[1], "w"); + FILE *file_dna_offsets = fopen(argv[2], "w"); + if (!file_dna) { printf("Unable to open file: %s\n", argv[1]); return_status = 1; } + else if (!file_dna_offsets) { + printf("Unable to open file: %s\n", argv[2]); + return_status = 1; + } else { const char *baseDirectory; - if (argc == 3) { - baseDirectory = argv[2]; + if (argc == 4) { + baseDirectory = argv[3]; } else { baseDirectory = BASE_HEADER; } - fprintf(file, "const unsigned char DNAstr[] = {\n"); - if (make_structDNA(baseDirectory, file)) { + fprintf(file_dna, "const unsigned char DNAstr[] = {\n"); + if (make_structDNA(baseDirectory, file_dna, file_dna_offsets)) { /* error */ - fclose(file); + fclose(file_dna); + file_dna = NULL; make_bad_file(argv[1], __LINE__); return_status = 1; } else { - fprintf(file, "};\n"); - fprintf(file, "const int DNAlen = sizeof(DNAstr);\n"); - - fclose(file); + fprintf(file_dna, "};\n"); + fprintf(file_dna, "const int DNAlen = sizeof(DNAstr);\n"); } } + + if (file_dna) { + fclose(file_dna); + } + if (file_dna_offsets) { + fclose(file_dna_offsets); + } } -- cgit v1.2.3