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