source: proiecte/swift/trunk/lib/hoard-371/src/heaplayers/experimental/multimalloc.h @ 176

Last change on this file since 176 was 176, checked in by (none), 14 years ago
  • imported repo from "guagal"
File size: 2.5 KB
Line 
1#ifndef _MULTIMALLOC_H_
2#define _MULTIMALLOC_H_
3
4#include <assert.h>
5
6#include <fstream.h>
7
8
9#include "dynarray.h"
10#include "stack.h"
11
12
13template <class SuperHeap>
14class MultiMalloc : public SuperHeap {
15public:
16
17
18        MultiMalloc (void) {
19                //f.open ("multimalloc.log");
20        }
21
22        ~MultiMalloc (void) {
23                //f.close ();
24        }
25
26        // Sets ptr to a list of up to num objects of size sz
27        // and returns how many free objects ptr points to.
28        int multimalloc (int num, size_t sz, void *& ptr)
29        {
30                //f << "multimalloc from " << this << endl << flush;
31                // printf ("multimalloc\n");
32                assert (num > 0);
33                if (stk.empty()) {
34#if 0
35                        //f << "malloc " << num << ", " << sz << "\n" << flush;
36                        ptr = SuperHeap::malloc (sz);
37                        assert (size(ptr) >= sz);
38                        assert (sz >= sizeof(FreeObject *));
39                        FreeObject * p = (FreeObject *) ptr;
40                        for (int i = 1; i < num; i++) {
41                                p->next = (FreeObject *) SuperHeap::malloc (sz);
42                                p = p->next;
43                                assert (size(p) >= sz);
44                        }
45                        p->next = NULL;
46#else
47                        size_t sz1 = align(sz + sizeof(double));
48                        ptr = SuperHeap::malloc (num * sz1); // Allow room for size & thread info.
49                        ptr = (double *) ptr + 1;
50                        FreeObject * p = (FreeObject *) ptr;
51                        for (int i = 0; i < num - 1; i++) {
52                                size(p) = sz;
53                                FreeObject * next = (FreeObject *) (((unsigned long) p) + sz1);
54                                p->next = next;
55                                p = p->next;
56                        }
57                        size(p) = sz;
58                        p->next = NULL;
59                        assert (size(p) + ((unsigned long) p) <= ((unsigned long) ptr + num * sz1));
60#endif
61
62#ifndef NDEBUG
63                        p = (FreeObject *) ptr;
64                        int c = 0;
65                        while (p != NULL) {
66                                c++;
67                                p = p->next;
68                        }
69                        assert (c == num);
70#endif
71                        return num;
72                } else {
73                        // Pop off some memory from the stack.
74                        assert (!stk.empty());
75                        np v = stk.pop();
76                        // JUST FOR CHECKING --
77                        assert (v.num == num);
78                        ptr = v.ptr;
79                        assert (v.num > 0);
80                        assert (size(ptr) >= sz);
81                        //f << "multimalloc " << v.num << ", " << sz << "\n" << flush;
82                        return v.num;
83                }
84        }
85
86        // Frees all num items pointed to by ptr.
87        void multifree (int num, void * ptr)
88        {
89                // printf ("multifree\n");
90                np v;
91                //f << "multifree " << num << ", size = " << size(ptr) << "\n" << flush;
92                v.num = num;
93                v.ptr = ptr;
94                stk.push (v);
95        }
96
97private:
98
99        MultiMalloc (const MultiMalloc&);
100        MultiMalloc& operator=(const MultiMalloc&);
101
102        class np {
103        public:
104                int num;
105                void * ptr;
106        };
107
108        class FreeObject {
109        public:
110                FreeObject * next;
111        };
112
113        void * malloc (size_t);
114        void free (void *);
115
116        Stack<np, DynArray<np> > stk;
117        //ofstream f;
118
119        static inline size_t align (size_t sz) {
120                return (sz + (sizeof(double) - 1)) & ~(sizeof(double) - 1);
121        }
122
123};
124
125
126#endif
Note: See TracBrowser for help on using the repository browser.