hammer/src/allocator.h
Steven Dee b1078c3d88 llvm doesn't care about your size
They apparently removed alloc_size at some point (it was a no-op
beforehand), causing the attribute to throw an error when clang compiles
anything including allocator.h.
2014-12-07 02:27:28 -05:00

66 lines
2 KiB
C

/* 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.
*/
#ifndef HAMMER_ALLOCATOR__H__
#define HAMMER_ALLOCATOR__H__
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
// 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;
typedef struct HArena_ HArena ; // hidden implementation
HArena *h_new_arena(HAllocator* allocator, size_t block_size); // pass 0 for default...
#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)))
#else
# define ATTR_MALLOC(n)
#endif
void* h_arena_malloc(HArena *arena, size_t count) ATTR_MALLOC(2);
void h_arena_free(HArena *arena, void* ptr); // For future expansion, with alternate memory managers.
void h_delete_arena(HArena *arena);
typedef struct {
size_t used;
size_t wasted;
} HArenaStats;
void h_allocator_stats(HArena *arena, HArenaStats *stats);
#ifdef __cplusplus
}
#endif
#endif // #ifndef LIB_ALLOCATOR__H__