Home | API | File List | Examples | Download

lib3ds_util.c

00001 /*
00002     Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com>
00003     All rights reserved.
00004     
00005     This program is free  software: you can redistribute it and/or modify 
00006     it under the terms of the GNU Lesser General Public License as published 
00007     by the Free Software Foundation, either version 2.1 of the License, or 
00008     (at your option) any later version.
00009 
00010     Thisprogram  is  distributed in the hope that it will be useful, 
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
00013     GNU Lesser General Public License for more details.
00014     
00015     You should  have received a copy of the GNU Lesser General Public License
00016     along with  this program; If not, see <http://www.gnu.org/licenses/>. 
00017 */
00018 #include "lib3ds_impl.h"
00019 
00020 
00021 void* lib3ds_util_realloc_array(void *ptr, int old_size, int new_size, int element_size) {
00022     if (!ptr)
00023         old_size = 0;
00024     if (old_size != new_size) {
00025         ptr = realloc(ptr, element_size * new_size);
00026         if (old_size < new_size) {
00027             memset((char*)ptr + element_size * old_size, 0, element_size * (new_size - old_size));
00028         }
00029     }
00030     return ptr;
00031 }
00032 
00033 
00034 void lib3ds_util_reserve_array(void ***ptr, int *n, int *size, int new_size, int force, Lib3dsFreeFunc free_func) {
00035     assert(ptr && n && size);
00036     if ((*size < new_size) || force) {
00037         if (force && free_func) {
00038             int i;
00039             for (i = new_size; i < *n; ++i) {
00040                 free_func((*ptr)[i]);
00041                 (*ptr)[i] = 0;
00042             }
00043         }
00044         *ptr = (void*)realloc(*ptr, sizeof(void*) * new_size);
00045         *size = new_size;
00046         if (*n > new_size) {
00047             *n = new_size;
00048         }
00049     }
00050 }
00051 
00052 
00053 void lib3ds_util_insert_array(void ***ptr, int *n, int *size, void *element, int index) {
00054     int i;
00055     assert(ptr && n && size && element);
00056     i = ((index >= 0) && (index < *n)) ? index : *n;
00057     if (i >= *size) {
00058         int new_size = 2 * (*size);
00059         #ifdef _DEBUG
00060             if (new_size < 1) {
00061                 new_size = 1;
00062             }
00063         #else
00064             if (new_size < 32) {
00065                 new_size = 32;
00066             }
00067         #endif
00068         lib3ds_util_reserve_array(ptr, n, size, new_size, FALSE, NULL);
00069     }
00070     assert(*ptr);
00071     if (i < *n) {
00072         memmove(&(*ptr)[i+1], &(*ptr)[i], sizeof(void*) * (*n - i));
00073     }
00074     (*ptr)[i] = element;
00075     *n = *n + 1;
00076 }
00077 
00078 
00079 void lib3ds_util_remove_array(void ***ptr, int *n, int index, Lib3dsFreeFunc free_func) {
00080     assert(ptr && n);
00081     if ((index >= 0) && (index < *n)) {
00082         assert(*ptr);
00083         free_func((*ptr)[index]);
00084         if (index < *n - 1) {
00085             memmove(&(*ptr)[index], &(*ptr)[index+1], sizeof(void*) * (*n - index - 1));
00086         }
00087         *n = *n - 1;
00088     }
00089 }