2018-08-21 21:23:22 +02:00
|
|
|
#ifndef ALLOCATOR_H
|
|
|
|
#define ALLOCATOR_H
|
|
|
|
|
2018-08-30 14:48:09 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
2018-12-05 09:46:35 +01:00
|
|
|
__BEGIN_DECLS
|
|
|
|
|
2018-08-21 21:23:22 +02:00
|
|
|
#ifndef H_MALLOC_PREFIX
|
|
|
|
#define h_malloc malloc
|
|
|
|
#define h_calloc calloc
|
|
|
|
#define h_realloc realloc
|
|
|
|
#define h_aligned_alloc aligned_alloc
|
|
|
|
#define h_free free
|
2018-09-02 11:24:25 +02:00
|
|
|
|
|
|
|
#define h_posix_memalign posix_memalign
|
|
|
|
|
2018-08-21 21:23:22 +02:00
|
|
|
#define h_malloc_usable_size malloc_usable_size
|
|
|
|
#define h_mallopt mallopt
|
|
|
|
#define h_malloc_trim malloc_trim
|
|
|
|
#define h_malloc_stats malloc_stats
|
|
|
|
#define h_mallinfo mallinfo
|
|
|
|
#define h_malloc_info malloc_info
|
2018-09-02 11:24:25 +02:00
|
|
|
|
|
|
|
#define h_memalign memalign
|
|
|
|
#define h_valloc valloc
|
|
|
|
#define h_pvalloc pvalloc
|
2018-09-02 11:13:07 +02:00
|
|
|
#define h_cfree cfree
|
2018-08-21 21:23:22 +02:00
|
|
|
#define h_malloc_get_state malloc_get_state
|
|
|
|
#define h_malloc_set_state malloc_set_state
|
2018-09-02 11:24:25 +02:00
|
|
|
|
|
|
|
#define h_iterate iterate
|
|
|
|
#define h_malloc_disable malloc_disable
|
|
|
|
#define h_malloc_enable malloc_enable
|
|
|
|
|
|
|
|
#define h_malloc_object_size malloc_object_size
|
|
|
|
#define h_malloc_object_size_fast malloc_object_size_fast
|
2018-09-18 23:28:52 +02:00
|
|
|
#define h_free_sized free_sized
|
2018-08-21 21:23:22 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// C standard
|
|
|
|
void *h_malloc(size_t size);
|
|
|
|
void *h_calloc(size_t nmemb, size_t size);
|
|
|
|
void *h_realloc(void *ptr, size_t size);
|
|
|
|
void *h_aligned_alloc(size_t alignment, size_t size);
|
|
|
|
void h_free(void *ptr);
|
|
|
|
|
|
|
|
// POSIX
|
|
|
|
int h_posix_memalign(void **memptr, size_t alignment, size_t size);
|
|
|
|
|
2018-12-05 09:27:09 +01:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
#define H_MALLOC_USABLE_SIZE_CONST const
|
|
|
|
#else
|
|
|
|
#define H_MALLOC_USABLE_SIZE_CONST
|
|
|
|
#endif
|
|
|
|
|
2018-08-21 21:23:22 +02:00
|
|
|
// glibc extensions
|
2018-12-05 09:27:09 +01:00
|
|
|
size_t h_malloc_usable_size(H_MALLOC_USABLE_SIZE_CONST void *ptr);
|
2018-08-21 21:23:22 +02:00
|
|
|
int h_mallopt(int param, int value);
|
|
|
|
int h_malloc_trim(size_t pad);
|
|
|
|
void h_malloc_stats(void);
|
2018-09-06 20:35:08 +02:00
|
|
|
#if defined(__GLIBC__) || defined(__ANDROID__)
|
2018-08-21 21:23:22 +02:00
|
|
|
struct mallinfo h_mallinfo(void);
|
2018-09-06 20:35:08 +02:00
|
|
|
#endif
|
2019-04-07 06:24:23 +02:00
|
|
|
#ifdef __GLIBC__
|
2018-08-21 21:23:22 +02:00
|
|
|
int h_malloc_info(int options, FILE *fp);
|
2019-04-07 06:24:23 +02:00
|
|
|
#endif
|
2018-08-21 21:23:22 +02:00
|
|
|
|
|
|
|
// obsolete glibc extensions
|
|
|
|
void *h_memalign(size_t alignment, size_t size);
|
2019-04-07 06:30:47 +02:00
|
|
|
#ifndef __ANDROID__
|
2018-08-21 21:23:22 +02:00
|
|
|
void *h_valloc(size_t size);
|
|
|
|
void *h_pvalloc(size_t size);
|
2019-04-07 06:30:47 +02:00
|
|
|
#endif
|
2019-04-07 06:24:23 +02:00
|
|
|
#ifdef __GLIBC__
|
2018-08-21 21:23:22 +02:00
|
|
|
void h_cfree(void *ptr);
|
2018-09-02 11:13:07 +02:00
|
|
|
void *h_malloc_get_state(void);
|
|
|
|
int h_malloc_set_state(void *state);
|
2019-04-07 06:24:23 +02:00
|
|
|
#endif
|
2018-08-21 21:23:22 +02:00
|
|
|
|
2018-09-02 11:08:45 +02:00
|
|
|
// Android extensions
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
size_t __mallinfo_narenas(void);
|
|
|
|
size_t __mallinfo_nbins(void);
|
|
|
|
struct mallinfo __mallinfo_arena_info(size_t arena);
|
|
|
|
struct mallinfo __mallinfo_bin_info(size_t arena, size_t bin);
|
|
|
|
int h_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t ptr, size_t size, void *arg),
|
|
|
|
void *arg);
|
|
|
|
void h_malloc_disable(void);
|
|
|
|
void h_malloc_enable(void);
|
|
|
|
#endif
|
|
|
|
|
2019-04-07 06:34:13 +02:00
|
|
|
// hardened_malloc extensions
|
2018-08-29 19:43:35 +02:00
|
|
|
|
|
|
|
// return an upper bound on object size for any pointer based on malloc metadata
|
|
|
|
size_t h_malloc_object_size(void *ptr);
|
|
|
|
|
|
|
|
// similar to malloc_object_size, but avoiding locking so the results are much more limited
|
|
|
|
size_t h_malloc_object_size_fast(void *ptr);
|
|
|
|
|
2018-10-03 20:37:05 +02:00
|
|
|
// The free function with an extra parameter for passing the size requested at
|
|
|
|
// allocation time.
|
|
|
|
//
|
|
|
|
// This offers the same functionality as C++14 sized deallocation and can be
|
|
|
|
// used to implement it.
|
|
|
|
//
|
|
|
|
// A performance-oriented allocator would use this as a performance
|
|
|
|
// enhancement with undefined behavior on a mismatch. Instead, this hardened
|
|
|
|
// allocator implementation uses it to improve security by checking that the
|
|
|
|
// passed size matches the allocated size.
|
2018-09-18 23:28:52 +02:00
|
|
|
void h_free_sized(void *ptr, size_t expected_size);
|
|
|
|
|
2018-12-05 09:46:35 +01:00
|
|
|
__END_DECLS
|
|
|
|
|
2018-08-21 21:23:22 +02:00
|
|
|
#endif
|