1/* 2 * Copyright 2017, Data61, CSIRO (ABN 41 687 119 230) 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#ifndef HEAP_CHECK_H_ 8#define HEAP_CHECK_H_ 9 10#include <assert.h> 11#include <stdbool.h> 12#include <stdint.h> 13#include <stdlib.h> 14 15static inline bool is_heap_pointer(void *p) { 16 extern char *morecore_area; 17 extern size_t morecore_size; 18 return (uintptr_t)morecore_area <= (uintptr_t)p && 19 (uintptr_t)morecore_area + morecore_size > (uintptr_t)p; 20} 21 22static inline void safe_free(void *p) { 23 assert(is_heap_pointer(p)); 24 free(p); 25} 26 27#endif 28