-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
user_malloc.c
1932 lines (1730 loc) · 49 KB
/
user_malloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* user-level memory-allocation routines
*
* Copyright 2020 by Gray Watson
*
* This file is part of the dmalloc package.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies, and that the name of Gray Watson not be used in advertising
* or publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be contacted via https://dmalloc.com/
*/
/*
* This file contains the user-level calls to the memory allocation
* routines. It handles a lot of the miscellaneous support garbage for
* chunk.c which is the real heap manager.
*/
#if HAVE_STDIO_H
# include <stdio.h> /* for FILE */
#endif
#if HAVE_STDLIB_H
# include <stdlib.h> /* for atexit */
#endif
#if HAVE_STRING_H
# include <string.h> /* for strlen */
#endif
#if HAVE_UNISTD_H
# include <unistd.h> /* for _exit */
#endif
/*
* cygwin includes
*/
#if HAVE_SYS_CYGWIN_H
# include <sys/cygwin.h>
#endif
#if HAVE_STDARG_H
# include <stdarg.h>
#endif
#if HAVE_W32API_WINDEF_H
# include <w32api/windef.h>
#endif
#if HAVE_W32API_WINBASE_H
# include <w32api/winbase.h>
#endif
#include "conf.h" /* up here for _INCLUDE */
#if LOG_PNT_TIMEVAL
# ifdef TIMEVAL_INCLUDE
# include TIMEVAL_INCLUDE
# endif
#else
# if HAVE_TIME /* NOT LOG_PNT_TIME */
# ifdef TIME_INCLUDE
# include TIME_INCLUDE
# endif
# endif
#endif
#if LOCK_THREADS
#if HAVE_PTHREAD_H
#include <pthread.h>
#endif
#if HAVE_PTHREADS_H
#include <pthreads.h>
#endif
#endif
#if SIGNAL_OKAY && HAVE_SIGNAL_H
#include <signal.h>
#endif
#define DMALLOC_DISABLE
#include "dmalloc.h"
#include "append.h"
#include "chunk.h"
#include "compat.h"
#include "debug_tok.h"
#include "env.h"
#include "error.h"
#include "error_val.h"
#include "heap.h"
#include "dmalloc_loc.h"
#include "user_malloc.h"
#include "return.h"
#if LOCK_THREADS
#if IDENT_WORKS
#ident "@(#) $Information: lock-threads is enabled $"
#else
static char *information = "@(#) $Information: lock-threads is enabled $";
#endif
#endif
#define INT_TYPE int
/* exported variables */
/* internal dmalloc error number for reference purposes only */
int dmalloc_errno = DMALLOC_ERROR_NONE;
/* logfile for dumping dmalloc info, DMALLOC_LOGFILE env var overrides this */
char *dmalloc_logpath = NULL;
/* local variables */
static int enabled_b = 0; /* have we started yet? */
static int in_alloc_b = 0; /* can't be here twice */
static int do_shutdown_b = 0; /* execute shutdown soon */
static int memalign_warn_b = 0; /* memalign warning printed?*/
static dmalloc_track_t tracking_func = NULL; /* memory trxn tracking func */
/* debug variables */
static char *start_file = NULL; /* file to start at */
static int start_line = 0; /* line to start */
static unsigned long start_iter = 0; /* start after X iterations */
static unsigned long start_size = 0; /* start after X bytes */
static int thread_lock_c = 0; /* lock counter */
/****************************** thread locking *******************************/
#if LOCK_THREADS
#ifdef THREAD_MUTEX_T
/*
* Define a global variable we use as a lock counter.
*
* NOTE: we do not use the PTHREAD_MUTEX_INITIALIZER since this
* basically delays the pthread_mutex_init call to when
* pthread_mutex_lock is called for the first time (at least on
* freebsd). Since we don't want to go recursive into the pthread
* library when we go to lock our mutex variable, we want to force the
* initialization to happen beforehand with a call to
* pthread_mute_init.
*/
static THREAD_MUTEX_T dmalloc_mutex;
#else
#error We need to have THREAD_MUTEX_T defined by the configure script
#endif
#endif
/*
* THREADS LOCKING:
*
* Because we need to protect for multiple threads making calls into
* the dmalloc library at the same time, we need to initialize and use
* a thread mutex variable. The problem is that most thread libraries
* uses malloc itself and do not like to go recursive.
*
* There are two places where we may have this problem. One is when
* we try to use our mutex-lock variable when pthreads is starting up
* in a shaky state. The thread library allocates some space, the
* dmalloc library needs to lock its mutex variable so calls back into
* the pthread library before it is ready for a call, and a core dump
* is probably the result.
*
* We hopefully solve this by having the dmalloc library not lock
* during the first couple of memory transactions. The number is
* controlled by lock-on dmalloc program environmental setting (set
* with ``dmalloc -o X''). You will have to play with the value. Too
* many will cause two threads to march into the dmalloc code at the
* same time generating a DMALLOC_ERROR_IN_TWICE error. Too few and
* you will get a core dump in the pthreads initialization code.
*
* The second place where we might go recursive is when we go to
* actually initialize our mutex-lock before we can use it. The
* THREAD_INIT_LOCK variable (in settings.h) defines that the
* initialization happens 2 memory transactions before the library
* begins to use the mutex (lock-on - 2). It we waited to initialize
* the variable right before we used it, the pthread library might
* want to allocate some memory for the variable causing a recursive
* call and probably a seg-fault -- at least in OSF. If people need
* to have this variable also be runtime configurable or would like to
* present an alternative default, please let me know.
*/
#if LOCK_THREADS
/*
* mutex lock the malloc library
*/
static void lock_thread(void)
{
/* we only lock if the lock-on counter has reached 0 */
if (thread_lock_c == 0) {
#if HAVE_PTHREAD_MUTEX_LOCK
pthread_mutex_lock(&dmalloc_mutex);
#endif
}
}
/*
* mutex unlock the malloc library
*/
static void unlock_thread(void)
{
/* if the lock-on counter has not reached 0 then count it down */
if (thread_lock_c > 0) {
thread_lock_c--;
/*
* As we approach the time when we start mutex locking the
* library, we need to init the mutex variable. This sets how
* many times before we start locking should we init the variable
* taking in account that the init itself might generate a call
* into the library. Ugh.
*/
if (thread_lock_c == THREAD_INIT_LOCK) {
#if HAVE_PTHREAD_MUTEX_INIT
/*
* NOTE: we do not use the PTHREAD_MUTEX_INITIALIZER since this
* basically delays the pthread_mutex_init call to when
* pthread_mutex_lock is called for the first time (at least on
* freebsd). Since we don't want to go recursive into the
* pthread library when we go to lock our mutex variable, we
* want to force the initialization to happen beforehand with a
* call to pthread_mute_init.
*/
pthread_mutex_init(&dmalloc_mutex, THREAD_LOCK_INIT_VAL);
#endif
}
}
else if (thread_lock_c == 0) {
#if HAVE_PTHREAD_MUTEX_UNLOCK
pthread_mutex_unlock(&dmalloc_mutex);
#endif
}
}
#endif
/****************************** local utilities ******************************/
/*
* check out a pointer to see if we were looking for it. this should
* be re-entrant and it may not return.
*/
static void check_pnt(const char *file, const int line, const void *pnt,
const char *label)
{
static unsigned long addr_c = 0;
char where_buf[64];
if (_dmalloc_address == NULL || pnt != _dmalloc_address) {
return;
}
addr_c++;
dmalloc_message("address '%p' found in '%s' at pass %ld from '%s'",
pnt, label, addr_c,
_dmalloc_chunk_desc_pnt(where_buf, sizeof(where_buf), file,
line));
/* NOTE: if address_seen_n == 0 then never quit */
if (_dmalloc_address_seen_n > 0 && addr_c >= _dmalloc_address_seen_n) {
dmalloc_errno = DMALLOC_ERROR_IS_FOUND;
dmalloc_error("check_pnt");
}
}
static void process_environ(const char *option_str)
{
/*
* we have a static here so we can store the string without getting
* into problems
*/
static char options[1024];
/* process the options flag */
if (option_str == NULL) {
options[0] = '\0';
}
else {
strncpy(options, option_str, sizeof(options));
options[sizeof(options) - 1] = '\0';
}
char *previous_logpath = dmalloc_logpath;
_dmalloc_environ_process(options, &_dmalloc_address,
(unsigned long *)&_dmalloc_address_seen_n, &_dmalloc_flags,
&_dmalloc_check_interval, &_dmalloc_lock_on,
&dmalloc_logpath, &start_file, &start_line,
&start_iter, &start_size, &_dmalloc_memory_limit);
thread_lock_c = _dmalloc_lock_on;
/* if we set the start stuff, then check-heap comes on later */
if (start_iter > 0 || start_size > 0) {
BIT_CLEAR(_dmalloc_flags, DMALLOC_DEBUG_CHECK_HEAP);
}
/* indicate that we should reopen the logfile if we need to */
if (previous_logpath == 0L || dmalloc_logpath == 0L
|| strcmp(previous_logpath, dmalloc_logpath) != 0) {
_dmalloc_reopen_log();
}
#if LOCK_THREADS == 0
/* was thread-lock-on specified but not configured? */
if (_dmalloc_lock_on > 0) {
dmalloc_errno = DMALLOC_ERROR_LOCK_NOT_CONFIG;
_dmalloc_die(0);
}
#endif
}
/************************** startup/shutdown calls ***************************/
#if SIGNAL_OKAY
/*
* signal catcher
*/
static RETSIGTYPE signal_handler(const int sig)
{
dmalloc_message("caught signal %d", sig);
/* if we are already inside malloc then do the shutdown later */
if (in_alloc_b) {
do_shutdown_b = 1;
}
else {
dmalloc_shutdown();
}
}
#endif
/*
* startup the memory-allocation module
*/
static int dmalloc_startup(const char *debug_str)
{
static int some_up_b = 0;
const char *env_str;
char env_buf[256];
/* have we started already? */
if (enabled_b) {
return 1;
}
if (! some_up_b) {
/* set this up here so if an error occurs below, it will not try again */
some_up_b = 1;
#if LOG_PNT_TIMEVAL
GET_TIMEVAL(_dmalloc_start);
#else
#if HAVE_TIME /* NOT LOG_PNT_TIME */
_dmalloc_start = time(NULL);
#endif
#endif
if (debug_str == NULL) {
env_str = loc_getenv(OPTIONS_ENVIRON, env_buf, sizeof(env_buf),
1 /* stay safe */);
}
else {
env_str = debug_str;
}
/* process the environmental variable(s) */
process_environ(env_str);
/*
* Tune the environment here. If we have a start-file,
* start-count, or interval enabled then make sure the check-heap
* flag is cleared.
*/
if (start_file != NULL
|| start_iter > 0
|| start_size > 0
|| _dmalloc_check_interval > 0) {
BIT_CLEAR(_dmalloc_flags, DMALLOC_DEBUG_CHECK_HEAP);
}
/* startup heap code */
if (! _dmalloc_heap_startup()) {
return 0;
}
/* startup the chunk lower-level code */
if (! _dmalloc_chunk_startup()) {
return 0;
}
}
#if LOCK_THREADS
if (thread_lock_c > 0) {
return 1;
}
#endif
/*
* We have initialized all of our code.
*
* NOTE: set this up here so if an error occurs below, it will not
* try again
*/
enabled_b = 1;
/*
* NOTE: we may go recursive below here becasue atexit or on_exit
* may ask for memory to be allocated. We won't worry about it and
* will just give it to them. We hope that atexit didn't start the
* allocating. Ugh.
*/
#if AUTO_SHUTDOWN
/* NOTE: I use the else here in case some dumb systems has both */
#if HAVE_ATEXIT
(void)atexit(dmalloc_shutdown);
#else
#if HAVE_ON_EXIT
(void)on_exit(dmalloc_shutdown, NULL);
#endif /* HAVE_ON_EXIT */
#endif /* ! HAVE_ATEXIT */
#endif /* AUTO_SHUTDOWN */
#if SIGNAL_OKAY
if (BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_CATCH_SIGNALS)) {
#ifdef SIGNAL1
(void)signal(SIGNAL1, signal_handler);
#endif
#ifdef SIGNAL2
(void)signal(SIGNAL2, signal_handler);
#endif
#ifdef SIGNAL3
(void)signal(SIGNAL3, signal_handler);
#endif
#ifdef SIGNAL4
(void)signal(SIGNAL4, signal_handler);
#endif
#ifdef SIGNAL5
(void)signal(SIGNAL5, signal_handler);
#endif
#ifdef SIGNAL6
(void)signal(SIGNAL6, signal_handler);
#endif
}
#endif /* SIGNAL_OKAY */
return 1;
}
/*
* static int dmalloc_in
*
* Call to the alloc routines has been made. Do some initialization,
* locking, and check some debug variables.
*
* Returns 1 on success or 0 on failure.
*
* ARGUMENTS:
*
* file -> File-name or return-address of the caller.
*
* line -> Line-number of the caller.
*
* check_heap_b -> Set to 1 if it is okay to check the heap. If set
* to 0 then the caller will check it itself or it is a non-invasive
* call.
*/
static int dmalloc_in(const char *file, const int line,
const int check_heap_b)
{
if (_dmalloc_aborting_b) {
return 0;
}
/*
* NOTE: we need to do this outside of lock to get env vars
* otherwise our _dmalloc_lock_on variable won't be initialized and
* the THREAD_LOCK will flip.
*/
if (! enabled_b) {
if (! dmalloc_startup(NULL /* no options string */)) {
return 0;
}
}
#if LOCK_THREADS
lock_thread();
#endif
if (in_alloc_b) {
dmalloc_errno = DMALLOC_ERROR_IN_TWICE;
dmalloc_error("dmalloc_in");
/* NOTE: dmalloc_error may die already */
_dmalloc_die(0);
/*NOTREACHED*/
}
in_alloc_b = 1;
/* increment our interval */
_dmalloc_iter_c++;
/* check start file/line specifications */
if ((! BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_CHECK_HEAP))
&& start_file != NULL
&& file != DMALLOC_DEFAULT_FILE
&& line != DMALLOC_DEFAULT_LINE
&& strcmp(start_file, file) == 0
&& (start_line == 0 || start_line == line)) {
BIT_SET(_dmalloc_flags, DMALLOC_DEBUG_CHECK_HEAP);
/*
* we disable the start file so we won't check this again and the
* interval can go on/off
*/
start_file = NULL;
}
/* start checking heap after X times */
else if (start_iter > 0) {
if (--start_iter == 0) {
BIT_SET(_dmalloc_flags, DMALLOC_DEBUG_CHECK_HEAP);
/*
* this is automatically disabled since it goes to 0 so the
* interval can go on/off
*/
}
}
else if (start_size > 0 && start_size >= _dmalloc_alloc_total) {
BIT_SET(_dmalloc_flags, DMALLOC_DEBUG_CHECK_HEAP);
start_size = 0;
/* disable this check so the interval can go on/off */
}
/* checking heap every X times */
else if (_dmalloc_check_interval > 0) {
if (_dmalloc_iter_c % _dmalloc_check_interval == 0) {
BIT_SET(_dmalloc_flags, DMALLOC_DEBUG_CHECK_HEAP);
}
else {
BIT_CLEAR(_dmalloc_flags, DMALLOC_DEBUG_CHECK_HEAP);
}
}
/* after all that, do we need to check the heap? */
if (check_heap_b && BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_CHECK_HEAP)) {
(void)_dmalloc_chunk_heap_check();
}
return 1;
}
/*
* Going out of the alloc routines back to user space.
*/
static void dmalloc_out(void)
{
in_alloc_b = 0;
#if LOCK_THREADS
unlock_thread();
#endif
if (do_shutdown_b) {
dmalloc_shutdown();
}
}
/***************************** exported routines *****************************/
/*
* void dmalloc_shutdown
*
* Shutdown the dmalloc library and provide statistics if necessary.
*/
void dmalloc_shutdown(void)
{
/* NOTE: do not generate errors for IN_TWICE here */
/* if we're already in die mode leave fast and quietly */
if (_dmalloc_aborting_b) {
return;
}
/*
* Make sure that the log file is open. We do this here because we
* might cause an allocation in the open() and don't want to go
* recursive.
*/
_dmalloc_open_log();
/* if we've died in dmalloc somewhere then leave fast and quietly */
if (in_alloc_b) {
return;
}
#if LOCK_THREADS
lock_thread();
#endif
/* we do it again in case the lock synced the flag to true now */
if (in_alloc_b) {
#if LOCK_THREADS
unlock_thread();
#endif
return;
}
in_alloc_b = 1;
/*
* Check the heap since we are dumping info from it. We check it
* when check-blank is enabled do make sure all of the areas have
* not been overwritten. Thanks Randell.
*/
if (BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_CHECK_HEAP)
|| BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_CHECK_BLANK)
|| BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_CHECK_SHUTDOWN)) {
(void)_dmalloc_chunk_heap_check();
}
/* dump some statistics to the logfile */
if (BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_LOG_STATS)) {
_dmalloc_chunk_log_stats();
}
/* report on non-freed pointers */
if (BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_LOG_NONFREE)) {
_dmalloc_chunk_log_changed(0, 1, 0,
#if DUMP_UNFREED_SUMMARY_ONLY
0
#else
1
#endif
);
}
#if LOG_PNT_TIMEVAL
{
TIMEVAL_TYPE now;
char time_buf1[64], time_buf2[64];
GET_TIMEVAL(now);
dmalloc_message("ending time = %s, elapsed since start = %s",
_dmalloc_ptimeval(&now, time_buf1, sizeof(time_buf1), 0),
_dmalloc_ptimeval(&now, time_buf2, sizeof(time_buf2), 1));
}
#else
#if HAVE_TIME /* NOT LOG_PNT_TIME */
{
TIME_TYPE now;
char time_buf1[64], time_buf2[64];
now = time(NULL);
dmalloc_message("ending time = %s, elapsed since start = %s",
_dmalloc_ptime(&now, time_buf1, sizeof(time_buf1), 0),
_dmalloc_ptime(&now, time_buf2, sizeof(time_buf2), 1));
}
#endif
#endif
in_alloc_b = 0;
#if LOCK_THREADS
unlock_thread();
#endif
/* NOTE: do not set enabled_b to false here */
}
#if FINI_DMALLOC
/*
* void __fini_dmalloc
*
* Automatic function to close dmalloc supported by some operating
* systems. Pretty cool OS/compiler hack. By default it is not
* necessary because we use atexit() and on_exit() to register the
* close functions which are more portable.
*/
void __fini_dmalloc(void)
{
dmalloc_shutdown();
}
#endif
/*
* DMALLOC_PNT dmalloc_malloc
*
* Allocate and return a memory block of a certain size.
*
* Returns a valid pointer on success or NULL on failure.
*
* ARGUMENTS:
*
* file -> File-name or return-address of the caller.
*
* line -> Line-number of the caller.
*
* size -> Number of bytes requested.
*
* func_id -> Function-id to identify the type of call. See
* dmalloc.h.
*
* alignment -> To align the new block to a certain number of bytes,
* set this to a value greater than 0.
*
* xalloc_b -> If set to 1 then print an error and exit if we run out
* of memory.
*/
DMALLOC_PNT dmalloc_malloc(const char *file, const int line,
const DMALLOC_SIZE size, const int func_id,
const DMALLOC_SIZE alignment,
const int xalloc_b)
{
void *new_p;
DMALLOC_SIZE align;
#if DMALLOC_SIZE_UNSIGNED == 0
if (size < 0) {
dmalloc_errno = DMALLOC_ERROR_BAD_SIZE;
dmalloc_error("malloc");
if (tracking_func != NULL) {
tracking_func(file, line, func_id, size, alignment, NULL, NULL);
}
return MALLOC_ERROR;
}
#endif
if (! dmalloc_in(file, line, 1)) {
if (tracking_func != NULL) {
tracking_func(file, line, func_id, size, alignment, NULL, NULL);
}
return MALLOC_ERROR;
}
if (alignment == 0) {
if (func_id == DMALLOC_FUNC_VALLOC) {
align = BLOCK_SIZE;
}
else {
align = 0;
}
}
else if (alignment >= BLOCK_SIZE) {
align = BLOCK_SIZE;
}
else {
/*
* NOTE: Currently, there is no support in the library for
* memalign on less than block boundaries. It will be non-trivial
* to support valloc with fence-post checking and the lack of the
* flag width for dblock allocations.
*/
if (! memalign_warn_b) {
dmalloc_message("WARNING: memalign called without library support");
memalign_warn_b = 1;
}
align = 0;
/* align = alignment */
}
new_p = _dmalloc_chunk_malloc(file, line, size, func_id, align);
check_pnt(file, line, new_p, "malloc");
dmalloc_out();
if (tracking_func != NULL) {
tracking_func(file, line, func_id, size, alignment, NULL, new_p);
}
if (xalloc_b && new_p == NULL) {
char desc[128];
loc_dprintf(STDERR,
"Out of memory while allocating %d bytes from '%s'\n",
size,
_dmalloc_chunk_desc_pnt(desc, sizeof(desc),
file, line));
_exit(1);
}
return new_p;
}
/*
* DMALLOC_PNT dmalloc_realloc
*
* Resizes and old pointer to a new number of bytes.
*
* Returns a valid pointer on success or NULL on failure.
*
* ARGUMENTS:
*
* file -> File-name or return-address of the caller.
*
* line -> Line-number of the caller.
*
* old_pnt -> Pointer to an existing memory chunk that we are
* resizing. If this is NULL then it basically does a malloc.
*
* new_size -> New number of bytes requested for the old pointer.
*
* func_id -> Function-id to identify the type of call. See
* dmalloc.h.
*
* xalloc_b -> If set to 1 then print an error and exit if we run out
* of memory.
*/
DMALLOC_PNT dmalloc_realloc(const char *file, const int line,
DMALLOC_PNT old_pnt, DMALLOC_SIZE new_size,
const int func_id, const int xalloc_b)
{
void *new_p;
#if DMALLOC_SIZE_UNSIGNED == 0
if (new_size < 0) {
dmalloc_errno = DMALLOC_ERROR_BAD_SIZE;
dmalloc_error("realloc");
if (tracking_func != NULL) {
tracking_func(file, line, func_id, new_size, 0, old_pnt, NULL);
}
return MALLOC_ERROR;
}
#endif
if (! dmalloc_in(file, line, 1)) {
if (tracking_func != NULL) {
tracking_func(file, line, func_id, new_size, 0, old_pnt, NULL);
}
return REALLOC_ERROR;
}
check_pnt(file, line, old_pnt, "realloc-in");
#if ALLOW_REALLOC_NULL
if (old_pnt == NULL) {
int new_func_id;
/* shift the function id to be calloc or malloc */
if (func_id == DMALLOC_FUNC_RECALLOC) {
new_func_id = DMALLOC_FUNC_CALLOC;
}
else {
new_func_id = DMALLOC_FUNC_MALLOC;
}
new_p = _dmalloc_chunk_malloc(file, line, new_size, new_func_id, 0);
}
else
#endif
#if ALLOW_REALLOC_SIZE_ZERO
if (new_size == 0) {
/*
* If realloc(old_pnt, 0) then free(old_pnt). Thanks to Stefan
* Froehlich for patiently pointing that the realloc in just
* about every Unix has this functionality.
*/
(void)_dmalloc_chunk_free(file, line, old_pnt, func_id);
new_p = NULL;
}
else
#endif
new_p = _dmalloc_chunk_realloc(file, line, old_pnt, new_size, func_id);
if (new_p != NULL) {
check_pnt(file, line, new_p, "realloc-out");
}
dmalloc_out();
if (tracking_func != NULL) {
tracking_func(file, line, func_id, new_size, 0, old_pnt, new_p);
}
if (xalloc_b && new_p == NULL) {
char desc[128];
loc_dprintf(STDERR,
"Out of memory while reallocating %d bytes from '%s'\n",
new_size, _dmalloc_chunk_desc_pnt(desc, sizeof(desc),
file, line));
_exit(1);
}
return new_p;
}
/*
* int dmalloc_free
*
* Release a pointer back into the heap.
*
* Returns FREE_NOERROR on success or FREE_ERROR on failure.
*
* Note: many operating systems define free to return (void) so this
* return value may be filtered. Dumb.
*
* ARGUMENTS:
*
* file -> File-name or return-address of the caller.
*
* line -> Line-number of the caller.
*
* pnt -> Existing pointer we are freeing.
*
* func_id -> Function-id to identify the type of call. See
* dmalloc.h.
*/
int dmalloc_free(const char *file, const int line, DMALLOC_PNT pnt,
const int func_id)
{
int ret;
if (! dmalloc_in(file, line, 1)) {
if (tracking_func != NULL) {
tracking_func(file, line, func_id, 0, 0, pnt, NULL);
}
return FREE_ERROR;
}
check_pnt(file, line, pnt, "free");
ret = _dmalloc_chunk_free(file, line, pnt, func_id);
dmalloc_out();
if (tracking_func != NULL) {
tracking_func(file, line, DMALLOC_FUNC_FREE, 0, 0, pnt, NULL);
}
return ret;
}
/*
* DMALLOC_PNT dmalloc_strndup
*
* Allocate and return an allocated block of memory holding a copy of
* a string of a certain number of characters.
*
* Returns a valid pointer on success or NULL on failure.
*
* ARGUMENTS:
*
* file -> File-name or return-address of the caller.
*
* line -> Line-number of the caller.
*
* string -> String we are duplicating.
*
* max_len -> Max length of the string we are duplicating. Set to -1 for none.
*
* xalloc_b -> If set to 1 then print an error and exit if we run out
* of memory.
*/
char *dmalloc_strndup(const char *file, const int line,
const char *string, const int max_len,
const int xalloc_b)
{
DMALLOC_SIZE size;
char *new_string;
const char *string_p;
/* check the arguments */
if (BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_CHECK_FUNCS)) {
/* we check for pointer plus \0 */
if (! dmalloc_verify_pnt_strsize(file, line, "strndup", string,
0 /* not exact */, 1 /* strlen */,
(max_len < 0 ? 0 : max_len))) {
dmalloc_message("bad pointer argument found in strdup");
}
}
/* so we have to figure out the max length of the string directly */
if (max_len < 0) {
size = strlen(string);
}
else {
for (string_p = string; string_p < string + max_len; string_p++) {
if (*string_p == '\0') {
break;
}
}
size = string_p - string;
}
/* allocate space for the \0 */
new_string = dmalloc_malloc(file, line, size + 1, DMALLOC_FUNC_STRDUP,
0 /* no alignment */, xalloc_b);
if (new_string != NULL) {
strncpy(new_string, string, size);
new_string[size] = '\0';
}
return new_string;
}
/*************************** external memory calls ***************************/
/*
* DMALLOC_PNT malloc
*
* Overloading the malloc(3) function. Allocate and return a memory
* block of a certain size.
*
* Returns a valid pointer on success or NULL on failure.
*