Blender V2.61 - r43446

util_memarena.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
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 
00019 #include "util_foreach.h"
00020 #include "util_math.h"
00021 #include "util_memarena.h"
00022 
00023 CCL_NAMESPACE_BEGIN
00024 
00025 MemArena::MemArena(bool use_calloc_, size_t buffer_size_)
00026 {
00027     use_calloc = use_calloc_;
00028     buffer_size = buffer_size_;
00029 
00030     last_left = 0;
00031     last_buffer = NULL;
00032 }
00033 
00034 MemArena::~MemArena()
00035 {
00036     foreach(uint8_t *buffer, buffers)
00037         delete [] buffer;
00038 }
00039 
00040 void *MemArena::alloc(size_t size)
00041 {
00042     if(size > last_left) {
00043         last_left = (size > buffer_size)? size: buffer_size;
00044         last_buffer = new uint8_t[last_left];
00045 
00046         if(use_calloc)
00047             memset(last_buffer, 0, last_left);
00048 
00049         buffers.push_back(last_buffer);
00050     }
00051 
00052     uint8_t *mem = last_buffer;
00053 
00054     last_buffer += size;
00055     last_left -= size;
00056 
00057     return (void*)mem;
00058 }
00059 
00060 CCL_NAMESPACE_END
00061