summaryrefslogtreecommitdiffstats
path: root/vespamalloc/src/vespamalloc/malloc/allocchunk.h
blob: 8b6a2ce3fc867f67d57051b231292b259a46dbd1 (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "common.h"
#include <algorithm>

namespace vespamalloc {

/**
 * @brief Pointer and tag - use instead of bare pointer for cmpSwap()
 *
 * When making a lock-free data structure by using cmpSwap
 * on pointers, you'll often run into the "ABA problem", see
 * http://en.wikipedia.org/wiki/ABA_problem for details.
 * The TaggedPtr makes it easy to do the woraround with tag bits,
 * but requires the double-word compare-and-swap instruction.
 * Very early Amd K7/8 CPUs are lacking this and will fail (Illegal Instruction).
 **/
struct TaggedPtr {
    TaggedPtr() noexcept : _ptr(nullptr), _tag(0) { }
    TaggedPtr(void *h, size_t t) noexcept : _ptr(h), _tag(t) {}

    void *_ptr;
    size_t _tag;
};

class AFListBase
{
public:
    using HeadPtr = TaggedPtr;
    using AtomicHeadPtr = std::atomic<HeadPtr>;
    AFListBase() : _next(NULL) { }
    void setNext(AFListBase * csl)           { _next = csl; }
    static void init();
    static void linkInList(AtomicHeadPtr & head, AFListBase * list);
    static void linkIn(AtomicHeadPtr & head, AFListBase * csl, AFListBase * tail);
protected:
    AFListBase * getNext()                      { return _next; }
    static AFListBase * linkOut(AtomicHeadPtr & head);
private:
    AFListBase       *_next;
};

template <typename MemBlockPtrT>
class AFList : public AFListBase
{
public:
    typedef size_t CountT;
    enum { NumBlocks = 126 };
    AFList() : _count(0) { }
    CountT count()              const { return _count; }
    void add(MemBlockPtrT & ptr)      {
        ptr.free();
        PARANOID_CHECK2( if (full()) { *(int*)0=0; });
        _memBlockList[_count++] = ptr;
    }
    void sub(MemBlockPtrT & mem)      {
        if (empty()) {
            return;
        }
        mem = _memBlockList[--_count];
    }
    bool empty()                const { return (_count == 0); }
    bool full()                 const { return (_count == NumBlocks); }
    size_t fill(void * mem, SizeClassT sc, size_t blocksPerChunk = NumBlocks);
    AFList * getNext()                { return static_cast<AFList *>(AFListBase::getNext()); }
    static AFList * linkOut(AtomicHeadPtr & head) {
        return static_cast<AFList *>(AFListBase::linkOut(head));
    }
private:
    CountT        _count;
    MemBlockPtrT _memBlockList[NumBlocks];
};


template <typename MemBlockPtrT>
size_t AFList<MemBlockPtrT>::fill(void * mem, SizeClassT sc, size_t blocksPerChunk)
{
    size_t sz = MemBlockPtrT::classSize(sc);
    int retval(std::max(0, int(blocksPerChunk-_count)));
    char * first = (char *) mem;
    for(int i=0; i < retval; i++) {
        _memBlockList[_count] = MemBlockPtrT(first + i*sz, MemBlockPtrT::unAdjustSize(sz));
        _memBlockList[_count].free();
        _count++;
    }
    return retval;
}

}