src/pool.c

00001 /*
00002  * Copyright (c) 2005 Jukka Zitting <jz@yukatan.fi>
00003  *
00004  * This program is free software; you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License as published
00006  * by the Free Software Foundation; either version 2 of the License, or
00007  * (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
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 #include <config.h>
00020 #include "midgard/pool.h"
00021 #include <glib.h>
00022 
00023 struct midgard_pool {
00024         GSList *entries;
00025 };
00026 
00027 midgard_pool *mgd_alloc_pool(void) {
00028         midgard_pool *pool = g_new(midgard_pool, 1);
00029         pool->entries = NULL;
00030         return pool;
00031 }
00032 
00033 void mgd_clear_pool(midgard_pool *pool) {
00034         g_assert(pool != NULL);
00035         while (pool->entries != NULL) {
00036             GSList *entry = pool->entries;
00037             pool->entries = g_slist_next(entry);
00038             entry = g_slist_remove(entry, entry->data);
00039         }
00040 }
00041 
00042 void mgd_free_from_pool(midgard_pool *pool, void *ptr) {
00043         g_assert(pool != NULL);
00044         g_assert(ptr != NULL);
00045         pool->entries = g_slist_remove(pool->entries, ptr);
00046         g_free(ptr);
00047 }
00048 
00049 void mgd_free_pool(midgard_pool *pool) {
00050         g_assert(pool != NULL);
00051         mgd_clear_pool(pool);
00052         g_free(pool);
00053 }
00054 
00055 void *mgd_alloc(midgard_pool *pool, int len) {
00056         g_assert(pool != NULL);
00057         g_assert(len >= 0);
00058         void *data = g_malloc(len);
00059         pool->entries = g_slist_prepend(pool->entries, data);
00060         return data;
00061 }
00062 
00063 char *mgd_stralloc(midgard_pool *pool, int len) {
00064         g_assert(pool != NULL);
00065         g_assert(len >= 0);
00066         char *str = g_malloc(len + 1);
00067         pool->entries = g_slist_prepend(pool->entries, str);
00068         return str;
00069 }
00070 
00071 
00072 char *mgd_strdup(midgard_pool * pool, const char *str) {
00073         g_assert(pool != NULL);
00074         g_assert(str != NULL);
00075         char *strcopy = g_strdup(str);
00076         pool->entries = g_slist_prepend(pool->entries, strcopy);
00077         return strcopy;
00078 }
00079 
00080 char *mgd_strndup(midgard_pool * pool, const char *str, int len) {
00081         g_assert(pool != NULL);
00082         g_assert(str != NULL);
00083         g_assert(len >= 0);
00084         char *strcopy = g_strndup(str, len);
00085         pool->entries = g_slist_prepend(pool->entries, strcopy);
00086         return strcopy;
00087 }

Generated on Thu Feb 22 06:15:18 2007 for midgard-core by  doxygen 1.4.6