Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / ep / ep_mem.h @ master

History | View | Annotate | Download (4.46 KB)

1
/* vim: set ai sw=8 sts=8 ts=8 :*/
2

    
3
/***********************************************************************
4
**  ----- BEGIN LICENSE BLOCK -----
5
**        LIBEP: Enhanced Portability Library (Reduced Edition)
6
**
7
**        Copyright (c) 2008-2019, Eric P. Allman.  All rights reserved.
8
**        Copyright (c) 2015-2019, Regents of the University of California.
9
**        All rights reserved.
10
**
11
**        Permission is hereby granted, without written agreement and without
12
**        license or royalty fees, to use, copy, modify, and distribute this
13
**        software and its documentation for any purpose, provided that the above
14
**        copyright notice and the following two paragraphs appear in all copies
15
**        of this software.
16
**
17
**        IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
18
**        SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
19
**        PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
20
**        EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21
**
22
**        REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
23
**        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
**        FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION,
25
**        IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO
26
**        OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
27
**        OR MODIFICATIONS.
28
**  ----- END LICENSE BLOCK -----
29
***********************************************************************/
30

    
31
////////////////////////////////////////////////////////////////////////
32
//
33
//  MEMORY ALLOCATION: HEAPS
34
//
35
////////////////////////////////////////////////////////////////////////
36

    
37
#ifndef _EP_MEM_H_
38
#define _EP_MEM_H_
39
#include <ep/ep.h>
40
__BEGIN_DECLS
41

    
42
#ifndef EP_MEM_DEBUG
43
# define EP_MEM_DEBUG        1
44
#endif
45

    
46
struct ep_malloc_functions
47
{
48
        void        *(*m_malloc)(size_t);
49
        void        *(*m_realloc)(void*, size_t);
50
        void        *(*m_valloc)(size_t);
51
        void        (*m_free)(void*);
52
};
53

    
54
// flag bits for allocation
55
#define EP_MEM_F_FAILOK                0x00000001        // return NULL on error
56
#define EP_MEM_F_ZERO                0x00000004        // zero memory before return
57
#define EP_MEM_F_TRASH                0x00000008        // return random-filled memory
58
#define EP_MEM_F_ALIGN                0x00000010        // return aligned memory (maybe)
59
#define EP_MEM_F_WAIT                0x00000020        // wait for memory on failure
60

    
61
#if EP_MEM_DEBUG
62
#  define _EP_MEM_FILE_LINE_        __FILE__, __LINE__
63
#else
64
#  define _EP_MEM_FILE_LINE_        NULL, 0
65
#endif
66

    
67
# define ep_mem_malloc(size)        ep_mem_ialloc((size), NULL, \
68
                                        0, \
69
                                        _EP_MEM_FILE_LINE_)
70
# define ep_mem_zalloc(size)        ep_mem_ialloc((size), NULL, \
71
                                        EP_MEM_F_ZERO, \
72
                                        _EP_MEM_FILE_LINE_)
73
# define ep_mem_ralloc(size)        ep_mem_ialloc((size), NULL, \
74
                                        EP_MEM_F_TRASH, \
75
                                        _EP_MEM_FILE_LINE_)
76
# define ep_mem_ealloc(size)        ep_mem_ialloc((size), NULL, \
77
                                        EP_MEM_F_FAILOK, \
78
                                        _EP_MEM_FILE_LINE_)
79
# define ep_mem_falloc(size, flags) \
80
                                ep_mem_ialloc((size), NULL, (flags), \
81
                                        _EP_MEM_FILE_LINE_)
82
# define ep_mem_erealloc(curmem, size) \
83
                                ep_mem_ialloc((size), \
84
                                        (curmem), EP_MEM_F_FAILOK, \
85
                                        _EP_MEM_FILE_LINE_)
86
# define ep_mem_realloc(curmem, size) \
87
                                ep_mem_ialloc((size), (curmem), 0, \
88
                                        _EP_MEM_FILE_LINE_)
89
# define ep_mem_zrealloc(curmem, size) \
90
                                ep_mem_ialloc((size), (curmem), \
91
                                        EP_MEM_F_ZERO, _EP_MEM_FILE_LINE_)
92
# define ep_mem_strdup(s)        ep_mem_istrdup(s, -1, 0, \
93
                                        _EP_MEM_FILE_LINE_)
94
# define ep_mem_fstrdup(s, flags) \
95
                                ep_mem_istrdup(s, -1, (flags), \
96
                                        _EP_MEM_FILE_LINE_)
97
# define ep_mem_strndup(s, l)        ep_mem_istrdup(s, l, 0, \
98
                                        _EP_MEM_FILE_LINE_)
99
# define ep_mem_mfree(p)        ep_mem_free(p)
100

    
101
extern void        *ep_mem_ialloc(                // allocate from heap w/ flags
102
                        size_t nbytes,                        // bytes to alloc
103
                        void *curmem,                        // if reallocing
104
                        uint32_t flags,                        // mod flags
105
                        const char *file,                // dbg: file name
106
                        int line);                        // dbg: line number
107
extern char        *ep_mem_istrdup(                // dup string from heap w/ flags
108
                        const char *s,                        // string to copy
109
                        ssize_t slen,                        // max length of s
110
                        uint32_t flags,                        // action modifiers
111
                        const char *file,                // dbg: file name
112
                        int line);                        // dbg: line number
113
extern void        ep_mem_mfree(                        // free from heap
114
                        void *p);                        // space to free
115

    
116
// for function pointers
117
extern void        *ep_mem_malloc_f(size_t nbytes);
118
extern void        *ep_mem_zalloc_f(size_t nbytes);
119
extern void        *ep_mem_ralloc_f(size_t nbytes);
120

    
121
extern void        ep_mem_trash(                        // trash memory
122
                        void *p,                        // ptr to memory
123
                        size_t nbytes);                        // number of trash bytes
124

    
125
extern void        ep_mem_set_malloc_functions(
126
                        struct ep_malloc_functions *);
127
extern void        ep_mem_set_recovery_func(
128
                        void (*f)(void));
129

    
130
__END_DECLS
131
#endif // _EP_MEM_H_