mempool-system.c revision 333e77916f5c17eac34c77b54281a5e9435032c3
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen/* Copyright (c) 2002-2003 Timo Sirainen */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen/* @UNSAFE: whole file */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#include "lib.h"
75bf6f49f6b8ee403f26a609d5c0c726a3262c54Timo Sirainen#include "mempool.h"
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen#include <stdlib.h>
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#ifdef HAVE_GC_GC_H
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen# include <gc/gc.h>
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#elif defined (HAVE_GC_H)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen# include <gc.h>
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#endif
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenstatic const char *pool_system_get_name(pool_t pool);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenstatic void pool_system_ref(pool_t pool);
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainenstatic void pool_system_unref(pool_t *pool);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenstatic void *pool_system_malloc(pool_t pool, size_t size);
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainenstatic void pool_system_free(pool_t pool, void *mem);
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainenstatic void *pool_system_realloc(pool_t pool, void *mem,
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen size_t old_size, size_t new_size);
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainenstatic void pool_system_clear(pool_t pool);
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainenstatic size_t pool_system_get_max_easy_alloc_size(pool_t pool);
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenstatic struct pool static_system_pool = {
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen pool_system_get_name,
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen pool_system_ref,
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen pool_system_unref,
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen pool_system_malloc,
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen pool_system_free,
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen pool_system_realloc,
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen pool_system_clear,
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen pool_system_get_max_easy_alloc_size,
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen FALSE,
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen FALSE
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen};
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainenpool_t system_pool = &static_system_pool;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenstatic const char *pool_system_get_name(pool_t pool __attr_unused__)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen return "system";
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen}
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenstatic void pool_system_ref(pool_t pool __attr_unused__)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainenstatic void pool_system_unref(pool_t *pool __attr_unused__)
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen{
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen}
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainenstatic void *pool_system_malloc(pool_t pool __attr_unused__, size_t size)
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen{
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen void *mem;
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen if (size == 0 || size > SSIZE_T_MAX)
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen#ifndef USE_GC
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen mem = calloc(size, 1);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen#else
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen mem = GC_malloc(size);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#endif
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen if (mem == NULL) {
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen i_fatal_status(FATAL_OUTOFMEM,
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen "pool_system_malloc(): Out of memory");
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen }
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen return mem;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenstatic void pool_system_free(pool_t pool __attr_unused__,
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen void *mem __attr_unused__)
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen{
bda9a6d9b021c122a01a85cb3cee2f996263d8f0Timo Sirainen#ifndef USE_GC
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen if (mem != NULL)
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen free(mem);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#endif
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenstatic void *pool_system_realloc(pool_t pool __attr_unused__, void *mem,
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen size_t old_size, size_t new_size)
4d2211dac61c615c5bdfd501ea54d46c89d41b0fTimo Sirainen{
4d2211dac61c615c5bdfd501ea54d46c89d41b0fTimo Sirainen if (new_size == 0 || new_size > SSIZE_T_MAX)
4d2211dac61c615c5bdfd501ea54d46c89d41b0fTimo Sirainen i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#ifndef USE_GC
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen mem = realloc(mem, new_size);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#else
4d2211dac61c615c5bdfd501ea54d46c89d41b0fTimo Sirainen mem = GC_realloc(mem, new_size);
4d2211dac61c615c5bdfd501ea54d46c89d41b0fTimo Sirainen#endif
4d2211dac61c615c5bdfd501ea54d46c89d41b0fTimo Sirainen if (mem == NULL) {
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen i_fatal_status(FATAL_OUTOFMEM,
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen "pool_system_realloc(): Out of memory");
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (old_size < new_size) {
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen /* clear new data */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen memset((char *) mem + old_size, 0, new_size - old_size);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen }
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen return mem;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenstatic void pool_system_clear(pool_t pool __attr_unused__)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen i_panic("pool_system_clear() must not be called");
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
2ebeb22b9a8a8bb7fbe2f2e2908478a220792b87Timo Sirainenstatic size_t pool_system_get_max_easy_alloc_size(pool_t pool __attr_unused__)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return 0;
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen