1 /*
2 * memoryman.c -- Simple memory allocation abstraction
3 *
4 * This file provides an abstraction from the underlying
5 * memory allocation facilities used by MyDNS
6 *
7 * It has been put here to provide hooks for changing memory
8 * management facilities to experiement with different libraries;
9 * to allow arena based storage allocation to use a common
10 * abstraction; and to give a location for statistic gathering
11 * and reporting
12 */
13
14 /* To include macros and support definitions */
15 #include "mydnsutil.h"
16
17 #define DEBUG_MEMMAN 1
18
19 static const char *
__mydns_arenaname(arena_t arena)20 __mydns_arenaname(arena_t arena) {
21 if (arena == ARENA_GLOBAL) { return "global"; }
22 if (arena == ARENA_LOCAL) { return "local"; }
23 return "SHARED";
24 }
25
26 int
_mydns_asprintf(char ** strp,const char * fmt,...)27 _mydns_asprintf(char **strp, const char *fmt, ...) {
28 int reslength;
29 va_list ap;
30
31 va_start(ap, fmt);
32 reslength = vasprintf(strp, fmt, ap);
33 va_end(ap);
34
35 if (reslength<0) Out_Of_Memory();
36
37 return (reslength);
38 }
39
40 int
_mydns_vasprintf(char ** strp,const char * fmt,va_list ap)41 _mydns_vasprintf(char **strp, const char *fmt, va_list ap) {
42 int reslength;
43
44 reslength = vasprintf(strp, fmt, ap);
45
46 if (reslength<0) Out_Of_Memory();
47
48 return (reslength);
49 }
50
51 char *
_mydns_strdup(const char * s,arena_t arena,const char * file,int line)52 _mydns_strdup(const char *s, arena_t arena, const char *file, int line) {
53 char *news = NULL;
54
55 #if HAVE_STRDUP
56 news = strdup(s);
57 if (!news) Out_Of_Memory();
58 #else
59 int slen = strlen(s);
60 news = _mydns_allocate(slen+1, 1, arena, "##char []##", file, line);
61 if (!news) Out_Of_Memory();
62 strncpy(news, s, slen);
63 news[slen] = '\0';
64 #endif
65
66 return (news);
67 }
68
69 char *
_mydns_strndup(const char * s,size_t size,arena_t arena,const char * file,int line)70 _mydns_strndup(const char *s, size_t size, arena_t arena, const char *file, int line) {
71 char *news = NULL;
72
73 #if HAVE_STRNDUP
74 news = strndup(s, size);
75 if (!news) Out_Of_Memory();
76 #else
77 news = _mydns_allocate(size+1, 1, arena, "##char []##", file, line);
78 if (!news) Out_Of_Memory();
79 strncpy(news, s, size);
80 news[size] = '\0';
81 #endif
82
83 return (news);
84 }
85
86 void *
_mydns_allocate(size_t size,size_t count,arena_t arena,const char * type,const char * file,int line)87 _mydns_allocate(size_t size, size_t count, arena_t arena, const char *type, const char *file, int line) {
88
89 void *newobject = NULL;
90 /* We currently ignore arena which is there to allow shared memory or local allocation */
91
92 newobject = calloc(count, size);
93
94 if (!newobject) Out_Of_Memory();
95
96 return (newobject);
97 }
98
99 void *
_mydns_reallocate(void * oldobject,size_t size,size_t count,arena_t arena,const char * type,const char * file,int line)100 _mydns_reallocate(void *oldobject, size_t size, size_t count, arena_t arena, const char *type, const char *file, int line) {
101 void *newobject = NULL;
102
103 /* We currently ignore arena which is there to allow shared memory or local allocation */
104
105 newobject = realloc(oldobject, count * size);
106
107 if (!newobject) Out_Of_Memory();
108
109 return (newobject);
110 }
111
112 void
_mydns_release(void * object,size_t count,arena_t arena,const char * file,int line)113 _mydns_release(void *object, size_t count, arena_t arena, const char *file, int line) {
114
115 /* We currently ignore arena which is there to allow shared memory or local allocation */
116
117 if (object) free(object); /* Use the system free directly - do not rely on the if NULL behaviour */
118
119 }
120
121 /* vi:set ts=3: */
122