Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenloader/intern/writefile.c
| Show First 20 Lines • Show All 291 Lines • ▼ Show 20 Lines | |||||
| * \{ */ | * \{ */ | ||||
| typedef struct { | typedef struct { | ||||
| const struct SDNA *sdna; | const struct SDNA *sdna; | ||||
| /** Use for file and memory writing (fixed size of #MYWRITE_BUFFER_SIZE). */ | /** Use for file and memory writing (fixed size of #MYWRITE_BUFFER_SIZE). */ | ||||
| uchar *buf; | uchar *buf; | ||||
| /** Number of bytes used in #WriteData.buf (flushed when exceeded). */ | /** Number of bytes used in #WriteData.buf (flushed when exceeded). */ | ||||
| int buf_used_len; | size_t buf_used_len; | ||||
| #ifdef USE_WRITE_DATA_LEN | #ifdef USE_WRITE_DATA_LEN | ||||
| /** Total number of bytes written. */ | /** Total number of bytes written. */ | ||||
| size_t write_len; | size_t write_len; | ||||
| #endif | #endif | ||||
| /** Set on unlikely case of an error (ignores further file writing). */ | /** Set on unlikely case of an error (ignores further file writing). */ | ||||
| bool error; | bool error; | ||||
| Show All 25 Lines | static WriteData *writedata_new(WriteWrap *ww) | ||||
| if ((ww == NULL) || (ww->use_buf)) { | if ((ww == NULL) || (ww->use_buf)) { | ||||
| wd->buf = MEM_mallocN(MYWRITE_BUFFER_SIZE, "wd->buf"); | wd->buf = MEM_mallocN(MYWRITE_BUFFER_SIZE, "wd->buf"); | ||||
| } | } | ||||
| return wd; | return wd; | ||||
| } | } | ||||
| static void writedata_do_write(WriteData *wd, const void *mem, int memlen) | static void writedata_do_write(WriteData *wd, const void *mem, size_t memlen) | ||||
| { | { | ||||
| if ((wd == NULL) || wd->error || (mem == NULL) || memlen < 1) { | if ((wd == NULL) || wd->error || (mem == NULL) || memlen < 1) { | ||||
| return; | return; | ||||
| } | } | ||||
| if (memlen > INT_MAX) { | |||||
| BLI_assert(!"Cannot write chunks bigger than INT_MAX."); | |||||
| return; | |||||
| } | |||||
| if (UNLIKELY(wd->error)) { | if (UNLIKELY(wd->error)) { | ||||
| return; | return; | ||||
| } | } | ||||
| /* memory based save */ | /* memory based save */ | ||||
| if (wd->use_memfile) { | if (wd->use_memfile) { | ||||
| BLO_memfile_chunk_add(&wd->mem, mem, memlen); | BLO_memfile_chunk_add(&wd->mem, mem, memlen); | ||||
| } | } | ||||
| Show All 19 Lines | |||||
| * \{ */ | * \{ */ | ||||
| /** | /** | ||||
| * Flush helps the de-duplicating memory for undo-save by logically segmenting data, | * Flush helps the de-duplicating memory for undo-save by logically segmenting data, | ||||
| * so differences in one part of memory won't cause unrelated data to be duplicated. | * so differences in one part of memory won't cause unrelated data to be duplicated. | ||||
| */ | */ | ||||
| static void mywrite_flush(WriteData *wd) | static void mywrite_flush(WriteData *wd) | ||||
| { | { | ||||
| if (wd->buf_used_len) { | if (wd->buf_used_len != 0) { | ||||
| writedata_do_write(wd, wd->buf, wd->buf_used_len); | writedata_do_write(wd, wd->buf, wd->buf_used_len); | ||||
| wd->buf_used_len = 0; | wd->buf_used_len = 0; | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * Low level WRITE(2) wrapper that buffers data | * Low level WRITE(2) wrapper that buffers data | ||||
| * \param adr: Pointer to new chunk of data | * \param adr: Pointer to new chunk of data | ||||
| * \param len: Length of new chunk of data | * \param len: Length of new chunk of data | ||||
| */ | */ | ||||
| static void mywrite(WriteData *wd, const void *adr, int len) | static void mywrite(WriteData *wd, const void *adr, size_t len) | ||||
| { | { | ||||
| if (UNLIKELY(wd->error)) { | if (UNLIKELY(wd->error)) { | ||||
| return; | return; | ||||
| } | } | ||||
| if (UNLIKELY(adr == NULL)) { | if (UNLIKELY(adr == NULL)) { | ||||
| BLI_assert(0); | BLI_assert(0); | ||||
| return; | return; | ||||
| } | } | ||||
| #ifdef USE_WRITE_DATA_LEN | #ifdef USE_WRITE_DATA_LEN | ||||
| wd->write_len += len; | wd->write_len += len; | ||||
| #endif | #endif | ||||
| if (wd->buf == NULL) { | if (wd->buf == NULL) { | ||||
| writedata_do_write(wd, adr, len); | writedata_do_write(wd, adr, len); | ||||
| } | } | ||||
| else { | else { | ||||
| /* if we have a single big chunk, write existing data in | /* if we have a single big chunk, write existing data in | ||||
| * buffer and write out big chunk in smaller pieces */ | * buffer and write out big chunk in smaller pieces */ | ||||
| if (len > MYWRITE_MAX_CHUNK) { | if (len > MYWRITE_MAX_CHUNK) { | ||||
| if (wd->buf_used_len) { | if (wd->buf_used_len != 0) { | ||||
| writedata_do_write(wd, wd->buf, wd->buf_used_len); | writedata_do_write(wd, wd->buf, wd->buf_used_len); | ||||
| wd->buf_used_len = 0; | wd->buf_used_len = 0; | ||||
| } | } | ||||
| do { | do { | ||||
| int writelen = MIN2(len, MYWRITE_MAX_CHUNK); | size_t writelen = MIN2(len, MYWRITE_MAX_CHUNK); | ||||
| writedata_do_write(wd, adr, writelen); | writedata_do_write(wd, adr, writelen); | ||||
| adr = (const char *)adr + writelen; | adr = (const char *)adr + writelen; | ||||
| len -= writelen; | len -= writelen; | ||||
| } while (len > 0); | } while (len > 0); | ||||
| return; | return; | ||||
| } | } | ||||
| Show All 31 Lines | |||||
| /** | /** | ||||
| * END the mywrite wrapper | * END the mywrite wrapper | ||||
| * \return 1 if write failed | * \return 1 if write failed | ||||
| * \return unknown global variable otherwise | * \return unknown global variable otherwise | ||||
| * \warning Talks to other functions with global parameters | * \warning Talks to other functions with global parameters | ||||
| */ | */ | ||||
| static bool mywrite_end(WriteData *wd) | static bool mywrite_end(WriteData *wd) | ||||
| { | { | ||||
| if (wd->buf_used_len) { | if (wd->buf_used_len != 0) { | ||||
| writedata_do_write(wd, wd->buf, wd->buf_used_len); | writedata_do_write(wd, wd->buf, wd->buf_used_len); | ||||
| wd->buf_used_len = 0; | wd->buf_used_len = 0; | ||||
| } | } | ||||
| if (wd->use_memfile) { | if (wd->use_memfile) { | ||||
| BLO_memfile_write_finalize(&wd->mem); | BLO_memfile_write_finalize(&wd->mem); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | static void writestruct_at_address_nr( | ||||
| bh.len = nr * wd->sdna->types_size[sp[0]]; | bh.len = nr * wd->sdna->types_size[sp[0]]; | ||||
| if (bh.len == 0) { | if (bh.len == 0) { | ||||
| return; | return; | ||||
| } | } | ||||
| mywrite(wd, &bh, sizeof(BHead)); | mywrite(wd, &bh, sizeof(BHead)); | ||||
| mywrite(wd, data, bh.len); | mywrite(wd, data, (size_t)bh.len); | ||||
| } | } | ||||
| static void writestruct_nr( | static void writestruct_nr( | ||||
| WriteData *wd, int filecode, const int struct_nr, int nr, const void *adr) | WriteData *wd, int filecode, const int struct_nr, int nr, const void *adr) | ||||
| { | { | ||||
| writestruct_at_address_nr(wd, filecode, struct_nr, nr, adr, adr); | writestruct_at_address_nr(wd, filecode, struct_nr, nr, adr, adr); | ||||
| } | } | ||||
| /* do not use for structs */ | /* do not use for structs */ | ||||
| static void writedata(WriteData *wd, int filecode, int len, const void *adr) | static void writedata(WriteData *wd, int filecode, size_t len, const void *adr) | ||||
| { | { | ||||
| BHead bh; | BHead bh; | ||||
| if (adr == NULL || len == 0) { | if (adr == NULL || len == 0) { | ||||
| return; | return; | ||||
| } | } | ||||
| if (len > INT_MAX) { | |||||
| BLI_assert(!"Cannot write chunks bigger than INT_MAX."); | |||||
| return; | |||||
| } | |||||
| /* align to 4 (writes uninitialized bytes in some cases) */ | /* align to 4 (writes uninitialized bytes in some cases) */ | ||||
| len = (len + 3) & ~3; | len = (len + 3) & ~((size_t)3); | ||||
| /* init BHead */ | /* init BHead */ | ||||
| bh.code = filecode; | bh.code = filecode; | ||||
| bh.old = adr; | bh.old = adr; | ||||
| bh.nr = 1; | bh.nr = 1; | ||||
| bh.SDNAnr = 0; | bh.SDNAnr = 0; | ||||
| bh.len = len; | bh.len = (int)len; | ||||
| mywrite(wd, &bh, sizeof(BHead)); | mywrite(wd, &bh, sizeof(BHead)); | ||||
| mywrite(wd, adr, len); | mywrite(wd, adr, len); | ||||
| } | } | ||||
| /* use this to force writing of lists in same order as reading (using link_list) */ | /* use this to force writing of lists in same order as reading (using link_list) */ | ||||
| static void writelist_nr(WriteData *wd, int filecode, const int struct_nr, const ListBase *lb) | static void writelist_nr(WriteData *wd, int filecode, const int struct_nr, const ListBase *lb) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 1,079 Lines • ▼ Show 20 Lines | if (ed) { | ||||
| LISTBASE_FOREACH (MetaStack *, ms, &ed->metastack) { | LISTBASE_FOREACH (MetaStack *, ms, &ed->metastack) { | ||||
| BLO_write_struct(writer, MetaStack, ms); | BLO_write_struct(writer, MetaStack, ms); | ||||
| } | } | ||||
| } | } | ||||
| if (sce->r.avicodecdata) { | if (sce->r.avicodecdata) { | ||||
| BLO_write_struct(writer, AviCodecData, sce->r.avicodecdata); | BLO_write_struct(writer, AviCodecData, sce->r.avicodecdata); | ||||
| if (sce->r.avicodecdata->lpFormat) { | if (sce->r.avicodecdata->lpFormat) { | ||||
| BLO_write_raw(writer, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat); | BLO_write_raw(writer, (size_t)sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat); | ||||
| } | } | ||||
| if (sce->r.avicodecdata->lpParms) { | if (sce->r.avicodecdata->lpParms) { | ||||
| BLO_write_raw(writer, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms); | BLO_write_raw(writer, (size_t)sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms); | ||||
| } | } | ||||
| } | } | ||||
| if (sce->r.ffcodecdata.properties) { | if (sce->r.ffcodecdata.properties) { | ||||
| IDP_BlendWrite(writer, sce->r.ffcodecdata.properties); | IDP_BlendWrite(writer, sce->r.ffcodecdata.properties); | ||||
| } | } | ||||
| /* writing dynamic list of TimeMarkers to the blend file */ | /* writing dynamic list of TimeMarkers to the blend file */ | ||||
| LISTBASE_FOREACH (TimeMarker *, marker, &sce->markers) { | LISTBASE_FOREACH (TimeMarker *, marker, &sce->markers) { | ||||
| ▲ Show 20 Lines • Show All 248 Lines • ▼ Show 20 Lines | else if (sl->spacetype == SPACE_NODE) { | ||||
| } | } | ||||
| } | } | ||||
| else if (sl->spacetype == SPACE_CONSOLE) { | else if (sl->spacetype == SPACE_CONSOLE) { | ||||
| SpaceConsole *con = (SpaceConsole *)sl; | SpaceConsole *con = (SpaceConsole *)sl; | ||||
| LISTBASE_FOREACH (ConsoleLine *, cl, &con->history) { | LISTBASE_FOREACH (ConsoleLine *, cl, &con->history) { | ||||
| /* 'len_alloc' is invalid on write, set from 'len' on read */ | /* 'len_alloc' is invalid on write, set from 'len' on read */ | ||||
| BLO_write_struct(writer, ConsoleLine, cl); | BLO_write_struct(writer, ConsoleLine, cl); | ||||
| BLO_write_raw(writer, cl->len + 1, cl->line); | BLO_write_raw(writer, (size_t)cl->len + 1, cl->line); | ||||
| } | } | ||||
| BLO_write_struct(writer, SpaceConsole, sl); | BLO_write_struct(writer, SpaceConsole, sl); | ||||
| } | } | ||||
| #ifdef WITH_GLOBAL_AREA_WRITING | #ifdef WITH_GLOBAL_AREA_WRITING | ||||
| else if (sl->spacetype == SPACE_TOPBAR) { | else if (sl->spacetype == SPACE_TOPBAR) { | ||||
| BLO_write_struct(writer, SpaceTopBar, sl); | BLO_write_struct(writer, SpaceTopBar, sl); | ||||
| } | } | ||||
| else if (sl->spacetype == SPACE_STATUSBAR) { | else if (sl->spacetype == SPACE_STATUSBAR) { | ||||
| ▲ Show 20 Lines • Show All 380 Lines • ▼ Show 20 Lines | while (a--) { | ||||
| case ID_TXT: | case ID_TXT: | ||||
| case ID_VF: | case ID_VF: | ||||
| case ID_MC: | case ID_MC: | ||||
| case ID_PC: | case ID_PC: | ||||
| case ID_PAL: | case ID_PAL: | ||||
| case ID_BR: | case ID_BR: | ||||
| case ID_IM: | case ID_IM: | ||||
| case ID_LA: | case ID_LA: | ||||
| case ID_MA: | case ID_MA: | ||||
campbellbarton: This looks to be caused by clang-format not finding it's configuration | |||||
| case ID_MB: | case ID_MB: | ||||
| case ID_CU: | case ID_CU: | ||||
| case ID_CA: | case ID_CA: | ||||
| case ID_WO: | case ID_WO: | ||||
| case ID_MSK: | case ID_MSK: | ||||
| case ID_SPK: | case ID_SPK: | ||||
| case ID_AR: | case ID_AR: | ||||
| case ID_LP: | case ID_LP: | ||||
| ▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | #define ID_BUFFER_STATIC_SIZE 8192 | ||||
| if (use_userdef) { | if (use_userdef) { | ||||
| write_userdef(&writer, &U); | write_userdef(&writer, &U); | ||||
| } | } | ||||
| /* Write DNA last, because (to be implemented) test for which structs are written. | /* Write DNA last, because (to be implemented) test for which structs are written. | ||||
| * | * | ||||
| * Note that we *borrow* the pointer to 'DNAstr', | * Note that we *borrow* the pointer to 'DNAstr', | ||||
| * so writing each time uses the same address and doesn't cause unnecessary undo overhead. */ | * so writing each time uses the same address and doesn't cause unnecessary undo overhead. */ | ||||
| writedata(wd, DNA1, wd->sdna->data_len, wd->sdna->data); | writedata(wd, DNA1, (size_t)wd->sdna->data_len, wd->sdna->data); | ||||
| /* end of file */ | /* end of file */ | ||||
| memset(&bhead, 0, sizeof(BHead)); | memset(&bhead, 0, sizeof(BHead)); | ||||
| bhead.code = ENDB; | bhead.code = ENDB; | ||||
| mywrite(wd, &bhead, sizeof(BHead)); | mywrite(wd, &bhead, sizeof(BHead)); | ||||
| blo_join_main(&mainlist); | blo_join_main(&mainlist); | ||||
| ▲ Show 20 Lines • Show All 204 Lines • ▼ Show 20 Lines | bool BLO_write_file_mem(Main *mainvar, MemFile *compare, MemFile *current, int write_flags) | ||||
| bool use_userdef = false; | bool use_userdef = false; | ||||
| const bool err = write_file_handle( | const bool err = write_file_handle( | ||||
| mainvar, NULL, compare, current, write_flags, use_userdef, NULL); | mainvar, NULL, compare, current, write_flags, use_userdef, NULL); | ||||
| return (err == 0); | return (err == 0); | ||||
| } | } | ||||
| void BLO_write_raw(BlendWriter *writer, int size_in_bytes, const void *data_ptr) | void BLO_write_raw(BlendWriter *writer, size_t size_in_bytes, const void *data_ptr) | ||||
| { | { | ||||
| writedata(writer->wd, DATA, size_in_bytes, data_ptr); | writedata(writer->wd, DATA, size_in_bytes, data_ptr); | ||||
| } | } | ||||
| void BLO_write_struct_by_name(BlendWriter *writer, const char *struct_name, const void *data_ptr) | void BLO_write_struct_by_name(BlendWriter *writer, const char *struct_name, const void *data_ptr) | ||||
| { | { | ||||
| BLO_write_struct_array_by_name(writer, struct_name, 1, data_ptr); | BLO_write_struct_array_by_name(writer, struct_name, 1, data_ptr); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| int BLO_get_struct_id_by_name(BlendWriter *writer, const char *struct_name) | int BLO_get_struct_id_by_name(BlendWriter *writer, const char *struct_name) | ||||
| { | { | ||||
| int struct_id = DNA_struct_find_nr(writer->wd->sdna, struct_name); | int struct_id = DNA_struct_find_nr(writer->wd->sdna, struct_name); | ||||
| return struct_id; | return struct_id; | ||||
| } | } | ||||
| void BLO_write_int32_array(BlendWriter *writer, int size, const int32_t *data_ptr) | void BLO_write_int32_array(BlendWriter *writer, uint num, const int32_t *data_ptr) | ||||
| { | { | ||||
| BLO_write_raw(writer, sizeof(int32_t) * size, data_ptr); | BLO_write_raw(writer, sizeof(int32_t) * (size_t)num, data_ptr); | ||||
| } | } | ||||
| void BLO_write_uint32_array(BlendWriter *writer, int size, const uint32_t *data_ptr) | void BLO_write_uint32_array(BlendWriter *writer, uint num, const uint32_t *data_ptr) | ||||
| { | { | ||||
| BLO_write_raw(writer, sizeof(uint32_t) * size, data_ptr); | BLO_write_raw(writer, sizeof(uint32_t) * (size_t)num, data_ptr); | ||||
| } | } | ||||
| void BLO_write_float_array(BlendWriter *writer, int size, const float *data_ptr) | void BLO_write_float_array(BlendWriter *writer, uint num, const float *data_ptr) | ||||
| { | { | ||||
| BLO_write_raw(writer, sizeof(float) * size, data_ptr); | BLO_write_raw(writer, sizeof(float) * (size_t)num, data_ptr); | ||||
| } | } | ||||
| void BLO_write_pointer_array(BlendWriter *writer, int size, const void *data_ptr) | void BLO_write_pointer_array(BlendWriter *writer, uint num, const void *data_ptr) | ||||
| { | { | ||||
| BLO_write_raw(writer, sizeof(void *) * size, data_ptr); | BLO_write_raw(writer, sizeof(void *) * (size_t)num, data_ptr); | ||||
| } | } | ||||
| void BLO_write_float3_array(BlendWriter *writer, int size, const float *data_ptr) | void BLO_write_float3_array(BlendWriter *writer, uint num, const float *data_ptr) | ||||
| { | { | ||||
| BLO_write_raw(writer, sizeof(float[3]) * size, data_ptr); | BLO_write_raw(writer, sizeof(float[3]) * (size_t)num, data_ptr); | ||||
| } | } | ||||
| /** | /** | ||||
| * Write a null terminated string. | * Write a null terminated string. | ||||
| */ | */ | ||||
| void BLO_write_string(BlendWriter *writer, const char *str) | void BLO_write_string(BlendWriter *writer, const char *data_ptr) | ||||
| { | { | ||||
| if (str != NULL) { | if (data_ptr != NULL) { | ||||
| BLO_write_raw(writer, strlen(str) + 1, str); | BLO_write_raw(writer, strlen(data_ptr) + 1, data_ptr); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * Sometimes different data is written depending on whether the file is saved to disk or used for | * Sometimes different data is written depending on whether the file is saved to disk or used for | ||||
| * undo. This function returns true when the current file-writing is done for undo. | * undo. This function returns true when the current file-writing is done for undo. | ||||
| */ | */ | ||||
| bool BLO_write_is_undo(BlendWriter *writer) | bool BLO_write_is_undo(BlendWriter *writer) | ||||
| { | { | ||||
| return writer->wd->use_memfile; | return writer->wd->use_memfile; | ||||
| } | } | ||||
| /** \} */ | /** \} */ | ||||
This looks to be caused by clang-format not finding it's configuration