Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / ep / ep_mem.h @ master

History | View | Annotate | Download (4.46 KB)

1 a901db09 Eric Allman
/* vim: set ai sw=8 sts=8 ts=8 :*/
2 0c663d10 Eric Allman
3 47c6ea64 Eric Allman
/***********************************************************************
4 055d3009 Eric Allman
**  ----- BEGIN LICENSE BLOCK -----
5
**        LIBEP: Enhanced Portability Library (Reduced Edition)
6
**
7 c87dd166 Eric Allman
**        Copyright (c) 2008-2019, Eric P. Allman.  All rights reserved.
8
**        Copyright (c) 2015-2019, Regents of the University of California.
9 6bd5476b Eric Allman
**        All rights reserved.
10 055d3009 Eric Allman
**
11 6bd5476b Eric Allman
**        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 055d3009 Eric Allman
**
17 6bd5476b Eric Allman
**        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 055d3009 Eric Allman
**
22 6bd5476b Eric Allman
**        REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
23 055d3009 Eric Allman
**        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 6bd5476b Eric Allman
**        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 055d3009 Eric Allman
**  ----- END LICENSE BLOCK -----
29 47c6ea64 Eric Allman
***********************************************************************/
30
31
////////////////////////////////////////////////////////////////////////
32
//
33
//  MEMORY ALLOCATION: HEAPS
34
//
35
////////////////////////////////////////////////////////////////////////
36
37
#ifndef _EP_MEM_H_
38
#define _EP_MEM_H_
39 e844af35 Eric Allman
#include <ep/ep.h>
40
__BEGIN_DECLS
41 47c6ea64 Eric Allman
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 9e55d093 Eric Allman
#define EP_MEM_F_FAILOK                0x00000001        // return NULL on error
56 47c6ea64 Eric Allman
#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 9e55d093 Eric Allman
#  define _EP_MEM_FILE_LINE_        NULL, 0
65 47c6ea64 Eric Allman
#endif
66
67 9e55d093 Eric Allman
# define ep_mem_malloc(size)        ep_mem_ialloc((size), NULL, \
68 47c6ea64 Eric Allman
                                        0, \
69
                                        _EP_MEM_FILE_LINE_)
70 9e55d093 Eric Allman
# define ep_mem_zalloc(size)        ep_mem_ialloc((size), NULL, \
71 47c6ea64 Eric Allman
                                        EP_MEM_F_ZERO, \
72
                                        _EP_MEM_FILE_LINE_)
73 9e55d093 Eric Allman
# define ep_mem_ralloc(size)        ep_mem_ialloc((size), NULL, \
74 47c6ea64 Eric Allman
                                        EP_MEM_F_TRASH, \
75
                                        _EP_MEM_FILE_LINE_)
76 9e55d093 Eric Allman
# define ep_mem_ealloc(size)        ep_mem_ialloc((size), NULL, \
77 47c6ea64 Eric Allman
                                        EP_MEM_F_FAILOK, \
78
                                        _EP_MEM_FILE_LINE_)
79
# define ep_mem_falloc(size, flags) \
80 9e55d093 Eric Allman
                                ep_mem_ialloc((size), NULL, (flags), \
81 47c6ea64 Eric Allman
                                        _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 5b07c525 Eric Allman
# define ep_mem_zrealloc(curmem, size) \
90
                                ep_mem_ialloc((size), (curmem), \
91
                                        EP_MEM_F_ZERO, _EP_MEM_FILE_LINE_)
92 47c6ea64 Eric Allman
# 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 80662ee3 Eric Allman
                        ssize_t slen,                        // max length of s
110 47c6ea64 Eric Allman
                        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 1f3c460e Eric Allman
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 e844af35 Eric Allman
__END_DECLS
131 47c6ea64 Eric Allman
#endif // _EP_MEM_H_