OLD | NEW |
(Empty) | |
| 1 diff -c -r talloc-2.0.1/talloc.c talloc/talloc.c |
| 2 *** talloc-2.0.1/talloc.c Tue Dec 15 06:16:57 2009 |
| 3 --- talloc/talloc.c Mon Oct 25 13:58:42 2010 |
| 4 *************** |
| 5 *** 30,36 **** |
| 6 inspired by http://swapped.cc/halloc/ |
| 7 */ |
| 8 |
| 9 ! #include "replace.h" |
| 10 #include "talloc.h" |
| 11 |
| 12 #ifdef TALLOC_BUILD_VERSION_MAJOR |
| 13 --- 30,37 ---- |
| 14 inspired by http://swapped.cc/halloc/ |
| 15 */ |
| 16 |
| 17 ! /* Commented out for building within Chromium */ |
| 18 ! /* #include "replace.h" */ |
| 19 #include "talloc.h" |
| 20 |
| 21 #ifdef TALLOC_BUILD_VERSION_MAJOR |
| 22 *************** |
| 23 *** 97,102 **** |
| 24 --- 98,110 ---- |
| 25 #endif |
| 26 #endif |
| 27 |
| 28 + /* inline isn't supported in C files in Visual Studio 2008 on Windows */ |
| 29 + #ifdef _MSC_VER |
| 30 + #define INLINE |
| 31 + #else |
| 32 + #define INLINE inline |
| 33 + #endif |
| 34 + |
| 35 /* this null_context is only used if talloc_enable_leak_report() or |
| 36 talloc_enable_leak_report_full() is called, otherwise it remains |
| 37 NULL |
| 38 *************** |
| 39 *** 224,230 **** |
| 40 } |
| 41 |
| 42 /* panic if we get a bad magic value */ |
| 43 ! static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr) |
| 44 { |
| 45 const char *pp = (const char *)ptr; |
| 46 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_H
DR_SIZE); |
| 47 --- 232,238 ---- |
| 48 } |
| 49 |
| 50 /* panic if we get a bad magic value */ |
| 51 ! static INLINE struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr) |
| 52 { |
| 53 const char *pp = (const char *)ptr; |
| 54 struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_H
DR_SIZE); |
| 55 *************** |
| 56 *** 277,283 **** |
| 57 /* |
| 58 return the parent chunk of a pointer |
| 59 */ |
| 60 ! static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr) |
| 61 { |
| 62 struct talloc_chunk *tc; |
| 63 |
| 64 --- 285,291 ---- |
| 65 /* |
| 66 return the parent chunk of a pointer |
| 67 */ |
| 68 ! static INLINE struct talloc_chunk *talloc_parent_chunk(const void *ptr) |
| 69 { |
| 70 struct talloc_chunk *tc; |
| 71 |
| 72 *************** |
| 73 *** 384,390 **** |
| 74 /* |
| 75 Allocate a bit of memory as a child of an existing pointer |
| 76 */ |
| 77 ! static inline void *__talloc(const void *context, size_t size) |
| 78 { |
| 79 struct talloc_chunk *tc = NULL; |
| 80 |
| 81 --- 392,398 ---- |
| 82 /* |
| 83 Allocate a bit of memory as a child of an existing pointer |
| 84 */ |
| 85 ! static INLINE void *__talloc(const void *context, size_t size) |
| 86 { |
| 87 struct talloc_chunk *tc = NULL; |
| 88 |
| 89 *************** |
| 90 *** 500,506 **** |
| 91 more efficient way to add a name to a pointer - the name must point to a |
| 92 true string constant |
| 93 */ |
| 94 ! static inline void _talloc_set_name_const(const void *ptr, const char *name) |
| 95 { |
| 96 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr); |
| 97 tc->name = name; |
| 98 --- 508,514 ---- |
| 99 more efficient way to add a name to a pointer - the name must point to a |
| 100 true string constant |
| 101 */ |
| 102 ! static INLINE void _talloc_set_name_const(const void *ptr, const char *name) |
| 103 { |
| 104 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr); |
| 105 tc->name = name; |
| 106 *************** |
| 107 *** 509,515 **** |
| 108 /* |
| 109 internal talloc_named_const() |
| 110 */ |
| 111 ! static inline void *_talloc_named_const(const void *context, size_t size, cons
t char *name) |
| 112 { |
| 113 void *ptr; |
| 114 |
| 115 --- 517,523 ---- |
| 116 /* |
| 117 internal talloc_named_const() |
| 118 */ |
| 119 ! static INLINE void *_talloc_named_const(const void *context, size_t size, cons
t char *name) |
| 120 { |
| 121 void *ptr; |
| 122 |
| 123 *************** |
| 124 *** 559,565 **** |
| 125 /* |
| 126 internal talloc_free call |
| 127 */ |
| 128 ! static inline int _talloc_free_internal(void *ptr, const char *location) |
| 129 { |
| 130 struct talloc_chunk *tc; |
| 131 |
| 132 --- 567,573 ---- |
| 133 /* |
| 134 internal talloc_free call |
| 135 */ |
| 136 ! static INLINE int _talloc_free_internal(void *ptr, const char *location) |
| 137 { |
| 138 struct talloc_chunk *tc; |
| 139 |
| 140 *************** |
| 141 *** 797,803 **** |
| 142 talloc_reference() has done. The context and pointer arguments |
| 143 must match those given to a talloc_reference() |
| 144 */ |
| 145 ! static inline int talloc_unreference(const void *context, const void *ptr) |
| 146 { |
| 147 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr); |
| 148 struct talloc_reference_handle *h; |
| 149 --- 805,811 ---- |
| 150 talloc_reference() has done. The context and pointer arguments |
| 151 must match those given to a talloc_reference() |
| 152 */ |
| 153 ! static INLINE int talloc_unreference(const void *context, const void *ptr) |
| 154 { |
| 155 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr); |
| 156 struct talloc_reference_handle *h; |
| 157 *************** |
| 158 *** 877,885 **** |
| 159 /* |
| 160 add a name to an existing pointer - va_list version |
| 161 */ |
| 162 ! static inline const char *talloc_set_name_v(const void *ptr, const char *fmt,
va_list ap) PRINTF_ATTRIBUTE(2,0); |
| 163 |
| 164 ! static inline const char *talloc_set_name_v(const void *ptr, const char *fmt,
va_list ap) |
| 165 { |
| 166 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr); |
| 167 tc->name = talloc_vasprintf(ptr, fmt, ap); |
| 168 --- 885,893 ---- |
| 169 /* |
| 170 add a name to an existing pointer - va_list version |
| 171 */ |
| 172 ! static INLINE const char *talloc_set_name_v(const void *ptr, const char *fmt,
va_list ap) PRINTF_ATTRIBUTE(2,0); |
| 173 |
| 174 ! static INLINE const char *talloc_set_name_v(const void *ptr, const char *fmt,
va_list ap) |
| 175 { |
| 176 struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr); |
| 177 tc->name = talloc_vasprintf(ptr, fmt, ap); |
| 178 *************** |
| 179 *** 1134,1139 **** |
| 180 --- 1142,1151 ---- |
| 181 } |
| 182 |
| 183 |
| 184 + static INLINE size_t min_size(size_t a, size_t b) |
| 185 + { |
| 186 + return a > b ? b : a; |
| 187 + } |
| 188 |
| 189 /* |
| 190 A talloc version of realloc. The context argument is only used if |
| 191 *************** |
| 192 *** 1143,1149 **** |
| 193 { |
| 194 struct talloc_chunk *tc; |
| 195 void *new_ptr; |
| 196 ! bool malloced = false; |
| 197 |
| 198 /* size zero is equivalent to free() */ |
| 199 if (unlikely(size == 0)) { |
| 200 --- 1155,1161 ---- |
| 201 { |
| 202 struct talloc_chunk *tc; |
| 203 void *new_ptr; |
| 204 ! int malloced = 0; |
| 205 |
| 206 /* size zero is equivalent to free() */ |
| 207 if (unlikely(size == 0)) { |
| 208 *************** |
| 209 *** 1196,1206 **** |
| 210 |
| 211 if (new_ptr == NULL) { |
| 212 new_ptr = malloc(TC_HDR_SIZE+size); |
| 213 ! malloced = true; |
| 214 } |
| 215 |
| 216 if (new_ptr) { |
| 217 ! memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE); |
| 218 } |
| 219 } |
| 220 else { |
| 221 --- 1208,1218 ---- |
| 222 |
| 223 if (new_ptr == NULL) { |
| 224 new_ptr = malloc(TC_HDR_SIZE+size); |
| 225 ! malloced = 1; |
| 226 } |
| 227 |
| 228 if (new_ptr) { |
| 229 ! memcpy(new_ptr, tc, min_size(tc->size,size) + TC_HDR_SIZ
E); |
| 230 } |
| 231 } |
| 232 else { |
| 233 *************** |
| 234 *** 1558,1564 **** |
| 235 return newp; |
| 236 } |
| 237 |
| 238 ! static inline char *__talloc_strlendup(const void *t, const char *p, size_t le
n) |
| 239 { |
| 240 char *ret; |
| 241 |
| 242 --- 1570,1576 ---- |
| 243 return newp; |
| 244 } |
| 245 |
| 246 ! static INLINE char *__talloc_strlendup(const void *t, const char *p, size_t le
n) |
| 247 { |
| 248 char *ret; |
| 249 |
| 250 *************** |
| 251 *** 1581,1586 **** |
| 252 --- 1593,1609 ---- |
| 253 return __talloc_strlendup(t, p, strlen(p)); |
| 254 } |
| 255 |
| 256 + #ifndef HAVE_STRNLEN |
| 257 + static size_t strnlen(const char* s, size_t n) |
| 258 + { |
| 259 + if (unlikely(!s)) return 0; |
| 260 + int i = 0; |
| 261 + while (i < n && *s++ != '\0') |
| 262 + ++i; |
| 263 + return i; |
| 264 + } |
| 265 + #endif |
| 266 + |
| 267 /* |
| 268 strndup with a talloc |
| 269 */ |
| 270 *************** |
| 271 *** 1590,1596 **** |
| 272 return __talloc_strlendup(t, p, strnlen(p, n)); |
| 273 } |
| 274 |
| 275 ! static inline char *__talloc_strlendup_append(char *s, size_t slen, |
| 276 const char *a, size_t alen) |
| 277 { |
| 278 char *ret; |
| 279 --- 1613,1619 ---- |
| 280 return __talloc_strlendup(t, p, strnlen(p, n)); |
| 281 } |
| 282 |
| 283 ! static INLINE char *__talloc_strlendup_append(char *s, size_t slen, |
| 284 const char *a, size_t alen) |
| 285 { |
| 286 char *ret; |
| 287 *************** |
| 288 *** 1736,1749 **** |
| 289 return ret; |
| 290 } |
| 291 |
| 292 ! static inline char *__talloc_vaslenprintf_append(char *s, size_t slen, |
| 293 const char *fmt, va_list ap) |
| 294 PRINTF_ATTRIBUTE(3,0); |
| 295 |
| 296 ! static inline char *__talloc_vaslenprintf_append(char *s, size_t slen, |
| 297 const char *fmt, va_list ap) |
| 298 { |
| 299 ssize_t alen; |
| 300 va_list ap2; |
| 301 char c; |
| 302 |
| 303 --- 1759,1777 ---- |
| 304 return ret; |
| 305 } |
| 306 |
| 307 ! static INLINE char *__talloc_vaslenprintf_append(char *s, size_t slen, |
| 308 const char *fmt, va_list ap) |
| 309 PRINTF_ATTRIBUTE(3,0); |
| 310 |
| 311 ! static INLINE char *__talloc_vaslenprintf_append(char *s, size_t slen, |
| 312 const char *fmt, va_list ap) |
| 313 { |
| 314 + /* ssize_t isn't present on Windows. */ |
| 315 + #ifndef _MSC_VER |
| 316 ssize_t alen; |
| 317 + #else |
| 318 + size_t alen; |
| 319 + #endif |
| 320 va_list ap2; |
| 321 char c; |
| 322 |
| 323 diff -c -r talloc-2.0.1/talloc.h talloc/talloc.h |
| 324 *** talloc-2.0.1/talloc.h Wed Oct 28 16:14:20 2009 |
| 325 --- talloc/talloc.h Mon Oct 25 15:11:18 2010 |
| 326 *************** |
| 327 *** 28,33 **** |
| 328 --- 28,37 ---- |
| 329 #include <stdlib.h> |
| 330 #include <stdio.h> |
| 331 #include <stdarg.h> |
| 332 + #ifndef _MSC_VER |
| 333 + #include <stdint.h> |
| 334 + #endif |
| 335 + #include <string.h> |
| 336 |
| 337 #define TALLOC_VERSION_MAJOR 2 |
| 338 #define TALLOC_VERSION_MINOR 0 |
OLD | NEW |