2018-08-22 00:53:22 +05:30
|
|
|
#ifndef ALLOCATOR_H
|
|
|
|
#define ALLOCATOR_H
|
|
|
|
|
2018-08-30 18:18:09 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
2019-12-29 00:48:52 +05:30
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2018-12-05 14:16:35 +05:30
|
|
|
|
2018-08-22 00:53:22 +05:30
|
|
|
#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 14:54:25 +05:30
|
|
|
|
|
|
|
#define h_posix_memalign posix_memalign
|
|
|
|
|
2018-08-22 00:53:22 +05:30
|
|
|
#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
|
2021-05-13 05:37:15 +05:30
|
|
|
#define h_mallinfo2 mallinfo2
|
2018-08-22 00:53:22 +05:30
|
|
|
#define h_malloc_info malloc_info
|
2018-09-02 14:54:25 +05:30
|
|
|
|
|
|
|
#define h_memalign memalign
|
|
|
|
#define h_valloc valloc
|
|
|
|
#define h_pvalloc pvalloc
|
2018-09-02 14:43:07 +05:30
|
|
|
#define h_cfree cfree
|
2018-08-22 00:53:22 +05:30
|
|
|
#define h_malloc_get_state malloc_get_state
|
|
|
|
#define h_malloc_set_state malloc_set_state
|
2018-09-02 14:54:25 +05:30
|
|
|
|
2019-09-08 04:00:04 +05:30
|
|
|
#define h_mallinfo_narenas mallinfo_narenas
|
|
|
|
#define h_mallinfo_nbins mallinfo_nbins
|
|
|
|
#define h_mallinfo_arena_info mallinfo_arena_info
|
|
|
|
#define h_mallinfo_bin_info mallinfo_bin_info
|
|
|
|
|
2020-09-15 07:03:53 +05:30
|
|
|
#define h_malloc_iterate malloc_iterate
|
2018-09-02 14:54:25 +05:30
|
|
|
#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-19 02:58:52 +05:30
|
|
|
#define h_free_sized free_sized
|
2018-08-22 00:53:22 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
// C standard
|
2022-01-04 20:26:29 +05:30
|
|
|
__attribute__((malloc)) __attribute__((alloc_size(1))) void *h_malloc(size_t size);
|
|
|
|
__attribute__((malloc)) __attribute__((alloc_size(1, 2))) void *h_calloc(size_t nmemb, size_t size);
|
2022-01-03 21:44:55 +05:30
|
|
|
__attribute__((alloc_size(2))) void *h_realloc(void *ptr, size_t size);
|
2022-01-04 20:26:29 +05:30
|
|
|
__attribute__((malloc)) __attribute__((alloc_size(2))) __attribute__((alloc_align(1)))
|
2018-08-22 00:53:22 +05:30
|
|
|
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 13:57:09 +05:30
|
|
|
#ifdef __ANDROID__
|
|
|
|
#define H_MALLOC_USABLE_SIZE_CONST const
|
|
|
|
#else
|
|
|
|
#define H_MALLOC_USABLE_SIZE_CONST
|
|
|
|
#endif
|
|
|
|
|
2018-08-22 00:53:22 +05:30
|
|
|
// glibc extensions
|
2018-12-05 13:57:09 +05:30
|
|
|
size_t h_malloc_usable_size(H_MALLOC_USABLE_SIZE_CONST void *ptr);
|
2018-08-22 00:53:22 +05:30
|
|
|
int h_mallopt(int param, int value);
|
|
|
|
int h_malloc_trim(size_t pad);
|
|
|
|
void h_malloc_stats(void);
|
2018-09-07 00:05:08 +05:30
|
|
|
#if defined(__GLIBC__) || defined(__ANDROID__)
|
2018-08-22 00:53:22 +05:30
|
|
|
struct mallinfo h_mallinfo(void);
|
2018-09-07 00:05:08 +05:30
|
|
|
#endif
|
2019-04-20 02:24:43 +05:30
|
|
|
#ifndef __ANDROID__
|
2018-08-22 00:53:22 +05:30
|
|
|
int h_malloc_info(int options, FILE *fp);
|
2019-04-07 09:54:23 +05:30
|
|
|
#endif
|
2018-08-22 00:53:22 +05:30
|
|
|
|
|
|
|
// obsolete glibc extensions
|
2022-01-04 20:26:29 +05:30
|
|
|
__attribute__((malloc)) __attribute__((alloc_size(2))) __attribute__((alloc_align(1)))
|
2018-08-22 00:53:22 +05:30
|
|
|
void *h_memalign(size_t alignment, size_t size);
|
2019-04-07 10:00:47 +05:30
|
|
|
#ifndef __ANDROID__
|
2022-01-04 20:34:26 +05:30
|
|
|
__attribute__((malloc)) __attribute__((alloc_size(1))) void *h_valloc(size_t size);
|
2022-01-04 20:26:29 +05:30
|
|
|
__attribute__((malloc)) void *h_pvalloc(size_t size);
|
2019-04-07 10:00:47 +05:30
|
|
|
#endif
|
2019-04-07 09:54:23 +05:30
|
|
|
#ifdef __GLIBC__
|
2019-06-19 08:20:10 +05:30
|
|
|
void h_cfree(void *ptr) __THROW;
|
2018-09-02 14:43:07 +05:30
|
|
|
void *h_malloc_get_state(void);
|
|
|
|
int h_malloc_set_state(void *state);
|
2019-04-07 09:54:23 +05:30
|
|
|
#endif
|
2018-08-22 00:53:22 +05:30
|
|
|
|
2018-09-02 14:38:45 +05:30
|
|
|
// Android extensions
|
|
|
|
#ifdef __ANDROID__
|
2019-09-08 04:00:04 +05:30
|
|
|
size_t h_mallinfo_narenas(void);
|
|
|
|
size_t h_mallinfo_nbins(void);
|
|
|
|
struct mallinfo h_mallinfo_arena_info(size_t arena);
|
|
|
|
struct mallinfo h_mallinfo_bin_info(size_t arena, size_t bin);
|
2020-09-15 07:03:53 +05:30
|
|
|
int h_malloc_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t ptr, size_t size, void *arg),
|
2018-09-02 14:38:45 +05:30
|
|
|
void *arg);
|
|
|
|
void h_malloc_disable(void);
|
|
|
|
void h_malloc_enable(void);
|
|
|
|
#endif
|
|
|
|
|
2019-04-07 10:04:13 +05:30
|
|
|
// hardened_malloc extensions
|
2018-08-29 23:13:35 +05:30
|
|
|
|
|
|
|
// return an upper bound on object size for any pointer based on malloc metadata
|
2022-01-04 20:44:41 +05:30
|
|
|
size_t h_malloc_object_size(const void *ptr);
|
2018-08-29 23:13:35 +05:30
|
|
|
|
|
|
|
// similar to malloc_object_size, but avoiding locking so the results are much more limited
|
2022-01-04 20:44:41 +05:30
|
|
|
size_t h_malloc_object_size_fast(const void *ptr);
|
2018-08-29 23:13:35 +05:30
|
|
|
|
2018-10-04 00:07:05 +05:30
|
|
|
// 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-19 02:58:52 +05:30
|
|
|
void h_free_sized(void *ptr, size_t expected_size);
|
|
|
|
|
2019-12-29 00:48:52 +05:30
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2018-12-05 14:16:35 +05:30
|
|
|
|
2018-08-22 00:53:22 +05:30
|
|
|
#endif
|