tre_mem.hpp

00001 #line 6930 "./lpsrc/tre.pak"
00002 /*
00003   tre-mem.h - TRE memory allocator interface
00004 
00005   Copyright (C) 2001-2003 Ville Laurikari <vl@iki.fi>
00006 
00007   This program is free software; you can redistribute it and/or modify
00008   it under the terms of the GNU General Public License version 2 (June
00009   1991) as published by the Free Software Foundation.
00010 
00011   This program is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014   GNU General Public License for more details.
00015 
00016   You should have received a copy of the GNU General Public License
00017   along with this program; if not, write to the Free Software
00018   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 */
00021 
00022 #ifndef TRE_MEM_H
00023 #define TRE_MEM_H 1
00024 
00025 #include <stdlib.h>
00026 
00027 #define TRE_MEM_BLOCK_SIZE 1024
00028 
00029 typedef struct tre_list {
00030   void *data;
00031   struct tre_list *next;
00032 } tre_list_t;
00033 
00034 typedef struct tre_mem_struct {
00035   tre_list_t *blocks;
00036   tre_list_t *current;
00037   char *ptr;
00038   size_t n;
00039   int failed;
00040   void **provided;
00041 } *tre_mem_t;
00042 
00043 
00044 tre_mem_t tre_mem_new_impl(int provided, void *provided_block);
00045 void *tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
00046                          int zero, size_t size);
00047 
00048 /* Returns a new memory allocator or NULL if out of memory. */
00049 #define tre_mem_new()  tre_mem_new_impl(0, NULL)
00050 
00051 /* Allocates a block of `size' bytes from `mem'.  Returns a pointer to the
00052    allocated block or NULL if an underlying malloc() failed. */
00053 #define tre_mem_alloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 0, size)
00054 
00055 /* Allocates a block of `size' bytes from `mem'.  Returns a pointer to the
00056    allocated block or NULL if an underlying malloc() failed.  The memory
00057    is set to zero. */
00058 #define tre_mem_calloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 1, size)
00059 
00060 #ifdef TRE_USE_ALLOCA
00061 /* alloca() versions.  Like above, but memory is allocated with alloca()
00062    instead of malloc(). */
00063 
00064 #define tre_mem_newa() \
00065   tre_mem_new_impl(1, alloca(sizeof(struct tre_mem_struct)))
00066 
00067 #define tre_mem_alloca(mem, size)                                             \
00068   ((mem)->n >= (size)                                                         \
00069    ? tre_mem_alloc_impl((mem), 1, NULL, 0, (size))                            \
00070    : tre_mem_alloc_impl((mem), 1, alloca(TRE_MEM_BLOCK_SIZE), 0, (size)))
00071 #endif /* TRE_USE_ALLOCA */
00072 
00073 
00074 /* Frees the memory allocator and all memory allocated with it. */
00075 void tre_mem_destroy(tre_mem_t mem);
00076 
00077 #endif /* TRE_MEM_H */
00078 
00079 /* EOF */

Generated on Fri Jun 8 03:04:31 2007 for Felix by  doxygen 1.5.2