aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/hybrid/allocator/arena.hxx
blob: b8be9981916c8be9ff36e91ef582359082ba7c76 (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
// file      : examples/cxx/hybrid/allocator/arena.hxx
// copyright : not copyrighted - public domain

#ifndef ARENA_HXX
#define ARENA_HXX

#include <stddef.h> // size_t

// Sample pooled memory arena. The primary goal here is to provide a
// simple, if naive, implementation. As a result, it probably shouldn't
// be used in production.
//
class arena
{
public:
  arena (void* memory, size_t size);

  void*
  allocate (size_t);

  void*
  reallocate (void*, size_t);

  void
  free (void*);

  void
  print_statistics ();

private:
  struct block
  {
    size_t cap;  // block capacity
    size_t size; // allocated size
    block* next; // next block
    size_t used; // used flag
  };

  block* head_; // Linked list of blocks.

  // Statistics.
  //
  size_t alloc_count_;
  size_t realloc_count_;
  size_t free_count_;

  size_t cur_allocated_;
  size_t max_allocated_;
};

#endif // ARENA_HXX