CustomData: Support for >2gig layers
authorCampbell Barton <ideasman42@gmail.com>
Mon, 16 May 2016 17:01:32 +0000 (03:01 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 16 May 2016 17:02:55 +0000 (03:02 +1000)
source/blender/blenkernel/intern/customdata.c

index b0d0dc08126214ec408123352ea30abd5357d110..ffc440eab15d900cbb875085f05fe95869d90288 100644 (file)
@@ -68,6 +68,8 @@
 #include <math.h>
 #include <string.h>
 
+#include "BLI_strict_flags.h"
+
 /* number of layers to add when growing a CustomData object */
 #define CUSTOMDATA_GROW 5
 
@@ -1530,13 +1532,11 @@ void CustomData_realloc(CustomData *data, int totelem)
        for (i = 0; i < data->totlayer; ++i) {
                CustomDataLayer *layer = &data->layers[i];
                const LayerTypeInfo *typeInfo;
-               int size;
                if (layer->flag & CD_FLAG_NOFREE) {
                        continue;
                }
                typeInfo = layerType_getInfo(layer->type);
-               size = totelem * typeInfo->size;
-               layer->data = MEM_reallocN(layer->data, size);
+               layer->data = MEM_reallocN(layer->data, (size_t)totelem * typeInfo->size);
        }
 }
 
@@ -1844,7 +1844,7 @@ static CustomDataLayer *customData_add_layer__internal(CustomData *data, int typ
                                                        int totelem, const char *name)
 {
        const LayerTypeInfo *typeInfo = layerType_getInfo(type);
-       const int size = totelem * typeInfo->size;
+       const size_t size = (size_t)totelem * typeInfo->size;
        int flag = 0, index = data->totlayer;
        void *newlayerdata = NULL;
 
@@ -2064,7 +2064,7 @@ static void *customData_duplicate_referenced_layer_index(CustomData *data, const
                const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
 
                if (typeInfo->copy) {
-                       void *dst_data = MEM_mallocN(totelem * typeInfo->size, "CD duplicate ref layer");
+                       void *dst_data = MEM_mallocN((size_t)totelem * typeInfo->size, "CD duplicate ref layer");
                        typeInfo->copy(layer->data, dst_data, totelem);
                        layer->data = dst_data;
                }
@@ -2172,7 +2172,7 @@ void CustomData_copy_elements(int type, void *src_data_ofs, void *dst_data_ofs,
        if (typeInfo->copy)
                typeInfo->copy(src_data_ofs, dst_data_ofs, count);
        else
-               memcpy(dst_data_ofs, src_data_ofs, count * typeInfo->size);
+               memcpy(dst_data_ofs, src_data_ofs, (size_t)count * typeInfo->size);
 }
 
 static void CustomData_copy_data_layer(
@@ -2181,16 +2181,14 @@ static void CustomData_copy_data_layer(
         int src_index, int dst_index, int count)
 {
        const LayerTypeInfo *typeInfo;
-       int src_offset;
-       int dst_offset;
 
        const void *src_data = source->layers[src_i].data;
        void *dst_data = dest->layers[dst_i].data;
 
        typeInfo = layerType_getInfo(source->layers[src_i].type);
 
-       src_offset = src_index * typeInfo->size;
-       dst_offset = dst_index * typeInfo->size;
+       const size_t src_offset = (size_t)src_index * typeInfo->size;
+       const size_t dst_offset = (size_t)dst_index * typeInfo->size;
 
        if (!count || !src_data || !dst_data) {
                if (count && !(src_data == NULL && dst_data == NULL)) {
@@ -2201,14 +2199,16 @@ static void CustomData_copy_data_layer(
                return;
        }
 
-       if (typeInfo->copy)
+       if (typeInfo->copy) {
                typeInfo->copy(POINTER_OFFSET(src_data, src_offset),
                               POINTER_OFFSET(dst_data, dst_offset),
                               count);
-       else
+       }
+       else {
                memcpy(POINTER_OFFSET(dst_data, dst_offset),
                       POINTER_OFFSET(src_data, src_offset),
-                      count * typeInfo->size);
+                      (size_t)count * typeInfo->size);
+       }
 }
 
 void CustomData_copy_data_named(const CustomData *source, CustomData *dest,
@@ -2270,7 +2270,7 @@ void CustomData_free_elem(CustomData *data, int index, int count)
                        typeInfo = layerType_getInfo(data->layers[i].type);
 
                        if (typeInfo->free) {
-                               int offset = index * typeInfo->size;
+                               size_t offset = (size_t)index * typeInfo->size;
 
                                typeInfo->free(POINTER_OFFSET(data->layers[i].data, offset), count, typeInfo->size);
                        }
@@ -2316,11 +2316,11 @@ void CustomData_interp(const CustomData *source, CustomData *dest,
                        void *src_data = source->layers[src_i].data;
 
                        for (j = 0; j < count; ++j) {
-                               sources[j] = POINTER_OFFSET(src_data, src_indices[j] * typeInfo->size);
+                               sources[j] = POINTER_OFFSET(src_data, (size_t)src_indices[j] * typeInfo->size);
                        }
 
                        typeInfo->interp(sources, weights, sub_weights, count,
-                                        POINTER_OFFSET(dest->layers[dest_i].data, dest_index * typeInfo->size));
+                                        POINTER_OFFSET(dest->layers[dest_i].data, (size_t)dest_index * typeInfo->size));
 
                        /* if there are multiple source & dest layers of the same type,
                         * we don't want to copy all source layers to the same dest, so
@@ -2349,7 +2349,7 @@ void CustomData_swap_corners(struct CustomData *data, int index, const int *corn
                typeInfo = layerType_getInfo(data->layers[i].type);
 
                if (typeInfo->swap) {
-                       const int offset = index * typeInfo->size;
+                       const size_t offset = (size_t)index * typeInfo->size;
 
                        typeInfo->swap(POINTER_OFFSET(data->layers[i].data, offset), corner_indices);
                }
@@ -2387,7 +2387,6 @@ void CustomData_swap(struct CustomData *data, const int index_a, const int index
 
 void *CustomData_get(const CustomData *data, int index, int type)
 {
-       int offset;
        int layer_index;
        
        BLI_assert(index >= 0);
@@ -2397,7 +2396,7 @@ void *CustomData_get(const CustomData *data, int index, int type)
        if (layer_index == -1) return NULL;
 
        /* get the offset of the desired element */
-       offset = layerType_getInfo(type)->size * index;
+       const size_t offset = (size_t)index * layerType_getInfo(type)->size;
 
        return POINTER_OFFSET(data->layers[layer_index].data, offset);
 }
@@ -2405,7 +2404,6 @@ void *CustomData_get(const CustomData *data, int index, int type)
 void *CustomData_get_n(const CustomData *data, int type, int index, int n)
 {
        int layer_index;
-       int offset;
 
        BLI_assert(index >= 0 && n >= 0);
 
@@ -2413,7 +2411,7 @@ void *CustomData_get_n(const CustomData *data, int type, int index, int n)
        layer_index = data->typemap[type];
        if (layer_index == -1) return NULL;
 
-       offset = layerType_getInfo(type)->size * index;
+       const size_t offset = (size_t)index * layerType_getInfo(type)->size;
        return POINTER_OFFSET(data->layers[layer_index + n].data, offset);
 }
 
@@ -2838,7 +2836,7 @@ void CustomData_bmesh_free_block_data(CustomData *data, void *block)
                        typeInfo = layerType_getInfo(data->layers[i].type);
 
                        if (typeInfo->free) {
-                               int offset = data->layers[i].offset;
+                               const size_t offset = data->layers[i].offset;
                                typeInfo->free(POINTER_OFFSET(block, offset), 1, typeInfo->size);
                        }
                }
@@ -3218,7 +3216,7 @@ void CustomData_to_bmesh_block(const CustomData *source, CustomData *dest,
                                int src_index, void **dest_block, bool use_default_init)
 {
        const LayerTypeInfo *typeInfo;
-       int dest_i, src_i, src_offset;
+       int dest_i, src_i;
 
        if (*dest_block == NULL)
                CustomData_bmesh_alloc_block(dest, dest_block);
@@ -3247,7 +3245,7 @@ void CustomData_to_bmesh_block(const CustomData *source, CustomData *dest,
                        void *dest_data = POINTER_OFFSET(*dest_block, offset);
 
                        typeInfo = layerType_getInfo(dest->layers[dest_i].type);
-                       src_offset = src_index * typeInfo->size;
+                       const size_t src_offset = (size_t)src_index * typeInfo->size;
 
                        if (typeInfo->copy)
                                typeInfo->copy(POINTER_OFFSET(src_data, src_offset), dest_data, 1);
@@ -3294,7 +3292,7 @@ void CustomData_from_bmesh_block(const CustomData *source, CustomData *dest,
                        const LayerTypeInfo *typeInfo = layerType_getInfo(dest->layers[dest_i].type);
                        int offset = source->layers[src_i].offset;
                        const void *src_data = POINTER_OFFSET(src_block, offset);
-                       void *dst_data = POINTER_OFFSET(dest->layers[dest_i].data, dst_index * typeInfo->size);
+                       void *dst_data = POINTER_OFFSET(dest->layers[dest_i].data, (size_t)dst_index * typeInfo->size);
 
                        if (typeInfo->copy)
                                typeInfo->copy(src_data, dst_data, 1);