op_libiberty.h revision 7984f7ab3e13cda0c3b04ffeb2608f232e57f93a
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
38/* Set the program name used by xmalloc.  */
39void xmalloc_set_program_name(char const *);
40
41/* Allocate memory without fail.  If malloc fails, this will print a
42   message to stderr (using the name set by xmalloc_set_program_name,
43   if any) and then call xexit.  */
44void * xmalloc(size_t) OP_ATTRIB_MALLOC;
45
46/* Reallocate memory without fail.  This works like xmalloc.  Note,
47   realloc type functions are not suitable for attribute malloc since
48   they may return the same address across multiple calls. */
49void * xrealloc(void *, size_t);
50
51/* Allocate memory without fail and set it to zero.  This works like xmalloc */
52void * xcalloc(size_t, size_t) OP_ATTRIB_MALLOC;
53
54/* Copy a string into a memory buffer without fail.  */
55char * xstrdup(char const *) OP_ATTRIB_MALLOC;
56
57/**
58 * Duplicates a region of memory without fail.  First, alloc_size bytes
59 * are allocated, then copy_size bytes from input are copied into
60 * it, and the new memory is returned.  If fewer bytes are copied than were
61 * allocated, the remaining memory is zeroed.
62 */
63void * xmemdup(void const *, size_t, size_t) OP_ATTRIB_MALLOC;
64
65#endif	/* !HAVE_LIBIBERTY_H */
66
67#ifdef ANDROID
68#define xmalloc(s)      malloc(s)
69#define xrealloc(p,s)   realloc(p,s)
70#define xstrdup(str)    strdup(str)
71#define xmalloc_set_program_name(n)
72#endif
73
74
75#ifdef __cplusplus
76}
77#endif
78
79#endif /* !HAVE_LIBIBERTY_H */
80
81#endif /* OP_LIBIBERTY_H */
82