aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/hybrid/allocator/arena.cxx
blob: facb8529f911e1f28f04a76cfd9a0da139204456 (plain)
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
// file      : examples/cxx/hybrid/allocator/arena.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : not copyrighted - public domain

#include <stdio.h>
#include <string.h> // memcpy

#include "arena.hxx"

arena::
arena (void* memory, size_t size)
    : alloc_count_ (0),
      realloc_count_ (0),
      free_count_ (0),
      cur_allocated_ (0),
      max_allocated_ (0)
{
  head_ = static_cast<block*> (memory);
  head_->cap  = size - sizeof (block);
  head_->next = 0;
  head_->used = false;
}

void* arena::
allocate (size_t size)
{
  block* b = head_;

  while (b != 0)
  {
    if (!b->used)
    {
      if (b->cap >= size)
      {
        // See if it makes sense to fragment this block.
        //
        if (b->cap - size >= sizeof (block) * 2)
        {
          block* b1 = reinterpret_cast<block*> (
            reinterpret_cast<char*> (b) + sizeof (block) + size);

          b1->cap = b->cap - size - sizeof (block);
          b1->next = b->next;
          b1->used = false;

          b->next = b1;
          b->cap = size;
        }

        b->used = true;
        b->size = size;

        alloc_count_++;
        cur_allocated_ += size;

        if (cur_allocated_ > max_allocated_)
          max_allocated_ = cur_allocated_;

        return b + 1;
      }

      // This block is not big enough. See if we can merge it
      // with the next block.
      //
      if (b->next != 0 && !b->next->used)
      {
        block* b1 = b->next;
        b->cap += b1->cap + sizeof (block);
        b->next = b1->next;

        // Try the merged block again.
        //
        continue;
      }
    }

    // This block is either in use or not big enough. Continue
    // searching.
    //
    b = b->next;
  }

  return 0;
}

void* arena::
reallocate (void* p, size_t size)
{
  // If the passed pointer is NULL, reallocate is equivalent to
  // allocate.
  //
  if (!p)
    return allocate (size);

  block* b = static_cast<block*> (p) - 1;

  // If the passed size is NULL, reallocate is equivalent to free.
  //
  if (size == 0)
  {
    free (p);
    return 0;
  }

  // If this block is not large enough to satisfy the request, try to
  // merge it with the next block(s).
  //
  while (b->cap < size && b->next != 0 && !b->next->used)
  {
    block* b1 = b->next;
    b->cap += b1->cap + sizeof (block);
    b->next = b1->next;
  }

  // If this block is now large enough then we can reuse the same
  // memory region.
  //
  if (b->cap >= size)
  {
    realloc_count_++;
    cur_allocated_ += size - b->size;

    if (cur_allocated_ > max_allocated_)
      max_allocated_ = cur_allocated_;

    b->size = size;
    return p;
  }

  // Otherwise allocate a new block and copy the data over.
  //
  void* r = allocate (size);

  if (r)
  {
    memcpy (r, p, b->size);
    free (p);
  }

  return r;
}

void arena::
free (void* p)
{
  if (p)
  {
    block* b = static_cast<block*> (p) - 1;

    cur_allocated_ -= b->size;
    free_count_++;

    b->used = false;
  }
}

void arena::
print_statistics ()
{
  printf ("\n");
  printf ("allocations:      %lu\n", alloc_count_);
  printf ("reallocations:    %lu\n", realloc_count_);
  printf ("deallocations:    %lu\n", free_count_);
  printf ("currently in use: %lu bytes\n", cur_allocated_);
  printf ("maximum in use:   %lu bytes\n", max_allocated_);
  printf ("\n");
}