00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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 }