#include <stddef.h>
#include "host.h"
Go to the source code of this file.
#define ALLOC_2D_ARRAY | ( | x, | |||
y, | |||||
mem, | |||||
ptrs, | |||||
type | ) |
Value:
/*make 2d array*/\ { \ INT32 TMP_i; \ mem=(type*)alloc_mem((x)*(y)*sizeof(type)); /*get memory*/\ ptrs=(type**)alloc_mem((x)*sizeof(type*)); /*get ptrs*/\ for (TMP_i=0;TMP_i<(x);TMP_i++)\ ptrs[TMP_i]=mem+(y)*TMP_i; /*set ptrs*/\ } \
Definition at line 103 of file memry.h.
Referenced by CHAR_PROTO::CHAR_PROTO(), CHAR_PROTO::enlarge_prototype(), and InvertMatrix().
#define ALLOC_BIG_2D_ARRAY | ( | x, | |||
y, | |||||
mem, | |||||
ptrs, | |||||
type | ) |
Value:
/*make 2d array*/\ { \ INT32 TMP_i; \ mem=(type*)alloc_big_mem((x)*(y)*sizeof(type)); /*get memory*/\ ptrs=(type**)alloc_big_mem((x)*sizeof(type*)); /*get ptrs*/\ for (TMP_i=0;TMP_i<(x);TMP_i++)\ ptrs[TMP_i]=mem+(y)*TMP_i; /*set ptrs*/\ } \
#define FREE_2D_ARRAY | ( | mem, | |||
ptrs | ) |
Value:
Destroy a 2D array created by ALLOC_2D_ARRAYDefinition at line 115 of file memry.h.
Referenced by CHAR_PROTO::enlarge_prototype(), and CHAR_PROTO::~CHAR_PROTO().
#define FREE_BIG_2D_ARRAY | ( | mem, | |||
ptrs | ) |
Value:
/*free a 2D array*/\ { \ free_big_mem(mem); /*free the memory*/\ free_big_mem(ptrs); /*and the ptrs*/\ } \
#define FULLMEMCHECKS 2 |
#define JUSTCHECKS 0 |
* (C) Copyright 1990, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License.
Definition at line 26 of file memry.h.
Referenced by MEM_ALLOCATOR::display_counts(), and MEM_ALLOCATOR::reduce_counts().
#define MEMCHECKS 1 |
Definition at line 27 of file memry.h.
Referenced by MEM_ALLOCATOR::alloc(), MEM_ALLOCATOR::alloc_p(), MEM_ALLOCATOR::check(), check_struct(), free_struct(), MEM_ALLOCATOR::new_block(), and MEM_ALLOCATOR::set_owner().
#define NEWDELETE |
Value:
/*replace new & delete*/\ void *operator new( /*fast allocator*/\ size_t size) /*size of object*/\ {\ return alloc_struct(size); /*simple to do*/\ }\ \ void operator delete( /*fast destructor*/\ void *deadstruct, /*thing to free*/\ size_t size) /*sizeof struct*/\ {\ free_struct(deadstruct,size); /*free it*/\ }\
#define NEWDELETE2 | ( | name | ) |
Value:
/*replace new & delete*/\ void *operator new( /*fast allocator*/\ size_t size) /*size of object*/\ {\ return alloc_struct(size,#name); /*simple to do*/\ }\ \ void operator delete( /*fast destructor*/\ void *deadstruct, /*thing to free*/\ size_t size) /*sizeof struct*/\ {\ free_struct(deadstruct,size,#name); /*free it*/\ }\
DLLSYM void* alloc_big_mem | ( | INT32 | count | ) |
Return a pointer to a buffer of count bytes aligned for any type.
Definition at line 430 of file memry.cpp.
References MEM_ALLOCATOR::alloc(), big_mem, MEM_ALLOCATOR::biggestblock, BIGSIZE, MEM_ALLOCATOR::init(), MAX_BIGCHUNK, NULL, and trace_caller().
Referenced by alloc_mem(), alloc_mem_p(), and IMAGE::read().
00432 { 00433 #ifdef TESTING_BIGSTUFF 00434 if (big_mem.biggestblock == 0) 00435 big_mem.init ((void *(*)(INT32)) malloc, free, 00436 BIGSIZE, BIGSIZE, MAX_BIGCHUNK); 00437 if (mem_mallocdepth > 0) 00438 return big_mem.alloc (count, trace_caller (mem_mallocdepth)); 00439 else 00440 return big_mem.alloc (count, NULL); 00441 #else 00442 return malloc ((size_t) count); 00443 #endif 00444 }
DLLSYM void* alloc_big_zeros | ( | INT32 | count | ) |
Return a pointer to a buffer of count bytes aligned for any type.
Definition at line 450 of file memry.cpp.
References MEM_ALLOCATOR::alloc(), big_mem, MEM_ALLOCATOR::biggestblock, BIGSIZE, MEM_ALLOCATOR::init(), MAX_BIGCHUNK, NULL, and trace_caller().
Referenced by IMAGE::create().
00452 { 00453 #ifdef TESTING_BIGSTUFF 00454 if (big_mem.biggestblock == 0) 00455 big_mem.init ((void *(*)(INT32)) malloc, free, 00456 BIGSIZE, BIGSIZE, MAX_BIGCHUNK); 00457 void *buf; //return value 00458 00459 if (mem_mallocdepth > 0) 00460 buf = big_mem.alloc (count, trace_caller (mem_mallocdepth)); 00461 else 00462 buf = big_mem.alloc (count, NULL); 00463 memset (buf, 0, count); 00464 return buf; 00465 #else 00466 return calloc ((size_t) count, 1); 00467 #endif 00468 }
DLLSYM void* alloc_mem | ( | INT32 | count | ) |
Return a pointer to a buffer of count bytes aligned for any type.
Definition at line 404 of file memry.cpp.
References MEM_ALLOCATOR::alloc(), alloc_big_mem(), MEM_ALLOCATOR::biggestblock, FIRSTSIZE, free_big_mem(), MEM_ALLOCATOR::init(), LASTSIZE, main_mem, MAX_CHUNK, NULL, and trace_caller().
Referenced by alloc_string(), alloc_struct(), c_alloc_mem(), check_pitch_sync2(), check_pitch_sync3(), clean_noise_from_words(), STATS::cluster(), compute_page_skew(), compute_row_stats(), IMAGE::convolver(), correlate_lines(), de_serialise_bytes(), delete_non_dropout_rows(), ELISTIZE(), QSPLINE::extrapolate(), find_textlines(), fix_rep_char(), GAPMAP::GAPMAP(), IMAGELINE::init(), linear_spline_baseline(), LMS::LMS(), make_baseline_spline(), make_height_array(), median_block_xheight(), QSPLINE::QSPLINE(), read_vec_file(), STATS::set_range(), STATS::STATS(), and tweak_row_baseline().
00406 { 00407 #ifdef RAYS_MALLOC 00408 #ifdef TESTING_BIGSTUFF 00409 if (main_mem.biggestblock == 0) 00410 main_mem.init (alloc_big_mem, free_big_mem, 00411 FIRSTSIZE, LASTSIZE, MAX_CHUNK); 00412 #else 00413 if (main_mem.biggestblock == 0) 00414 main_mem.init ((void *(*)(INT32)) malloc, free, 00415 FIRSTSIZE, LASTSIZE, MAX_CHUNK); 00416 #endif 00417 if (mem_mallocdepth > 0) 00418 return main_mem.alloc (count, trace_caller (mem_mallocdepth)); 00419 else 00420 return main_mem.alloc (count, NULL); 00421 #else 00422 return malloc ((size_t) count); 00423 #endif 00424 }
DLLSYM void* alloc_mem_p | ( | INT32 | count | ) |
Allocate permanent space which will never be returned.
This space is allocated from the top end of a memory block to avoid the fragmentation which would result from alternate use of alloc_mem for permanent and temporary blocks.
Definition at line 378 of file memry.cpp.
References alloc_big_mem(), MEM_ALLOCATOR::alloc_p(), MEM_ALLOCATOR::biggestblock, FIRSTSIZE, free_big_mem(), MEM_ALLOCATOR::init(), LASTSIZE, main_mem, MAX_CHUNK, NULL, and trace_caller().
Referenced by c_alloc_mem_p(), and new_struct_block().
00380 { 00381 #ifdef RAYS_MALLOC 00382 #ifdef TESTING_BIGSTUFF 00383 if (main_mem.biggestblock == 0) 00384 main_mem.init (alloc_big_mem, free_big_mem, 00385 FIRSTSIZE, LASTSIZE, MAX_CHUNK); 00386 #else 00387 if (main_mem.biggestblock == 0) 00388 main_mem.init ((void *(*)(INT32)) malloc, free, 00389 FIRSTSIZE, LASTSIZE, MAX_CHUNK); 00390 #endif 00391 if (mem_mallocdepth > 0) 00392 return main_mem.alloc_p (count, trace_caller (mem_mallocdepth)); 00393 else 00394 return main_mem.alloc_p (count, NULL); 00395 #else 00396 return malloc ((size_t) count); 00397 #endif 00398 }
DLLSYM char* alloc_string | ( | INT32 | count | ) |
Allocate space for a string.
The space can only be used for chars as it is not aligned.
Allocation is guaranteed to be fast and not cause fragmentation for small strings (upto 10*worst alignment).
Calls for larger strings will be satisfied with alloc_mem.
Use free_string to free the space from alloc_string.
Definition at line 83 of file memry.cpp.
References alloc_mem(), alloc_struct(), MAX_CHUNK, MAX_STRUCTS, NULL, and tprintf().
Referenced by c_alloc_string(), STRING::operator+(), STRING::operator+=(), STRING::operator=(), and STRING::STRING().
00085 { 00086 #ifdef RAYS_MALLOC 00087 char *string; //allocated string 00088 00089 if (count < 1 || count > MAX_CHUNK) { 00090 tprintf ("Invalid size %d requested of alloc_string", count); 00091 return NULL; 00092 } 00093 00094 count++; //add size byte 00095 if (count <= MAX_STRUCTS * sizeof (MEMUNION)) { 00096 string = (char *) alloc_struct (count, "alloc_string"); 00097 //get a fast structure 00098 if (string == NULL) { 00099 tprintf ("No memory for alloc_string"); 00100 return NULL; 00101 } 00102 string[0] = (INT8) count; //save its length 00103 } 00104 else { 00105 //get a big block 00106 string = (char *) alloc_mem (count); 00107 if (string == NULL) { 00108 tprintf ("No memory for alloc_string"); 00109 return NULL; 00110 } 00111 string[0] = 0; //mark its id 00112 } 00113 return &string[1]; //string for user 00114 #else 00115 return static_cast<char*>(malloc(count)); 00116 #endif 00117 }
DLLSYM void* alloc_struct | ( | INT32 | count, | |
const char * | ||||
) |
Allocate space for a structure.
This function is designed to be fast and fragmentation free for arbitrary combinations of small objects. (Upto 40 bytes in length.)
It can be used for any size of object up to 512K, but you must use free_struct to release the memory it gives. alloc_mem is better for arbitrary data blocks of large size (>40 bytes.) alloc_struct always aborts if the allocation fails.
Definition at line 158 of file memry.cpp.
References alloc_mem(), blocks_in_use, free_structs, identify_struct_owner(), MAX_CHUNK, MAX_CLASSES, MAX_STRUCTS, new_struct_block(), NULL, owner_counts, MEMUNION::ptr, STRUCT_BLOCK_SIZE, struct_blocks, structs_in_use, and tprintf().
Referenced by alloc_string(), c_alloc_struct(), REJMAP::initialise(), REJMAP::REJMAP(), and REJMAP::remove_pos().
00165 { 00166 #ifdef RAYS_MALLOC 00167 MEMUNION *element; //current element 00168 MEMUNION *returnelement; //return value 00169 INT32 struct_count; //no of required structs 00170 INT32 blocksize; //no of structs in block 00171 INT32 index; //index to structure 00172 00173 if (count < 1 || count > MAX_CHUNK) { 00174 tprintf ("Invalid size %d requested of alloc_struct", count); 00175 return NULL; 00176 } 00177 00178 // tprintf("Allocating structure of size %d\n",count); 00179 //no of MEMUNIONS-1 00180 struct_count = (count - 1) / sizeof (MEMUNION); 00181 if (struct_count < MAX_STRUCTS) { 00182 //can do fixed sizes 00183 #ifdef COUNTING_CLASS_STRUCTURES 00184 if (name != NULL) { 00185 index = identify_struct_owner (struct_count, name); 00186 if (index < MAX_CLASSES) 00187 owner_counts[struct_count][index]++; 00188 } 00189 #endif 00190 //head of freelist 00191 returnelement = free_structs[struct_count]; 00192 if (returnelement == NULL) { 00193 //need a new block 00194 //get one 00195 element = (MEMUNION *) new_struct_block (); 00196 if (element == NULL) { 00197 tprintf ("No memory to satisfy request for %d", (int) count); 00198 return NULL; 00199 } 00200 //add to block list 00201 element->ptr = struct_blocks[struct_count]; 00202 struct_blocks[struct_count] = element; 00203 blocks_in_use[struct_count]++; 00204 element++; //free cell 00205 returnelement = element; //going to return 1st 00206 blocksize = STRUCT_BLOCK_SIZE / (struct_count + 1) - 1; 00207 00208 for (index = 1; index < blocksize; index++) { 00209 //make links 00210 element->ptr = element + struct_count + 1; 00211 element += struct_count + 1; 00212 } 00213 element->ptr = NULL; //end of freelist 00214 } 00215 //new free one 00216 free_structs[struct_count] = returnelement->ptr; 00217 //count number issued 00218 structs_in_use[struct_count]++; 00219 } 00220 else { 00221 //just get some 00222 returnelement = (MEMUNION *) alloc_mem (count); 00223 if (returnelement == NULL) { 00224 tprintf ("No memory to satisfy request for %d", (int) count); 00225 return NULL; 00226 } 00227 } 00228 return returnelement; //free cell 00229 #else 00230 return malloc(count); 00231 #endif 00232 }
DLLSYM void check_mem | ( | const char * | string, | |
INT8 | level | |||
) |
Check consistency of all memory controlled by alloc_mem.
Definition at line 60 of file memry.cpp.
References big_mem, MEM_ALLOCATOR::check(), check_structs(), and main_mem.
Referenced by MEM_ALLOCATOR::alloc(), MEM_ALLOCATOR::alloc_p(), c_check_mem(), MEM_ALLOCATOR::display_counts(), do_check_mem(), MEM_ALLOCATOR::new_block(), MEM_ALLOCATOR::reduce_counts(), and MEM_ALLOCATOR::set_owner().
00063 { 00064 big_mem.check (string, level); 00065 main_mem.check (string, level); 00066 check_structs(level); 00067 }
DLLSYM void free_big_mem | ( | void * | oldchunk | ) |
Free a block allocated by alloc_big_mem.
It checks that the pointer is legal and maintains counts of the amount of free memory above and below the current free pointer.
Definition at line 497 of file memry.cpp.
References big_mem, MEM_ALLOCATOR::callers, MEM_ALLOCATOR::dealloc(), main_mem, NULL, and trace_caller().
Referenced by alloc_mem(), alloc_mem_p(), and IMAGE::destroy().
00499 { 00500 #ifdef TESTING_BIGSTUFF 00501 if (mem_freedepth > 0 && main_mem.callers != NULL) 00502 big_mem.dealloc (oldchunk, trace_caller (mem_freedepth)); 00503 else 00504 big_mem.dealloc (oldchunk, NULL); 00505 #else 00506 free(oldchunk); 00507 #endif 00508 }
DLLSYM void free_mem | ( | void * | oldchunk | ) |
Free a block allocated by alloc_mem (or alloc_mem_p).
It checks that the pointer is legal and maintains counts of the amount of free memory above and below the current free pointer.
Definition at line 477 of file memry.cpp.
References MEM_ALLOCATOR::callers, MEM_ALLOCATOR::dealloc(), main_mem, NULL, and trace_caller().
Referenced by c_free_mem(), check_pitch_sync2(), check_pitch_sync3(), clean_noise_from_words(), STATS::cluster(), compute_page_skew(), compute_row_stats(), convert_choice_lists(), correlate_lines(), STRING::de_dump(), delete_non_dropout_rows(), QSPLINE::extrapolate(), find_textlines(), fix_rep_char(), free_string(), free_struct(), IMAGELINE::init(), make_baseline_spline(), make_first_xheight(), median_block_xheight(), old_struct_block(), program_editdown(), QSPLINE::QSPLINE(), read_vec_file(), STATS::set_range(), tweak_row_baseline(), GAPMAP::~GAPMAP(), IMAGELINE::~IMAGELINE(), LMS::~LMS(), PIXROW::~PIXROW(), QSPLINE::~QSPLINE(), and STATS::~STATS().
00479 { 00480 #ifdef RAYS_MALLOC 00481 if (mem_freedepth > 0 && main_mem.callers != NULL) 00482 main_mem.dealloc (oldchunk, trace_caller (mem_freedepth)); 00483 else 00484 main_mem.dealloc (oldchunk, NULL); 00485 #else 00486 free(oldchunk); 00487 #endif 00488 }
DLLSYM void free_string | ( | char * | string | ) |
Free a string allocated by alloc_string.
Definition at line 123 of file memry.cpp.
References free_mem(), free_struct(), MAX_STRUCTS, and tprintf().
Referenced by c_free_string(), STRING::operator+=(), STRING::operator=(), and STRING::~STRING().
00125 { 00126 #ifdef RAYS_MALLOC 00127 if (((ptrdiff_t) string & 3) == 1) { //one over word 00128 string--; //get id marker 00129 if (*string == 0) { 00130 free_mem(string); //generally free it 00131 return; 00132 } 00133 else if (*string <= MAX_STRUCTS * sizeof (MEMUNION)) { 00134 //free structure 00135 free_struct (string, *string, "alloc_string"); 00136 return; 00137 } 00138 } 00139 tprintf ("Non-string given to free_string"); 00140 #else 00141 free(string); 00142 #endif 00143 }
Free memory allocated by alloc_struct. The size must be supplied.
Definition at line 239 of file memry.cpp.
References ASSERT_HOST, blocks_in_use, check_struct(), free_mem(), free_structs, identify_struct_owner(), MAX_CHUNK, MAX_CLASSES, MAX_STRUCTS, MEMCHECKS, NULL, old_struct_block(), owner_counts, MEMUNION::ptr, STRUCT_BLOCK_SIZE, struct_blocks, structs_in_use, and tprintf().
Referenced by c_free_struct(), free_string(), REJMAP::initialise(), REJMAP::remove_pos(), and REJMAP::~REJMAP().
00247 { 00248 #ifdef RAYS_MALLOC 00249 MEMUNION *end_element; //current element 00250 MEMUNION *element; //current element 00251 MEMUNION *prev_element; //previous element 00252 MEMUNION *prev_block; //previous element 00253 MEMUNION *nextblock; //next block in list 00254 MEMUNION *block; //next block in list 00255 INT32 struct_count; //no of required structs 00256 INT32 index; //to structure counts 00257 00258 if (count < 1 || count > MAX_CHUNK) { 00259 tprintf ("Invalid size %d requested of free_struct", count); 00260 return; 00261 } 00262 00263 // tprintf("Freeing structure of size %d\n",count); 00264 //no of MEMUNIONS-1 00265 struct_count = (count - 1) / sizeof (MEMUNION); 00266 00267 if (deadstruct == NULL) { 00268 //not really legal 00269 check_struct(MEMCHECKS, count); 00270 } 00271 else { 00272 if (struct_count < MAX_STRUCTS) { 00273 //can do fixed sizes 00274 #ifdef COUNTING_CLASS_STRUCTURES 00275 if (name != NULL) { 00276 index = identify_struct_owner (struct_count, name); 00277 if (index < MAX_CLASSES) { 00278 owner_counts[struct_count][index]--; 00279 ASSERT_HOST (owner_counts[struct_count][index] >= 0); 00280 } 00281 } 00282 #endif 00283 element = (MEMUNION *) deadstruct; 00284 //add to freelist 00285 element->ptr = free_structs[struct_count]; 00286 free_structs[struct_count] = element; 00287 //one less in use 00288 structs_in_use[struct_count]--; 00289 if (structs_in_use[struct_count] == 0) { 00290 index = 0; 00291 for (element = struct_blocks[struct_count]; 00292 element != NULL; element = nextblock) { 00293 //traverse and destroy 00294 nextblock = element->ptr; 00295 //free all the blocks 00296 old_struct_block(element); 00297 index++; 00298 } 00299 //none left any more 00300 struct_blocks[struct_count] = NULL; 00301 //no free structs 00302 free_structs[struct_count] = NULL; 00303 blocks_in_use[struct_count] = 0; 00304 } 00305 else if (structs_in_use[struct_count] < 0) { 00306 tprintf ("Negative number of structs of size %d in use", 00307 (int) count); 00308 } 00309 else if (structs_in_use[struct_count] < blocks_in_use[struct_count]) { 00310 prev_block = NULL; 00311 for (block = struct_blocks[struct_count]; 00312 block != NULL; block = nextblock) { 00313 nextblock = block; 00314 index = STRUCT_BLOCK_SIZE / (struct_count + 1) - 1; 00315 end_element = block + STRUCT_BLOCK_SIZE; 00316 for (element = free_structs[struct_count]; 00317 element != NULL; element = element->ptr) { 00318 if (element > nextblock && element < end_element) { 00319 index--; 00320 if (index == 0) 00321 break; 00322 } 00323 } 00324 if (index == 0) { 00325 index = STRUCT_BLOCK_SIZE / (struct_count + 1) - 1; 00326 for (element = 00327 free_structs[struct_count], prev_element = NULL; 00328 element != NULL; element = element->ptr) { 00329 if (element > nextblock && element < end_element) { 00330 index--; 00331 if (prev_element != NULL) 00332 prev_element->ptr = element->ptr; 00333 else 00334 free_structs[struct_count] = element->ptr; 00335 if (index == 0) 00336 break; 00337 } 00338 else 00339 prev_element = element; 00340 } 00341 if (prev_block != NULL) 00342 prev_block->ptr = block->ptr; 00343 else 00344 struct_blocks[struct_count] = block->ptr; 00345 nextblock = block->ptr; 00346 blocks_in_use[struct_count]--; 00347 //free all the blocks 00348 old_struct_block(block); 00349 } 00350 else { 00351 prev_block = block; 00352 //traverse and destroy 00353 nextblock = block->ptr; 00354 } 00355 } 00356 } 00357 } 00358 else 00359 free_mem(deadstruct); //free directly 00360 } 00361 #else 00362 free(deadstruct); 00363 #endif // RAYS_MALLOC 00364 }