Lolly 1.4.28
Loading...
Searching...
No Matches
Macros | Functions | Variables
fast_alloc.cpp File Reference
#include "fast_alloc.hpp"
#include "tm_ostream.hpp"
#include <cstring>
Include dependency graph for fast_alloc.cpp:

Go to the source code of this file.

Macros

#define alloc_ptr(i)   alloc_table[i]
 
#define ind(ptr)   (*((void**) ptr))
 

Functions

bool break_stub (void *ptr)
 
int mem_used ()
 
voidsafe_malloc (size_t sz)
 
voidenlarge_malloc (size_t sz)
 
voidfast_alloc (size_t sz)
 
void fast_free (void *ptr, size_t sz)
 
voidfast_realloc (void *ptr, size_t old_size, size_t new_size)
 
voidfast_new (size_t s)
 
void fast_delete (void *ptr)
 
voidfast_alloc_mw (size_t s)
 
void fast_free_mw (void *ptr, size_t s)
 
int compute_free (void *ptr)
 
void mem_info ()
 
void mem_init ()
 

Variables

voidalloc_table [MAX_FAST]
 
charalloc_mem = NULL
 
size_t alloc_remains = 0
 
int allocated = 0
 
int fast_chunks = 0
 
int large_uses = 0
 
int MEM_DEBUG = 0
 

Macro Definition Documentation

◆ alloc_ptr

#define alloc_ptr ( i)    alloc_table[i]

Definition at line 41 of file fast_alloc.cpp.

◆ ind

#define ind ( ptr)    (*((void**) ptr))

Definition at line 42 of file fast_alloc.cpp.

Function Documentation

◆ break_stub()

bool break_stub ( void * ptr)

◆ mem_used()

int mem_used ( )

Definition at line 267 of file fast_alloc.cpp.

267 {
270 int i;
271 for (i= WORD_LENGTH; i < MAX_FAST; i+= WORD_LENGTH)
274 return small_uses + large_uses;
275}
The list class represents a linked list.
Definition list.hpp:48
void * alloc_table[MAX_FAST]
int large_uses
int fast_chunks
size_t alloc_remains
int compute_free(void *ptr)
#define WORD_LENGTH
#define MAX_FAST
#define BLOCK_SIZE

◆ safe_malloc()

void * safe_malloc ( size_t sz)

Definition at line 48 of file fast_alloc.cpp.

48 {
49 void* ptr= malloc (sz);
50 if (ptr == NULL) {
51 cerr << "Fatal error: out of memory\n";
52 abort ();
53 }
54 return ptr;
55}
tm_ostream & cerr

◆ enlarge_malloc()

void * enlarge_malloc ( size_t sz)

Definition at line 58 of file fast_alloc.cpp.

58 {
59 if (alloc_remains < sz) {
60 if (alloc_remains > 0) {
63 }
65#ifdef DEBUG_ON
71#endif
74 }
75 void* ptr= alloc_mem;
76 alloc_mem+= sz;
77 alloc_remains-= sz;
78 return ptr;
79}
char * alloc_mem
#define ind(ptr)
#define alloc_ptr(i)
void * safe_malloc(size_t sz)

◆ fast_alloc()

void * fast_alloc ( size_t sz)

Definition at line 82 of file fast_alloc.cpp.

82 {
83 sz= (sz + WORD_LENGTH_INC) & WORD_MASK;
84 if (sz < MAX_FAST) {
85 void* ptr= alloc_ptr (sz);
86 if (ptr == NULL) return enlarge_malloc (sz);
87 alloc_ptr (sz)= ind (ptr);
88#ifdef DEBUG_ON
90#endif
91 return ptr;
92 }
93 else {
94 if (MEM_DEBUG >= 3) cout << "Big alloc of " << sz << " bytes\n";
95 if (MEM_DEBUG >= 3) cout << "Memory used: " << mem_used () << " bytes\n";
96 large_uses+= sz;
97 return safe_malloc (sz);
98 }
99}
bool break_stub(void *ptr)
int mem_used()
int MEM_DEBUG
void * enlarge_malloc(size_t sz)
#define WORD_LENGTH_INC
#define WORD_MASK
tm_ostream & cout

◆ fast_free()

void fast_free ( void * ptr,
size_t sz )

Definition at line 102 of file fast_alloc.cpp.

102 {
103 sz= (sz + WORD_LENGTH_INC) & WORD_MASK;
104 if (sz < MAX_FAST) {
105#ifdef DEBUG_ON
106 break_stub (ptr);
107 break_stub (alloc_ptr (sz));
108#endif
109 ind (ptr) = alloc_ptr (sz);
110 alloc_ptr (sz)= ptr;
111 }
112 else {
113 if (MEM_DEBUG >= 3) cout << "Big free of " << sz << " bytes\n";
114 large_uses-= sz;
115 free (ptr);
116 if (MEM_DEBUG >= 3) cout << "Memory used: " << mem_used () << " bytes\n";
117 }
118}

◆ fast_realloc()

void * fast_realloc ( void * ptr,
size_t old_size,
size_t new_size )

Definition at line 121 of file fast_alloc.cpp.

121 {
125 if (MEM_DEBUG >= 3)
126 cout << "Big realloc from " << old_size << " to " << new_size
127 << " bytes\n";
129 if (MEM_DEBUG >= 3) cout << "Memory used: " << mem_used () << " bytes\n";
130 return realloc (ptr, new_size);
131 }
132 else {
133 void* newptr= fast_alloc (new_size);
136 return newptr;
137 }
138}
void fast_free(void *ptr, size_t sz)
void * fast_alloc(size_t sz)

◆ fast_new()

void * fast_new ( size_t s)

Definition at line 141 of file fast_alloc.cpp.

141 {
142 void* ptr;
143#ifdef DEBUG_ON
144 s= (s + (4 * WORD_LENGTH) + WORD_LENGTH_INC) & WORD_MASK;
145#else
147#endif
148 if (s < MAX_FAST) {
149 ptr= alloc_ptr (s);
150 if (ptr == NULL) ptr= enlarge_malloc (s);
151 else alloc_ptr (s)= ind (ptr);
152#ifdef DEBUG_ON
153 break_stub (ptr);
154#endif
155 }
156 else {
157 if (MEM_DEBUG >= 3) cout << "Big alloc of " << s << " bytes\n";
158 if (MEM_DEBUG >= 3) cout << "Memory used: " << mem_used () << " bytes\n";
159 ptr= safe_malloc (s);
160 // if ((((int) ptr) & 15) != 0) cout << "Unaligned new " << ptr << "\n";
161 large_uses+= s;
162 }
163#ifdef DEBUG_ON
164 char* mem = (char*) ptr;
165 *((size_t*) ptr) = s;
166 ptr = ((char*) ptr) + WORD_LENGTH;
167 *((size_t*) ptr) = s;
168 ptr = ((char*) ptr) + WORD_LENGTH;
169 *((size_t*) ptr) = ~s;
170 ptr = ((char*) ptr) + WORD_LENGTH;
171 *((int*) (mem + s - WORD_LENGTH))= 0x55aa;
172 return (void*) ptr;
173#else
174 *((size_t*) ptr)= s;
175 return (void*) (((char*) ptr) + WORD_LENGTH);
176#endif
177}

◆ fast_delete()

void fast_delete ( void * ptr)

Definition at line 203 of file fast_alloc.cpp.

203 {
204#ifdef DEBUG_ON
205 size_t s;
206 ptr= alloc_check ("fast_delete", ptr, &s);
207#else
208 ptr = (void*) (((char*) ptr) - WORD_LENGTH);
209 size_t s= *((size_t*) ptr);
210#endif
211 if (s < MAX_FAST) {
212#ifdef DEBUG_ON
213 break_stub (ptr);
214 break_stub (alloc_ptr (s));
215#endif
216 ind (ptr) = alloc_ptr (s);
217 alloc_ptr (s)= ptr;
218 }
219 else {
220 if (MEM_DEBUG >= 3) cout << "Big free of " << s << " bytes\n";
221 // if ((((int) ptr) & 15) != 0) cout << "Unaligned delete " << ptr << "\n";
222 free (ptr);
223 large_uses-= s;
224 if (MEM_DEBUG >= 3) cout << "Memory used: " << mem_used () << " bytes\n";
225 }
226}
void * alloc_check(const char *msg, void *ptr, size_t *sp)

◆ fast_alloc_mw()

void * fast_alloc_mw ( size_t s)

Definition at line 233 of file fast_alloc.cpp.

233 {
234 if (s < MAX_FAST) {
235 void* ptr= alloc_ptr (s);
236 if (ptr == NULL) return enlarge_malloc (s);
237 alloc_ptr (s)= ind (ptr);
238 return ptr;
239 }
240 else return safe_malloc (s);
241}

◆ fast_free_mw()

void fast_free_mw ( void * ptr,
size_t s )

Definition at line 244 of file fast_alloc.cpp.

244 {
245 if (s < MAX_FAST) {
246 ind (ptr) = alloc_ptr (s);
247 alloc_ptr (s)= ptr;
248 }
249 else free (ptr);
250}

◆ compute_free()

int compute_free ( void * ptr)

Definition at line 257 of file fast_alloc.cpp.

257 {
258 int i= -1;
259 while (ptr != NULL) {
260 i++;
261 ptr= ind (ptr);
262 }
263 return i;
264}

◆ mem_info()

void mem_info ( )

Definition at line 278 of file fast_alloc.cpp.

278 {
279 cout << "\n---------------- memory statistics ----------------\n";
282 int i;
283 for (i= WORD_LENGTH; i < MAX_FAST; i+= WORD_LENGTH)
287 // cout << "Fast chunks : " << chunks_use << " bytes\n";
288 // cout << "Free on chunks: " << alloc_remains << " bytes\n";
289 cout << "User : " << total_uses << " bytes\n";
290 cout << "Allocator : " << chunks_use + large_uses << " bytes\n";
291 cout << "Small mallocs : "
292 << ((100 * ((float) small_uses)) / ((float) total_uses)) << "%\n";
293}

◆ mem_init()

void mem_init ( )

Definition at line 308 of file fast_alloc.cpp.

308{}

Variable Documentation

◆ alloc_table

void* alloc_table[MAX_FAST]

Definition at line 25 of file fast_alloc.cpp.

◆ alloc_mem

char* alloc_mem = NULL

Definition at line 26 of file fast_alloc.cpp.

◆ alloc_remains

size_t alloc_remains = 0

Definition at line 31 of file fast_alloc.cpp.

◆ allocated

int allocated = 0

Definition at line 32 of file fast_alloc.cpp.

◆ fast_chunks

int fast_chunks = 0

Definition at line 33 of file fast_alloc.cpp.

◆ large_uses

int large_uses = 0

Definition at line 34 of file fast_alloc.cpp.

◆ MEM_DEBUG

int MEM_DEBUG = 0

Definition at line 35 of file fast_alloc.cpp.