Blender V2.61 - r43446
|
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 * Efficient memory allocation for lots of similar small chunks. 00027 */ 00028 00035 #include "MEM_guardedalloc.h" 00036 00037 #include "BLI_memarena.h" 00038 #include "BLI_linklist.h" 00039 00040 struct MemArena { 00041 unsigned char *curbuf; 00042 int bufsize, cursize; 00043 const char *name; 00044 00045 int use_calloc; 00046 int align; 00047 00048 LinkNode *bufs; 00049 }; 00050 00051 MemArena *BLI_memarena_new(int bufsize, const char *name) 00052 { 00053 MemArena *ma= MEM_callocN(sizeof(*ma), "memarena"); 00054 ma->bufsize= bufsize; 00055 ma->align = 8; 00056 ma->name= name; 00057 00058 return ma; 00059 } 00060 00061 void BLI_memarena_use_calloc(MemArena *ma) 00062 { 00063 ma->use_calloc= 1; 00064 } 00065 00066 void BLI_memarena_use_malloc(MemArena *ma) 00067 { 00068 ma->use_calloc= 0; 00069 } 00070 00071 void BLI_memarena_use_align(struct MemArena *ma, int align) 00072 { 00073 /* align should be a power of two */ 00074 ma->align = align; 00075 } 00076 00077 void BLI_memarena_free(MemArena *ma) 00078 { 00079 BLI_linklist_free(ma->bufs, (void(*)(void*)) MEM_freeN); 00080 MEM_freeN(ma); 00081 } 00082 00083 /* amt must be power of two */ 00084 #define PADUP(num, amt) ((num+(amt-1))&~(amt-1)) 00085 00086 void *BLI_memarena_alloc(MemArena *ma, int size) 00087 { 00088 void *ptr; 00089 00090 /* ensure proper alignment by rounding 00091 * size up to multiple of 8 */ 00092 size= PADUP(size, ma->align); 00093 00094 if (size>ma->cursize) { 00095 unsigned char *tmp; 00096 00097 if(size > ma->bufsize - (ma->align - 1)) 00098 { 00099 ma->cursize = PADUP(size+1, ma->align); 00100 } 00101 else ma->cursize = ma->bufsize; 00102 00103 if(ma->use_calloc) 00104 ma->curbuf= MEM_callocN(ma->cursize, ma->name); 00105 else 00106 ma->curbuf= MEM_mallocN(ma->cursize, ma->name); 00107 00108 BLI_linklist_prepend(&ma->bufs, ma->curbuf); 00109 00110 /* align alloc'ed memory (needed if align > 8) */ 00111 tmp = (unsigned char*)PADUP( (intptr_t) ma->curbuf, ma->align); 00112 ma->cursize -= (tmp - ma->curbuf); 00113 ma->curbuf = tmp; 00114 } 00115 00116 ptr= ma->curbuf; 00117 ma->curbuf+= size; 00118 ma->cursize-= size; 00119 00120 return ptr; 00121 } 00122