mem/mem_realloc.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 #include <errno.h>
00019 
00020 #include "mem.h"
00021 #include "mem_internal.h"
00022 
00023 void *mem_realloc(void *s, int n)
00024 {
00025         if (!s) {
00026                 if (n > 0)
00027                         return mem_alloc(n);
00028                 else
00029                         return NULL;
00030         }
00031 
00032         else if (n < 1) {
00033                 mem_free(s);
00034                 return NULL;
00035         }
00036 
00037         _mem_pool_t *p;
00038 
00039         mem_for_each(_mem_pool, p)
00040                 if (p->mem == s)
00041                         break;
00042 
00043         if (p->mem != s) {
00044                 errno = EINVAL;
00045                 return NULL;
00046         }
00047 
00048         char *m = realloc(p->mem, n);
00049 
00050         if (!m)
00051                 return NULL;
00052 
00053         p->mem = m;
00054 
00055         if (n > p->len)
00056                 mem_set(m + p->len, 0, n - p->len);
00057 
00058         p->len = n;
00059 
00060         return p->mem;
00061 }

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