/home/travis/build/MoarVM/MoarVM/src/core/fixedsizealloc.h
Line | Count | Source |
1 | | /* The top-level data structure for the fixed size allocator. */ |
2 | | struct MVMFixedSizeAlloc { |
3 | | /* Size classes for the fixed size allocator. Each one represents a bunch |
4 | | * of objects of the same size. The allocated sizes are rounded and then |
5 | | * one of these buckets is used (more size classes are allocated if a |
6 | | * need arises). */ |
7 | | MVMFixedSizeAllocSizeClass *size_classes; |
8 | | |
9 | | /* Spin lock used for reading from the free list, to avoid ABA. */ |
10 | | AO_t freelist_spin; |
11 | | |
12 | | /* Mutex for when we can't do a cheap/simple allocation. */ |
13 | | uv_mutex_t complex_alloc_mutex; |
14 | | |
15 | | /* Head of the "free at next safepoint" list of overflows (that is, |
16 | | * items that don't fit in a fixed size allocator bin). */ |
17 | | MVMFixedSizeAllocSafepointFreeListEntry *free_at_next_safepoint_overflows; |
18 | | }; |
19 | | |
20 | | /* Free list entry. Must be no bigger than the smallest size class. */ |
21 | | struct MVMFixedSizeAllocFreeListEntry { |
22 | | void *next; |
23 | | }; |
24 | | |
25 | | /* Entry in the "free at next safe point" linked list. */ |
26 | | struct MVMFixedSizeAllocSafepointFreeListEntry { |
27 | | void *to_free; |
28 | | MVMFixedSizeAllocSafepointFreeListEntry *next; |
29 | | }; |
30 | | |
31 | | /* Pages of objects of a particular size class. */ |
32 | | struct MVMFixedSizeAllocSizeClass { |
33 | | /* Each page holds allocated chunks of memory. */ |
34 | | char **pages; |
35 | | |
36 | | /* Head of the free list. */ |
37 | | MVMFixedSizeAllocFreeListEntry *free_list; |
38 | | |
39 | | /* The current allocation position if we've nothing on the |
40 | | * free list. */ |
41 | | char *alloc_pos; |
42 | | |
43 | | /* The current page allocation limit (once we hit this, we need |
44 | | * to go to the next page) Also just used when no free list. */ |
45 | | char *alloc_limit; |
46 | | |
47 | | /* The current page number that we're allocating in. */ |
48 | | MVMuint32 cur_page; |
49 | | |
50 | | /* The number of pages allocated. */ |
51 | | MVMuint32 num_pages; |
52 | | |
53 | | /* Head of the "free at next safepoint" list. */ |
54 | | MVMFixedSizeAllocSafepointFreeListEntry *free_at_next_safepoint_list; |
55 | | }; |
56 | | |
57 | | /* The number of bits we discard from the requested size when binning |
58 | | * the allocation request into a size class. For example, if this is |
59 | | * 3 bits then: |
60 | | * Request for 2 bytes ==> bin 0 (objects 0 - 8 bytes) |
61 | | * Request for 4 bytes ==> bin 0 (objects 0 - 8 bytes) |
62 | | * Request for 8 bytes ==> bin 0 (objects 0 - 8 bytes) |
63 | | * Request for 12 bytes ==> bin 1 (objects 9 - 16 bytes) |
64 | | * Request for 16 bytes ==> bin 1 (objects 9 - 16 bytes) |
65 | | */ |
66 | 62.8M | #define MVM_FSA_BIN_BITS 3 |
67 | | |
68 | | /* Mask used to know if we hit a size class exactly or have to round up. */ |
69 | 30.5M | #define MVM_FSA_BIN_MASK ((1 << MVM_FSA_BIN_BITS) - 1) |
70 | | |
71 | | /* Number of bins in the FSA. Beyond this, we just degrade to malloc/free. */ |
72 | 30.6M | #define MVM_FSA_BINS 96 |
73 | | |
74 | | /* The number of items that go into each page. */ |
75 | 45.0k | #define MVM_FSA_PAGE_ITEMS 128 |
76 | | |
77 | | /* Functions. */ |
78 | | MVMFixedSizeAlloc * MVM_fixed_size_create(MVMThreadContext *tc); |
79 | | void * MVM_fixed_size_alloc(MVMThreadContext *tc, MVMFixedSizeAlloc *fsa, size_t bytes); |
80 | | void * MVM_fixed_size_alloc_zeroed(MVMThreadContext *tc, MVMFixedSizeAlloc *fsa, size_t bytes); |
81 | | void MVM_fixed_size_destroy(MVMFixedSizeAlloc *al); |
82 | | void MVM_fixed_size_free(MVMThreadContext *tc, MVMFixedSizeAlloc *fsa, size_t bytes, void *free); |
83 | | void MVM_fixed_size_free_at_safepoint(MVMThreadContext *tc, MVMFixedSizeAlloc *fsa, size_t bytes, void *free); |
84 | | void MVM_fixed_size_safepoint(MVMThreadContext *tc, MVMFixedSizeAlloc *al); |