Mercurial > ~dholland > hg > tradcpp > index.cgi
comparison utils.c @ 41:ef8bedab8a4b
Add malloc debug code.
author | David A. Holland |
---|---|
date | Sat, 30 Mar 2013 21:50:37 -0400 |
parents | 337110e7240a |
children | 60184aa42604 |
comparison
equal
deleted
inserted
replaced
40:291fefe664f2 | 41:ef8bedab8a4b |
---|---|
27 * POSSIBILITY OF SUCH DAMAGE. | 27 * POSSIBILITY OF SUCH DAMAGE. |
28 */ | 28 */ |
29 | 29 |
30 #include <stdlib.h> | 30 #include <stdlib.h> |
31 #include <string.h> | 31 #include <string.h> |
32 #include <assert.h> | |
32 #include <err.h> | 33 #include <err.h> |
33 | 34 |
34 #include "utils.h" | 35 #include "utils.h" |
36 | |
37 #define MALLOCDEBUG | |
35 | 38 |
36 const char ws[] = | 39 const char ws[] = |
37 " \t\f\v" | 40 " \t\f\v" |
38 ; | 41 ; |
39 const char alnum[] = | 42 const char alnum[] = |
41 "abcdefghijklmnopqrstuvwxyz" | 44 "abcdefghijklmnopqrstuvwxyz" |
42 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
43 "_" | 46 "_" |
44 ; | 47 ; |
45 | 48 |
49 //////////////////////////////////////////////////////////// | |
50 // malloc | |
51 | |
52 #define ROUNDUP(len, size) ((size) * (((len) + (size) - 1) / (size))) | |
53 | |
54 #ifdef MALLOCDEBUG | |
55 | |
56 struct mallocheader { | |
57 struct mallocheader *self; | |
58 size_t len; | |
59 }; | |
60 | |
61 static | |
62 size_t | |
63 adjustsize(size_t len) | |
64 { | |
65 const size_t sz = sizeof(struct mallocheader); | |
66 return ROUNDUP(len, sz) + 2*sz; | |
67 } | |
68 | |
69 static | |
70 void * | |
71 placeheaders(void *block, size_t len) | |
72 { | |
73 struct mallocheader *bothdr, *tophdr; | |
74 size_t roundedlen; | |
75 void *ret; | |
76 | |
77 roundedlen = ROUNDUP(len, sizeof(struct mallocheader)); | |
78 bothdr = block; | |
79 bothdr->len = len; | |
80 bothdr->self = block; | |
81 ret = bothdr + 1; | |
82 tophdr = (void *)(((unsigned char *)ret) + roundedlen); | |
83 tophdr->len = len; | |
84 tophdr->self = bothdr; | |
85 return ret; | |
86 } | |
87 | |
88 static | |
89 void * | |
90 checkheaders(void *block, size_t len) | |
91 { | |
92 struct mallocheader *bothdr, *tophdr; | |
93 size_t roundedlen; | |
94 | |
95 if (block == NULL) { | |
96 assert(len == 0); | |
97 return block; | |
98 } | |
99 | |
100 roundedlen = ROUNDUP(len, sizeof(struct mallocheader)); | |
101 bothdr = block; | |
102 bothdr--; | |
103 assert(bothdr->self == bothdr); | |
104 assert(bothdr->len == len); | |
105 tophdr = (void *)(((unsigned char *)(bothdr + 1)) + roundedlen); | |
106 assert(tophdr->self == bothdr); | |
107 assert(tophdr->len == len); | |
108 return bothdr; | |
109 } | |
110 | |
111 #else | |
112 | |
113 #define adjustsize(len) (len) | |
114 #define placeheaders(block, len) ((void)(len), (block)) | |
115 #define checkheaders(ptr, len) ((void)(len), (ptr)) | |
116 | |
117 #endif /* MALLOCDEBUG */ | |
118 | |
46 void * | 119 void * |
47 domalloc(size_t len) | 120 domalloc(size_t len) |
48 { | 121 { |
49 void *ret; | 122 void *ret; |
50 | 123 size_t blocklen; |
51 ret = malloc(len); | 124 |
125 blocklen = adjustsize(len); | |
126 ret = malloc(blocklen); | |
52 if (ret == NULL) { | 127 if (ret == NULL) { |
53 warnx("Out of memory"); | 128 warnx("Out of memory"); |
54 die(); | 129 die(); |
55 } | 130 } |
56 return ret; | 131 |
132 return placeheaders(ret, len); | |
57 } | 133 } |
58 | 134 |
59 void * | 135 void * |
60 dorealloc(void *ptr, size_t oldlen, size_t newlen) | 136 dorealloc(void *ptr, size_t oldlen, size_t newlen) |
61 { | 137 { |
62 void *ret; | 138 void *ret; |
63 | 139 void *blockptr; |
64 (void)oldlen; | 140 size_t newblocklen; |
65 ret = realloc(ptr, newlen); | 141 |
142 blockptr = checkheaders(ptr, oldlen); | |
143 newblocklen = adjustsize(newlen); | |
144 | |
145 ret = realloc(blockptr, newblocklen); | |
66 if (ret == NULL) { | 146 if (ret == NULL) { |
67 warnx("Out of memory"); | 147 warnx("Out of memory"); |
68 die(); | 148 die(); |
69 } | 149 } |
70 return ret; | 150 |
151 return placeheaders(ret, newlen); | |
71 } | 152 } |
72 | 153 |
73 void | 154 void |
74 dofree(void *ptr, size_t len) | 155 dofree(void *ptr, size_t len) |
75 { | 156 { |
76 (void)len; | 157 void *blockptr; |
77 free(ptr); | 158 |
78 } | 159 blockptr = checkheaders(ptr, len); |
160 free(blockptr); | |
161 } | |
162 | |
163 //////////////////////////////////////////////////////////// | |
164 // string allocators | |
79 | 165 |
80 char * | 166 char * |
81 dostrdup(const char *s) | 167 dostrdup(const char *s) |
82 { | 168 { |
83 char *ret; | 169 char *ret; |
131 dostrfree(char *s) | 217 dostrfree(char *s) |
132 { | 218 { |
133 dofree(s, strlen(s)+1); | 219 dofree(s, strlen(s)+1); |
134 } | 220 } |
135 | 221 |
222 //////////////////////////////////////////////////////////// | |
223 // other stuff | |
224 | |
136 size_t | 225 size_t |
137 notrailingws(char *buf, size_t len) | 226 notrailingws(char *buf, size_t len) |
138 { | 227 { |
139 while (len > 0 && strchr(ws, buf[len-1])) { | 228 while (len > 0 && strchr(ws, buf[len-1])) { |
140 buf[--len] = '\0'; | 229 buf[--len] = '\0'; |