-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcf.c
2212 lines (1865 loc) · 54.6 KB
/
cf.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
/*
* Copyright (c) Hannes Payer [email protected]
* cs.uni-salzburg.at/~hpayer
*
* University Salzburg, www.uni-salzburg.at
* Department of Computer Science, cs.uni-salzburg.at
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "cf.h"
#include "arch_dep.h"
#include "page_stack.h"
#include "aa_stack.h"
#include "aa_bucket_stack.h"
#include "list.h"
#include "benchmarks/bench.h" //include for statistics TODO cleanup
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <assert.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <stdint.h>
/*
TODO: CLEANUP
TODO: implement page locking scheme
TODO: make threads dynamic
TODO: make bitmap operations non-blocking
TODO: make size-classes dynamic
TODO: need free a buckets initialization?
TODO: make partial compaction bound dynamic for each thread
TODO: integrate nice with benchmark
*/
////////////////////////////////////////////////////////////////////////////////////////
// Macros/Constants
////////////////////////////////////////////////////////////////////////////////////////
/**
* page size
*/
#define PAGESIZE 16384
/**
* pages are 16k, low 14 bits are 0, and high 18 bits contain address
*/
#define PAGE_SHIFT (14)
#define PAGE_MASK ((1<<PAGE_SHIFT) - 1)
/**
* 160B data of page header struct + 24B lock
*/
#define PAGEHEADER 184
/**
* memory of a page used for objects (heap memory)
*/
#define PAGEDATASIZE (PAGESIZE - PAGEHEADER)
/**
* size of an abstract address
*/
#define ABSTRACTADDRESSSIZE sizeof(void **)
/**
* largest size-class
*/
#define LARGESTSC 8000
/**
* compaction abort flag
* another compaction operation moved the target block
*/
#define ABORT_COMPACT_FLAG 0x1
/**
* free abort flag
* another free operation freed the source block
*/
#define ABORT_FREE_FLAG 0x2
/**
* pages needed for thread local data structures
*/
#define THREAD_LOCAL_PAGES 2
/**
* page size of machine
*/
#define MACHINE_PAGESIZE 4096
/**
* compare exchange
*/
#define CAS cmpxchg
/**
* list flags for debugging
*/
#define NFULL_LIST_FLAG 1
#define FULL_LIST_FLAG 2
#define SOURCE_LIST_FLAG 3
#define EMPTYING_LIST_FLAG 4
#define FREE_LIST_FLAG 5
////////////////////////////////////////////////////////////////////////////////////////
// Structures
////////////////////////////////////////////////////////////////////////////////////////
/**
* a page
*/
struct page {
char memory[PAGEDATASIZE]; /**< memory where objects are stored */
struct list_head list;
uint32_t nr_used_page_blocks; /**< number of used page blocks of the page*/
uint32_t list_flags; /**< determines state of page*/
uint32_t sourceentries; /**< number of source entries of the page (if page is a source page)*/
struct size_class *sc; /**< reference to size-class*/
uint32_t used_page_block_bitmap[34]; /**< 2-dimensional bitmap to find used/free page blocks*/
pthread_mutex_t lock; /**< page lock*/
};
/**
* a size-class
*/
struct size_class {
struct list_head head_full_pages; /**< head of full pages of size-class*/
struct list_head head_nfull_pages; /**< head of not-full pages of size-class*/
struct list_head head_source_pages; /**< head of source pages of size-class*/
struct list_head head_emptying_pages; /**< head of emptying pages of size-class*/
uint32_t nfullentries; /**< number of not-full pages of size-class*/
uint32_t max_nfullentries; /**< maximum reached number of not-full pages of size-class (stats)*/
uint32_t sourceentries; /**< number of source pages of size-class*/
uint32_t max_sourceentries; /**< maximum reached number of source pages of size-class (stats)*/
int32_t partial_compaction_bound; /**< partial compaction bound*/
int workers; /**< threads working on size-class (stats)*/
int block_size; /**< page-block size*/
int nr_pages; /**< number of pages (stats)*/
int max_nr_pages; /**< maximum reached number of pages (stats)*/
#if defined(LOCK_CLASS) || defined(LOCK_PAGE)
pthread_mutex_t lock; /**< size-class lock*/
#endif
} __attribute__((aligned(128))); /* IMPORTANT: avoids cache invalidations*/
/**
* private thread data
*/
struct thread_data {
int id; /**< thread id*/
int pages_count; /**< number of local pages*/
struct mem_page *pages; /**< local pages*/
int pbuckets_count; /**< number of local page buckets*/
struct mem_page *pbuckets; /**< page buckets list*/
struct mem_page *last_pbucket; /**< last bucket of page buckets list*/
int aas_count; /**< number of local abstract addresses*/
long aas; /**< local abstract addresses */
int abuckets_count; /**< number of local abstract address buckets*/
struct mem_aa *abuckets; /**< local abstract addresses buckets*/
struct mem_aa *last_abucket; /**< last bucket of abstract address buckets list*/
int free_abuckets_count; /**< number of free abstract address buckets*/
struct mem_aa_bucket *free_abuckets; /**< free abstract address buckets*/
struct size_class *size_classes; /**< private size-classes*/
};
static struct thread_data *get_thread_data();
////////////////////////////////////////////////////////////////////////////////////////
// Globals
////////////////////////////////////////////////////////////////////////////////////////
#ifdef GLOBAL_STATS
static long nr_sources = 0;
static long nr_nfull = 0;
static long used_pages = 0;
static long max_sources = 0;
static long max_nfull = 0;
static long max_used_pages = 0;
#endif
/**
* number of pages
*/
static unsigned long nr_pages = 0;
/**
* head of free pages list
*/
static nb_stack free_page_head;
/**
* head of free abstract address list
*/
static nb_stack free_aa_head;
/**
* head of fre abstract address buckets list
*/
static nb_stack free_aa_buckets_head;
/**
* pages memory
*/
static struct page *pages = NULL;
/**
* number of per thread local free abstract address buckets
*/
static int nr_local_free_abuckets = 1;
/**
* number of per thread local page buckets
*/
static int nr_local_pbuckets = 1;
/**
* number of per thread local abstract address buckets
*/
static int nr_local_abuckets = 1;
/**
* number of per thread local pages per bucket
*/
static int nr_local_pages = 1;
/**
* number of per thread local abstract addresses per bucket
*/
static int nr_local_aas = 2;
/**
* abstract address space size
*/
static unsigned long nr_aas;
/**
* number of abstract address buckets
*/
static unsigned long nr_aa_buckets;
/**
* memcopy multiplicator >=1 (used for tests only)
*
*/
static int memcpy_multiplicator = 1;
/**
* memcopy increment, 0 = not in use
*/
static int incremental_memcpy = 0;
/**
* private size-classes in use (boolean)
*/
static int private_classes = 0;
/**
* abstract address space memory
*/
static unsigned long *aa_space;
/**
* abstract address buckets
*/
static struct mem_aa *aa_buckets;
/**
* heap size (concrete address space)
*/
static unsigned long heap_size;
/**
* thread id counter (atomically incremented)
*/
static int next_thread_id = -1;
/**
* partial compaction bound
*/
static int partial_compaction_bound;
/**
* maps an allocation request to a size-class
*/
static uint16_t size_2_size_class_map[2000];
/**
* maps a size-class index to the max page-block size of the given size-class
*/
static uint16_t size_class_2_size_map[SIZECLASSES];
/**
* thread data pthread specific key
*/
static pthread_key_t cf_key;
/**
* thread global size-classes memory
*/
static char global_classes[THREAD_LOCAL_PAGES*MACHINE_PAGESIZE];
/*
* benchmark data TODO:move to benchmark
*/
static int pages_count = 0;
static int max_pages_count = 0;
////////////////////////////////////////////////////////////////////////////////////////
// Locks
////////////////////////////////////////////////////////////////////////////////////////
#if LOCK_GLOBAL
static pthread_mutex_t global_alloc_lock = PTHREAD_MUTEX_INITIALIZER;
static inline void lock()
{
pthread_mutex_lock(&global_alloc_lock);
}
static inline void unlock()
{
pthread_mutex_unlock(&global_alloc_lock);
}
static pthread_mutex_t page_list_lock = PTHREAD_MUTEX_INITIALIZER;
static inline void lock_free_page()
{
pthread_mutex_lock(&page_list_lock);
}
static inline void unlock_free_page()
{
pthread_mutex_unlock(&page_list_lock);
}
static pthread_mutex_t address_list_lock = PTHREAD_MUTEX_INITIALIZER;
static inline void lock_free_address()
{
pthread_mutex_lock(&address_list_lock);
}
static inline void unlock_free_address()
{
pthread_mutex_unlock(&address_list_lock);
}
static inline void init_page_lock(struct page *page) { }
static inline void lock_page(struct page *page) { }
static inline void unlock_page(struct page *page) { }
static inline void init_sizeClass_lock(struct size_class *sc) { }
static inline void lock_sizeClass(struct size_class *sc) { }
static inline void unlock_sizeClass(struct size_class *sc) { }
#elif defined(LOCK_CLASS) || defined(LOCK_PAGE)
static inline void lock() { }
static inline void unlock() { }
static pthread_mutex_t page_list_lock = PTHREAD_MUTEX_INITIALIZER;
static inline void lock_free_page()
{
pthread_mutex_lock(&page_list_lock);
}
static inline void unlock_free_page()
{
pthread_mutex_unlock(&page_list_lock);
}
static pthread_mutex_t address_list_lock = PTHREAD_MUTEX_INITIALIZER;
static inline void lock_free_address()
{
pthread_mutex_lock(&address_list_lock);
}
static inline void unlock_free_address()
{
pthread_mutex_unlock(&address_list_lock);
}
#ifdef LOCK_PAGE
static inline void init_page_lock(struct page *page)
{
pthread_mutex_init(&page->lock, NULL);
}
static inline void lock_page(struct page *page)
{
struct thread_data *data = get_thread_data();
uint64_t end;
uint64_t start;
int i = page->sc - data->size_classes;
start = get_utime();
pthread_mutex_lock(&page->lock);
end = get_utime();
#if USE_STATS
struct bench_stats *stats = get_bench_stats();
stats->lock_time[i] += end - start;
if(stats->lock_max_time[i] < end - start)
stats->lock_max_time[i] = end -start;
stats->num_lock_class[i]++;
if(stats->num_lock_class[i] == 5)
stats->lock_max_time[i] = 0;
#endif
}
static inline void unlock_page(struct page *page)
{
pthread_mutex_unlock(&page->lock);
}
#else
static inline void init_page_lock(struct page *page) { }
static inline void lock_page(struct page *page) { }
static inline void unlock_page(struct page *page) { }
#endif
static inline void init_sizeClass_lock(struct size_class *sc)
{
pthread_mutex_init(&sc->lock, NULL);
}
static inline void lock_sizeClass(struct size_class *sc)
{
if(incremental_memcpy)
atomic_inc(&sc->workers);
// struct bench_stats *stats = get_bench_stats();TODO:check this
// struct thread_data *data = get_thread_data();
// uint64_t end;
// uint64_t start;
// int i = sc - data->size_classes;
// start = get_utime();
pthread_mutex_lock(&sc->lock);
#if 0
end = get_utime();
stats->lock_time[i] += end - start;
if(stats->lock_max_time[i] < end - start)
stats->lock_max_time[i] = end -start;
stats->num_lock_class[i]++;
#endif
}
static inline void unlock_sizeClass(struct size_class *sc)
{
pthread_mutex_unlock(&sc->lock);
if(incremental_memcpy)
atomic_dec(&sc->workers);
}
#elif LOCK_NONE
/* all noops */
static inline void lock() { }
static inline void unlock() { }
static inline void lock_free_page() { }
static inline void unlock_free_page() { }
static inline void lock_free_address() { }
static inline void unlock_free_address() { }
static inline void init_page_lock(struct page *page) { }
static inline void lock_page(struct page *page) { }
static inline void unlock_page(struct page *page) { }
static inline void init_sizeClass_lock(struct size_class *sc) { }
static inline void lock_sizeClass(struct size_class *sc) { }
static inline void unlock_sizeClass(struct size_class *sc) { }
#else
#error unknown locking scheme
#endif
/////////////////////////////////////////////////////////////////////
// Abstract Address Space
/////////////////////////////////////////////////////////////////////
/**
* give free abstract address back
* @parm id abstract address
*/
static void put_free_aa(long id)
{
struct thread_data *data;
struct mem_aa *maa;
data = get_thread_data();
/*full abstract addresses bucket*/
if (data->aas_count == nr_local_aas){
/*no free abstract address buckets*/
if(!data->free_abuckets_count){
data->free_abuckets = aa_bucket_get(&free_aa_buckets_head);
assert(data->free_abuckets != NULL);
if(data->free_abuckets){
data->free_abuckets_count = nr_local_free_abuckets;
}
}
/*take free abstract addresses bucket*/
maa = (struct mem_aa *)data->free_abuckets;
data->free_abuckets = data->free_abuckets->local_aa_bucket;
data->free_abuckets_count--;
/*install the free bucket and initialize new bucket*/
maa->next = data->abuckets;
maa->local_aas = data->aas;
data->abuckets = maa;
data->abuckets_count++;
data->aas_count = 0;
/*initialize last pointer*/
if(data->abuckets_count == 1){
data->last_abucket = data->abuckets;
}
/*return buckets to the global free list*/
if(data->abuckets_count >= nr_local_abuckets){
aa_put(data->abuckets, data->last_abucket, &free_aa_head);
data->abuckets_count = 0;
data->abuckets = NULL;
data->last_abucket = NULL;
}
}
/*skip first entry*/
if(data->aas_count){
aa_space[id] = data->aas;
}
/*create list links*/
data->aas = id;
data->aas_count++;
}
/**
* get free abstract address
* @return abstract address id
*/
static long get_free_aa()
{
struct thread_data *data;
struct mem_aa *maa;
unsigned long next = 0;
unsigned long new = 0;
data = get_thread_data();
/*there are no abstract addresses*/
if (data->aas_count == 0) {
/*there are no abstract address buckets*/
if(data->abuckets_count == 0){
maa = aa_get(&free_aa_head);
if(!maa){
return -1;
}
}
else{
maa = data->abuckets;
data->abuckets = maa->next;
data->abuckets_count--;
}
/*install new bucket data*/
data->aas = maa->local_aas;
data->aas_count = nr_local_aas;
/*give some of the free buckets back if we have to much*/
if(data->free_abuckets_count >= nr_local_free_abuckets){
aa_bucket_put(data->free_abuckets, data->free_abuckets, &free_aa_buckets_head);
data->free_abuckets = NULL;
data->free_abuckets_count = 0;
}
/*add the free bucket to the private free buckets list*/
((struct mem_aa_bucket *)maa)->local_aa_bucket = data->free_abuckets;
data->free_abuckets = (struct mem_aa_bucket *)maa;
data->free_abuckets_count++;
}
next = data->aas;
new = aa_space[next];
data->aas = new;
data->aas_count--;
return next;
}
/**
* initialize abstract address space
*/
static void init_free_aa_list(){
int i;
free_aa_head = NB_STACK_INITIALIZER;
free_aa_buckets_head = NB_STACK_INITIALIZER;
for(i=1; i<=nr_aas; i++){
if(i%nr_local_aas){
aa_space[i] = i-1;
}
else{
aa_buckets[(i-1)/nr_local_aas].local_aas = i-1;
aa_put(&aa_buckets[(i-1)/nr_local_aas], &aa_buckets[(i-1)/nr_local_aas], &free_aa_head);
}
}
}
/**
* get an abstract address
* @param page_block page-block pointer
* @return assigned abstract address
*/
static inline void **get_abstract_address(void *page_block){
long free_id;
void **retval = NULL;
free_id = get_free_aa();
//printf("free id %d\n", free_id);
if(free_id>=0 && free_id<nr_aas) {
aa_space[free_id] = (long)page_block;
retval = (void **)&aa_space[free_id];
}
return retval;
}
/**
* clear abstract address
* @param aa abstract address
*/
static void clear_abstract_address(void *aa){
long id;
id = ((unsigned long *)aa) - aa_space;
//printf("clear id %ld\n", id);
put_free_aa(id);
}
/**
* print the abstract address space
*/
void print_aa_space(){
printf("\nAbstract Address Space:\n");
uint32_t i;
for(i=0; i<nr_aas; i++){
printf("%d: %p ", i, (void *)aa_space[i]);
}
printf("\n\n");
}
/////////////////////////////////////////////////////////////////////
// Pages
/////////////////////////////////////////////////////////////////////
/**
* init free pages
*/
static void init_free_pages_list(){
size_t i;
int j;
struct page *p = pages;
struct mem_page *mp;
free_page_head = NB_STACK_INITIALIZER;
/*add pages to free list*/
j = 0;
mp = NULL;
for(i=0; i<nr_pages; i++){
p = pages + i;
p->list_flags = FREE_LIST_FLAG;
init_page_lock(p);
if (j < nr_local_pages) {
((struct mem_page *)p)->local_pages = mp;
mp = (struct mem_page *)p;
++j;
}
if(j == nr_local_pages){
page_put(mp, mp, &free_page_head);
j=0;
mp = NULL;
}
}
//printf("pages btw %p and %p\n", pages, p - 1);
//page_print_head(&free_page_head);
}
/**
* returns a page from the page free-list
* @return free page
*/
static inline struct page *get_free_page(){
struct page *p;
struct mem_page *mp;
struct thread_data *data;
data = get_thread_data();
if (data->pages_count == 0) {
if(data->pbuckets_count == 0){
mp = page_get(&free_page_head);
if (mp) {
data->pages_count = nr_local_pages;
data->pages = mp;
} else {
return NULL;
}
}
else{
data->pages = data->pbuckets;
data->pages_count = nr_local_pages;
data->pbuckets = data->pbuckets->next;
data->pbuckets_count--;
}
}
p = (struct page *)data->pages;
data->pages = data->pages->local_pages;
data->pages_count--;
if (p) {
assert(p->list_flags == FREE_LIST_FLAG);
p->list_flags = 0;
#ifdef GLOBAL_STATS
used_pages++;
if (used_pages > max_used_pages)
max_used_pages = used_pages;
#endif
#ifdef USE_STATS
struct bench_stats *stats;
stats = get_bench_stats();
stats->used_pages++;
if (stats->used_pages > stats->max_used_pages)
stats->max_used_pages = stats->used_pages;
#endif
}
return p;
}
/**
* return free page to the list of free pages
* @param p free page
*/
static inline void add_free_page(struct page *p){
struct mem_page *mp;
struct thread_data *data;
assert(p!=NULL);
assert(p->list_flags == 0);
#ifdef USE_STATS
struct bench_stats *stats;
stats = get_bench_stats();
stats->used_pages--;
#endif
#ifdef GLOBAL_STATS
used_pages--;
#endif
data = get_thread_data();
if (data->pages_count == nr_local_pages) {
data->pages->next = data->pbuckets;
data->pbuckets = data->pages;
data->pbuckets_count++;
data->pages_count = 0;
data->pages = NULL;
if(data->pbuckets_count == 1){
data->last_pbucket = data->pbuckets;
}
if(data->pbuckets_count >= nr_local_pbuckets){
page_put(data->pbuckets, data->last_pbucket, &free_page_head);
data->pbuckets_count = 0;
data->pbuckets = NULL;
data->last_pbucket = NULL;
}
}
p->list_flags = FREE_LIST_FLAG;
mp = (struct mem_page *)p;
mp->local_pages = data->pages;
data->pages = mp;
data->pages_count++;
}
/**
* checks state of page; full or not full
* @param p page
* @param page_block_size size of page-block
* @return 1 if full, otherwise 0
*/
static int inline is_page_full(struct page *p, int page_block_size){
return (p->nr_used_page_blocks == PAGEDATASIZE/page_block_size);
}
/////////////////////////////////////////////////////////////////////
// Page-blocks
/////////////////////////////////////////////////////////////////////
static inline int get_index_direct_of_page_block(struct page *p, void *page_block, int size)
{
int diff;
diff = (char *)page_block - (char *)p;
return diff/size;
}
/**
* return the page of a page block
* @param page_block page block
* @return page of page block
*/
static inline struct page *get_page_direct_of_page_block(void *page_block){
return (struct page *)((long)page_block & ~PAGE_MASK);
}
/**
* return the size-class of a page
* @param p page
* @return size-class
*/
static inline struct size_class *get_size_class_of_page(struct page *p){
return p->sc;
}
/**
* return the page-block size of a size-class
* @param sc size-class
* @return page block size
*/
static inline int get_page_block_size_of_size_class(struct size_class *sc){
return sc->block_size;
}
/**
* find a used page block of a page
* @param p page
* @return page block index
*/
static inline int find_used_page_block(struct page *p)
{
int bitmap_index;
int bitmap_2D_index;
bitmap_index = _ffs(p->used_page_block_bitmap[0]);
bitmap_2D_index = _ffs(p->used_page_block_bitmap[bitmap_index+2]);
return bitmap_index*32 + bitmap_2D_index;
}
/**
* clear bit
* @param mem bitmap
* @param bitmap_index index in bitmap
* @param bitmap_2D_index index in 2nd dimension
*/
static inline void __clear_bit(uint32_t mem[], int bitmap_index, int bitmap_2D_index)
{
mem[bitmap_index+2] &= ((1 << bitmap_2D_index)^0xffffffff);
if(mem[bitmap_index+2] == 0){
mem[0] &= ((1 << bitmap_index)^0xffffffff);
}
mem[1] &= ((1 << bitmap_index)^0xffffffff);
}
/**
* set bit
* @param mem bitmap
* @param bitmap_index index in bitmap
* @param bitmap_2D_index index in 2nd dimension
*/
static inline void __set_bit(uint32_t mem[], int bitmap_index, int bitmap_2D_index)
{
mem[0] |= (1 << bitmap_index);
mem[bitmap_index+2] |= (1 << bitmap_2D_index);
if(mem[bitmap_index+2] == 0xffffffff){
mem[1] |= (1 << bitmap_index);
}
}
/**
* clear used bit
* @param p page
* @param index page-block index
*/
static inline void page_clear_used_bit(struct page *p, int index)
{
int bitmap_index;
int bitmap_2D_index;
bitmap_index = index/32;
bitmap_2D_index = index%32;
__clear_bit(p->used_page_block_bitmap, bitmap_index, bitmap_2D_index);
}
/**
* free used page-block bitmap entry
* @param p page
* @param index bitmap index
*/
static inline void free_used_page_block(struct page *p, int index){
lock_page(p);
page_clear_used_bit(p, index);
assert(p->nr_used_page_blocks > 0);
p->nr_used_page_blocks--;
unlock_page(p);
}
/**
* find free bitmap entry
* @param p page
* @return bitmap index
*/
static inline int get_free_page_block(struct page *p){
int bitmap_index;
int bitmap_2D_index;
lock_page(p);
bitmap_index = _ffs(p->used_page_block_bitmap[1]^0xffffffff);
bitmap_2D_index = _ffs(p->used_page_block_bitmap[bitmap_index+2]^0xffffffff);
__set_bit(p->used_page_block_bitmap, bitmap_index, bitmap_2D_index);
p->nr_used_page_blocks++;
unlock_page(p);
return bitmap_index*32 + bitmap_2D_index;
}
/////////////////////////////////////////////////////////////////////
// Size-classes
/////////////////////////////////////////////////////////////////////
/**
* TODO: CLEANUP
* init the size-class mappings
*/
static void init_size_classes_mapping(){
int i;
uint16_t base;
uint16_t res;
uint16_t tmp;
uint16_t class;
class = 0;
for(i=0; i<MINPAGEBLOCKSIZE/4; i++){
size_2_size_class_map[i] = class;
}
size_class_2_size_map[class] = MINPAGEBLOCKSIZE;