mem/mem_alloc.c

Go to the documentation of this file.
00001 // Copyright (C) 2006-2007 Benedikt Böhm <hollow@gentoo.org>
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00016 
00017 #include <stdlib.h>
00018 
00019 #include "mem.h"
00020 #include "mem_internal.h"
00021 
00022 _mem_pool_t *_mem_pool = 0;
00023 
00024 void *mem_alloc(int n)
00025 {
00026         if (!_mem_pool) {
00027                 if ((_mem_pool = malloc(sizeof(_mem_pool_t))) == NULL)
00028                         return NULL;
00029 
00030                 INIT_LIST_HEAD(&(_mem_pool->list));
00031         }
00032 
00033         _mem_pool_t *new;
00034 
00035         if ((new = malloc(sizeof(_mem_pool_t))) == NULL)
00036                 return NULL;
00037 
00038         new->len = n;
00039 
00040         if ((new->mem = malloc(new->len)) == NULL) {
00041                 free(new);
00042                 return NULL;
00043         }
00044 
00045         mem_set(new->mem, 0, new->len);
00046 
00047         list_add_tail(&(new->list), &(_mem_pool->list));
00048 
00049         return new->mem;
00050 }

Generated on Tue Jun 19 20:38:26 2007 for lucid by  doxygen 1.5.2