-
Notifications
You must be signed in to change notification settings - Fork 12
/
malloc.c
1458 lines (1348 loc) · 38 KB
/
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
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#ifdef SHM
#define malloc shm_malloc
#define realloc shm_realloc
#define free shm_free
#define calloc shm_calloc
#define malloc_small shm_malloc_small
#define valloc shm_valloc
#define sbrk shm_sbrk
#define brk shm_brk
#define minit abort
#define mresize shm_mresize
#define msize shm_msize
#define heapdump shm_heapdump
#else
#ifdef INDIRECT
#define malloc _malloc
#define realloc _realloc
#define free _free
#define calloc _calloc
#define valloc _valloc
#define mresize _mresize
#define msize _msize
#define malloc_small _malloc_small
#define heapdump _heapdump
#endif
extern void *sbrk(intptr_t);
extern int brk(void *);
#endif
#define _S(x) #x
#define S(x) _S(x)
/* these are the different locking schemes. The numbers associated with
** them are unimportant; they need only be different */
#define SYSVSEM 1 /* SysV Semaphores */
#define FLOCK 2 /* File Locks */
#define SPINLOCK 3 /* atomic test-and-set spinlocks */
#define PMUTEX 4 /* pthreads mutexes */
#if defined(SHM) || defined(_REENTRANT) || defined(_POSIX_THREADS)
#ifndef LOCKTYPE
#if defined(_POSIX_THREADS) /* && !defined(__CYGWIN__) */
#define LOCKTYPE PMUTEX
#elif defined(__GNUC__) && (defined(mc68000) || defined(sparc) || \
defined(m88k) || defined(__alpha__) || \
defined(__ppc__) || defined(__i386__))
#define LOCKTYPE SPINLOCK
#else
#define LOCKTYPE FLOCK
#endif
#endif
#else /* !SHM && !_REENTRANT && !_POSIX_THREADS */
#undef LOCKTYPE
#endif /* SHM || _REENTRANT || _POSIX_THREADS */
#if defined(__CYGWIN__)
/* don't try to use thread-local vars on cygwin */
#define thread_local
#elif defined(__STDC__) && __STDC_VERSION__ >= 199901L
#define thread_local __thread
#elif defined(__GNUC__) && __GNUC__ >= 4
#define thread_local __thread
#else
#define thread_local
#endif
#include "malloc.h"
#if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
#undef SMLIST
static inline int SMLIST(int sz) {
int rv;
asm("bsr %1,%0" : "=r"(rv) : "r"((sz-1)|1));
rv -= 7-size256;
if (rv < 0) rv = 0;
if (rv >= NUMSMALL) rv = -1;
return rv;
}
#endif
#ifdef SHM
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
struct basepage * const membase =
#if __SIZEOF_POINTER__ == 8
# define membase ((struct basepage *)0x1000000000L)
#else
# if defined(sun)
# define membase ((struct basepage *)0xe0000000)
# elif defined(sgi)
# define membase ((struct basepage *)0x08000000)
# elif defined(_AIX)
# define membase ((struct basepage *)0x40000000)
# elif defined(__FreeBSD__)
# define membase ((struct basepage *)0x10000000)
# elif defined(__hpux__)
# define membase ((struct basepage *)0xa0000000)
# elif defined(__CYGWIN__)
# define membase ((struct basepage *)0x40000000)
# else
# define membase ((struct basepage *)0x80000000)
# endif
#endif
membase;
/* minimum number of additional pages to mmap when expanding the heap */
#define MMAP_INCR 16
static void *localbrk;
static int mfd;
#else /* !SHM */
static struct basepage *membase;
#endif /* SHM */
#if defined(SHM) || defined(_REENTRANT) || defined(_POSIX_THREADS)
#if LOCKTYPE == SYSVSEM
#if defined(_AIX) || defined(__osf__)
/* AIX and OSF/1 have eliminated union semun, but are otherwise compatable */
union semun {
int val;
struct semid_ds *buf;
ushort *array;
};
#endif /* _AIX || __osf__ */
static int semid;
static struct sembuf sembuf;
#define FIRSTKEY 1 /* first semaphore key to try */
static int lock_init(int init)
{
if (init) {
int um = umask(0); umask(um);
um = ~um & 0777;
membase->semkey = FIRSTKEY;
while ((semid = semget(membase->semkey, NUMSMALL+1,
IPC_CREAT|IPC_EXCL|um)) < 0 &&
errno == EEXIST)
membase->semkey++;
if (semid >= 0) {
ushort arr[NUMSMALL+1];
int i;
union semun semu;
semu.array = arr;
for (i = 0; i<=NUMSMALL; i++)
arr[i] = 1;
if (semctl(semid, 0, SETALL, semu) < 0) {
semctl(semid, 0, IPC_RMID, semu);
return -1; } } }
else
semid = semget(membase->semkey, 0, 0);
return (semid < 0) ? -1 : 0;
}
#define LOCK(q) do { \
sembuf.sem_num = q < NUMSMALL ? q : NUMSMALL; \
sembuf.sem_op = -1; \
sembuf.sem_flg = 0; \
while (semop(semid, &sembuf, 1)<0) \
assert(errno == EINTR); } while(0)
#define UNLOCK(q) do { \
sembuf.sem_num = q < NUMSMALL ? q : NUMSMALL; \
sembuf.sem_op = 1; \
sembuf.sem_flg = 0; \
while (semop(semid, &sembuf, 1)<0) \
assert(errno == EINTR); } while(0)
#define LOCK_FINI
#define LOCK_DESTROY do { \
union semun semu; \
semu.val = 0; \
semctl(semid, 0, IPC_RMID, semu); } while(0)
#endif /* SYSVSEM */
#if LOCKTYPE == FLOCK
static int lfd;
static struct flock lock;
static int lock_init(int init)
{
char lfile[1024];
strcpy(lfile, membase->mfile);
strcat(lfile, ".lock");
lock.l_whence = SEEK_SET;
lock.l_len = 1;
if (!init) {
if ((lfd = open(lfile, O_RDWR, 0)) < 0) return -1;
} else if ((lfd = open(lfile, O_RDWR|O_CREAT, 0666)) < 0)
return -1;
else
ftruncate(lfd, lfile, NUMSMALL+1);
fcntl(lfd, F_SETFD, FD_CLOEXEC);
return 0;
}
#define LOCK(q) do { \
lock.l_type = F_WRLCK; \
lock.l_start = (q); \
while (fcntl(lfd, F_SETLKW, &lock) < 0) \
assert(errno == EINTR); } while(0)
#define UNLOCK(q) do { \
lock.l_type = F_UNLCK; \
lock.l_start = (q); \
while (fcntl(lfd, F_SETLK, &lock) < 0) \
assert(errno == EINTR); } while(0)
#define LOCK_FINI close(lfd)
#define LOCK_DESTROY do { \
char lfile[1024]; \
strcpy(lfile, membase->mfile); \
strcat(lfile, ".lock"); \
unlink(lfile); \
close(lfd); } while(0)
#endif /* FLOCK */
#if LOCKTYPE == SPINLOCK
#include <sys/time.h>
static int lock_init(int init)
{
if (init) {
int i;
for (i=NUMSMALL; i>=0; i--)
membase->locks[i] = 0; }
return 0;
}
#define LOCK(q) do { \
volatile TAS_t *_l = &membase->locks[q]; \
int _try = 10; \
while (_try > 0 && (*_l || TAS(*_l))) _try--; \
if (!_try) while (*_l || TAS(*_l)) { \
struct timeval to = { 0, 1000 }; \
select(0, 0, 0, 0, &to); } \
MEMORY_BARRIER; \
} while(0)
#define UNLOCK(q) do { \
MEMORY_BARRIER; \
membase->locks[q] = 0; \
} while(0)
#define LOCK_FINI
#define LOCK_DESTROY
#endif /* SPINLOCK */
#if LOCKTYPE == PMUTEX
static int lock_init(int init)
{
if (init) {
int i;
for (i=NUMSMALL; i>=0; i--)
pthread_mutex_init(&membase->locks[i], 0); }
return 0;
}
#define LOCK(q) pthread_mutex_lock(&membase->locks[q])
#define UNLOCK(q) pthread_mutex_unlock(&membase->locks[q])
#define LOCK_FINI
#define LOCK_DESTROY
#endif /* PMUTEX */
#else /* !SHM && !_REENTRANT && !_POSIX_THREADS */
#define LOCK(q)
#define UNLOCK(q)
static int lock_init()
{
return 0;
}
#endif /* SHM || _REENTRANT || _POSIX_THREADS */
typedef unsigned long U;
#define TARGET(l) (2 << ((NUMSMALL-1 - (l))/2))
#define PAGENUM(p) (((U)(p) - (U)membase) / PAGESIZE)
#define PAGEADDR(n) ((void *)((U)membase + (U)(n) * PAGESIZE))
#define PAGEBASE(p) ((U)p & ~(PAGESIZE-1))
#define I2(pn) ((pn) % (PAGESIZE/sizeof(struct page)))
#define I1(pn) ((pn) / (PAGESIZE/sizeof(struct page)))
#define ADDR2PAGE(p) (&membase->pages[I1(PAGENUM(p))][I2(PAGENUM(p))])
#define NUM2PAGE(n) (&membase->pages[I1(n)][I2(n)])
#define VALID(p) (((U)(p) > (U)membase) && ((U)(p) < (U)membase->end))
#define FREEPAGE(n) ((struct freepage *)PAGEADDR(n))
#ifdef MALLOC_DEBUG
static unsigned long lcrng(unsigned long s)
{
unsigned long long mod = (1LL<<31) - 1;
unsigned long long t = s * 16807LL;
t = (t&mod) + (t>>31);
if (t>mod) t -= mod;
return t;
}
#define GUARD 0xa1962f8dU
#define DB(code) code
#else /* !MALLOC_DEBUG */
#define DB(code)
#endif /* MALLOC_DEBUG */
static inline int pcmp(unsigned _a, unsigned _b)
{
struct page *a = NUM2PAGE(_a), *b = NUM2PAGE(_b);
int v;
v = a->count - b->count;
return v ? v : (long)_a - (long)_b;
}
#if 0
/*
* FIXME -- profile this sorter and maybe choose a better one?
* FIXME -- this pivot choice is pessimal for a reversed list, but very
* FIXME -- good (O(n)) for almost sorted lists, which should be our
* FIXME -- common case. Probably not a big deal as the lists should
* FIXME -- rarely be big
* FIXME -- we're also assuming the optimizer will do a good job CSEing
* FIXME -- these NUM2PAGE macros after inlining pcmp
*
* This algorithm has very bad behavior with a list that is sorted except
* for the last element, which turns out to be a somewhat common case here.
*
* Quicksort with last-sorted pivot
*/
unsigned page_list_sort(unsigned p, unsigned **tail)
{
unsigned a, *a_tail, b, *b_tail, pivot;
if (!p) return p;
pivot = a = p;
a_tail = &a;
p = NUM2PAGE(p)->page;
while (p) {
if (pcmp(pivot, p) > 0) break;
a_tail = &NUM2PAGE(pivot)->page;
pivot = p;
p = NUM2PAGE(p)->page; }
if (!p) {
if (tail) *tail = &NUM2PAGE(pivot)->page;
return a; }
b_tail = &b;
while (p) {
if (pcmp(pivot, p) > 0) {
*a_tail = p;
a_tail = &NUM2PAGE(p)->page; }
else {
*b_tail = p;
b_tail = &NUM2PAGE(p)->page; }
p = NUM2PAGE(p)->page; }
*a_tail = 0;
*b_tail = 0;
if (a) a = page_list_sort(a, &a_tail);
if (b) b = page_list_sort(b, &b_tail);
*a_tail = pivot;
NUM2PAGE(pivot)->page = b;
if (tail)
*tail = b ? b_tail : &NUM2PAGE(pivot)->page;
return a;
}
#else
/*
* FIXME -- profile this sorter and maybe choose a better one?
*
* simple split/merge sort */
unsigned page_list_sort(unsigned p)
{
unsigned a, b, *t;
int asort = 0;
if (!p) return p;
a = b = p;
b = NUM2PAGE(b)->page;
if (!b) return p;
while (b) {
if (!(b = NUM2PAGE(b)->page)) break;
unsigned l = a;
a = NUM2PAGE(a)->page;
if (!asort && pcmp(l, a) > 0)
asort = 1;
b = NUM2PAGE(b)->page; }
b = page_list_sort(NUM2PAGE(a)->page);
NUM2PAGE(a)->page = 0;
a = p;
if (asort) a = page_list_sort(a);
t = &p;
while (a && b) {
if (pcmp(a, b) <= 0) {
*t = a;
t = &NUM2PAGE(a)->page;
a = *t;
} else {
*t = b;
t = &NUM2PAGE(b)->page;
b = *t; } }
*t = a | b;
return p;
}
#define page_list_sort(p, t) page_list_sort(p)
#endif
static thread_local struct localfree freelists[NUMSMALL];
#ifdef MALLOC_DEBUG
static thread_local struct backup {
struct backup *next;
struct chunk *item;
} *backupfree[NUMSMALL], *backupaux[NUMSMALL], *spare;
static void tbackup(int i)
{
struct chunk *p;
struct backup *q;
for (p=freelists[i].free, q=backupfree[i]; p && q; p=p->next, q=q->next)
if (p != q->item) {
printf("***"S(free)" list for size %d corrupted\n", SIZE(i));
abort(); }
if (p || q) {
printf("***"S(free)" list for size %d corrupted\n", SIZE(i));
abort(); }
for (p=freelists[i].aux, q=backupaux[i]; p && q; p=p->next, q=q->next)
if (p != q->item) {
printf("***"S(free)" list for size %d corrupted\n", SIZE(i));
abort(); }
if (p || q) {
printf("***"S(free)" list for size %d corrupted\n", SIZE(i));
abort(); }
}
static struct backup *balloc()
{
struct backup *p;
int i;
if (!spare) {
p = valloc(PAGESIZE);
i = PAGESIZE/sizeof(struct backup) - 1;
p[i].next = 0;
for (i--; i>=0; i--)
p[i].next = &p[i+1];
spare = p; }
p = spare;
spare = p->next;
return p;
}
static void bfree(struct backup *p)
{
p->next = spare;
spare = p;
}
static void *gcheck(void *_b)
{
U *b = _b;
if (b) {
b -= 2;
if(b[0] != GUARD || b[1] != lcrng((U)b)) {
printf("***guard corrupted at %p\n", b);
abort(); } }
return b;
}
static void *gsetup(void *_b)
{
U *b = _b;
if (b) {
*b++ = GUARD;
*b++ = lcrng((U)_b); }
return b;
}
#endif /* MALLOC_DEBUG */
void *malloc(size_t size)
{
int sc;
void *rv;
DB( size += 2*sizeof(U); )
sc = SMLIST(size);
if (sc >= 0)
rv = malloc_small(sc);
else
rv = valloc(size);
DB( rv = gsetup(rv); )
return rv;
}
static void msetup()
{
memset(membase, 0, 3 * PAGESIZE);
memcpy(membase->magic, "SHM ", 4);
membase->param[0] = 0;
membase->param[1] = sizeof(void *);
#ifdef LOCKTYPE
membase->param[2] = LOCKTYPE;
#endif
#ifdef MALLOC_DEBUG
membase->param[2] |= 0x80;
#endif
membase->param[3] = NUMSMALL;
membase->base = membase;
membase->pages = (struct page **)((U)membase + PAGESIZE);
membase->end = (void *)((U)membase + PAGESIZE*3);
membase->pages[0] = (struct page *)((U)membase + PAGESIZE*2);
membase->pages[0][0].code = BASE;
membase->pages[0][1].code = BASE;
membase->pages[0][2].code = BASE;
}
#ifdef SHM
static int msetup_valid()
{
if (memcmp(membase->magic, "SHM ", 4)) return 0;
if (membase->param[0] != 0) return 0;
if (membase->param[1] != sizeof(void *)) return 0;
if ((membase->param[2] & 0x7f) != LOCKTYPE) return 0;
#ifdef MALLOC_DEBUG
if (!(membase->param[2] & 0x80)) return 0;
#else
if (membase->param[2] & 0x80) return 0;
#endif
if (membase->param[3] != NUMSMALL) return 0;
if (membase->base != membase) return 0;
return 1;
}
#endif
#ifndef SHM
static void minit()
{
U p;
p = (U)sbrk(0);
if (p%PAGESIZE) {
sbrk(PAGESIZE - p%PAGESIZE);
p = (U)sbrk(0); }
assert(p%PAGESIZE == 0);
membase = (struct basepage *)p;
sbrk(PAGESIZE * 3);
msetup();
lock_init(1);
}
#ifdef WINNT
int brk(void *p)
{
void *op = sbrk(0);
return (int)sbrk((int)p - (int)op);
}
#endif /* WINNT */
#else /* SHM */
#ifdef __CYGWIN__
/* Cygwin's mmap can't deal with multiple partial mappings of a file, so
* in order to map more of our shared mem file, we need to unmap what we
* have mapped and then remap the whole thing as one chunk. This doesn't
* work for anonymous mapping (we'd lose what was previously mapped), so
* we always do them in multiples of 64K which seems to work out ok */
static void *cygwin_mmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset)
{
if (fd >= 0 && addr != membase) {
munmap(membase, offset);
length += offset;
addr = membase;
offset = 0;
} else if (fd < 0) {
/* how much was already mapped by a previous mmap */
size_t done = -(intptr_t)addr & 0xffff;
if (length <= done)
return addr;
addr = (char *)addr + done;
length -= done;
/* round up to 64K */
length |= 0xffff;
length++;
offset = 0; /* should be ignored by mmap */
}
return mmap(addr, length, prot, flags, fd, offset);
}
#define mmap cygwin_mmap
#endif /* __CYGWIN__ */
static struct sigaction oldsegv;
static void shm_segv()
{
void *newbrk;
int flags = MAP_SHARED|MAP_FIXED;
/* if a SEGV occurred and there's new memory to be mapped, map it
** and retry */
if (mfd < 0) flags |= MAP_ANONYMOUS;
newbrk = membase->eof;
if (newbrk > localbrk) {
if (mfd >= 0) lseek(mfd, 0, SEEK_SET);
mmap(localbrk, newbrk - localbrk, PROT_READ|PROT_WRITE,
flags, mfd, localbrk - (void *)membase);
localbrk = newbrk; }
else {
/* no more to map, must be a real SEGV */
sigaction(SIGSEGV, &oldsegv, 0); }
}
int shm_destroy()
{
LOCK_DESTROY;
unlink(membase->mfile);
munmap(membase, localbrk - (void *)membase);
close(mfd);
return 0;
}
int shm_init(const char *mfile, void (*init_fn)())
{
int tmp, wait = 5;
int flags = MAP_SHARED|MAP_FIXED;
struct sigaction segv;
mfd = -1;
if (!mfile) flags |= MAP_ANONYMOUS;
while(mfd == -1) {
if (mfile && (mfd = open(mfile, O_RDWR, 0)) >= 0) {
/* make sure the file isn't empty */
while (read(mfd, &tmp, sizeof(tmp)) == 0) {
if (wait-- < 0) {
close(mfd);
errno = EINVAL;
return -1; }
sleep(1); }
lseek(mfd, 0, SEEK_SET);
if ((long)mmap(membase, PAGESIZE, PROT_READ|PROT_WRITE,
flags, mfd, 0) == -1) {
close(mfd);
return -1; }
/* wait until initialization is complete */
while (!membase->init && wait-- > 0) sleep(1);
if (!membase->init || !msetup_valid()) {
munmap(membase, PAGESIZE);
close(mfd);
errno = EINVAL;
return -1; }
localbrk = membase->eof;
lseek(mfd, 0, SEEK_SET);
if ((long)mmap((void *)membase + PAGESIZE,
(localbrk - (void *)membase) - PAGESIZE,
PROT_READ|PROT_WRITE, flags, mfd, PAGESIZE) == -1) {
close(mfd);
return -1; } }
else if (!mfile || (errno == ENOENT &&
(mfd = open(mfile, O_RDWR|O_CREAT|O_EXCL, 0666)) >= 0)) {
if (mfd >= 0) {
if (ftruncate(mfd, 3*PAGESIZE) < 0) {
close(mfd);
return -1; }
lseek(mfd, 0, SEEK_SET); }
if ((long)mmap(membase, 3*PAGESIZE, PROT_READ|PROT_WRITE,
flags, mfd, 0) == -1) {
close(mfd);
return -1; }
msetup();
localbrk = membase->brk = membase->eof = membase->end;
strcpy(membase->mfile, mfile ? mfile : "");
membase->global = 0;
break; }
else if (errno != EEXIST)
return -1; }
fcntl(mfd, F_SETFD, FD_CLOEXEC);
if (lock_init(!membase->init) < 0) {
munmap(membase, localbrk - (void *)membase);
close(mfd);
return -1; }
if (!membase->init && init_fn)
init_fn();
segv.sa_flags = 0;
sigemptyset(&segv.sa_mask);
segv.sa_handler = shm_segv;
sigaction(SIGSEGV, &segv, &oldsegv);
membase->init = 1;
return 0;
}
static void flush_to_global_freelist(int, struct chunk *, struct chunk *);
/* flush out local free lists, so we can exit leaving memory consistent */
int shm_fini()
{
int l;
for (l=0; l<NUMSMALL; l++) {
if (freelists[l].aux || freelists[l].free)
flush_to_global_freelist(l, freelists[l].free, freelists[l].aux);
freelists[l].aux = freelists[l].free = 0;
freelists[l].count = 0; }
//munmap(membase, localbrk - (void *)membase);
//close(mfd);
LOCK_FINI;
return 0;
}
/* clear all the free lists, as they really belong to the parent */
int shm_child()
{
int l;
for (l=0; l<NUMSMALL; l++)
freelists[l].aux = freelists[l].free = 0;
return 0;
}
static int shm_brk(void *newbrk)
{
char tmp = 0;
int flags = MAP_SHARED|MAP_FIXED;
if (mfd < 0) flags |= MAP_ANONYMOUS;
if (newbrk <= membase->brk) {
if (ftruncate(mfd, newbrk - (void *)membase) < 0)
return -1;
if (newbrk < membase->eof)
munmap(newbrk, membase->eof - newbrk);
membase->brk = membase->eof = localbrk = newbrk; }
else if (newbrk <= membase->eof) {
membase->brk = newbrk;
if (newbrk > localbrk) {
if (mfd >= 0) lseek(mfd, 0, SEEK_SET);
if ((long)mmap(localbrk, membase->eof - localbrk,
PROT_READ|PROT_WRITE, flags, mfd,
localbrk - (void *)membase) == -1)
return -1;
localbrk = membase->eof; } }
else {
void *neweof = membase->brk + PAGESIZE * MMAP_INCR;
if (newbrk > neweof)
neweof = newbrk;
if (mfd >= 0) {
if (ftruncate(mfd, neweof - (void *)membase) < 0)
return -1;
if (lseek(mfd, neweof - (void *)membase - 1, SEEK_SET) < 0)
return -1;
if (write(mfd, &tmp, 1) != 1)
return -1;
lseek(mfd, 0, SEEK_SET); }
membase->eof = neweof;
if ((long)mmap(localbrk, neweof - localbrk,
PROT_READ|PROT_WRITE, flags, mfd,
localbrk - (void *)membase) == -1)
return -1;
localbrk = neweof;
membase->brk = newbrk; }
return 0;
}
static void *shm_sbrk(int delta)
{
void *oldbrk = membase->brk;
return shm_brk(membase->brk + delta) < 0 ? (void *)-1 : oldbrk;
}
void *shm_global()
{
return membase->global;
}
void shm_set_global(void *v)
{
membase->global = v;
}
#endif /* SHM */
/*
** The free page list contains all the entirely free pages. It is organized
** as a `list of lists' with blocks of the same size in the same list.
** the lists are sorted order of size (smallest first), and each list is
** sorted in memory order (lowest address first)
*/
#ifdef MALLOC_DEBUG
/* check the global freepage lists to ensure consistency, and ensure that
* 'p' is present (free) on there */
static void fp_verify(struct freepage *p)
{
struct freepage *t1, *t2;
struct page *pp;
int i;
if (membase->freepages && (!VALID(membase->freepages) ||
membase->freepages->parent != &membase->freepages)) {
printf("***"S(free)"list corrupt (base table)\n");
abort(); }
for (t1=membase->freepages; t1; t1 = t1->bigger) {
if (t1->bigger && (!VALID(t1->bigger) ||
t1->size >= t1->bigger->size ||
t1->bigger->parent != &t1->bigger)) {
printf("***"S(free)"list corrupt (page %p ?)\n", t1);
abort(); }
for (t2=t1; t2; t2 = t2->next) {
if (p == t2) p = 0;
if (t2->next && (!VALID(t2->next) || t2->next->bigger ||
t2->size != t2->next->size ||
t2->next->parent != &t2->next)) {
printf("***"S(free)"list corrupt (page %p ?)\n", t2);
abort(); }
pp = ADDR2PAGE(t2);
if (pp->code != BIG+FREE ||
PAGEADDR(pp->page - t2->size) != (void *)t2)
{
printf("***page tables corrupt (page %p)\n", t2);
abort(); }
for (i=1; i<t2->size; i++) {
struct page *ip = NUM2PAGE(PAGENUM(t2) + i);
if (ip->code != MIDDLE || PAGEADDR(ip->page) != (void *)t2) {
printf("***page tables corrupt (page %p)\n",
(char *)t2 + i*PAGESIZE);
abort(); } } } }
if (p) {
printf("***apparently free page %p not on "S(free)"list\n", p);
abort(); }
}
#else /* !MALLOC_DEBUG */
#define fp_verify(p)
#endif /* MALLOC_DEBUG */
static void fp_remove(struct freepage *p)
{
fp_verify(p);
if (p->next) {
(*p->parent) = p->next;
p->next->parent = p->parent;
if ((p->next->bigger = p->bigger))
p->bigger->parent = &p->next->bigger; }
else {
if (((*p->parent) = p->bigger))
p->bigger->parent = p->parent; }
}
static void fp_add(struct freepage *p)
{
struct freepage **t = &membase->freepages;
fp_verify(0);
while (*t && (*t)->size < p->size)
t = &(*t)->bigger;
if (*t && (*t)->size == p->size) {
while (*t && (U)*t < (U)p)
t = &(*t)->next;
if ((p->next = (*t))) {
if ((p->bigger = p->next->bigger)) {
p->bigger->parent = &p->bigger;
p->next->bigger = 0; }
p->next->parent = &p->next; }
else
p->bigger = 0; }
else {
p->next = 0;
if ((p->bigger = (*t)))
p->bigger->parent = &p->bigger; }
*t = p;
p->parent = t;
}
static struct freepage *fp_find(U size)
{
struct freepage *t;
fp_verify(0);
for (t=membase->freepages; t && t->size < (int)size; t = t->bigger);
if (t) fp_remove(t);
return t;
}
void *malloc_small(int l)
{
struct chunk *new;
if (!membase) minit();
assert(l >= SMLIST(sizeof(void *)) && l < NUMSMALL);
DB(tbackup(l));
if (!freelists[l].free) {
if (freelists[l].aux) {
freelists[l].free = freelists[l].aux;
freelists[l].aux = 0;
DB(backupfree[l] = backupaux[l]);
DB(backupaux[l] = 0); }
else {
int i;
struct chunk *new_fl = 0;
if (!freelists[l].target)
freelists[l].target = TARGET(l);
LOCK(l);
for (i=freelists[l].target; i; i--) {
unsigned pn;
struct page *p;
if (!(pn = membase->freechunks[l])) {
int j;
if (!(new = valloc(PAGESIZE))) {
UNLOCK(l);
return 0; }
pn = PAGENUM(new);
p = ADDR2PAGE(new);
p->code = l;
p->count = j = PERPAGE(l);
p->free = 0;
p->page = 0;
while (--j) {
struct chunk *prev = new;
new = (struct chunk *)((U)new + SIZE(l));
prev->next = new; }
new->next = 0;
membase->freechunks[l] = pn; }
p = NUM2PAGE(pn);
new = (struct chunk *)((U)PAGEADDR(pn) + p->free*SIZE(l));
if (new->next)
p->free = ((U)new->next - (U)PAGEADDR(pn))/SIZE(l);
else {
p->free = 0;
assert(p->count == 1); }
if (!--p->count) {
assert(p->free == 0);
membase->freechunks[l] = p->page; }
new->next = new_fl;
new_fl = new; }
freelists[l].free = new_fl;
DB({struct chunk *p;
struct backup **q;
for (p=new_fl, q=&backupfree[l]; p; p=p->next, q=&(*q)->next) {
*q = balloc();
(*q)->item = p; }
*q = 0; });
UNLOCK(l); }
freelists[l].count = freelists[l].target; }
new = freelists[l].free;
freelists[l].free = new->next;
DB({struct backup *tmp = backupfree[l];
backupfree[l] = tmp->next;
bfree(tmp); });
freelists[l].count--;
DB(tbackup(l));
return (void *)new;
}
/* allocate 'size' pages without expanding the heap.
* Return 0 if that's not possible
* must hold LOCK(NUMSMALL) before calling */
static void *alloc_pages(int size)
{
unsigned i;
void *p = fp_find(size);
if (p) {
unsigned pn = PAGENUM(p);
struct page *pg = NUM2PAGE(pn);
pg->code = BIG;
if (pg->page - pn > (int)size) {
unsigned extra = pn + size;
struct page *extrapg = NUM2PAGE(extra);
extrapg->code = BIG+FREE;
extrapg->page = pg->page;
FREEPAGE(extra)->size = i = pg->page - extra;
while (--i)
NUM2PAGE(extra + i)->page = extra;
fp_add(FREEPAGE(extra)); } }
return p;
}
/* Free a block of one or more pages, without shrinking the heap.
* Coalesce with adjacent free block and return the resulting (possibly
* larger) free block
* Must hold LOCK(NUMSMALL) before calling. */
static struct freepage *free_pages(void *p)
{
unsigned i, adj;
struct page *pg = ADDR2PAGE(p), *adjpg;
struct freepage *fpage = p;
assert(pg->code == BIG);
pg->code = BIG+FREE;
adj = PAGENUM(p)-1;
adjpg = NUM2PAGE(adj);
if (adjpg->code == MIDDLE) {
adj = adjpg->page;