File: gmpallocator.cpp

package info (click to toggle)
gfan 0.3dfsg-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,016 kB
  • sloc: cpp: 17,728; makefile: 251
file content (170 lines) | stat: -rw-r--r-- 3,106 bytes parent folder | download | duplicates (2)
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gmp.h>
#include <assert.h>

#define NBUCKETSGMP 9

static int bufNum(int size)
{
  if(size<=8)return 0;
  int ret=0;
  while(size>8)
    {
      ret++;
      if(ret>=(NBUCKETSGMP))return -1;
      size>>1;
    }
  return ret;
}

class AllocBucket
{
  void *linkList;
  int size;
public:
  void init(int size_)
  {
    linkList=0;
    size=size_;
  }
  void grow()
  {
    int bufSize=1024*64;
    void *buf=malloc(bufSize);
    assert(buf);
    int n=bufSize/size;
    for(int i=n-1;i>=0;i--)
      {
	void **addr=(void**)((char*)buf+i*size);
	*addr=linkList;
	linkList=addr;
      }
    fprintf(stderr,"GROWING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  }
  void *alloc()
  {
    do
      {
	if(linkList)
	  {
	    void *ret=linkList;
	    linkList=*((void**)linkList);
	    return ret;
	  }
	grow();
      }
    while(1);
    return 0;
  }
  void free(void* ptr)
  {
    *((void **)ptr)=linkList;
    linkList=ptr;
  }
};

static AllocBucket theBucketList[NBUCKETSGMP];

void *myAllocateFunction(size_t alloc_size)
{
  //  fprintf(stderr,"Allocating: %i bytes.\n",alloc_size);

  int bn=bufNum(alloc_size);
  if(bn!=-1)
    {
      return theBucketList[bn].alloc();
    }
  return malloc(alloc_size);
}

void *myReallocateFunction (void *ptr, size_t old_size, size_t new_size)
{
  //  fprintf(stderr,"Reallocating: %i --> %i bytes.\n",old_size,new_size);
  int min=old_size;
  if(new_size<old_size)min=new_size;
  int bn_old=bufNum(old_size);
  int bn_new=bufNum(new_size);
  if(bn_old != -1)
    {
      if(bn_new!=-1)
	{
	  if(bn_new==bn_old)return ptr;
	  else
	    {
	      void *ret=theBucketList[bn_new].alloc();
	      memcpy(ret,ptr,min);
	      theBucketList[bn_old].free(ptr);
	    }
	}
      else
	{
	  void *ret=malloc(new_size);
	  memcpy(ret,ptr,min);
	  theBucketList[bn_old].free(ptr);
	  return ret;
	}
    }
  else
    if(bn_new!=-1)
      {
	void *ret=theBucketList[bn_new].alloc();
	memcpy(ret,ptr,min);
	free(ptr);
	return ret;
      }
  return realloc(ptr,new_size);
}

void myDeallocateFunction (void *ptr, size_t size)
{
  //  fprintf(stderr,"Freeing: %i bytes.\n",size);

  int bn=bufNum(size);
  if(bn!=-1)return theBucketList[bn].free(ptr);
  return free(ptr);
}

//Debug
void *myAllocateFunctionD(size_t alloc_size)
{
  fprintf(stderr,"Allocating: %i bytes.\n",alloc_size);
  return malloc(alloc_size);
}

void *myReallocateFunctionD(void *ptr, size_t old_size, size_t new_size)
{
  fprintf(stderr,"Reallocating: %i --> %i bytes.\n",old_size,new_size);
  return realloc(ptr,new_size);
}

void myDeallocateFunctionD(void *ptr, size_t size)
{
  fprintf(stderr,"Freeing: %i bytes.\n",size);
  return free(ptr);
}


void changeGmpAllocator()
{
  int e=8;
  for(int i=0;i<NBUCKETSGMP;i++)
    {
      theBucketList[i].init(e);      
      e+=e;
    }

  mp_set_memory_functions (&myAllocateFunction,&myReallocateFunction,&myDeallocateFunction);
}

class AllocatorDummy
{
public:
  AllocatorDummy()
  {
    changeGmpAllocator();
  }
};

//static AllocatorDummy d;//disable allocator for better compatibility