Blender V2.61 - r43446

BKE_array_mallocn.h

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00028 #ifndef BKE_ARRAY_MALLOCN_H
00029 #define BKE_ARRAY_MALLOCN_H
00030 
00036 /* example of usage:
00037  *
00038  * int *arr = NULL;
00039  * V_DECLARE(arr);
00040  * int i;
00041  *
00042  * for (i=0; i<10; i++) {
00043  *  V_GROW(arr);
00044  *  arr[i] = something;
00045  * }
00046  * V_FREE(arr);
00047  *
00048  * arrays are buffered, using double-buffering (so on each reallocation,
00049  * the array size is doubled).  supposedly this should give good Big Oh
00050  * behaviour, though it may not be the best in practice.
00051  */
00052 
00053 #define V_DECLARE(vec) int _##vec##_count=0; void *_##vec##_tmp
00054 
00055 /* in the future, I plan on having V_DECLARE allocate stack memory it'll
00056  * use at first, and switch over to heap when it needs more.  that'll mess
00057  * up cases where you'd want to use this API to build a dynamic list for
00058  * non-local use, so all such cases should use this macro.*/
00059 #define V_DYNDECLARE(vec) V_DECLARE(vec)
00060 
00061 /*this returns the entire size of the array, including any buffering.*/
00062 #define V_SIZE(vec) ((signed int)((vec)==NULL ? 0 : MEM_allocN_len(vec) / sizeof(*vec)))
00063 
00064 /*this returns the logical size of the array, not including buffering.*/
00065 #define V_COUNT(vec) _##vec##_count
00066 
00067 /*grow the array by one.  zeroes the new elements.*/
00068 #define V_GROW(vec) \
00069     V_SIZE(vec) > _##vec##_count ? _##vec##_count++ : \
00070     ((_##vec##_tmp = MEM_callocN(sizeof(*vec)*(_##vec##_count*2+2), #vec " " __FILE__ " ")),\
00071     (void)(vec && memcpy(_##vec##_tmp, vec, sizeof(*vec) * _##vec##_count)),\
00072     (void)(vec && (MEM_freeN(vec),1)),\
00073     (vec = _##vec##_tmp),\
00074     _##vec##_count++)
00075 
00076 #define V_FREE(vec) if (vec) MEM_freeN(vec);
00077 
00078 /*resets the logical size of an array to zero, but doesn't
00079   free the memory.*/
00080 #define V_RESET(vec) _##vec##_count=0
00081 
00082 /*set the count of the array*/
00083 #define V_SETCOUNT(vec, count) _##vec##_count = (count)
00084 
00085 #endif // BKE_ARRAY_MALLOCN_H