1/** 2 * @file op_libiberty.h 3 * Wrapper for libiberty - always use this instead of 4 * libiberty.h 5 * 6 * @remark Copyright 2002 OProfile authors 7 * @remark Read the file COPYING 8 * 9 * @author John Levon 10 * @author Philippe Elie 11 */ 12 13#ifndef OP_LIBIBERTY_H 14#define OP_LIBIBERTY_H 15 16#include <stddef.h> 17 18#include "config.h" 19 20#ifdef MALLOC_ATTRIBUTE_OK 21#define OP_ATTRIB_MALLOC __attribute__((malloc)) 22#else 23#define OP_ATTRIB_MALLOC 24#endif 25 26#ifdef HAVE_LIBIBERTY_H 27#include <libiberty.h> 28#else 29 30#ifdef __cplusplus 31extern "C" { 32#endif 33 34/* some system have a libiberty.a but no libiberty.h so we must provide 35 * ourself the missing proto */ 36#ifndef HAVE_LIBIBERTY_H 37/* Set the program name used by xmalloc. */ 38void xmalloc_set_program_name(char const *); 39 40/* Allocate memory without fail. If malloc fails, this will print a 41 message to stderr (using the name set by xmalloc_set_program_name, 42 if any) and then call xexit. */ 43void * xmalloc(size_t) OP_ATTRIB_MALLOC; 44 45/* Reallocate memory without fail. This works like xmalloc. Note, 46 realloc type functions are not suitable for attribute malloc since 47 they may return the same address across multiple calls. */ 48void * xrealloc(void *, size_t); 49 50/* Allocate memory without fail and set it to zero. This works like xmalloc */ 51void * xcalloc(size_t, size_t) OP_ATTRIB_MALLOC; 52 53/* Copy a string into a memory buffer without fail. */ 54char * xstrdup(char const *) OP_ATTRIB_MALLOC; 55 56/** 57 * Duplicates a region of memory without fail. First, alloc_size bytes 58 * are allocated, then copy_size bytes from input are copied into 59 * it, and the new memory is returned. If fewer bytes are copied than were 60 * allocated, the remaining memory is zeroed. 61 */ 62void * xmemdup(void const *, size_t, size_t) OP_ATTRIB_MALLOC; 63 64#endif /* !HAVE_LIBIBERTY_H */ 65 66#ifdef ANDROID 67#define xmalloc(s) malloc(s) 68#define xrealloc(p,s) realloc(p,s) 69#define xstrdup(str) strdup(str) 70#define xmalloc_set_program_name(n) 71#endif 72 73#ifdef __cplusplus 74} 75#endif 76 77#endif /* !HAVE_LIBIBERTY_H */ 78 79#endif /* OP_LIBIBERTY_H */ 80