-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
chunk.c
3087 lines (2764 loc) · 84.8 KB
/
chunk.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
/*
* Memory chunk low-level 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 algorithm level routine for the heap. They handle the
* manipulation and administration of chunks of memory.
*/
#include <ctype.h>
#if HAVE_STRING_H
# include <string.h>
#endif
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#define DMALLOC_DISABLE
#include "conf.h"
#if LOG_PNT_TIMEVAL
#ifdef TIMEVAL_INCLUDE
# include TIMEVAL_INCLUDE
#endif
#else
# if LOG_PNT_TIME
# ifdef TIME_INCLUDE
# include TIME_INCLUDE
# endif
# endif
#endif
#include "dmalloc.h"
#include "append.h"
#include "chunk.h"
#include "chunk_loc.h"
#include "compat.h"
#include "debug_tok.h"
#include "dmalloc_loc.h"
#include "dmalloc_rand.h"
#include "dmalloc_tab.h"
#include "error.h"
#include "error_val.h"
#include "heap.h"
/*
* Library Copyright and URL information for ident and what programs
*/
#if IDENT_WORKS
#ident "@(#) $Copyright: Dmalloc package Copyright 2020 by Gray Watson $"
#ident "@(#) $URL: Source for dmalloc available from https://dmalloc.com/ $"
#else
static char *copyright =
"@(#) $Copyright: Dmalloc package Copyright 2020 by Gray Watson $";
static char *source_url =
"@(#) $URL: Source for dmalloc available from https://dmalloc.com/ $";
#endif
#if LOCK_THREADS
#if IDENT_WORKS
#ident "@(#) $Information: lock-threads is enabled $"
#else
static char *information = "@(#) $Information: lock-threads is enabled $";
#endif
#endif
/*
* exported variables
*/
/* limit in how much memory we are allowed to allocate */
unsigned long _dmalloc_memory_limit = 0;
/* total number of bytes that the heap has allocated */
unsigned long _dmalloc_alloc_total = 0;
/*
* local variables
*/
/*
* Skip list of our free list sorted by size in bytes. Bit of a hack
* here. Basically we cannot do a alloc for the structure and we'd
* like it to be static storage so we allocate an array of them to
* make sure we have enough forward pointers, when all we need is
* SKIP_SLOT_SIZE(MAX_SKIP_LEVEL + 1) bytes.
*/
static skip_alloc_t skip_free_alloc[MAX_SKIP_LEVEL /* read note ^^ */];
static skip_alloc_t *skip_free_list = skip_free_alloc;
/* skip list of all of our allocated blocks sorted by address */
static skip_alloc_t skip_address_alloc[MAX_SKIP_LEVEL /* read note ^^ */];
static skip_alloc_t *skip_address_list = skip_address_alloc;
/* update slots which we use to update the skip lists */
static skip_alloc_t skip_update[MAX_SKIP_LEVEL /* read note ^^ */];
/* linked list of slots of various sizes */
static skip_alloc_t *entry_free_list[MAX_SKIP_LEVEL];
/* linked list of blocks of the sizes */
static entry_block_t *entry_blocks[MAX_SKIP_LEVEL];
/* linked list of freed blocks on hold waiting for the FREED_POINTER_DELAY */
static skip_alloc_t *free_wait_list_head = NULL;
static skip_alloc_t *free_wait_list_tail = NULL;
/* administrative structures */
static char fence_bottom[FENCE_BOTTOM_SIZE];
static char fence_top[FENCE_TOP_SIZE];
static int bit_sizes[BASIC_BLOCK]; /* number bits for div-blocks*/
/* memory tables */
static mem_table_t mem_table_alloc;
static mem_entry_t mem_table_alloc_entries[MEM_ALLOC_ENTRIES];
static mem_table_t mem_table_changed;
static mem_entry_t mem_table_changed_entries[MEM_ALLOC_ENTRIES];
/* memory stats */
static unsigned long alloc_current = 0; /* current memory usage */
static unsigned long alloc_maximum = 0; /* maximum memory usage */
static unsigned long alloc_cur_given = 0; /* current mem given */
static unsigned long alloc_max_given = 0; /* maximum mem given */
static unsigned long alloc_one_max = 0; /* maximum at once */
static unsigned long free_space_bytes = 0; /* count the free bytes */
/* pointer stats */
static unsigned long alloc_cur_pnts = 0; /* current pointers */
static unsigned long alloc_max_pnts = 0; /* maximum pointers */
static unsigned long alloc_tot_pnts = 0; /* total pointers */
/* admin counts */
static unsigned long heap_check_c = 0; /* count of heap-checks */
static unsigned long user_block_c = 0; /* count of blocks */
static unsigned long admin_block_c = 0; /* count of admin blocks */
/* alloc counts */
static unsigned long func_malloc_c = 0; /* count the mallocs */
static unsigned long func_calloc_c = 0; /* # callocs, done in alloc */
static unsigned long func_realloc_c = 0; /* count the reallocs */
static unsigned long func_recalloc_c = 0; /* count the reallocs */
static unsigned long func_memalign_c = 0; /* count the memaligns */
static unsigned long func_valloc_c = 0; /* count the veallocs */
static unsigned long func_new_c = 0; /* count the news */
static unsigned long func_free_c = 0; /* count the frees */
static unsigned long func_delete_c = 0; /* count the deletes */
/**************************** skip list routines *****************************/
/*
* static int random_level
*
* Return a random level to be associated with a new free-list entry.
*
* Returns random level from 0 to max_level - 1.
*
* ARGUMENTS:
*
* max_level -> Maximum level of the free-list.
*/
static int random_level(const int max_level)
{
int level_c;
for (level_c = 0; level_c < max_level; level_c++) {
/*
* Basically we count the number of times that the random number
* generator returns an odd number in a row. On average this
* should return 0 1/2 the time, 1 1/4 of the time, 2 1/8 of a
* time, and N 1/(2^(N - 1)) of the time. This is what we want.
* We could test for this in the configure scripts.
*
* Since many machines return random numbers which aren't that
* random, there may be better ways of doing this. In the past I
* had (_dmalloc_rand() % 10000 >= 5000) or something but I'd
* rather not have the % overhead here.
*/
if (_dmalloc_rand() & 1) {
break;
}
}
return level_c;
}
/*
* static skip_alloc_t *find_address
*
* Look for a specific address in the skip list. If it exist then a
* pointer to the matching slot is returned otherwise NULL. Either
* way, the links that were traversed to get there are set in the
* update slot which has the maximum number of levels.
*
* RETURNS:
*
* Success - Pointer to the slot which matches the block-num and size
* pair.
*
* Failure - NULL and this will not set dmalloc_errno
*
* ARGUMENTS:
*
* address -> Address we are looking for.
*
* free_b -> Look on the free list otherwise look on the used list.
*
* exact_b -> Set to 1 to find the exact pointer. If 0 then the
* address could be inside a block.
*
* update_p -> Pointer to the skip_alloc entry we are using to hold
* the update pointers.
*/
static skip_alloc_t *find_address(const void *address, const int free_b,
const int exact_b,
skip_alloc_t *update_p)
{
int level_c;
skip_alloc_t *slot_p, *found_p = NULL, *next_p;
/* skip_address_max_level */
level_c = MAX_SKIP_LEVEL - 1;
if (free_b) {
slot_p = skip_free_list;
}
else {
slot_p = skip_address_list;
}
/* traverse list to smallest entry */
while (1) {
/* next on we are looking for */
next_p = slot_p->sa_next_p[level_c];
/*
* sort by address
*/
/* are we are at the end of a row? */
if (next_p == NULL) {
/* just go down a level */
}
else if (next_p == found_p
|| (char *)next_p->sa_mem > (char *)address) {
/* just go down a level */
}
else if ((char *)next_p->sa_mem == (char *)address) {
/* found it and go down a level */
found_p = next_p;
}
/*
* (char *)next_p->sa_mem < (char *)address
*/
else if ((! exact_b)
&& ((char *)next_p->sa_mem + next_p->sa_total_size >
(char *)address)) {
/*
* if we are doing loose searches and this block contains this
* pointer then we have a match
*/
found_p = next_p;
}
else {
/* next slot is less, go right */
slot_p = next_p;
continue;
}
/* we are lowering the level */
update_p->sa_next_p[level_c] = slot_p;
if (level_c == 0) {
break;
}
level_c--;
}
return found_p;
}
/*
* static skip_alloc_t *find_free_size
*
* Look for a specific size in the free skip list. If it exist then a
* pointer to the matching slot is returned otherwise NULL. Either
* way, the links that were traversed to get there are set in the
* update slot which has the maximum number of levels.
*
* Returns a pointer to the slot which matches the size pair on
* success or NULL on failure.
*
* ARGUMENTS:
*
* address -> Address we are looking for.
*
* update_p -> Pointer to the skip_alloc entry we are using to hold
* the update pointers.
*/
static skip_alloc_t *find_free_size(const unsigned int size,
skip_alloc_t *update_p)
{
int level_c, cmp;
skip_alloc_t *slot_p, *found_p = NULL, *next_p;
/* skip_free_max_level */
level_c = MAX_SKIP_LEVEL - 1;
slot_p = skip_free_list;
/* traverse list to smallest entry */
while (1) {
/* next on we are looking for */
next_p = slot_p->sa_next_p[level_c];
/* are we are at the end of a row? */
if (next_p == NULL
|| next_p == found_p) {
/* just go down a level */
}
else {
cmp = next_p->sa_total_size - size;
if (cmp < 0) {
/* next slot is less, go right */
slot_p = next_p;
continue;
}
else if (cmp == 0) {
/*
* we found a match but it may not be the first slot with this
* size and we want the first match
*/
found_p = next_p;
}
}
/* we are lowering the level */
update_p->sa_next_p[level_c] = slot_p;
if (level_c == 0) {
break;
}
level_c--;
}
/* space should be free */
if (found_p != NULL && (! BIT_IS_SET(found_p->sa_flags, ALLOC_FLAG_FREE))) {
/* sanity check */
dmalloc_errno = DMALLOC_ERROR_ADDRESS_LIST;
dmalloc_error("find_free_size");
return NULL;
}
return found_p;
}
/*
* static int insert_slot
*
* Insert an address entry into a skip list.
*
* Returns 1 on success or 0 on failure.
*
* ARGUMENTS:
*
* slot_p <-> Slot that we are inserting into the skip list.
*
* free_b -> Insert a free address in the free-size list otherwise it
* will go into the used address list.
*/
static int insert_slot(skip_alloc_t *slot_p, const int free_b)
{
skip_alloc_t *adjust_p, *update_p;
int level_c;
update_p = skip_update;
if (free_b) {
(void)find_free_size(slot_p->sa_total_size, update_p);
/*
* NOTE: we can get a new_p because there might be other blocks of
* the same size which we will be inserting before.
*/
}
else if (find_address(slot_p->sa_mem, 0 /* used list */, 1 /* exact */,
update_p) != NULL) {
/*
* Sanity check. We should not have found it since that means
* that someone has the same size and block-num.
*/
dmalloc_errno = DMALLOC_ERROR_ADDRESS_LIST;
dmalloc_error("insert_slot");
return 0;
}
/* update the block skip list */
for (level_c = 0; level_c <= slot_p->sa_level_n; level_c++) {
/*
* We are inserting our new slot after each of the slots in the
* update array. So for each level, we get the slot we are
* adjusting, we take it's next pointers and set them in the new
* slot, and we point its next pointers to the new slot.
*/
adjust_p = update_p->sa_next_p[level_c];
slot_p->sa_next_p[level_c] = adjust_p->sa_next_p[level_c];
adjust_p->sa_next_p[level_c] = slot_p;
}
return 1;
}
/*
* static int alloc_slots
*
* Allocate a block of new slots of a certain size and add them to the
* free list. If there are none in the linked list then we will
* allocate a block of the size.
*
* Returns a valid pointer to a single block that was allocated for
* the slots on success or NULL on failure.
*
* ARGUMENTS:
*
* level_n -> Number of the level we are looking for. Set to 0 to
* have it be chosen at random.
*/
static void *alloc_slots(const int level_n)
{
skip_alloc_t *new_p;
entry_block_t *block_p;
unsigned int *magic3_p, magic3;
int size, new_c;
if (BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_LOG_ADMIN)) {
dmalloc_message("need a block of slots for level %d", level_n);
}
/* we need to allocate a new block of the slots of this level */
block_p = _dmalloc_heap_alloc(BLOCK_SIZE);
if (block_p == NULL) {
/*
* Sanity check. Out of heap memory. Error code set in
* _dmalloc_heap_alloc().
*/
return NULL;
}
memset(block_p, 0, BLOCK_SIZE);
admin_block_c++;
/* intialize the block structure */
block_p->eb_magic1 = ENTRY_BLOCK_MAGIC1;
block_p->eb_level_n = level_n;
block_p->eb_magic2 = ENTRY_BLOCK_MAGIC2;
/* add the block on the entry block linked list */
block_p->eb_next_p = entry_blocks[level_n];
entry_blocks[level_n] = block_p;
/* put the magic3 at the end of the block */
magic3_p = (unsigned int *)((char *)block_p + BLOCK_SIZE -
sizeof(*magic3_p));
magic3 = ENTRY_BLOCK_MAGIC3;
memcpy(magic3_p, &magic3, sizeof(*magic3_p));
/* get the size of the slot */
size = SKIP_SLOT_SIZE(level_n);
/* add in all of the unused slots to the linked list */
new_c = 1;
for (new_p = &block_p->eb_first_slot;
(char *)new_p + size < (char *)magic3_p;
new_p = (skip_alloc_t *)((char *)new_p + size)) {
new_p->sa_level_n = level_n;
new_p->sa_next_p[0] = entry_free_list[level_n];
entry_free_list[level_n] = new_p;
new_c++;
}
/* extern pointer information set in _dmalloc_heap_alloc */
return block_p;
}
/*
* static int remove_slot
*
* Remove a slot from the skip list.
*
* Returns 1 on success or 0 on failure.
*
* ARGUMENTS:
*
* delete_p -> Pointer to the block we are deleting from the list.
*
* update_p -> Pointer to the skip_alloc entry we are using to hold
* the update pointers.
*/
static int remove_slot(skip_alloc_t *delete_p, skip_alloc_t *update_p)
{
skip_alloc_t *adjust_p;
int level_c;
/* update the block skip list */
for (level_c = 0; level_c <= MAX_SKIP_LEVEL; level_c++) {
/*
* The update node holds pointers to the slots which are pointing
* to the one we want since we need to update those pointers
* ahead.
*/
adjust_p = update_p->sa_next_p[level_c];
/*
* If the pointer in question is not pointing to the deleted slot
* then the deleted slot is shorter than this level and we are
* done. This is guaranteed if we have a proper skip list.
*/
if (adjust_p->sa_next_p[level_c] != delete_p) {
break;
}
/*
* We are deleting a slot after each of the slots in the update
* array. So for each level, we get the slot we are adjusting, we
* set it's next pointers to the next pointers at the same level
* from the deleted slot.
*/
adjust_p->sa_next_p[level_c] = delete_p->sa_next_p[level_c];
}
/*
* Sanity check here, we should always have at least 1 pointer to
* the found node that we are deleting.
*/
if (level_c == 0) {
dmalloc_errno = DMALLOC_ERROR_ADDRESS_LIST;
dmalloc_error("remove_slot");
return 0;
}
return 1;
}
/*
* static skip_alloc_t *get_slot
*
* Get a new slot of a certain size. This calls alloc_slot and then
* does a whole bunch of things if alloc_slots generates the need for
* two new slots. Jumping through hoops to get this right.
*
* Returns a valid skip-alloc pointer on success or NULL on failure.
*/
static skip_alloc_t *get_slot(void)
{
skip_alloc_t *new_p;
int level_n, slot_size;
void *admin_mem;
/* generate the level for our new slot */
level_n = random_level(MAX_SKIP_LEVEL);
slot_size = SKIP_SLOT_SIZE(level_n);
/* get an extry from the free list */
new_p = entry_free_list[level_n];
if (new_p != NULL) {
/* shift the linked list over */
entry_free_list[level_n] = new_p->sa_next_p[0];
/* zero our slot entry */
memset(new_p, 0, slot_size);
new_p->sa_level_n = level_n;
return new_p;
}
/*
* Okay, this is a little wild. Holding on?
*
* So we are trying to get a slot of a certain size to store
* something in a skip list. We didn't have any on the free-list so
* now we will allocate a block. We allocate a block of memory to
* hold the slots meaning that we may need 1 new slot to account for
* the admin and external memory in addition to the 1 requested.
*
* To do it right, would take a recursive call to get_slot which I
* am not willing to do so we will have 2 blocks in a row which have
* the same height. This is less than efficient but oh well.
*/
/* add in all of the unused slots to the linked list */
admin_mem = alloc_slots(level_n);
if (admin_mem == NULL) {
/* Sanity check. Error code set in alloc_slots(). */
return NULL;
}
/* get one for the admin memory */
new_p = entry_free_list[level_n];
if (new_p == NULL) {
/*
* Sanity check. We should have created a whole bunch of
* addresses.
*/
dmalloc_errno = DMALLOC_ERROR_ADDRESS_LIST;
dmalloc_error("get_slot");
return NULL;
}
entry_free_list[level_n] = new_p->sa_next_p[0];
memset(new_p, 0, slot_size);
new_p->sa_flags = ALLOC_FLAG_ADMIN;
new_p->sa_mem = admin_mem;
new_p->sa_total_size = BLOCK_SIZE;
new_p->sa_level_n = level_n;
/* now put it in the used list */
if (! insert_slot(new_p, 0 /* used list */)) {
/* Sanity check. error code set in insert_slot(). */
return NULL;
}
/* now get one for the user */
new_p = entry_free_list[level_n];
if (new_p == NULL) {
/*
* Sanity check. We should have created a whole bunch of
* addresses.
*/
dmalloc_errno = DMALLOC_ERROR_ADDRESS_LIST;
dmalloc_error("get_slot");
return NULL;
}
entry_free_list[level_n] = new_p->sa_next_p[0];
memset(new_p, 0, slot_size);
new_p->sa_level_n = level_n;
/* level_np set up top */
return new_p;
}
/*
* static skip_alloc_t *insert_address
*
* Insert an address entry into a skip list.
*
* Returns a valid slot pointer on sucess or NULL on failure.
*
* ARGUMENTS:
*
* address -> Address we are inserting into the address list.
*
* free_b -> Insert a free address in the free-size list otherwise it
* will go into the used address list.
*
* tot_size -> Total size of the chunk that we are inserting into the
* list.
*/
static skip_alloc_t *insert_address(void *address, const int free_b,
const unsigned int tot_size)
{
skip_alloc_t *new_p;
/* get a new entry */
new_p = get_slot();
if (new_p == NULL) {
/* error code set in get_slot */
return NULL;
}
if (free_b) {
new_p->sa_flags = ALLOC_FLAG_FREE;
}
else {
new_p->sa_flags = ALLOC_FLAG_USER;
}
new_p->sa_mem = address;
new_p->sa_total_size = tot_size;
/* now try and insert the slot into the skip-list */
if (! insert_slot(new_p, free_b)) {
/* Sanity check. error code set in insert_slot(). */
return NULL;
}
return new_p;
}
/******************************* misc routines *******************************/
/*
* static int expand_chars
*
* Copies a buffer into a output buffer while translates
* non-printables into %03o octal values. If it can, it will also
* translate certain \ characters (\r, \n, etc.) into \\%c. The
* routine is useful for printing out binary values.
*
* Note: It does _not_ add a \0 at the end of the output buffer.
*
* Returns the number of characters added to the output buffer.
*
* ARGUMENTS:
*
* buf - the buffer to convert.
*
* buf_size - size of the buffer. If < 0 then it will expand till it
* sees a \0 character.
*
* out - destination buffer for the convertion.
*
* out_size - size of the output buffer.
*/
static int expand_chars(const void *buf, const int buf_size,
char *out, const int out_size)
{
const unsigned char *buf_p, *spec_p;
char *out_p = out, *bounds_p;
/* setup our max pointer */
bounds_p = out + out_size;
/* run through the input buffer, counting the characters as we go */
for (buf_p = (const unsigned char *)buf;
buf_p < (const unsigned char *)buf + buf_size;
buf_p++) {
/* search for special characters */
for (spec_p = (unsigned char *)SPECIAL_CHARS + 1;
*(spec_p - 1) != '\0';
spec_p += 2) {
if (*spec_p == *buf_p) {
break;
}
}
/* did we find one? */
if (*(spec_p - 1) != '\0') {
if (out_p + 2 >= bounds_p) {
break;
}
out_p = append_format(out_p, bounds_p, "\\%c", *(spec_p - 1));
continue;
}
/* print out any 7-bit printable characters */
if (*buf_p < 128 && isprint(*buf_p)) {
if (out_p + 1 >= bounds_p) {
break;
}
*out_p = *(char *)buf_p;
out_p += 1;
}
else {
if (out_p + 4 >= bounds_p) {
break;
}
out_p = append_format(out_p, bounds_p, "\\%03o", *buf_p);
}
}
/* try to punch the null if we have space in case the %.*s doesn't work */
if (out_p < bounds_p) {
*out_p = '\0';
}
return out_p - out;
}
/*
* static void get_pnt_info
*
* With a slot, set a number of pointers to places within the block.
*
* ARGUMENTS:
*
* slot_p -> Pointer to a slot structure that we are getting info on.
*
* info_p <-> Pointer to an info structure that we are filling with
* information.
*/
static void get_pnt_info(const skip_alloc_t *slot_p, pnt_info_t *info_p)
{
info_p->pi_fence_b = BIT_IS_SET(slot_p->sa_flags, ALLOC_FLAG_FENCE);
info_p->pi_valloc_b = BIT_IS_SET(slot_p->sa_flags, ALLOC_FLAG_VALLOC);
info_p->pi_blanked_b = BIT_IS_SET(slot_p->sa_flags, ALLOC_FLAG_BLANK);
info_p->pi_alloc_start = slot_p->sa_mem;
if (info_p->pi_fence_b) {
if (info_p->pi_valloc_b) {
info_p->pi_user_start = (char *)info_p->pi_alloc_start + BLOCK_SIZE;
info_p->pi_fence_bottom = (char *)info_p->pi_user_start -
FENCE_BOTTOM_SIZE;
}
else {
info_p->pi_fence_bottom = info_p->pi_alloc_start;
info_p->pi_user_start = (char *)info_p->pi_alloc_start +
FENCE_BOTTOM_SIZE;
}
}
else {
info_p->pi_fence_bottom = NULL;
info_p->pi_user_start = info_p->pi_alloc_start;
}
info_p->pi_user_bounds = (char *)info_p->pi_user_start +
slot_p->sa_user_size;
info_p->pi_alloc_bounds = (char *)slot_p->sa_mem + slot_p->sa_total_size;
if (info_p->pi_fence_b) {
info_p->pi_fence_top = info_p->pi_user_bounds;
info_p->pi_upper_bounds = (char *)info_p->pi_alloc_bounds - FENCE_TOP_SIZE;
}
else {
info_p->pi_fence_top = NULL;
info_p->pi_upper_bounds = info_p->pi_alloc_bounds;
}
}
/*
* static char *display_pnt
*
* Write into a buffer a discription of a pointer.
*
* Returns a pointer to buffer 1st argument.
*
* ARGUMENTS:
*
* user_pnt -> Pointer that we are displaying.
*
* alloc_p -> Pointer to the skip slot which we are displaying.
*
* buf <-> Passed in buffer which will be filled with a description of
* the pointer.
*
* buf_size -> Size of the buffer in bytes.
*/
static char *display_pnt(const void *user_pnt, const skip_alloc_t *alloc_p,
char *buf, const int buf_size)
{
char *buf_p, *bounds_p;
int elapsed_b;
buf_p = buf;
bounds_p = buf_p + buf_size;
buf_p = append_format(buf_p, bounds_p, "%p", user_pnt);
#if LOG_PNT_SEEN_COUNT
buf_p = append_format(buf_p, bounds_p, "|s%lu", alloc_p->sa_seen_c);
#endif
#if LOG_PNT_ITERATION
buf_p = append_format(buf_p, bounds_p, "|i%lu", alloc_p->sa_iteration);
#endif
if (BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_LOG_ELAPSED_TIME)) {
elapsed_b = 1;
}
else {
elapsed_b = 0;
}
if (elapsed_b || BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_LOG_CURRENT_TIME)) {
#if LOG_PNT_TIMEVAL
{
char time_buf[64];
buf_p = append_format(buf_p, bounds_p, "|w%s",
_dmalloc_ptimeval(&alloc_p->sa_timeval, time_buf,
sizeof(time_buf), elapsed_b));
}
#else
#if LOG_PNT_TIME
{
char time_buf[64];
buf_p = append_format(buf_p, bounds_p, "|w%s",
_dmalloc_ptime(&alloc_p->sa_time, time_buf,
sizeof(time_buf), elapsed_b));
}
#endif
#endif
}
#if LOG_PNT_THREAD_ID
{
char thread_id[256];
buf_p = append_string(buf_p, bounds_p, "|t");
THREAD_ID_TO_STRING(thread_id, sizeof(thread_id), alloc_p->sa_thread_id);
buf_p = append_string(buf_p, bounds_p, thread_id);
}
#endif
append_null(buf_p, bounds_p);
return buf;
}
/*
* static void log_error_info
*
* Logging information about a pointer, usually during an error condition.
*
* ARGUMENTS:
*
* now_file -> File from where we generated the error.
*
* now_line -> Line number from where we generated the error.
*
* user_pnt -> Pointer in question. This can be 0L then it will use
* the slot_p memory pointer.
*
* slot_p -> Pointer to the slot associated with the user_pnt or 0L.
*
* reason -> Reason string why something happened.
*
* where -> What routine is calling log_error_info. For instance
* malloc or chunk_check.
*/
static void log_error_info(const char *now_file,
const unsigned int now_line,
const void *user_pnt,
const skip_alloc_t *slot_p,
const char *reason, const char *where)
{
static int dump_bottom_b = 0, dump_top_b = 0;
char out[(DUMP_SPACE + FENCE_BOTTOM_SIZE + FENCE_TOP_SIZE) * 4];
char where_buf[MAX_FILE_LENGTH + 64];
char where_buf2[MAX_FILE_LENGTH + 64];
const char *prev_file;
const void *dump_pnt = user_pnt;
const void *start_user;
unsigned int prev_line, user_size;
skip_alloc_t *other_p;
pnt_info_t pnt_info;
int out_len, dump_size, offset;
if (slot_p == NULL) {
prev_file = NULL;
prev_line = 0;
user_size = 0;
start_user = user_pnt;
}
else {
prev_file = slot_p->sa_file;
prev_line = slot_p->sa_line;
user_size = slot_p->sa_user_size;
if (user_pnt == NULL) {
get_pnt_info(slot_p, &pnt_info);
start_user = pnt_info.pi_user_start;
}
else {
start_user = user_pnt;
}
}
/* get a proper reason string */
if (reason != NULL) {
dmalloc_message(" error details: %s", reason);
}
/* dump the pointer information */
if (start_user == NULL) {
dmalloc_message(" from '%s' prev access '%s'",
_dmalloc_chunk_desc_pnt(where_buf, sizeof(where_buf),
now_file, now_line),
_dmalloc_chunk_desc_pnt(where_buf2, sizeof(where_buf2),
prev_file, prev_line));
}
else {
dmalloc_message(" pointer '%p' from '%s' prev access '%s'",
start_user,
_dmalloc_chunk_desc_pnt(where_buf, sizeof(where_buf),
now_file, now_line),
_dmalloc_chunk_desc_pnt(where_buf2, sizeof(where_buf2),
prev_file, prev_line));
}
/*
* If we aren't logging bad space or we didn't error with an
* overwrite error then don't log the bad bytes.
*/
if ((! BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_LOG_BAD_SPACE))
|| (dmalloc_errno != DMALLOC_ERROR_UNDER_FENCE
&& dmalloc_errno != DMALLOC_ERROR_OVER_FENCE
&& dmalloc_errno != DMALLOC_ERROR_FREE_OVERWRITTEN)) {
/* we call the error function after writing more info to the logfile */
dmalloc_error(where);
return;
}