summaryrefslogtreecommitdiff
path: root/muse2/muse/memory.h
blob: 9e9969656f92bc438ba0f98187a22afeb405475c (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//=========================================================
//  MusE
//  Linux Music Editor
//  $Id: memory.h,v 1.4.2.3 2009/12/15 22:08:50 spamatica Exp $
//
//  (C) Copyright 2003-2004 Werner Schweer (ws@seh.de)
//=========================================================

#ifndef __MEMORY_H__
#define __MEMORY_H__

#include <stdio.h>
#include <stdlib.h>
#include <cstddef>
#include <map>

// most of the following code is based on examples
// from Bjarne Stroustrup: "Die C++ Programmiersprache"

//---------------------------------------------------------
//   Pool
//---------------------------------------------------------

class Pool {
      struct Verweis {
            Verweis* next;
            };
      struct Chunk {
            enum { size = 4 * 1024 };
            Chunk* next;
            char mem[size];
            };
      enum { dimension = 21 };
      Chunk* chunks[dimension];
      Verweis* head[dimension];
      Pool(Pool&);
      void operator=(Pool&);
      void grow(int idx);

   public:
      Pool();
      ~Pool();
      void* alloc(size_t n);
      void free(void* b, size_t n);
      };

//---------------------------------------------------------
//   alloc
//---------------------------------------------------------

inline void* Pool::alloc(size_t n)
      {
      if (n == 0)
            return 0;
      int idx = ((n + sizeof(unsigned long) - 1) / sizeof(unsigned long)) - 1;
      if (idx >= dimension) {
            printf("panic: alloc %zd %d %d\n", n, idx, dimension);
            exit(-1);
            }
      if (head[idx] == 0)
            grow(idx);
      Verweis* p = head[idx];
      head[idx] = p->next;
      return p;
      }

//---------------------------------------------------------
//   free
//---------------------------------------------------------

inline void Pool::free(void* b, size_t n)
      {
      if (b == 0 || n == 0)
            return;
      int idx = ((n + sizeof(unsigned long) - 1) / sizeof(unsigned long)) - 1;
      if (idx >= dimension) {
            printf("panic: free %zd %d %d\n", n, idx, dimension);
            exit(-1);
            }
      Verweis* p = static_cast<Verweis*>(b);
      p->next = head[idx];
      head[idx] = p;
      }

extern Pool audioRTmemoryPool;
extern Pool midiRTmemoryPool;

//---------------------------------------------------------
//   audioRTalloc
//---------------------------------------------------------

template <class T> class audioRTalloc
      {
   public:
      typedef T         value_type;
      typedef size_t    size_type;
      typedef ptrdiff_t difference_type;

      typedef T*        pointer;
      typedef const T*  const_pointer;

      typedef T&        reference;
      typedef const T&  const_reference;

      pointer address(reference x) const { return &x; }
      const_pointer address(const_reference x) const { return &x; }

      audioRTalloc();
      template <class U> audioRTalloc(const audioRTalloc<U>&) {}
      ~audioRTalloc() {}

      pointer allocate(size_type n, void * = 0) {
            return static_cast<T*>(audioRTmemoryPool.alloc(n * sizeof(T)));
            }
      void deallocate(pointer p, size_type n) {
            audioRTmemoryPool.free(p, n * sizeof(T));
            }

      audioRTalloc<T>&  operator=(const audioRTalloc&) { return *this; }
      void construct(pointer p, const T& val) {
            new ((T*) p) T(val);
            }
      void destroy(pointer p) {
            p->~T();
            }
      size_type max_size() const { return size_t(-1); }

      template <class U> struct rebind { typedef audioRTalloc<U> other; };
      template <class U> audioRTalloc& operator=(const audioRTalloc<U>&) { return *this; }
      };

template <class T> audioRTalloc<T>::audioRTalloc() {}

//---------------------------------------------------------
//   midiRTalloc
//---------------------------------------------------------

template <class T> class midiRTalloc
      {
   public:
      typedef T         value_type;
      typedef size_t    size_type;
      typedef ptrdiff_t difference_type;

      typedef T*        pointer;
      typedef const T*  const_pointer;

      typedef T&        reference;
      typedef const T&  const_reference;

      pointer address(reference x) const { return &x; }
      const_pointer address(const_reference x) const { return &x; }

      midiRTalloc();
      template <class U> midiRTalloc(const midiRTalloc<U>&) {}
      ~midiRTalloc() {}

      pointer allocate(size_type n, void * = 0) {
            return static_cast<T*>(midiRTmemoryPool.alloc(n * sizeof(T)));
            }
      void deallocate(pointer p, size_type n) {
            midiRTmemoryPool.free(p, n * sizeof(T));
            }

      midiRTalloc<T>&  operator=(const midiRTalloc&) { return *this; }
      void construct(pointer p, const T& val) {
            new ((T*) p) T(val);
            }
      void destroy(pointer p) {
            p->~T();
            }
      size_type max_size() const { return size_t(-1); }

      template <class U> struct rebind { typedef midiRTalloc<U> other; };
      template <class U> midiRTalloc& operator=(const midiRTalloc<U>&) { return *this; }
      };

template <class T> midiRTalloc<T>::midiRTalloc() {}

#endif