2012-05-12 22:26:59 +01:00
|
|
|
/* Arena allocator for Hammer.
|
|
|
|
|
* Copyright (C) 2012 Meredith L. Patterson, Dan "TQ" Hirsch
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation, version 2.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-04-23 19:39:44 +01:00
|
|
|
#ifndef HAMMER_ALLOCATOR__H__
|
|
|
|
|
#define HAMMER_ALLOCATOR__H__
|
2012-04-23 00:35:58 +01:00
|
|
|
#include <sys/types.h>
|
2015-10-30 21:23:45 +01:00
|
|
|
#include <setjmp.h>
|
2012-04-23 00:35:58 +01:00
|
|
|
|
2014-01-16 18:58:36 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-12-07 02:23:01 -05:00
|
|
|
#if defined __llvm__
|
|
|
|
|
# if __has_attribute(malloc)
|
|
|
|
|
# define ATTR_MALLOC(n) __attribute__((malloc))
|
|
|
|
|
# else
|
|
|
|
|
# define ATTR_MALLOC(n)
|
|
|
|
|
# endif
|
|
|
|
|
#elif defined SWIG
|
|
|
|
|
# define ATTR_MALLOC(n)
|
|
|
|
|
#elif defined __GNUC__
|
|
|
|
|
# define ATTR_MALLOC(n) __attribute__((malloc, alloc_size(2)))
|
2013-11-16 07:56:47 +01:00
|
|
|
#else
|
2014-12-07 02:23:01 -05:00
|
|
|
# define ATTR_MALLOC(n)
|
2013-11-16 07:56:47 +01:00
|
|
|
#endif
|
2014-12-07 02:23:01 -05:00
|
|
|
|
2015-11-30 16:37:00 +01:00
|
|
|
// TODO(thequux): Turn this into an "HAllocatorVtable", and add a wrapper that also takes an environment pointer.
|
|
|
|
|
typedef struct HAllocator_ {
|
|
|
|
|
void* (*alloc)(struct HAllocator_* allocator, size_t size);
|
|
|
|
|
void* (*realloc)(struct HAllocator_* allocator, void* ptr, size_t size);
|
|
|
|
|
void (*free)(struct HAllocator_* allocator, void* ptr);
|
|
|
|
|
} HAllocator;
|
|
|
|
|
|
|
|
|
|
void* h_alloc(HAllocator* allocator, size_t size) ATTR_MALLOC(2);
|
|
|
|
|
|
|
|
|
|
typedef struct HArena_ HArena ; // hidden implementation
|
|
|
|
|
|
|
|
|
|
HArena *h_new_arena(HAllocator* allocator, size_t block_size); // pass 0 for default...
|
|
|
|
|
|
2014-12-07 02:23:01 -05:00
|
|
|
void* h_arena_malloc(HArena *arena, size_t count) ATTR_MALLOC(2);
|
2012-10-08 17:11:47 +02:00
|
|
|
void h_arena_free(HArena *arena, void* ptr); // For future expansion, with alternate memory managers.
|
2012-05-26 14:06:52 +02:00
|
|
|
void h_delete_arena(HArena *arena);
|
2015-10-30 21:23:45 +01:00
|
|
|
void h_arena_set_except(HArena *arena, jmp_buf *except);
|
2012-04-23 00:35:58 +01:00
|
|
|
|
2012-05-12 23:04:54 +01:00
|
|
|
typedef struct {
|
|
|
|
|
size_t used;
|
|
|
|
|
size_t wasted;
|
2012-05-26 13:01:23 +02:00
|
|
|
} HArenaStats;
|
2012-05-12 23:04:54 +01:00
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
void h_allocator_stats(HArena *arena, HArenaStats *stats);
|
2012-05-12 23:04:54 +01:00
|
|
|
|
2014-01-16 18:58:36 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2012-04-23 00:35:58 +01:00
|
|
|
|
|
|
|
|
#endif // #ifndef LIB_ALLOCATOR__H__
|