1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
15 * Szabo, Janos Zoltan – initial implementation
17 ******************************************************************************/
24 #undef mprintf_va_list
26 #undef mputprintf_va_list
43 * work-around for missing va_copy() in GCC
45 #if defined(__GNUC__) && !defined(va_copy)
47 # define va_copy(dest, src) __va_copy(dest, src)
49 # define va_copy(dest, src) (dest) = (src)
53 /** Initial buffer size for mprintf */
56 /** @def MEMORY_DEBUG_ADDRESS
58 * If you want to catch memory management functions (malloc, realloc,
59 * free) which operate on a given address, define this macro and set
60 * memory_debug_address to the address you want to monitor.
62 * @note This macro has no effect if \c MEMORY_DEBUG is not defined
64 #ifdef DOXYGEN_SPECIAL
65 /* Make the macro visible to Doxygen */
66 #define MEMORY_DEBUG_ADDRESS
69 #ifdef MEMORY_DEBUG_ADDRESS
70 /** @brief Memory checkpoint address. */
71 void *memory_debug_address
= NULL
;
73 /** @brief Memory checkpoint ordinal. */
74 size_t memory_debug_ordinal
= (size_t)-1;
78 * @name Number of memory allocations and frees performed.
80 * @note These are tracked even without MEMORY_DEBUG.
82 static size_t malloc_count
= 0, free_count
= 0;
86 * If preprocessor symbol \c MEMORY_DEBUG is defined the memory
87 * management routines do some self-checks against improper use. This
88 * implies extra memory usage and significant performance penalty.
92 #define FILENAME_FORMAT "%s:%d: "
94 /* Currently allocated memory amount, peak, and number of allocations */
95 static size_t allocated_size
= 0, peak_size
= 0;
96 static unsigned int alloc_count
= 0;
97 /* Sum of all the allocated amounts */
98 static unsigned long long total_allocated
= 0;
100 typedef struct memory_block_struct
{
102 struct memory_block_struct
*prev
, *next
;
103 const char *filename
; /* pointer to string literal */
105 unsigned int ordinal
; /* The ordinal which uniquely identifies this allocation */
106 void *magic
; /* only to reserve some place for the magic guard */
107 double begin
; /* beginning of useful memory with proper alignment */
110 static memory_block
*list_head
= NULL
, *list_tail
= NULL
;
112 static void add_to_list(memory_block
*block_ptr
)
114 block_ptr
->prev
= list_tail
;
115 block_ptr
->next
= NULL
;
116 if (list_tail
!= NULL
) list_tail
->next
= block_ptr
;
117 else list_head
= block_ptr
;
118 list_tail
= block_ptr
;
121 static void remove_from_list(memory_block
*block_ptr
)
123 if (block_ptr
->prev
!= NULL
)
124 block_ptr
->prev
->next
= block_ptr
->next
;
125 else list_head
= block_ptr
->next
;
126 if (block_ptr
->next
!= NULL
)
127 block_ptr
->next
->prev
= block_ptr
->prev
;
128 else list_tail
= block_ptr
->prev
;
131 /* Layout of memory (allocated in one chunk)
132 * +----------+ <--- block_ptr
144 * +----------+ +-----------+
145 * | begin | | user data |
149 * +-----------+ +-----------+
154 * The magic values initially contain the value of block_ptr.
155 * When the block is freed, a bit-flipped value is written instead.
156 * During realloc and free, the magic values are checked to match block_ptr.
157 * If they don't, the a memory overrun or underrun has occurred
158 * (except if the magic value happens to match the bit-flipped block_ptr,
159 * in which case it's likely a double-free).
162 static void set_magic_values(memory_block
*block_ptr
)
164 unsigned char *ptr
= (unsigned char*)&block_ptr
->begin
;
165 memcpy(&block_ptr
->magic
, &block_ptr
, sizeof(block_ptr
));
166 memcpy(ptr
+ block_ptr
->size
, &block_ptr
, sizeof(block_ptr
));
169 static void check_magic_values(const char *filename
, int line
,
170 memory_block
*block_ptr
, int is_realloc
)
172 void *inv_ptr
= (void*)(~(size_t)block_ptr
);
173 unsigned char *ptr
= (unsigned char*)&block_ptr
->begin
;
174 /* compare the magic */
175 if (memcmp(&block_ptr
->magic
, &block_ptr
, sizeof(block_ptr
))) {
178 if (memcmp(&block_ptr
->magic
, &inv_ptr
, sizeof(inv_ptr
)))
179 err_str
= "memory corruption";
180 else err_str
= "duplicate free/realloc";
182 fprintf(stderr
, FILENAME_FORMAT
, filename
, line
);
184 fprintf(stderr
, "Fatal error: %s detected at block begin when %s "
185 "pointer %p.\n", err_str
, is_realloc
? "reallocating" : "freeing",
187 if (block_ptr
->filename
) fprintf(stderr
,
188 FILENAME_FORMAT
"Last freed here.\n", block_ptr
->filename
, block_ptr
->lineno
);
191 memcpy(&block_ptr
->magic
, &inv_ptr
, sizeof(inv_ptr
));/*invert magic*/
192 if (memcmp(ptr
+ block_ptr
->size
, &block_ptr
, sizeof(block_ptr
))) {
194 fprintf(stderr
, FILENAME_FORMAT
, filename
, line
);
196 fprintf(stderr
, "Fatal error: memory corruption detected "
197 "at block end when %s pointer %p.\n",
198 is_realloc
? "reallocating" : "freeing", ptr
);
199 if (block_ptr
->filename
) fprintf(stderr
,
200 FILENAME_FORMAT
"Last freed here.\n", block_ptr
->filename
, block_ptr
->lineno
);
203 memcpy(ptr
+ block_ptr
->size
, &inv_ptr
, sizeof(inv_ptr
));
204 block_ptr
->filename
= filename
;
205 block_ptr
->lineno
= line
;
208 /** @def MEMORY_DEBUG_FREE
209 * @brief Enable checking for uses of unallocated/deallocated memory areas.
211 * If preprocessor symbol \c MEMORY_DEBUG_FREE is defined the memory
212 * management routines can verify that unused and deallocated memory areas
213 * are not written accidentally by the program after calling \a Free().
214 * This verification can be done using functions \a check_mem_leak() or
215 * \a check_mem_corrupt().
217 * Note: This functionality can significantly increase the memory requirements
218 * of the program since the deallocated blocks cannot be recycled. They are
219 * not returned to the system using \a free() until the end of the program run.
221 #ifdef DOXYGEN_SPECIAL
222 /* Make the macro visible to Doxygen */
223 #define MEMORY_DEBUG_FREE
226 #ifdef MEMORY_DEBUG_FREE
228 static memory_block
*free_list_head
= NULL
, *free_list_tail
= NULL
;
230 static void add_to_free_list(memory_block
*block_ptr
)
232 block_ptr
->prev
= free_list_tail
;
233 block_ptr
->next
= NULL
;
234 if (free_list_tail
!= NULL
) free_list_tail
->next
= block_ptr
;
235 else free_list_head
= block_ptr
;
236 free_list_tail
= block_ptr
;
239 static void check_free_list(void)
241 memory_block
*block_ptr
= free_list_head
;
243 while (block_ptr
!= NULL
) {
244 void *inv_ptr
= (void*)(~(size_t)block_ptr
);
245 unsigned char *ptr
= (unsigned char*)&block_ptr
->begin
;
247 if (memcmp(&block_ptr
->magic
, &inv_ptr
, sizeof(inv_ptr
))) {
248 fprintf(stderr
, "Fatal error: memory corruption detected in front "
249 "of freed pointer %p\n", ptr
);
252 for (i
= 0; i
< block_ptr
->size
; i
++) {
253 if (ptr
[i
] != 0xAA) {
254 fprintf(stderr
, "Fatal error: memory overwriting detected in "
255 "deallocated area: base pointer: %p, offset: %u, data "
256 "written: %02X\n", ptr
, i
, ptr
[i
]);
260 if (memcmp(ptr
+ block_ptr
->size
, &inv_ptr
, sizeof(inv_ptr
))) {
261 fprintf(stderr
, "Fatal error: memory corruption detected at the "
262 "end of freed pointer %p\n", ptr
);
265 block_ptr
= block_ptr
->next
;
268 fprintf(stderr
, "%u deallocated memory block%s OK\n", counter
,
269 counter
> 1 ? "s are" : " is");
272 static void release_free_blocks(void)
274 memory_block
*block_ptr
= free_list_head
;
275 while (block_ptr
!= NULL
) {
276 memory_block
*next_ptr
= block_ptr
->next
;
278 block_ptr
= next_ptr
;
280 free_list_head
= NULL
;
281 free_list_tail
= NULL
;
284 /* MEMORY_DEBUG_FREE */
286 #ifdef MEMORY_DEBUG_ADDRESS
287 /** Check the address and the ordinal of the allocation against the checkpoints
289 * If the address or the ordinal matches, a line is printed to the standard
290 * error. Breakpoints can be set on the printf to trigger when the watched
291 * allocation happens.
293 * @param block_ptr pointer to a memory block structure which is being
294 * allocated/reallocated/freed
295 * @param oper the actual operation: 0=Malloc, 1=Realloc, 2=Free
297 static void check_memory_address(memory_block
*block_ptr
, int oper
)
299 void *ptr
= (unsigned char*)&block_ptr
->begin
;
300 if (ptr
== memory_debug_address
)
302 if (block_ptr
->filename
) {
303 fprintf(stderr
, FILENAME_FORMAT
, block_ptr
->filename
, block_ptr
->lineno
);
305 fprintf(stderr
, "MEMDEBUG: returning pointer %p while %sing memory.\n",
306 ptr
, oper
==0?"allocat":oper
==1?"reallocat":"free");
308 if (block_ptr
->ordinal
== memory_debug_ordinal
) {
309 if (block_ptr
->filename
) {
310 fprintf(stderr
, FILENAME_FORMAT
, block_ptr
->filename
, block_ptr
->lineno
);
312 fprintf(stderr
, "MEMDEBUG: returning ordinal %lu while %sing memory.\n",
313 (unsigned long)block_ptr
->ordinal
,
314 oper
==0 ? "allocat" : (oper
==1 ? "reallocat" : "free"));
318 /* MEMORY_DEBUG_ADDRESS */
320 /*#define FATAL_ERROR_INTERNAL(f,l,s) fatal_error(f,l,s)*/
321 #define MALLOC_INTERNAL(f,l,s) Malloc_dbg(f,l,s)
322 #define MEMPTYSTR_INTERNAL(f,l) memptystr_dbg(f,l)
323 #define MCOPYSTR_INTERNAL(f,l,s) mcopystr_dbg(f,l,s)
324 #define MCOPYSTRN_INTERNAL(f,l,s,l2) mcopystrn_dbg(f,l,s,l2)
325 #define REALLOC_INTERNAL(f,l,p,s) Realloc_dbg(f,l,p,s)
326 #define FREE_INTERNAL(f,l,p) Free_dbg(f,l,p)
327 #define MPRINTF_VA_LIST_INTERNAL(f,l,s,p) mprintf_va_list_dbg(f,l,s,p)
329 static const size_t offset
= (unsigned char*)&(((memory_block
*)NULL
)->begin
)
330 - (unsigned char*)NULL
;
332 static void extract_location(void *p
, const char **fn
, int *ln
)
334 memory_block
*block_ptr
= NULL
;
335 block_ptr
= (memory_block
*)(void*)((unsigned char*)p
- offset
);
336 *fn
= block_ptr
->filename
;
337 *ln
= block_ptr
->lineno
;
341 /* not MEMORY_DEBUG */
343 #define FATAL_ERROR_INTERNAL(f,l,s) fatal_error(s)
344 #define MALLOC_INTERNAL(f,l,s) Malloc(s)
345 #define MEMPTYSTR_INTERNAL(f,l) memptystr()
346 #define MCOPYSTR_INTERNAL(f,l,s) mcopystr(s)
347 #define MCOPYSTRN_INTERNAL(f,l,s,l2) mcopystrn(s,l2)
348 #define REALLOC_INTERNAL(f,l,p,s) Realloc(p,s)
349 #define FREE_INTERNAL(f,l,p) Free(p)
350 #define MPRINTF_VA_LIST_INTERNAL(f,l,s,p) mprintf_va_list(s,p)
352 #define extract_location(p,f,l) ((void)p,(void)f,(void)l)
355 /** Report a fatal error.
357 * @param size the number of bytes that could not be allocated
359 * This function does not return.
361 __attribute__ ((__noreturn__
))
362 static void fatal_error(
364 const char *filename
, int line
,
368 const char *err_msg
= strerror(errno
);
370 fprintf(stderr
, FILENAME_FORMAT
"Fatal error: cannot allocate %lu bytes"
371 " of memory after allocating %lu bytes: ", filename
, line
,
372 (unsigned long) size
, (unsigned long) allocated_size
);
374 fprintf(stderr
, "Fatal error: cannot allocate %lu bytes of memory: ",
375 (unsigned long) size
);
377 if (err_msg
!= NULL
) fprintf(stderr
, "%s. Exiting.\n", err_msg
);
378 else fprintf(stderr
, "Unknown error (errno: %d). Exiting.\n", errno
);
383 void *Malloc(size_t size
)
385 return Malloc_dbg(0,0,size
);
388 void *Malloc_dbg(const char *filename
, int line
, size_t size
)
390 void *Malloc(size_t size
)
396 memory_block
*block_ptr
;
397 block_ptr
= (memory_block
*)malloc(sizeof(*block_ptr
) -
398 sizeof(block_ptr
->begin
) + size
+ sizeof(block_ptr
));
399 if (block_ptr
== NULL
) fatal_error(filename
, line
, size
);
400 block_ptr
->filename
= filename
;
401 block_ptr
->lineno
= line
;
402 block_ptr
->size
= size
;
403 add_to_list(block_ptr
);
404 set_magic_values(block_ptr
);
405 ptr
= &block_ptr
->begin
;
406 total_allocated
+= size
;
407 block_ptr
->ordinal
= alloc_count
++;
408 allocated_size
+= size
;
409 #ifdef MEMORY_DEBUG_ADDRESS
410 check_memory_address(block_ptr
, 0);
412 if (peak_size
< allocated_size
) peak_size
= allocated_size
;
415 if (ptr
== NULL
) fatal_error(size
);
424 void *Realloc(void *ptr
, size_t size
)
426 return Realloc_dbg(0,0,ptr
,size
);
429 void *Realloc_dbg(const char *filename
, int line
, void *ptr
, size_t size
)
431 void *Realloc(void *ptr
, size_t size
)
434 if (ptr
== NULL
) return MALLOC_INTERNAL(filename
, line
, size
);
435 else if (size
== 0) {
436 FREE_INTERNAL(filename
, line
, ptr
);
441 memory_block
*block_ptr
= NULL
;
442 block_ptr
= (memory_block
*)(void*)((unsigned char*)ptr
- offset
);
443 check_magic_values(filename
, line
, block_ptr
, 1);
444 remove_from_list(block_ptr
);
445 allocated_size
-= block_ptr
->size
;
446 if (size
< block_ptr
->size
)
447 memset((unsigned char*)ptr
+ size
, 0x55, block_ptr
->size
- size
);
448 block_ptr
= (memory_block
*)realloc(block_ptr
, sizeof(*block_ptr
) -
449 sizeof(block_ptr
->begin
) + sizeof(block_ptr
) + size
);
450 if (block_ptr
== NULL
) fatal_error(filename
, line
, size
);
451 #ifdef MEMORY_DEBUG_ADDRESS
452 check_memory_address(block_ptr
, 1);
454 block_ptr
->filename
= filename
;
455 block_ptr
->lineno
= line
;
456 block_ptr
->size
= size
;
457 add_to_list(block_ptr
);
458 set_magic_values(block_ptr
);
459 new_ptr
= &block_ptr
->begin
;
460 total_allocated
+= size
;
462 allocated_size
+= size
;
463 if (peak_size
< allocated_size
) peak_size
= allocated_size
;
465 new_ptr
= realloc(ptr
, size
);
466 if (new_ptr
== NULL
) FATAL_ERROR_INTERNAL(filename
, line
, size
);
478 extern void Free_dbg(const char *filename
, int line
, void *ptr
)
485 memory_block
*block_ptr
= NULL
;
486 block_ptr
= (memory_block
*)(void*)((unsigned char*)ptr
- offset
);
487 #ifdef MEMORY_DEBUG_ADDRESS
488 check_memory_address(block_ptr
, 2);
490 check_magic_values(filename
, line
, block_ptr
, 0);
491 remove_from_list(block_ptr
);
492 allocated_size
-= block_ptr
->size
;
493 memset(ptr
, 0xAA, block_ptr
->size
);
494 #ifdef MEMORY_DEBUG_FREE
495 add_to_free_list(block_ptr
);
507 static const size_t maxprint
= 32;
510 void check_mem_leak(const char *program_name
)
513 fprintf(stderr
, "%s(%d): memory usage statistics:\n"
514 "total allocations: %lu\n"
515 "malloc/new calls: %lu\n"
516 "free/delete calls: %lu\n"
517 "peak memory usage: %lu bytes\n"
518 "average block size: %g bytes\n",
519 program_name
, (int)getpid(),
520 (unsigned long)alloc_count
, (unsigned long)malloc_count
,
521 (unsigned long) free_count
, (unsigned long) peak_size
,
522 (double)total_allocated
/ (double)alloc_count
);
523 if (list_head
!= NULL
) {
524 memory_block
*block_ptr
= list_head
;
526 fprintf(stderr
, "unallocated blocks:\n");
528 if (block_ptr
->filename
!= 0) {
529 fprintf(stderr
, FILENAME_FORMAT
,
530 block_ptr
->filename
, block_ptr
->lineno
);
532 fprintf(stderr
, "\tMemory leak at %p, size %lu, ord %lu: ",
533 (void*)&block_ptr
->begin
, (unsigned long)block_ptr
->size
,
534 (unsigned long)block_ptr
->ordinal
);
536 const unsigned char * mm
= (const unsigned char*)&block_ptr
->begin
;
537 size_t x
, limit
= (block_ptr
->size
> maxprint
) ? maxprint
539 for (x
= 0; x
< limit
; ++x
) {
540 fputc( isprint(mm
[x
]) ? mm
[x
] : '.', stderr
);
544 block_ptr
= block_ptr
->next
;
546 } while (block_ptr
!= NULL
);
547 fprintf(stderr
, "total unallocated: %lu bytes in %lu blocks\n",
548 (unsigned long) allocated_size
, (unsigned long) counter
);
550 #ifdef MEMORY_DEBUG_FREE
552 release_free_blocks();
555 if (malloc_count
!= free_count
) {
556 fprintf(stderr
, "%s: warning: memory leakage detected.\n"
557 "Total malloc calls: %lu, free calls: %lu\n"
558 "Please submit a bug report including the current input file(s).\n",
560 (unsigned long) malloc_count
, (unsigned long) free_count
);
566 void check_mem_corrupt(const char *program_name
)
569 memory_block
*block_ptr
;
570 fprintf(stderr
, "%s: checking memory blocks for corruption\n",
572 for (block_ptr
= list_head
; block_ptr
!= NULL
; block_ptr
= block_ptr
->next
)
574 unsigned char *ptr
= (unsigned char*)&block_ptr
->begin
;
575 if (memcmp(ptr
- sizeof(block_ptr
), &block_ptr
, sizeof(block_ptr
))) {
576 fprintf(stderr
, "Fatal error: memory corruption detected in front "
577 "of pointer %p\n", ptr
);
580 if (memcmp(ptr
+ block_ptr
->size
, &block_ptr
, sizeof(block_ptr
))) {
581 fprintf(stderr
, "Fatal error: memory corruption detected at the "
582 "end of pointer %p\n", ptr
);
587 fprintf(stderr
, "%s: %lu memory block%s OK\n", program_name
,
588 (unsigned long) counter
, counter
> 1 ? "s are" : " is");
589 #ifdef MEMORY_DEBUG_FREE
595 /** @brief Find the string length of an expstring_t
597 Finds the length of the C string pointed at by \p str, using some assumptions
598 satisfied by an expstring_t.
600 @pre There is an offset from the beginning of the string which is a power
601 of two, is within the buffer allocated for \p str and it contains '\\0'
602 @pre The allocated buffer is at most twice as long as strlen(str)
603 @pre \p bufsize is a valid pointer
605 @param [in] str an expstring_t, must not be null
606 @param [out] bufsize pointer to a location where the minimum buffer size
607 (always a power of two) is deposited.
608 @return the length of \p str as a C string
610 static size_t fast_strlen(const expstring_t str
, size_t *bufsize
)
612 size_t size
, min
, max
;
613 /* Find the 0 byte at the end of the allocated buffer */
614 for (size
= 1; str
[size
- 1] != '\0'; size
*= 2) {}
616 if (size
== 1) return 0;
617 /* Binary search the second half of the buffer */
620 while (max
- min
> 1) {
621 size_t med
= (min
+ max
) / 2;
622 if (str
[med
] != '\0') min
= med
;
628 /** @brief Find the first power of two which is greater than \p len
630 * @param len the desired length
631 * @return a power of 2 greater than \p len
633 * \p len is taken to be the number of characters needed. The returned value
634 * will always be greater than \p len to accommodate the null terminator.
636 static size_t roundup_size(size_t len
)
639 for (size
= 1; len
>= size
; size
*= 2);
644 expstring_t
mprintf_va_list(const char *fmt
, va_list pvar
)
646 return mprintf_va_list_dbg(0,0,fmt
,pvar
);
649 expstring_t
mprintf_va_list_dbg(const char *filename
, int line
, const char *fmt
, va_list pvar
)
651 expstring_t
mprintf_va_list(const char *fmt
, va_list pvar
)
659 va_copy(pvar2
, pvar
);
660 len
= vsnprintf(buf
, BUFSIZE
, fmt
, pvar2
);
663 /* The result does not fit in buf and we have no idea how many bytes
664 * are needed (only old versions of libc may return -1).
665 * Try to double the buffer size until it is large enough. */
666 for (size
= 2 * BUFSIZE
; ; size
*= 2) {
667 ptr
= (expstring_t
)MALLOC_INTERNAL(filename
, line
, size
);
668 va_copy(pvar2
, pvar
);
669 len
= vsnprintf(ptr
, size
, fmt
, pvar2
);
671 if (len
>= 0 && (size_t)len
< size
) break;
672 FREE_INTERNAL(filename
, line
, ptr
);
675 } else if (len
>= BUFSIZE
) {
676 /* The result does not fit in buf, but we know how many bytes we need.
677 * Allocate a buffer that is large enough and repeat vsnprintf() */
679 size
= roundup_size(slen
);
680 ptr
= (expstring_t
)MALLOC_INTERNAL(filename
, line
, size
);
681 /* use the original pvar since this is the last try */
682 if (vsnprintf(ptr
, size
, fmt
, pvar
) != len
) {
683 perror("Fatal error: unexpected vsnprintf() return value");
687 /* the complete result is in buf */
689 size
= roundup_size(slen
);
690 ptr
= (expstring_t
)MALLOC_INTERNAL(filename
, line
, size
);
691 memcpy(ptr
, buf
, slen
);
693 memset(ptr
+ slen
, '\0', size
- slen
);
698 expstring_t
mprintf_dbg(const char *filename
, int line
, const char *fmt
, ...)
703 ptr
= mprintf_va_list_dbg(filename
, line
, fmt
, pvar
);
709 expstring_t
mprintf(const char *fmt
, ...)
714 ptr
= mprintf_va_list(fmt
, pvar
);
719 /* Tracking the origin: extracts the filename/line from the original allocation
720 * of the expstring_t. */
721 expstring_t
mputprintf_va_list(expstring_t str
, const char *fmt
, va_list pvar
)
723 const char *filename
= NULL
;
727 size_t len
= fast_strlen(str
, &size
);
728 size_t rest
= size
- len
;
731 /* make a copy of pvar to allow re-use later */
732 va_copy(pvar2
, pvar
);
733 len2
= vsnprintf(str
+ len
, rest
, fmt
, pvar2
);
735 extract_location(str
, &filename
, &line
);
737 /* the result does not fit in buf and we have no idea how many
738 * bytes are needed (only old versions of libc may return -1)
739 * try to double the buffer size until it is large enough */
743 str
= (expstring_t
)REALLOC_INTERNAL(filename
, line
, str
, size
);
745 va_copy(pvar2
, pvar
);
746 len2
= vsnprintf(str
+ len
, rest
, fmt
, pvar2
);
748 } while (len2
< 0 || (size_t)len2
>= rest
);
749 newlen
= len
+ (size_t)len2
;
750 memset(str
+ newlen
, '\0', size
- newlen
);
751 } else if ((size_t)len2
>= rest
) {
752 /* there isn't enough space in buffer, but we know how many bytes
753 * we need: reallocate the buffer to be large enough and repeat
755 size_t newlen
= len
+ (size_t)len2
;
756 size
= roundup_size(newlen
);
757 str
= (expstring_t
)REALLOC_INTERNAL(filename
, line
, str
, size
);
758 /* use the original pvar since this is the last try */
759 if (vsnprintf(str
+ len
, size
- len
, fmt
, pvar
) != len2
) {
760 perror("Fatal error: unexpected vsnprintf() return value");
763 memset(str
+ newlen
, '\0', size
- newlen
);
765 } else str
= MPRINTF_VA_LIST_INTERNAL(filename
, line
, fmt
, pvar
);
769 expstring_t
mputprintf(expstring_t str
, const char *fmt
, ...)
773 str
= mputprintf_va_list(str
, fmt
, pvar
);
779 expstring_t
memptystr(void)
781 return memptystr_dbg(0,0);
784 expstring_t
memptystr_dbg(const char *filename
, int line
)
786 expstring_t
memptystr(void)
789 expstring_t ptr
= (expstring_t
)MALLOC_INTERNAL(filename
, line
, 1);
795 expstring_t
mcopystr(const char *str
)
797 return mcopystr_dbg(0,0,str
);
800 expstring_t
mcopystr_dbg(const char *filename
, int line
, const char *str
)
802 expstring_t
mcopystr(const char *str
)
806 size_t len
= strlen(str
);
807 size_t size
= roundup_size(len
);
808 expstring_t ptr
= (expstring_t
)MALLOC_INTERNAL(filename
, line
, size
);
809 memcpy(ptr
, str
, len
);
810 memset(ptr
+ len
, '\0', size
- len
);
812 } else return MEMPTYSTR_INTERNAL(filename
, line
);
816 expstring_t
mcopystrn(const char *str
, size_t len
)
818 return mcopystrn_dbg(0,0,str
, len
);
821 expstring_t
mcopystrn_dbg(const char *filename
, int line
, const char *str
,
824 expstring_t
mcopystrn(const char *str
, size_t len
)
827 if (len
!= 0 && str
!= NULL
) {
828 size_t size
= roundup_size(len
);
829 expstring_t ptr
= (expstring_t
)MALLOC_INTERNAL(filename
, line
, size
);
830 memcpy(ptr
, str
, len
);
831 memset(ptr
+ len
, '\0', size
- len
);
833 } else return MEMPTYSTR_INTERNAL(filename
, line
);
836 /* Tracking the origin: extracts the filename/line from the original allocation
837 * of the expstring_t. */
838 expstring_t
mputstr(expstring_t str
, const char *str2
)
840 const char *filename
= NULL
;
845 size_t len
= fast_strlen(str
, &size
);
846 size_t len2
= strlen(str2
);
847 size_t newlen
= len
+ len2
;
848 if (size
<= newlen
) {
849 size_t newsize
= roundup_size(newlen
);
850 extract_location(str
, &filename
, &line
);
851 str
= (expstring_t
)REALLOC_INTERNAL(filename
, line
, str
, newsize
);
852 memset(str
+ newlen
, '\0', newsize
- newlen
);
854 memcpy(str
+ len
, str2
, len2
);
855 } else str
= MCOPYSTR_INTERNAL(filename
, line
, str2
);
860 /* Tracking the origin: extracts the filename/line from the original allocation
861 * of the expstring_t. */
862 expstring_t
mputstrn(expstring_t str
, const char *str2
, size_t len2
)
864 const char *filename
= NULL
;
866 if (len2
!= 0 && str2
!= NULL
) {
869 size_t len
= fast_strlen(str
, &size
);
870 size_t newlen
= len
+ len2
;
871 if (size
<= newlen
) {
872 size_t newsize
= roundup_size(newlen
);
873 extract_location(str
, &filename
, &line
);
874 str
= (expstring_t
)REALLOC_INTERNAL(filename
, line
, str
, newsize
);
875 memset(str
+ newlen
, '\0', newsize
- newlen
);
877 memcpy(str
+ len
, str2
, len2
);
878 } else str
= MCOPYSTRN_INTERNAL(filename
, line
, str2
, len2
);
883 /* Tracking the origin: extracts the filename/line from the original allocation
884 * of the expstring_t. */
885 expstring_t
mputc(expstring_t str
, char c
)
887 const char *filename
= NULL
;
892 size_t len
= fast_strlen(str
, &size
);
893 if (size
<= len
+ 1) {
894 extract_location(str
, &filename
, &line
);
895 str
= (expstring_t
)REALLOC_INTERNAL(filename
, line
, str
, 2 * size
);
896 memset(str
+ size
, '\0', size
);
902 str
= (expstring_t
)MALLOC_INTERNAL(filename
, line
, 2);
905 } else str
= MEMPTYSTR_INTERNAL(filename
, line
);
910 /* Tracking the origin: extracts the filename/line from the original allocation
911 * of the expstring_t. */
912 expstring_t
mtruncstr(expstring_t str
, size_t newlen
)
914 const char *filename
= NULL
;
918 size_t len
= fast_strlen(str
, &size
);
920 size_t newsize
= roundup_size(newlen
);
921 if (newsize
< size
) {
922 extract_location(str
, &filename
, &line
);
923 str
= (expstring_t
)REALLOC_INTERNAL(filename
, line
, str
, newsize
);
925 memset(str
+ newlen
, '\0', newsize
- newlen
);
931 size_t mstrlen(const expstring_t str
)
935 return fast_strlen(str
, &size
);
939 char * buildstr(int b
) {
940 if (b
< 0 || b
> 99) return NULL
; /* invalid */
941 if (b
== 99) return memptystr(); /* empty string for full version */
942 return mprintf("%02d", b
);
This page took 0.063677 seconds and 5 git commands to generate.